-
Notifications
You must be signed in to change notification settings - Fork 22
/
datasets.py
executable file
·258 lines (214 loc) · 9.72 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import csv, torchvision, numpy as np, random, os
from PIL import Image
from torch.utils.data import Sampler, Dataset, DataLoader, BatchSampler, SequentialSampler, RandomSampler, Subset
from torchvision import transforms, datasets
from collections import defaultdict
class PairBatchSampler(Sampler):
def __init__(self, dataset, batch_size, num_iterations=None):
self.dataset = dataset
self.batch_size = batch_size
self.num_iterations = num_iterations
def __iter__(self):
indices = list(range(len(self.dataset)))
random.shuffle(indices)
for k in range(len(self)):
if self.num_iterations is None:
offset = k*self.batch_size
batch_indices = indices[offset:offset+self.batch_size]
else:
batch_indices = random.sample(range(len(self.dataset)),
self.batch_size)
pair_indices = []
for idx in batch_indices:
y = self.dataset.get_class(idx)
pair_indices.append(random.choice(self.dataset.classwise_indices[y]))
yield batch_indices + pair_indices
def __len__(self):
if self.num_iterations is None:
return (len(self.dataset)+self.batch_size-1) // self.batch_size
else:
return self.num_iterations
class DatasetWrapper(Dataset):
# Additinoal attributes
# - indices
# - classwise_indices
# - num_classes
# - get_class
def __init__(self, dataset, indices=None):
self.base_dataset = dataset
if indices is None:
self.indices = list(range(len(dataset)))
else:
self.indices = indices
# torchvision 0.2.0 compatibility
if torchvision.__version__.startswith('0.2'):
if isinstance(self.base_dataset, datasets.ImageFolder):
self.base_dataset.targets = [s[1] for s in self.base_dataset.imgs]
else:
if self.base_dataset.train:
self.base_dataset.targets = self.base_dataset.train_labels
else:
self.base_dataset.targets = self.base_dataset.test_labels
self.classwise_indices = defaultdict(list)
for i in range(len(self)):
y = self.base_dataset.targets[self.indices[i]]
self.classwise_indices[y].append(i)
self.num_classes = max(self.classwise_indices.keys())+1
def __getitem__(self, i):
return self.base_dataset[self.indices[i]]
def __len__(self):
return len(self.indices)
def get_class(self, i):
return self.base_dataset.targets[self.indices[i]]
class ConcatWrapper(Dataset): # TODO: Naming
@staticmethod
def cumsum(sequence):
r, s = [], 0
for e in sequence:
l = len(e)
r.append(l + s)
s += l
return r
@staticmethod
def numcls(sequence):
s = 0
for e in sequence:
l = e.num_classes
s += l
return s
@staticmethod
def clsidx(sequence):
r, s, n = defaultdict(list), 0, 0
for e in sequence:
l = e.classwise_indices
for c in range(s, s + e.num_classes):
t = np.asarray(l[c-s]) + n
r[c] = t.tolist()
s += e.num_classes
n += len(e)
return r
def __init__(self, datasets):
super(ConcatWrapper, self).__init__()
assert len(datasets) > 0, 'datasets should not be an empty iterable'
self.datasets = list(datasets)
# for d in self.datasets:
# assert not isinstance(d, IterableDataset), "ConcatDataset does not support IterableDataset"
self.cumulative_sizes = self.cumsum(self.datasets)
self.num_classes = self.numcls(self.datasets)
self.classwise_indices = self.clsidx(self.datasets)
def __len__(self):
return self.cumulative_sizes[-1]
def __getitem__(self, idx):
if idx < 0:
if -idx > len(self):
raise ValueError("absolute value of index should not exceed dataset length")
idx = len(self) + idx
dataset_idx = bisect.bisect_right(self.cumulative_sizes, idx)
if dataset_idx == 0:
sample_idx = idx
else:
sample_idx = idx - self.cumulative_sizes[dataset_idx - 1]
return self.datasets[dataset_idx][sample_idx]
def get_class(self, idx):
if idx < 0:
if -idx > len(self):
raise ValueError("absolute value of index should not exceed dataset length")
idx = len(self) + idx
dataset_idx = bisect.bisect_right(self.cumulative_sizes, idx)
if dataset_idx == 0:
sample_idx = idx
else:
sample_idx = idx - self.cumulative_sizes[dataset_idx - 1]
true_class = self.datasets[dataset_idx].base_dataset.targets[self.datasets[dataset_idx].indices[sample_idx]]
return self.datasets[dataset_idx].base_dataset.target_transform(true_class)
@property
def cummulative_sizes(self):
warnings.warn("cummulative_sizes attribute is renamed to "
"cumulative_sizes", DeprecationWarning, stacklevel=2)
return self.cumulative_sizes
def load_dataset(name, root, sample='default', **kwargs):
# Dataset
if name in ['imagenet','tinyimagenet', 'CUB200', 'STANFORD120', 'MIT67']:
# TODO
if name == 'tinyimagenet':
transform_train = transforms.Compose([
transforms.RandomResizedCrop(32),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
transform_test = transforms.Compose([
transforms.Resize(32),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
train_val_dataset_dir = os.path.join(root, "train")
test_dataset_dir = os.path.join(root, "val")
trainset = DatasetWrapper(datasets.ImageFolder(root=train_val_dataset_dir, transform=transform_train))
valset = DatasetWrapper(datasets.ImageFolder(root=test_dataset_dir, transform=transform_test))
elif name == 'imagenet':
transform_train = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
transform_test = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
train_val_dataset_dir = os.path.join(root, "train")
test_dataset_dir = os.path.join(root, "val")
trainset = DatasetWrapper(datasets.ImageFolder(root=train_val_dataset_dir, transform=transform_train))
valset = DatasetWrapper(datasets.ImageFolder(root=test_dataset_dir, transform=transform_test))
else:
transform_train = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
transform_test = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))
])
train_val_dataset_dir = os.path.join(root, name, "train")
test_dataset_dir = os.path.join(root, name, "test")
trainset = DatasetWrapper(datasets.ImageFolder(root=train_val_dataset_dir, transform=transform_train))
valset = DatasetWrapper(datasets.ImageFolder(root=test_dataset_dir, transform=transform_test))
elif name.startswith('cifar'):
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465),
(0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
if name == 'cifar10':
CIFAR = datasets.CIFAR10
else:
CIFAR = datasets.CIFAR100
trainset = DatasetWrapper(CIFAR(root, train=True, download=True, transform=transform_train))
valset = DatasetWrapper(CIFAR(root, train=False, download=True, transform=transform_test))
else:
raise Exception('Unknown dataset: {}'.format(name))
# Sampler
if sample == 'default':
get_train_sampler = lambda d: BatchSampler(RandomSampler(d), kwargs['batch_size'], False)
get_test_sampler = lambda d: BatchSampler(SequentialSampler(d), kwargs['batch_size'], False)
elif sample == 'pair':
get_train_sampler = lambda d: PairBatchSampler(d, kwargs['batch_size'])
get_test_sampler = lambda d: BatchSampler(SequentialSampler(d), kwargs['batch_size'], False)
else:
raise Exception('Unknown sampling: {}'.format(sampling))
trainloader = DataLoader(trainset, batch_sampler=get_train_sampler(trainset), num_workers=4)
valloader = DataLoader(valset, batch_sampler=get_test_sampler(valset), num_workers=4)
return trainloader, valloader