-
Notifications
You must be signed in to change notification settings - Fork 7
/
data_loader.py
379 lines (335 loc) · 18 KB
/
data_loader.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import os, sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
import torch
from torchvision import datasets, transforms
import numpy as np
import torchvision
import matplotlib.pyplot as plt
import argparse
def per_image_standardization(x):
y = x.view(-1, x.shape[1] * x.shape[2] * x.shape[3])
mean = y.mean(dim=1, keepdim=True).expand_as(y)
std = y.std(dim=1, keepdim=True).expand_as(y)
adjusted_std = torch.max(std, 1.0 / torch.sqrt(torch.cuda.FloatTensor([x.shape[1] * x.shape[2] * x.shape[3]])))
y = (y - mean) / adjusted_std
standarized_input = y.view(x.shape[0], x.shape[1], x.shape[2], x.shape[3])
return standarized_input
def load_data(data_aug, batch_size, workers, dataset, data_target_dir):
if dataset == 'cifar10':
mean = [x / 255 for x in [125.3, 123.0, 113.9]]
std = [x / 255 for x in [63.0, 62.1, 66.7]]
elif dataset == 'cifar100':
mean = [x / 255 for x in [129.3, 124.1, 112.4]]
std = [x / 255 for x in [68.2, 65.4, 70.4]]
elif dataset == 'svhn':
mean = [x / 255 for x in [127.5, 127.5, 127.5]]
std = [x / 255 for x in [127.5, 127.5, 127.5]]
else:
assert False, "Unknow dataset : {}".format(dataset)
if data_aug == 1:
if dataset == 'svhn':
train_transform = transforms.Compose(
[transforms.RandomCrop(32, padding=2), transforms.ToTensor(),
transforms.Normalize(mean, std)])
test_transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize(mean, std)])
else:
train_transform = transforms.Compose(
[transforms.RandomHorizontalFlip(), transforms.RandomCrop(32, padding=4), transforms.ToTensor(),
transforms.Normalize(mean, std)])
test_transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize(mean, std)])
else:
train_transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize(mean, std)])
test_transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize(mean, std)])
if dataset == 'cifar10':
train_data = datasets.CIFAR10(data_target_dir, train=True, transform=train_transform, download=True)
test_data = datasets.CIFAR10(data_target_dir, train=False, transform=test_transform, download=True)
num_classes = 10
elif dataset == 'cifar100':
train_data = datasets.CIFAR100(data_target_dir, train=True, transform=train_transform, download=True)
test_data = datasets.CIFAR100(data_target_dir, train=False, transform=test_transform, download=True)
num_classes = 100
elif dataset == 'svhn':
train_data = datasets.SVHN(data_target_dir, split='train', transform=train_transform, download=True)
test_data = datasets.SVHN(data_target_dir, split='test', transform=test_transform, download=True)
num_classes = 10
elif dataset == 'stl10':
train_data = datasets.STL10(data_target_dir, split='train', transform=train_transform, download=True)
test_data = datasets.STL10(data_target_dir, split='test', transform=test_transform, download=True)
num_classes = 10
elif dataset == 'imagenet':
assert False, 'Do not finish imagenet code'
else:
assert False, 'Do not support dataset : {}'.format(dataset)
train_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size, shuffle=True,
num_workers=workers, pin_memory=True)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size, shuffle=False,
num_workers=workers, pin_memory=True)
return train_loader, test_loader, num_classes
def load_data_subset(data_aug, batch_size, workers, dataset, data_target_dir, labels_per_class=100,
valid_labels_per_class=500):
import numpy as np
from functools import reduce
from operator import __or__
from torch.utils.data.sampler import SubsetRandomSampler
if dataset == 'cifar10':
mean = [x / 255 for x in [125.3, 123.0, 113.9]]
std = [x / 255 for x in [63.0, 62.1, 66.7]]
elif dataset == 'cifar100':
mean = [x / 255 for x in [129.3, 124.1, 112.4]]
std = [x / 255 for x in [68.2, 65.4, 70.4]]
elif dataset == 'svhn':
mean = [x / 255 for x in [127.5, 127.5, 127.5]]
std = [x / 255 for x in [127.5, 127.5, 127.5]]
elif dataset == 'tiny-imagenet-200':
mean = [x / 255 for x in [127.5, 127.5, 127.5]]
std = [x / 255 for x in [127.5, 127.5, 127.5]]
elif dataset == 'mnist':
pass
else:
assert False, "Unknow dataset : {}".format(dataset)
if data_aug == 1:
print('data aug')
if dataset == 'svhn':
train_transform = transforms.Compose(
[transforms.RandomCrop(32, padding=2), transforms.ToTensor(),
transforms.Normalize(mean, std)])
test_transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize(mean, std)])
elif dataset == 'mnist':
hw_size = 24
train_transform = transforms.Compose([
transforms.RandomCrop(hw_size),
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
test_transform = transforms.Compose([
transforms.CenterCrop(hw_size),
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
elif dataset == 'tiny-imagenet-200':
train_transform = transforms.Compose(
[transforms.RandomHorizontalFlip(),
transforms.RandomCrop(64, padding=4),
transforms.ToTensor(),
transforms.Normalize(mean, std)])
test_transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize(mean, std)])
else:
train_transform = transforms.Compose(
[transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32, padding=2),
transforms.ToTensor(),
transforms.Normalize(mean, std)])
test_transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize(mean, std)])
else:
print('no data aug')
if dataset == 'mnist':
hw_size = 28
train_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
test_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
else:
train_transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize(mean, std)])
test_transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize(mean, std)])
if dataset == 'cifar10':
train_data = datasets.CIFAR10(data_target_dir, train=True, transform=train_transform, download=True)
test_data = datasets.CIFAR10(data_target_dir, train=False, transform=test_transform, download=True)
num_classes = 10
elif dataset == 'cifar100':
train_data = datasets.CIFAR100(data_target_dir, train=True, transform=train_transform, download=True)
test_data = datasets.CIFAR100(data_target_dir, train=False, transform=test_transform, download=True)
num_classes = 100
elif dataset == 'svhn':
train_data = datasets.SVHN(data_target_dir, split='train', transform=train_transform, download=True)
test_data = datasets.SVHN(data_target_dir, split='test', transform=test_transform, download=True)
num_classes = 10
elif dataset == 'mnist':
train_data = datasets.MNIST(data_target_dir, train=True, transform=train_transform, download=True)
test_data = datasets.MNIST(data_target_dir, train=False, transform=test_transform, download=True)
num_classes = 10
elif dataset == 'stl10':
train_data = datasets.STL10(data_target_dir, split='train', transform=train_transform, download=True)
test_data = datasets.STL10(data_target_dir, split='test', transform=test_transform, download=True)
num_classes = 10
elif dataset == 'tiny-imagenet-200':
train_root = os.path.join(data_target_dir, 'train') # this is path to training images folder
validation_root = os.path.join(data_target_dir, 'val/images') # this is path to validation images folder
train_data = datasets.ImageFolder(train_root, transform=train_transform)
test_data = datasets.ImageFolder(validation_root, transform=test_transform)
num_classes = 200
elif dataset == 'imagenet':
assert False, 'Do not finish imagenet code'
else:
assert False, 'Do not support dataset : {}'.format(dataset)
n_labels = num_classes
def get_sampler(labels, n=None, n_valid=None):
# Only choose digits in n_labels
# n = number of labels per class for training
# n_val = number of lables per class for validation
# print type(labels)
# print (n_valid)
(indices,) = np.where(reduce(__or__, [labels == i for i in np.arange(n_labels)]))
# Ensure uniform distribution of labels
np.random.shuffle(indices)
indices_valid = np.hstack(
[list(filter(lambda idx: labels[idx] == i, indices))[:n_valid] for i in range(n_labels)])
indices_train = np.hstack(
[list(filter(lambda idx: labels[idx] == i, indices))[n_valid:n_valid + n] for i in range(n_labels)])
indices_unlabelled = np.hstack(
[list(filter(lambda idx: labels[idx] == i, indices))[:] for i in range(n_labels)])
indices_train = torch.from_numpy(indices_train)
indices_valid = torch.from_numpy(indices_valid)
indices_unlabelled = torch.from_numpy(indices_unlabelled)
sampler_train = SubsetRandomSampler(indices_train)
sampler_valid = SubsetRandomSampler(indices_valid)
sampler_unlabelled = SubsetRandomSampler(indices_unlabelled)
return sampler_train, sampler_valid, sampler_unlabelled
# Dataloaders for MNIST
if dataset == 'svhn':
train_sampler, valid_sampler, unlabelled_sampler = get_sampler(train_data.labels, labels_per_class,
valid_labels_per_class)
elif dataset == 'mnist':
train_sampler, valid_sampler, unlabelled_sampler = get_sampler(train_data.targets.numpy(), labels_per_class,
valid_labels_per_class)
elif dataset == 'tiny-imagenet-200':
pass
else:
train_sampler, valid_sampler, unlabelled_sampler = get_sampler(train_data.targets, labels_per_class,
valid_labels_per_class)
if dataset == 'tiny-imagenet-200':
labelled = torch.utils.data.DataLoader(train_data, batch_size=batch_size, shuffle=True, num_workers=workers,
pin_memory=True)
validation = None
unlabelled = None
test = torch.utils.data.DataLoader(test_data, batch_size=batch_size, shuffle=False, num_workers=workers,
pin_memory=True)
else:
labelled = torch.utils.data.DataLoader(train_data, batch_size=batch_size, sampler=train_sampler, shuffle=False,
num_workers=workers, pin_memory=True)
validation = torch.utils.data.DataLoader(train_data, batch_size=batch_size, sampler=valid_sampler,
shuffle=False, num_workers=workers, pin_memory=True)
unlabelled = torch.utils.data.DataLoader(train_data, batch_size=batch_size, sampler=unlabelled_sampler,
shuffle=False, num_workers=workers, pin_memory=True)
test = torch.utils.data.DataLoader(test_data, batch_size=batch_size, shuffle=False, num_workers=workers,
pin_memory=True)
return labelled, validation, unlabelled, test, num_classes
def create_val_folder(data_set_path):
"""
Used for Tiny-imagenet dataset
Copied from https://github.com/soumendukrg/BME595_DeepLearning/blob/master/Homework-06/train.py
This method is responsible for separating validation images into separate sub folders,
so that test and val data can be read by the pytorch dataloaders
"""
path = os.path.join(data_set_path, 'val/images') # path where validation data is present now
filename = os.path.join(data_set_path, 'val/val_annotations.txt') # file where image2class mapping is present
fp = open(filename, "r") # open file in read mode
data = fp.readlines() # read line by line
# Create a dictionary with image names as key and corresponding classes as values
val_img_dict = {}
for line in data:
words = line.split("\t")
val_img_dict[words[0]] = words[1]
fp.close()
# Create folder if not present, and move image into proper folder
for img, folder in val_img_dict.items():
newpath = (os.path.join(path, folder))
if not os.path.exists(newpath): # check if folder exists
os.makedirs(newpath)
if os.path.exists(os.path.join(path, img)): # Check if image exists in default directory
os.rename(os.path.join(path, img), os.path.join(newpath, img))
def create_deformation_sets(workers, data_target_dir, transformers):
mean = [x / 255 for x in [129.3, 124.1, 112.4]]
std = [x / 255 for x in [68.2, 65.4, 70.4]]
for (folder, transformer) in transformers:
test_transform = transforms.Compose(
[
transformer,
transforms.ToTensor(),
transforms.Normalize(mean, std)
])
test_data = datasets.CIFAR100(data_target_dir, train=False, transform=test_transform, download=False)
test = torch.utils.data.DataLoader(test_data, batch_size=5000, shuffle=False, num_workers=workers,
pin_memory=True)
x_test = []
y_test = []
for index, data in enumerate(test):
images, labels = data
x_test.extend([image.data.cpu().numpy().tolist() for image in images])
y_test.extend(labels.data.cpu().tolist())
if not os.path.exists(folder):
os.makedirs(folder)
img_path = os.path.join(folder, 'images')
lbl_path = os.path.join(folder, 'targets')
classes_path = os.path.join(folder, 'classes')
transformer_path = os.path.join(folder, 'transformer')
np.save(img_path, x_test)
np.save(lbl_path, y_test)
np.save(classes_path, np.array(test.dataset.classes))
np.save(transformer_path, np.array([str(transformer)]))
print(f'{str(transformer)} is created')
def load_transformed_test_sets(path, batch_size=100, workers=0):
data_loaders = []
for r, d, f in os.walk(path):
if len(f) > 0:
for file in f:
file_path = os.path.join(r, file)
if file == 'images.npy':
x_test = np.load(file_path)
elif file == 'targets.npy':
y_test = np.load(file_path)
elif file == 'classes.npy':
classes = np.load(file_path)
elif file == 'transformer.npy':
transformer = np.load(file_path)
tensor_x = torch.Tensor(np.array(x_test))
tensor_y = torch.Tensor(np.array(y_test))
dataset = torch.utils.data.TensorDataset(tensor_x, tensor_y)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=False, num_workers=workers,
pin_memory=True)
dataloader.transformer = transformer[0]
dataloader.classes = classes
data_loaders.append(dataloader)
return data_loaders
def imshow(img, title):
img = img / 2 + 0.5
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.title(title)
plt.show()
def test_load_transformed_test_sets(path='./data/test/affine/'):
transformers = [(os.path.join(path, 'rotate_20'), transforms.RandomAffine(degrees=20)),
(os.path.join(path, 'rotate_40'), transforms.RandomAffine(degrees=40)),
(os.path.join(path, 'shear_28_6'), transforms.RandomAffine(degrees=0, shear=28.6)),
(os.path.join(path, 'rotate_57_3'), transforms.RandomAffine(degrees=0, shear=57.3)),
(os.path.join(path, 'zoom_60'), transforms.RandomAffine(degrees=0, scale=(.60, .60))),
(os.path.join(path, 'zoom_80'), transforms.RandomAffine(degrees=0, scale=(.80, .80))),
(os.path.join(path, 'zoom_120'), transforms.RandomAffine(degrees=0, scale=(1.20, 1.20))),
(os.path.join(path, 'zoom_140'), transforms.RandomAffine(degrees=0, scale=(1.40, 1.40)))]
create_deformation_sets(workers=0, data_target_dir='./data/cifar100_afine', transformers=transformers)
data_loaders = load_transformed_test_sets(path, batch_size=16, workers=0)
for t_loader in data_loaders:
for index, (images, targets) in enumerate(t_loader):
imshow(torchvision.utils.make_grid(images), t_loader.transformer)
break
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Generate Deformed Images test set',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--affine_path', type=str, default='./data/test/affine/',
help='file where results are to be written.')
args = parser.parse_args()
test_load_transformed_test_sets(path=args.affine_path)