-
Notifications
You must be signed in to change notification settings - Fork 4
/
dataset.py
495 lines (388 loc) · 17.5 KB
/
dataset.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
from torchvision import transforms
from PIL import Image
import os
import torch
import glob
from torchvision.datasets import MNIST, CIFAR10, FashionMNIST, ImageFolder
import numpy as np
from PIL import Image, ImageOps, ImageEnhance
import random
from torch.utils.data import Dataset
IMAGE_SIZE = 256
mean_train = [0.485, 0.456, 0.406]
std_train = [0.229, 0.224, 0.225]
def get_data_transforms(size, isize):
mean_train = [0.485, 0.456, 0.406]
std_train = [0.229, 0.224, 0.225]
data_transforms = transforms.Compose([
transforms.Resize((size, size)),
transforms.ToTensor(),
transforms.CenterCrop(isize),
transforms.Normalize(mean=mean_train,
std=std_train)])
gt_transforms = transforms.Compose([
transforms.Resize((size, size)),
transforms.CenterCrop(isize),
transforms.ToTensor()])
return data_transforms, gt_transforms
def get_data_transforms_augmix(size, isize):
mean_train = [0.485, 0.456, 0.406]
std_train = [0.229, 0.224, 0.225]
data_transforms = transforms.Compose([
transforms.AugMix(severity=10, all_ops=False),
transforms.Resize((size, size)),
transforms.ToTensor(),
transforms.CenterCrop(isize),
transforms.Normalize(mean=mean_train,
std=std_train)])
gt_transforms = transforms.Compose([
transforms.Resize((size, size)),
transforms.CenterCrop(isize),
transforms.ToTensor()])
return data_transforms, gt_transforms
class MVTecDataset(torch.utils.data.Dataset):
def __init__(self, root, transform, gt_transform, phase):
if phase == 'train':
self.img_path = os.path.join(root, 'train')
else:
self.img_path = os.path.join(root, 'test')
self.gt_path = os.path.join(root, 'ground_truth')
self.transform = transform
self.gt_transform = gt_transform
# load dataset
self.img_paths, self.gt_paths, self.labels, self.types = self.load_dataset() # self.labels => good : 0, anomaly : 1
def load_dataset(self):
img_tot_paths = []
gt_tot_paths = []
tot_labels = []
tot_types = []
defect_types = os.listdir(self.img_path)
for defect_type in defect_types:
if defect_type == 'good':
img_paths = glob.glob(os.path.join(self.img_path, defect_type) + "/*.png")
img_tot_paths.extend(img_paths)
gt_tot_paths.extend([0] * len(img_paths))
tot_labels.extend([0] * len(img_paths))
tot_types.extend(['good'] * len(img_paths))
else:
img_paths = glob.glob(os.path.join(self.img_path, defect_type) + "/*.png")
gt_paths = glob.glob(os.path.join(self.gt_path, defect_type) + "/*.png")
img_paths.sort()
gt_paths.sort()
img_tot_paths.extend(img_paths)
gt_tot_paths.extend(gt_paths)
tot_labels.extend([1] * len(img_paths))
tot_types.extend([defect_type] * len(img_paths))
assert len(img_tot_paths) == len(gt_tot_paths), "Something wrong with test and ground truth pair!"
return img_tot_paths, gt_tot_paths, tot_labels, tot_types
def __len__(self):
return len(self.img_paths)
def __getitem__(self, idx):
img_path, gt, label, img_type = self.img_paths[idx], self.gt_paths[idx], self.labels[idx], self.types[idx]
img = Image.open(img_path).convert('RGB')
img = self.transform(img)
if gt == 0:
gt = torch.zeros([1, img.size()[-2], img.size()[-2]])
else:
gt = Image.open(gt)
gt = self.gt_transform(gt)
assert img.size()[1:] == gt.size()[1:], "image.size != gt.size !!!"
return img, gt, label, img_type
class MVTecDatasetOOD(torch.utils.data.Dataset):
def __init__(self, root, transform, gt_transform, phase, _class_):
if phase == 'train':
self.img_path = os.path.join(root, 'train')
else:
self.img_path = os.path.join(root, 'test')
self.gt_path = os.path.join('/home/cttri/anomaly/data/mvtec/'+_class_, 'ground_truth')
self.transform = transform
self.gt_transform = gt_transform
# load dataset
self.img_paths, self.gt_paths, self.labels, self.types = self.load_dataset() # self.labels => good : 0, anomaly : 1
def load_dataset(self):
img_tot_paths = []
gt_tot_paths = []
tot_labels = []
tot_types = []
defect_types = os.listdir(self.img_path)
for defect_type in defect_types:
if defect_type == 'good':
img_paths = glob.glob(os.path.join(self.img_path, defect_type) + "/*.png")
img_tot_paths.extend(img_paths)
gt_tot_paths.extend([0] * len(img_paths))
tot_labels.extend([0] * len(img_paths))
tot_types.extend(['good'] * len(img_paths))
else:
img_paths = glob.glob(os.path.join(self.img_path, defect_type) + "/*.png")
gt_paths = glob.glob(os.path.join(self.gt_path, defect_type) + "/*.png")
img_paths.sort()
gt_paths.sort()
img_tot_paths.extend(img_paths)
gt_tot_paths.extend(gt_paths)
tot_labels.extend([1] * len(img_paths))
tot_types.extend([defect_type] * len(img_paths))
assert len(img_tot_paths) == len(gt_tot_paths), "Something wrong with test and ground truth pair!"
return img_tot_paths, gt_tot_paths, tot_labels, tot_types
def __len__(self):
return len(self.img_paths)
def __getitem__(self, idx):
img_path, gt, label, img_type = self.img_paths[idx], self.gt_paths[idx], self.labels[idx], self.types[idx]
img = Image.open(img_path).convert('RGB')
img = self.transform(img)
if gt == 0:
gt = torch.zeros([1, img.size()[-2], img.size()[-2]])
else:
gt = Image.open(gt)
gt = self.gt_transform(gt)
assert img.size()[1:] == gt.size()[1:], "image.size != gt.size !!!"
return img, gt, label, img_type
class MNIST_M(Dataset):
def __init__(self, path_to_image_tests, labs_test, transform):
self.full_filenames = path_to_image_tests
self.labels = labs_test
self.transform = transform
def __len__(self):
# return size of dataset
return len(self.full_filenames)
def __getitem__(self, idx):
# open image, apply transforms and return with label
image = Image.open(self.full_filenames[idx]) # PIL image
image = self.transform(image)
return image, self.labels[idx]
class PACSDataset(torch.utils.data.Dataset):
def __init__(self, root, transform):
self.img_path = root
self.transform = transform
# load dataset
self.img_paths = glob.glob(self.img_path+'/*.png')
def __len__(self):
return len(self.img_paths)
def __getitem__(self, idx):
img_path= self.img_paths[idx]
img = Image.open(img_path).convert('RGB')
img = self.transform(img)
return img, 0
def load_data(dataset_name='mnist',normal_class=0,batch_size='16'):
if dataset_name == 'cifar10':
img_transform = transforms.Compose([
transforms.Resize((32, 32)),
#transforms.CenterCrop(28),
transforms.ToTensor(),
transforms.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))
])
os.makedirs("./Dataset/CIFAR10/train", exist_ok=True)
dataset = CIFAR10('./Dataset/CIFAR10/train', train=True, download=True, transform=img_transform)
print("Cifar10 DataLoader Called...")
print("All Train Data: ", dataset.data.shape)
dataset.data = dataset.data[np.array(dataset.targets) == normal_class]
dataset.targets = [normal_class] * dataset.data.shape[0]
print("Normal Train Data: ", dataset.data.shape)
os.makedirs("./Dataset/CIFAR10/test", exist_ok=True)
test_set = CIFAR10("./Dataset/CIFAR10/test", train=False, download=True, transform=img_transform)
print("Test Train Data:", test_set.data.shape)
elif dataset_name == 'mnist':
img_transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor()
])
os.makedirs("./Dataset/MNIST/train", exist_ok=True)
dataset = MNIST('./Dataset/MNIST/train', train=True, download=True, transform=img_transform)
print("MNIST DataLoader Called...")
print("All Train Data: ", dataset.data.shape)
dataset.data = dataset.data[np.array(dataset.targets) == normal_class]
dataset.targets = [normal_class] * dataset.data.shape[0]
print("Normal Train Data: ", dataset.data.shape)
os.makedirs("./Dataset/MNIST/test", exist_ok=True)
test_set = MNIST("./Dataset/MNIST/test", train=False, download=True, transform=img_transform)
print("Test Train Data:", test_set.data.shape)
elif dataset_name == 'fashionmnist':
img_transform = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor()
])
os.makedirs("./Dataset/FashionMNIST/train", exist_ok=True)
dataset = FashionMNIST('./Dataset/FashionMNIST/train', train=True, download=True, transform=img_transform)
print("FashionMNIST DataLoader Called...")
print("All Train Data: ", dataset.data.shape)
dataset.data = dataset.data[np.array(dataset.targets) == normal_class]
dataset.targets = [normal_class] * dataset.data.shape[0]
print("Normal Train Data: ", dataset.data.shape)
os.makedirs("./Dataset/FashionMNIST/test", exist_ok=True)
test_set = FashionMNIST("./Dataset/FashionMNIST/test", train=False, download=True, transform=img_transform)
print("Test Train Data:", test_set.data.shape)
elif dataset_name == 'retina':
data_path = 'Dataset/OCT2017/train'
orig_transform = transforms.Compose([
transforms.Resize([128, 128]),
transforms.ToTensor()
])
dataset = ImageFolder(root=data_path, transform=orig_transform)
test_data_path = 'Dataset/OCT2017/test'
test_set = ImageFolder(root=test_data_path, transform=orig_transform)
else:
raise Exception(
"You enter {} as dataset, which is not a valid dataset for this repository!".format(dataset_name))
train_dataloader = torch.utils.data.DataLoader(
dataset,
batch_size=batch_size,
shuffle=True,
)
test_dataloader = torch.utils.data.DataLoader(
test_set,
batch_size=1,
shuffle=False,
)
return train_dataloader, test_dataloader
def int_parameter(level, maxval):
"""Helper function to scale `val` between 0 and maxval .
Args:
level: Level of the operation that will be between [0, `PARAMETER_MAX`].
maxval: Maximum value that the operation can have. This will be scaled to
level/PARAMETER_MAX.
Returns:
An int that results from scaling `maxval` according to `level`.
"""
return int(level * maxval / 10)
def float_parameter(level, maxval):
"""Helper function to scale `val` between 0 and maxval.
Args:
level: Level of the operation that will be between [0, `PARAMETER_MAX`].
maxval: Maximum value that the operation can have. This will be scaled to
level/PARAMETER_MAX.
Returns:
A float that results from scaling `maxval` according to `level`.
"""
return float(level) * maxval / 10.
def sample_level(n):
return np.random.uniform(low=0.1, high=n)
def autocontrast(pil_img, _):
return ImageOps.autocontrast(pil_img)
def equalize(pil_img, _):
return ImageOps.equalize(pil_img)
def posterize(pil_img, level):
level = int_parameter(sample_level(level), 4)
return ImageOps.posterize(pil_img, 4 - level)
def rotate(pil_img, level):
degrees = int_parameter(sample_level(level), 30)
if np.random.uniform() > 0.5:
degrees = -degrees
return pil_img.rotate(degrees, resample=Image.Resampling.BILINEAR)
def solarize(pil_img, level):
level = int_parameter(sample_level(level), 256)
return ImageOps.solarize(pil_img, 256 - level)
def shear_x(pil_img, level):
level = float_parameter(sample_level(level), 0.3)
if np.random.uniform() > 0.5:
level = -level
return pil_img.transform((IMAGE_SIZE, IMAGE_SIZE),
Image.Transform.AFFINE, (1, level, 0, 0, 1, 0),
resample=Image.Resampling.BILINEAR)
def shear_y(pil_img, level):
level = float_parameter(sample_level(level), 0.3)
if np.random.uniform() > 0.5:
level = -level
return pil_img.transform((IMAGE_SIZE, IMAGE_SIZE),
Image.Transform.AFFINE, (1, 0, 0, level, 1, 0),
resample=Image.Resampling.BILINEAR)
def translate_x(pil_img, level):
level = int_parameter(sample_level(level), IMAGE_SIZE / 3)
if np.random.random() > 0.5:
level = -level
return pil_img.transform((IMAGE_SIZE, IMAGE_SIZE),
Image.Transform.AFFINE, (1, 0, level, 0, 1, 0),
resample=Image.Resampling.BILINEAR)
def translate_y(pil_img, level):
level = int_parameter(sample_level(level), IMAGE_SIZE / 3)
if np.random.random() > 0.5:
level = -level
return pil_img.transform((IMAGE_SIZE, IMAGE_SIZE),
Image.Transform.AFFINE, (1, 0, 0, 0, 1, level),
resample=Image.Resampling.BILINEAR)
# operation that overlaps with ImageNet-C's test set
def color(pil_img, level):
level = float_parameter(sample_level(level), 1.8) + 0.1
return ImageEnhance.Color(pil_img).enhance(level)
# operation that overlaps with ImageNet-C's test set
def contrast(pil_img, level):
level = float_parameter(sample_level(level), 1.8) + 0.1
return ImageEnhance.Contrast(pil_img).enhance(level)
# operation that overlaps with ImageNet-C's test set
def brightness(pil_img, level):
level = float_parameter(sample_level(level), 1.8) + 0.1
return ImageEnhance.Brightness(pil_img).enhance(level)
# operation that overlaps with ImageNet-C's test set
def sharpness(pil_img, level):
level = float_parameter(sample_level(level), 1.8) + 0.1
return ImageEnhance.Sharpness(pil_img).enhance(level)
def augmvtec(image, preprocess, severity=3, width=3, depth=-1, alpha=1.):
aug_list = [
autocontrast, equalize, posterize, solarize, color, sharpness
]
severity = random.randint(0, severity)
ws = np.float32(np.random.dirichlet([1] * width))
m = np.float32(np.random.beta(alpha, alpha))
preprocess_img = preprocess(image)
mix = torch.zeros_like(preprocess_img)
for i in range(width):
image_aug = image.copy()
depth = depth if depth > 0 else np.random.randint(
1, 4)
for _ in range(depth):
op = np.random.choice(aug_list)
image_aug = op(image_aug, severity)
# Preprocessing commutes since all coefficients are convex
mix += ws[i] * preprocess(image_aug)
mixed = (1 - m) * preprocess_img + m * mix
return mixed
def augpacs(image, preprocess, severity=3, width=3, depth=-1, alpha=1.):
aug_list = [
autocontrast, equalize, posterize, solarize, color, contrast, brightness, sharpness
]
severity = random.randint(0, severity)
ws = np.float32(np.random.dirichlet([1] * width))
m = np.float32(np.random.beta(alpha, alpha))
preprocess_img = preprocess(image)
mix = torch.zeros_like(preprocess_img)
for i in range(width):
image_aug = image.copy()
depth = depth if depth > 0 else np.random.randint(
1, 4)
for _ in range(depth):
op = np.random.choice(aug_list)
image_aug = op(image_aug, severity)
# Preprocessing commutes since all coefficients are convex
mix += ws[i] * preprocess(image_aug)
mixed = (1 - m) * preprocess_img + m * mix
return mixed
class AugMixDatasetMVTec(torch.utils.data.Dataset):
"""Dataset wrapper to perform AugMix augmentation."""
def __init__(self, dataset, preprocess):
self.dataset = dataset
self.preprocess = preprocess
self.gray_preprocess = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=mean_train,
std=std_train),
transforms.Grayscale(3)
])
def __getitem__(self, i):
x, _ = self.dataset[i]
return self.preprocess(x), augmvtec(x, self.preprocess), self.gray_preprocess(x)
def __len__(self):
return len(self.dataset)
class AugMixDatasetPACS(torch.utils.data.Dataset):
"""Dataset wrapper to perform AugMix augmentation."""
def __init__(self, dataset, preprocess):
self.dataset = dataset
self.preprocess = preprocess
self.gray_preprocess = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=mean_train,
std=std_train),
transforms.Grayscale(3)
])
def __getitem__(self, i):
x, _ = self.dataset[i]
return self.preprocess(x), augpacs(x, self.preprocess), self.gray_preprocess(x)
def __len__(self):
return len(self.dataset)