-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathdatasets.py
125 lines (87 loc) · 2.88 KB
/
datasets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from abc import ABC, abstractmethod
from typing import Any, Dict
import numpy as np
from torch import LongTensor
from torch.utils.data import Dataset
from oml.const import INDEX_KEY, LABELS_KEY, PAIR_1ST_KEY, PAIR_2ND_KEY, TColor
class IBaseDataset(Dataset):
input_tensors_key: str
index_key: str
extra_data: Dict[str, Any]
def __getitem__(self, item: int) -> Dict[str, Any]:
"""
Args:
item: Idx of the sample
Returns:
Dictionary including the following keys:
``self.input_tensors_key``
``self.index_key: int = item``
"""
raise NotImplementedError()
class ILabeledDataset(IBaseDataset, ABC):
"""
This is an interface for the datasets which provide labels of containing items.
"""
labels_key: str = LABELS_KEY
def __getitem__(self, item: int) -> Dict[str, Any]:
"""
Args:
item: Idx of the sample
Returns:
Dictionary including the following keys:
``self.labels_key``
"""
raise NotImplementedError()
@abstractmethod
def get_labels(self) -> np.ndarray:
raise NotImplementedError()
class IQueryGalleryDataset(IBaseDataset, ABC):
"""
This is an interface for the datasets which hold the information on how to split
the data into the query and gallery. The query and gallery ids may overlap.
It doesn't need the ground truth labels, so it can be used for prediction on not annotated data.
"""
@abstractmethod
def get_query_ids(self) -> LongTensor:
raise NotImplementedError()
@abstractmethod
def get_gallery_ids(self) -> LongTensor:
raise NotImplementedError()
class IQueryGalleryLabeledDataset(IQueryGalleryDataset, ILabeledDataset, ABC):
"""
This interface is similar to `IQueryGalleryDataset`, but there are ground truth labels.
"""
class IPairsDataset(Dataset, ABC):
"""
This is an interface for the datasets which return pair of something.
"""
pairs_1st_key: str = PAIR_1ST_KEY
pairs_2nd_key: str = PAIR_2ND_KEY
index_key: str = INDEX_KEY
@abstractmethod
def __getitem__(self, item: int) -> Dict[str, Any]:
"""
Args:
item: Idx of the sample
Returns:
Dictionary with the following keys:
``self.pairs_1st_key``
``self.pairs_2nd_key``
``self.index_key``
"""
raise NotImplementedError()
class IVisualizableDataset(Dataset, ABC):
"""
Base class for the datasets which know how to visualise their items.
"""
@abstractmethod
def visualize(self, item: int, color: TColor) -> np.ndarray:
raise NotImplementedError()
__all__ = [
"IBaseDataset",
"ILabeledDataset",
"IQueryGalleryLabeledDataset",
"IQueryGalleryDataset",
"IPairsDataset",
"IVisualizableDataset",
]