-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
196 lines (175 loc) · 7.87 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
import matplotlib.pyplot as plt
import random
import numpy as np
import os
from PIL import Image
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
import torch
DEFAULT_MVTEC_DIR = "../data/mvtec_anomaly_detection/"
DEFAULT_LIVESTOCK_DIR = "../data/livestock/part_III_cropped"
class MVTEC_train_Dataset(Dataset):
def __init__(self, img_size, category, fake_dataset_size, transform=None,
target_transform=None):
self.img_dir = os.path.join(DEFAULT_MVTEC_DIR, category, "train/good/")
self.img_files = [os.path.join(self.img_dir, img)
for img in os.listdir(self.img_dir)
if os.path.isfile(os.path.join(self.img_dir, img))]
self.img_size = img_size
if category in ['hazelnut', 'bottle', 'metal_nut', 'screw']:
self.transform = transforms.Compose([
transforms.RandomRotation(degrees=45),
transforms.RandomVerticalFlip(),
transforms.RandomHorizontalFlip(),
transforms.Resize(size=(img_size, img_size)),
])
elif category in ['toothbrush', 'transistor']:
self.transform = transforms.Compose([
transforms.RandomRotation(degrees=5),
transforms.RandomHorizontalFlip(),
transforms.Resize(size=(img_size, img_size)),
])
elif category in ['capsule', 'zipper']:
self.transform = transforms.Compose([
transforms.RandomRotation(degrees=5),
transforms.RandomVerticalFlip(),
transforms.Resize(size=(img_size, img_size)),
])
elif category in ['cable', 'pill']:
self.transform = transforms.Compose([
transforms.RandomRotation(degrees=5),
transforms.Resize(size=(img_size, img_size)),
])
elif category in ['wood', 'leather', 'grid', 'carpet', 'tile']: # textures
self.transform = transforms.Compose([
transforms.Resize(size=(img_size, img_size)),
transforms.RandomVerticalFlip(),
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(size=(img_size, img_size),
pad_if_needed=True, padding_mode="symmetric"),
])
else:
raise RuntimeError("Bad category")
self.img_size = img_size
self.target_transform = target_transform
self.fake_dataset_size = fake_dataset_size
self.nb_img = len(self.img_files)
self.nb_channels = 3
def __len__(self):
return max(self.nb_img, self.fake_dataset_size)
def __getitem__(self, index):
index = index % self.nb_img
img = Image.open(self.img_files[index])
np_img = np.asarray(img)
if np_img.ndim == 2:
np_img = np.stack([np_img for i in range(3)], axis=2)
img = Image.fromarray(np_img)
out = self.transform(img)
out = transforms.ToTensor()(out)
return out, 1 # one if the ground truth if there is one
class MVTEC_test_Dataset(Dataset):
def __init__(self, img_size, category, defect,
transform=None, target_transform=None):
if defect is not None:
self.img_dir = os.path.join(DEFAULT_MVTEC_DIR, category, "test", defect)
else:
defects = os.listdir(os.path.join(DEFAULT_MVTEC_DIR, category, "test"))
defect = defects[0]
self.img_dir = os.path.join(DEFAULT_MVTEC_DIR, category, "test", defect)
self.img_files = [os.path.join(self.img_dir, img)
for img in os.listdir(self.img_dir)
if os.path.isfile(os.path.join(self.img_dir, img))]
self.gt_dir = os.path.join(DEFAULT_MVTEC_DIR, category, "ground_truth", defect)
if defect != "good":
self.gt_files = [os.path.join(self.gt_dir, n[-7:-4] + "_mask.png")
for n in self.img_files]
else:
self.gt_files = []
self.category = category
self.ori_size = 1024
self.img_size = img_size
self.transform = transforms.Compose([
transforms.Resize(size=(self.img_size, self.img_size)),
transforms.ToTensor(),
])
self.target_transform = transforms.Compose([
transforms.Resize(size=(self.img_size, self.img_size)),
transforms.ToTensor(),
])
self.img_size = img_size
self.nb_channels = 3
def __len__(self):
return len(self.img_files)
def __getitem__(self, index):
img = Image.open(self.img_files[index])
np_img = np.asarray(img)
if np_img.ndim == 2:
np_img = np.stack([np_img for i in range(3)], axis=2)
img = Image.fromarray(np_img)
transformed_img = self.transform(img)
if self.gt_files:
gt = Image.open(self.gt_files[index])
transformed_gt = self.target_transform(gt)
else:
transformed_gt = torch.zeros(transformed_img.shape)
return transformed_img, transformed_gt
class LivestockTrainDataset(Dataset):
def __init__(self, img_size, fake_dataset_size):
if os.path.isdir(DEFAULT_LIVESTOCK_DIR):
self.img_dir = os.path.join(DEFAULT_LIVESTOCK_DIR, "Train")
else:
self.img_dir = UNDEFINE
self.img_files = list(
np.random.choice([os.path.join(self.img_dir, img)
for img in os.listdir(self.img_dir)
if (os.path.isfile(os.path.join(self.img_dir,
img)) and img.endswith('jpg'))],
size=fake_dataset_size)
)
self.fake_dataset_size = fake_dataset_size # needed otherwise there are
# 125000 images, and this is too much
self.transform = transforms.Compose([
transforms.Resize(size=(img_size, img_size)),
transforms.PILToTensor(),
transforms.Lambda(lambda img: img.float()),
transforms.Lambda(lambda img: img / 255.)
])
self.nb_img = len(self.img_files)
self.nb_channels = 3
def __len__(self):
return self.nb_img
def __getitem__(self, index):
index = index % self.nb_img
img = Image.open(self.img_files[index])
return self.transform(img), 1 # one if the ground truth if there is one
class LivestockTestDataset(Dataset):
def __init__(self, img_size, fake_dataset_size):
if os.path.isdir(DEFAULT_LIVESTOCK_DIR):
self.img_dir = os.path.join(DEFAULT_LIVESTOCK_DIR, "Test")
else:
self.img_dir = UNDEFINE
self.img_files = list(
np.random.choice(
[os.path.join(self.img_dir, img)
for img in os.listdir(self.img_dir)
if (os.path.isfile(os.path.join(self.img_dir, img))
and img.endswith('.jpg'))],
size=fake_dataset_size)
)
self.fake_dataset_size = fake_dataset_size # needed otherwise there are
self.gt_files = [s.replace(".jpg", "_gt.png") for s in self.img_files]
self.transform = transforms.Compose([
transforms.Resize(size=(img_size, img_size)),
transforms.PILToTensor(),
transforms.Lambda(lambda img: img.float()),
transforms.Lambda(lambda img: img / 255.)
])
self.nb_img = len(self.img_files) # recompute the size,
# fake_dataset_size may have changed it
self.nb_channels = 3
def __len__(self):
return self.fake_dataset_size
def __getitem__(self, index):
img = Image.open(self.img_files[index])
gt = Image.open(self.gt_files[index])
return self.transform(img), self.transform(gt)