-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataset.py
More file actions
421 lines (368 loc) · 13.9 KB
/
dataset.py
File metadata and controls
421 lines (368 loc) · 13.9 KB
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
import os
import pandas as pd
import numpy as np
from enum import Enum
import PIL
import torch
from torchvision import transforms
from omegaconf import ListConfig
MVTecAD_CLASSNAMES = [
"bottle",
"cable",
"capsule",
"carpet",
"grid",
"hazelnut",
"leather",
"metal_nut",
"pill",
"screw",
"tile",
"toothbrush",
"transistor",
"wood",
"zipper",
]
VisA_CLASSNAMES = [
'candle',
'capsules',
'cashew',
'chewinggum',
'fryum',
'macaroni1',
'macaroni2',
'pcb1',
'pcb2',
'pcb3',
'pcb4',
'pipe_fryum'
]
BTAD_CLASSNAMES = [
'01',
'02',
'03'
]
MPDD_CLASSNAMES = [
'bracket_black',
'bracket_brown',
'bracket_white',
'connector',
'metal_plate',
'tubes'
]
IMAGENET_MEAN = [0.485, 0.456, 0.406]
IMAGENET_STD = [0.229, 0.224, 0.225]
class DatasetSplit(Enum):
TRAIN = "train"
VAL = "val"
TEST = "test"
class MVTecDataset(torch.utils.data.Dataset):
"""
PyTorch Dataset for MVTec.
"""
def __init__(
self,
source,
classname,
resize=256,
imagesize=224,
split=DatasetSplit.TRAIN,
train_val_split=1.0,
class_list=MVTecAD_CLASSNAMES,
normal_state='good',
**kwargs,
):
"""
Args:
source: [str]. Path to the MVTec data folder.
classname: [str or None]. Name of MVTec class that should be
provided in this dataset. If None, the datasets
iterates over all available images.
resize: [int]. (Square) Size the loaded image initially gets
resized to.
imagesize: [int]. (Square) Size the resized loaded image gets
(center-)cropped to.
split: [enum-option]. Indicates if training or test split of the
data should be used. Has to be an option taken from
DatasetSplit, e.g. mvtec.DatasetSplit.TRAIN. Note that
mvtec.DatasetSplit.TEST will also load mask data.
"""
super().__init__()
self.source = source
self.split = split
self.classname = classname
self.train_val_split = train_val_split
self.class_list = class_list
self.normal_state = normal_state
self.imgpaths_per_class, self.data_to_iterate = self.get_image_data()
self.transform_img = [
transforms.Resize(resize),
transforms.CenterCrop(imagesize),
transforms.ToTensor(),
transforms.Normalize(mean=IMAGENET_MEAN, std=IMAGENET_STD),
]
self.transform_img = transforms.Compose(self.transform_img)
self.transform_mask = [
transforms.Resize(resize),
transforms.CenterCrop(imagesize),
transforms.ToTensor(),
]
self.transform_mask = transforms.Compose(self.transform_mask)
self.imagesize = (3, imagesize, imagesize)
def __getitem__(self, idx):
classname, anomaly, image_path, mask_path = self.data_to_iterate[idx]
image = PIL.Image.open(image_path).convert("RGB")
image = self.transform_img(image)
if self.split == DatasetSplit.TEST and mask_path is not None:
mask = PIL.Image.open(mask_path)
mask = self.transform_mask(mask)
else:
mask = torch.zeros([1, *image.size()[1:]])
mask[mask > 0.0], mask[mask == 0.0] = 1, 0
return {
"image": image,
"mask": mask,
"classname": classname,
"anomaly": anomaly,
"is_anomaly": int(anomaly != self.normal_state),
"image_name": "/".join(image_path.split("/")[-4:]),
"image_path": image_path,
}
def __len__(self):
return len(self.data_to_iterate)
def get_image_data(self):
if self.classname == 'all':
self.classnames_to_use = self.class_list
elif isinstance(self.classname, list) or isinstance(self.classname, ListConfig):
for c in self.classname:
assert c in self.class_list
self.classnames_to_use = self.classname
else:
self.classnames_to_use = [self.classname]
imgpaths_per_class = {}
maskpaths_per_class = {}
for classname in self.classnames_to_use:
classpath = os.path.join(self.source, classname, self.split.value)
maskpath = os.path.join(self.source, classname, "ground_truth")
anomaly_types = os.listdir(classpath)
imgpaths_per_class[classname] = {}
maskpaths_per_class[classname] = {}
for anomaly in anomaly_types:
anomaly_path = os.path.join(classpath, anomaly)
anomaly_files = sorted(os.listdir(anomaly_path))
imgpaths_per_class[classname][anomaly] = [
os.path.join(anomaly_path, x) for x in anomaly_files
]
if self.train_val_split < 1.0:
n_images = len(imgpaths_per_class[classname][anomaly])
train_val_split_idx = int(n_images * self.train_val_split)
if self.split == DatasetSplit.TRAIN:
imgpaths_per_class[classname][anomaly] = imgpaths_per_class[
classname
][anomaly][:train_val_split_idx]
elif self.split == DatasetSplit.VAL:
imgpaths_per_class[classname][anomaly] = imgpaths_per_class[
classname
][anomaly][train_val_split_idx:]
if self.split == DatasetSplit.TEST and anomaly != self.normal_state:
anomaly_mask_path = os.path.join(maskpath, anomaly)
anomaly_mask_files = sorted(os.listdir(anomaly_mask_path))
maskpaths_per_class[classname][anomaly] = [
os.path.join(anomaly_mask_path, x) for x in anomaly_mask_files
]
else:
maskpaths_per_class[classname][self.normal_state] = None
# Unrolls the data dictionary to an easy-to-iterate list.
data_to_iterate = []
for classname in sorted(imgpaths_per_class.keys()):
for anomaly in sorted(imgpaths_per_class[classname].keys()):
for i, image_path in enumerate(imgpaths_per_class[classname][anomaly]):
data_tuple = [classname, anomaly, image_path]
if self.split == DatasetSplit.TEST and anomaly != self.normal_state:
data_tuple.append(maskpaths_per_class[classname][anomaly][i])
else:
data_tuple.append(None)
data_to_iterate.append(data_tuple)
return imgpaths_per_class, data_to_iterate
class BTADDataset(MVTecDataset):
"""
PyTorch Dataset for VisA.
"""
def __init__(
self,
source,
classname,
resize=256,
imagesize=224,
split=DatasetSplit.TRAIN,
train_val_split=1.0,
class_list=BTAD_CLASSNAMES,
normal_state='ok',
**kwargs,
):
"""
Args:
source: [str]. Path to the MVTec data folder.
classname: [str or None]. Name of MVTec class that should be
provided in this dataset. If None, the datasets
iterates over all available images.
resize: [int]. (Square) Size the loaded image initially gets
resized to.
imagesize: [int]. (Square) Size the resized loaded image gets
(center-)cropped to.
split: [enum-option]. Indicates if training or test split of the
data should be used. Has to be an option taken from
DatasetSplit, e.g. mvtec.DatasetSplit.TRAIN. Note that
mvtec.DatasetSplit.TEST will also load mask data.
"""
super().__init__(
source = source,
classname = classname,
resize = resize,
imagesize = imagesize,
split = split,
train_val_split = train_val_split,
class_list = class_list,
normal_state = normal_state
)
class MPDDDataset(MVTecDataset):
"""
PyTorch Dataset for VisA.
"""
def __init__(
self,
source,
classname,
imagesize=224,
split=DatasetSplit.TRAIN,
train_val_split=1.0,
class_list=MPDD_CLASSNAMES,
normal_state='good',
**kwargs,
):
"""
Args:
source: [str]. Path to the MVTec data folder.
classname: [str or None]. Name of MVTec class that should be
provided in this dataset. If None, the datasets
iterates over all available images.
resize: [int]. (Square) Size the loaded image initially gets
resized to.
imagesize: [int]. (Square) Size the resized loaded image gets
(center-)cropped to.
split: [enum-option]. Indicates if training or test split of the
data should be used. Has to be an option taken from
DatasetSplit, e.g. mvtec.DatasetSplit.TRAIN. Note that
mvtec.DatasetSplit.TEST will also load mask data.
"""
super().__init__(
source = source,
classname = classname,
imagesize = imagesize,
split = split,
train_val_split = train_val_split,
class_list = class_list,
normal_state = normal_state
)
class VisADataset(MVTecDataset):
"""
PyTorch Dataset for VisA.
"""
def __init__(
self,
source,
classname,
resize=256,
imagesize=224,
split=DatasetSplit.TRAIN,
train_val_split=1.0,
class_list=VisA_CLASSNAMES,
normal_state='good',
**kwargs,
):
"""
Args:
source: [str]. Path to the MVTec data folder.
classname: [str or None]. Name of MVTec class that should be
provided in this dataset. If None, the datasets
iterates over all available images.
resize: [int]. (Square) Size the loaded image initially gets
resized to.
imagesize: [int]. (Square) Size the resized loaded image gets
(center-)cropped to.
split: [enum-option]. Indicates if training or test split of the
data should be used. Has to be an option taken from
DatasetSplit, e.g. mvtec.DatasetSplit.TRAIN. Note that
mvtec.DatasetSplit.TEST will also load mask data.
"""
super().__init__(
source = source,
classname = classname,
resize = resize,
imagesize = imagesize,
split = split,
train_val_split = train_val_split,
class_list = class_list,
normal_state = normal_state
)
def __getitem__(self, idx):
classname, anomaly, image_path, mask_path = self.data_to_iterate[idx]
image = PIL.Image.open(image_path).convert("RGB")
image = self.transform_img(image)
if self.split == DatasetSplit.TEST and mask_path is not None:
mask = PIL.Image.open(mask_path)
mask = np.array(mask)
mask[mask > 0] = 255
mask = PIL.Image.fromarray(mask)
mask = self.transform_mask(mask)
else:
mask = torch.zeros([1, *image.size()[1:]])
mask[mask > 0.0], mask[mask == 0.0] = 1, 0
return {
"image": image,
"mask": mask,
"classname": classname,
"anomaly": anomaly,
"is_anomaly": int(anomaly != self.normal_state),
"image_name": "/".join(image_path.split("/")[-4:]),
"image_path": image_path,
}
def get_image_data(self):
self.classnames_to_use = [self.classname] if self.classname != 'all' else self.class_list
split_csv = pd.read_csv(os.path.join(self.source, 'split_csv', '1cls.csv'))
split_csv_cls = split_csv[
split_csv['object'].isin(self.classnames_to_use) & (split_csv['split'] == self.split.value)
]
split_csv_cls = split_csv_cls.drop('split', axis=1)
# data_to_iterate
split_csv_cls.loc[split_csv_cls['label'] == 'normal', 'label'] = self.normal_state
split_csv_cls.loc[split_csv_cls['label'] == self.normal_state, 'mask'] = None
split_csv_cls['image'] = split_csv_cls['image'].apply(lambda x: os.path.join(self.source, x))
split_csv_cls['mask'] = split_csv_cls['mask'].apply(lambda x: os.path.join(self.source, x) if x != None else x)
data_to_iterate = list(map(list, split_csv_cls.values))
# imgpaths_per_class
imgpaths_per_class = {}
for cls in self.classnames_to_use:
imgpaths_per_class[cls] = {}
split_csv_cls_path = split_csv_cls[split_csv_cls['object']==cls]
for label in split_csv_cls_path.label.unique():
imgpaths_per_class[cls][label] = split_csv_cls_path.loc[split_csv_cls_path['label'] == label, 'image'].tolist()
return imgpaths_per_class, data_to_iterate
def create_dataset(
dataname: str,
source: str,
classname: str,
resize: int = 256,
imagesize: int = 224,
train_val_split: float = 1.0,
split: DatasetSplit = DatasetSplit.TRAIN
):
dataset = eval(f'{dataname}Dataset')(
source = source,
classname = classname,
resize = resize,
imagesize = imagesize,
split = split,
train_val_split = train_val_split
)
return dataset