-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
525 lines (414 loc) · 19.1 KB
/
utils.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import os
import shutil
import torch
import yaml
import numpy as np
import torch.utils.data as data
from PIL import Image
from typing import IO, Any, Callable, Dict, List, Optional, Tuple, Union
from torchvision.datasets import MNIST, FashionMNIST, KMNIST
import random
# <<Are labels always...>> load from the given numpy file and process into RGB format
class MyMNIST(data.Dataset):
"""`mnist <http://yann.lecun.com/exdb/mnist/>`_ Dataset.
Args:
root (string): Root directory of dataset where ``mnist/processed/training.pt``
and ``mnist/processed/test.pt`` exist.
train (bool, optional): If True, creates dataset from ``training.pt``,
otherwise from ``test.pt``.
download (bool, optional): If true, downloads the dataset from the internet and
puts it in root directory. If dataset is already downloaded, it is not
downloaded again.
transform (callable, optional): A function/transform that takes in an PIL image
and returns a transformed version. E.g, ``transforms.RandomCrop``
target_transform (callable, optional): A function/transform that takes in the
target and transforms it.
"""
def __init__(self, root, sample_file, label_file, transform=None, target_transform=None):
super(MyMNIST, self).__init__()
self.transform = transform
self.target_transform = target_transform
self.data = np.load(str(root) + '//' + sample_file)
self.targets = np.load(str(root) + '//' + label_file)
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is index of the target class.
"""
if len(self.data.shape) > 3:
img, target = self.data[:, :, :, index], int(self.targets[index])
else:
img, target = self.data[:, :, index], int(self.targets[index])
# doing this so that it is consistent with all other datasets
# to return a PIL Image
# img = Image.fromarray(img, mode='L')
img = Image.fromarray(np.uint8(img)).convert('RGB') # unit8
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self):
if len(self.data.shape) > 3:
return self.data.shape[3]
else:
return self.data.shape[2]
# <<Are labels always...>> load from the given numpy file
class MNIST_bg(data.Dataset):
"""`MNIST <http://yann.lecun.com/exdb/mnist/>`_ Dataset.
Args:
root (string): Root directory of dataset where ``MNIST/processed/training.pt``
and ``MNIST/processed/test.pt`` exist.
train (bool, optional): If True, creates dataset from ``training.pt``,
otherwise from ``test.pt``.
download (bool, optional): If true, downloads the dataset from the internet and
puts it in root directory. If dataset is already downloaded, it is not
downloaded again.
transform (callable, optional): A function/transform that takes in an PIL image
and returns a transformed version. E.g, ``transforms.RandomCrop``
target_transform (callable, optional): A function/transform that takes in the
target and transforms it.
"""
def __init__(self, root, sample_file, label_file, transform=None, target_transform=None):
super(MNIST_bg, self).__init__()
self.transform = transform
self.target_transform = target_transform
self.data = np.load(str(root) + '//' + sample_file)
self.targets = np.load(str(root) + '//' + label_file)
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is index of the target class.
"""
img, target = self.data[:, :, :, index], int(self.targets[index])
# doing this so that it is consistent with all other datasets
# to return a PIL Image
# img = Image.fromarray(img, mode='L')
img = Image.fromarray(np.uint8(img)).convert('L') # unit8
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self):
return self.data.shape[3]
# Override the path and 'L' model of torchvision MNIST
class MyMNISTRAW(MNIST):
def __init__(self,
root: str,
train: bool = True,
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
download: bool = False) -> None:
super().__init__(root, train, transform, target_transform, download) #
@property # fix the MNIST path: xxx/MNIST/raw
def raw_folder(self) -> str:
return os.path.join(self.root, 'MNIST', 'raw')
@property # fix the MNIST path: xxx/MNIST/processed
def processed_folder(self) -> str:
return os.path.join(self.root, 'MNIST', 'processed')
def __getitem__(self, index: int) -> Tuple[Any, Any]:
img, target = self.data[index], int(self.targets[index])
img = Image.fromarray(img.numpy()).convert('RGB') # original code uses mode='L'
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
# Override the path and 'L' model of torchvision FashionMNIST
class MyFashionMNIST(FashionMNIST):
def __init__(self,
root: str,
train: bool = True,
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
download: bool = False) -> None:
super().__init__(root, train, transform, target_transform, download)
@property # fix the FashionMNIST path: xxx/FashionMNIST/raw
def raw_folder(self) -> str:
return os.path.join(self.root, 'FashionMNIST', 'raw')
@property # fix the FashionMNIST path: xxx/FashionMNIST/processed
def processed_folder(self) -> str:
return os.path.join(self.root, 'FashionMNIST', 'processed')
def __getitem__(self, index: int) -> Tuple[Any, Any]:
img, target = self.data[index], int(self.targets[index])
img = Image.fromarray(img.numpy()).convert('RGB') # original code uses mode='L'
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
# Override the path and 'L' model of torchvision KMNIST
class MyKMNIST(KMNIST):
def __init__(self,
root: str,
train: bool = True,
transform: Optional[Callable] = None,
target_transform: Optional[Callable] = None,
download: bool = False) -> None:
super().__init__(root, train, transform, target_transform, download)
@property # fix the KMNIST path: xxx/KMNIST/raw
def raw_folder(self) -> str:
return os.path.join(self.root, 'KMNIST', 'raw')
@property # fix the KMNIST path: xxx/KMNIST/processed
def processed_folder(self) -> str:
return os.path.join(self.root, 'KMNIST', 'processed')
def __getitem__(self, index: int) -> Tuple[Any, Any]:
img, target = self.data[index], int(self.targets[index])
img = Image.fromarray(img.numpy()).convert('RGB') # original code uses mode='L'
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
# copy from the rotation code
class CIFAR10_1(data.Dataset):
def __init__(self, root, transform=None, target_transform=None, version='v6'):
self.data = np.load('%s/cifar10.1_%s_data.npy' % (root, version))
self.targets = np.load('%s/cifar10.1_%s_labels.npy' % (root, version)).astype('long')
self.transform = transform
self.target_transform = target_transform
def __getitem__(self, index):
img, target = self.data[index], self.targets[index]
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self):
return len(self.targets)
class CIFAR10_c(data.Dataset):
def __init__(self, root, transform=None, target_transform=None):
self.data = np.load(root)
self.targets = np.load('%s/labels.npy' % (os.path.dirname(root))).astype('long')
self.transform = transform
self.target_transform = target_transform
def __getitem__(self, index):
img, target = self.data[index], self.targets[index]
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self):
return len(self.targets)
class CIFAR100_c(data.Dataset):
def __init__(self, root, transform=None, target_transform=None):
self.data = np.load(root)
self.targets = np.load('%s/labels.npy' % (os.path.dirname(root))).astype('long')
self.transform = transform
self.target_transform = target_transform
def __getitem__(self, index):
img, target = self.data[index], self.targets[index]
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self):
return len(self.targets)
class MyCIFAR10(data.Dataset):
"""`mnist <http://yann.lecun.com/exdb/mnist/>`_ Dataset.
Args:
root (string): Root directory of dataset where ``mnist/processed/training.pt``
and ``mnist/processed/test.pt`` exist.
train (bool, optional): If True, creates dataset from ``training.pt``,
otherwise from ``test.pt``.
download (bool, optional): If true, downloads the dataset from the internet and
puts it in root directory. If dataset is already downloaded, it is not
downloaded again.
transform (callable, optional): A function/transform that takes in an PIL image
and returns a transformed version. E.g, ``transforms.RandomCrop``
target_transform (callable, optional): A function/transform that takes in the
target and transforms it.
"""
def __init__(self, root, sample_file, label_file, transform=None, target_transform=None):
super(MyCIFAR10, self).__init__()
self.transform = transform
self.target_transform = target_transform
self.data = np.load(str(root) + '//' + sample_file)
self.targets = np.load(str(root) + '//' + label_file).astype('long')
def __getitem__(self, index: int) -> Tuple[Any, Any]:
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is index of the target class.
"""
img, target = self.data[index], self.targets[index]
# doing this so that it is consistent with all other datasets
# to return a PIL Image
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self):
return len(self.targets)
class MyCIFAR100(data.Dataset):
"""`mnist <http://yann.lecun.com/exdb/mnist/>`_ Dataset.
Args:
root (string): Root directory of dataset where ``mnist/processed/training.pt``
and ``mnist/processed/test.pt`` exist.
train (bool, optional): If True, creates dataset from ``training.pt``,
otherwise from ``test.pt``.
download (bool, optional): If true, downloads the dataset from the internet and
puts it in root directory. If dataset is already downloaded, it is not
downloaded again.
transform (callable, optional): A function/transform that takes in an PIL image
and returns a transformed version. E.g, ``transforms.RandomCrop``
target_transform (callable, optional): A function/transform that takes in the
target and transforms it.
"""
def __init__(self, root, sample_file, label_file, transform=None, target_transform=None):
super(MyCIFAR100, self).__init__()
self.transform = transform
self.target_transform = target_transform
self.data = np.load(str(root) + '//' + sample_file)
self.targets = np.load(str(root) + '//' + label_file).astype('long')
def __getitem__(self, index: int) -> Tuple[Any, Any]:
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is index of the target class.
"""
img, target = self.data[index], self.targets[index]
# doing this so that it is consistent with all other datasets
# to return a PIL Image
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def __len__(self):
return len(self.targets)
class MyCoco(data.Dataset):
labels_map = {
'airplane': 0, # train: 1680, val: 553
'aeroplane': 0,
'bicycle': 1, # train: 929, val: 428
'bird': 2, # train: 959, val: 451
'boat': 3, # train: 888, val: 475
'bottle': 4, # train: 377, val: 167
'bus': 5, # train: 2000, val: 995
'car': 6, # train: 1761, val: 886
'dog': 7, # train: 1977, val: 1052
'horse': 8, # train: 1834, val: 867
'motorcycle': 9, # train: 2000, val: 1079
'motorbike': 9,
'person': 10, # train: 2000, val: 2000
'tv': 11, # train: 1400, val: 688
'tvmonitor': 11,
}
# Way 1: Convert the images in train, val, seed, unseen test sets of coco to numpy format
# def __init__(self, root, transform=None, target_transform=None) -> None:
# super(MyCoco, self).__init__()
# self.transform = transform
# self.target_transform = target_transform
#
# self.data = []
# self.targets = []
#
# for class_folder in os.listdir(root):
# class_folder_path = os.path.join(root, class_folder)
# class_files = os.listdir(class_folder_path)
# for img_name in class_files:
# img_path = os.path.join(class_folder_path, img_name)
# target = self.labels_map[class_folder]
# # 224 when using models pre-trained on ImageNet
# img = Image.open(img_path).resize((224, 224)).convert('RGB')
# img = np.array(img)
# self.data.append(img)
# self.targets.append(target)
# self.data = np.stack(self.data, axis=3)
# self.targets = np.array(self.targets)
# np.save(root+"/data.npy", self.data)
# np.save(root+"/targets.npy", self.targets)
# Way 2: load the numpy files directly
def __init__(self, root, sample_file, label_file, transform=None, target_transform=None) -> None:
super(MyCoco, self).__init__()
self.transform = transform
self.target_transform = target_transform
self.data = np.load(str(root) + '//' + sample_file)
self.targets = np.load(str(root) + '//' + label_file).astype('long')
def __len__(self):
return len(self.targets)
def __getitem__(self, index):
if len(self.data.shape) > 3:
img, target = self.data[:, :, :, index], int(self.targets[index])
else:
img, target = self.data[:, :, index], int(self.targets[index])
# doing this so that it is consistent with all other datasets
# to return a PIL Image
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
class MyTinyImageNet(data.Dataset):
def __init__(self, root, sample_file, label_file, transform=None, target_transform=None) -> None:
super(MyTinyImageNet, self).__init__()
self.transform = transform
self.target_transform = target_transform
self.data = np.load(str(root) + '//' + sample_file)
self.targets = np.load(str(root) + '//' + label_file).astype('long')
def __len__(self):
return len(self.targets)
def __getitem__(self, index):
if len(self.data.shape) > 3:
img, target = self.data[:, :, :, index], int(self.targets[index])
else:
img, target = self.data[:, :, index], int(self.targets[index])
# doing this so that it is consistent with all other datasets
# to return a PIL Image
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target
def set_seed_torch(seed=0):
# Set seed based on args.seed and the update number so that we get
# reproducible results when resuming from checkpoints
assert isinstance(seed, int)
random.seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def save_checkpoint(state, is_best, filename='checkpoint.pth'): # forbidden to save as tar.gz
torch.save(state, filename)
if is_best:
shutil.copyfile(filename, os.path.split(filename)[0] + '/checkpoint_best.pth')
def save_config_file(model_checkpoints_folder, args):
if not os.path.exists(model_checkpoints_folder):
os.makedirs(model_checkpoints_folder)
with open(os.path.join(model_checkpoints_folder, 'config.yml'), 'w') as outfile:
yaml.dump(args, outfile, default_flow_style=False)
def accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res