forked from cool-xuan/msflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasets.py
176 lines (148 loc) · 6.51 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
import os
from PIL import Image
import numpy as np
import torch
from torchvision.io import read_video, write_jpeg
from torch.utils.data import Dataset
from torchvision import transforms as T
from torchvision.transforms import InterpolationMode
__all__ = ('MVTecDataset', )
MVTEC_CLASS_NAMES = ['bottle', 'cable', 'capsule', 'carpet', 'grid',
'hazelnut', 'leather', 'metal_nut', 'pill', 'screw',
'tile', 'toothbrush', 'transistor', 'wood', 'zipper']
class MVTecDataset(Dataset):
def __init__(self, c, is_train=True):
assert c.class_name in MVTEC_CLASS_NAMES, 'class_name: {}, should be in {}'.format(c.class_name, MVTEC_CLASS_NAMES)
self.dataset_path = c.data_path
self.class_name = c.class_name
self.is_train = is_train
self.input_size = c.input_size
# load dataset
self.x, self.y, self.mask = self.load_dataset_folder()
# set transforms
if is_train:
self.transform_x = T.Compose([
T.Resize(c.input_size, InterpolationMode.LANCZOS),
T.ToTensor()])
# test:
else:
self.transform_x = T.Compose([
T.Resize(c.input_size, InterpolationMode.LANCZOS),
T.ToTensor()])
# mask
self.transform_mask = T.Compose([
T.Resize(c.input_size, InterpolationMode.NEAREST),
T.ToTensor()])
self.normalize = T.Compose([T.Normalize(c.img_mean, c.img_std)])
def __getitem__(self, idx):
x, y, mask = self.x[idx], self.y[idx], self.mask[idx]
#x = Image.open(x).convert('RGB')
x = Image.open(x)
if self.class_name in ['zipper', 'screw', 'grid']: # handle greyscale classes
x = np.expand_dims(np.array(x), axis=2)
x = np.concatenate([x, x, x], axis=2)
x = Image.fromarray(x.astype('uint8')).convert('RGB')
#
x = self.normalize(self.transform_x(x))
#
if y == 0:
mask = torch.zeros([1, *self.input_size])
else:
mask = Image.open(mask)
mask = self.transform_mask(mask)
return x, y, mask
def __len__(self):
return len(self.x)
def load_dataset_folder(self):
phase = 'train' if self.is_train else 'test'
x, y, mask = [], [], []
img_dir = os.path.join(self.dataset_path, self.class_name, phase)
gt_dir = os.path.join(self.dataset_path, self.class_name, 'ground_truth')
img_types = sorted(os.listdir(img_dir))
for img_type in img_types:
# load images
img_type_dir = os.path.join(img_dir, img_type)
if not os.path.isdir(img_type_dir):
continue
img_fpath_list = sorted([os.path.join(img_type_dir, f)
for f in os.listdir(img_type_dir)])
x.extend(img_fpath_list)
# load gt labels
if img_type == 'good':
y.extend([0] * len(img_fpath_list))
mask.extend([None] * len(img_fpath_list))
else:
y.extend([1] * len(img_fpath_list))
gt_type_dir = os.path.join(gt_dir, img_type)
img_fname_list = [os.path.splitext(os.path.basename(f))[0] for f in img_fpath_list]
gt_fpath_list = [os.path.join(gt_type_dir, img_fname + '_mask.png')
for img_fname in img_fname_list]
mask.extend(gt_fpath_list)
assert len(x) == len(y), 'number of x and y should be same'
return list(x), list(y), list(mask)
VISA_CLASS_NAMES = ['candle', 'capsules', 'cashew', 'chewinggum',
'fryum', 'macaroni1', 'macaroni2',
'pcb1', 'pcb2', 'pcb3', 'pcb4', 'pipe_fryum']
class VisADataset(Dataset):
def __init__(self, c, is_train=True):
assert c.class_name in VISA_CLASS_NAMES, 'class_name: {}, should be in {}'.format(c.class_name, MVTEC_CLASS_NAMES)
self.dataset_path = c.data_path
self.class_name = c.class_name
self.is_train = is_train
self.input_size = c.input_size
# load dataset
self.x, self.y, self.mask = self.load_dataset_folder()
# set transforms
if is_train:
self.transform_x = T.Compose([
T.Resize(c.input_size, InterpolationMode.LANCZOS),
T.ToTensor()])
# test:
else:
self.transform_x = T.Compose([
T.Resize(c.input_size, InterpolationMode.LANCZOS),
T.ToTensor()])
# mask
self.transform_mask = T.Compose([
T.Resize(c.input_size, InterpolationMode.NEAREST),
T.ToTensor()])
self.normalize = T.Compose([T.Normalize(c.img_mean, c.img_std)])
def __getitem__(self, idx):
x, y, mask = self.x[idx], self.y[idx], self.mask[idx]
x = Image.open(x)
x = self.normalize(self.transform_x(x))
if y == 0:
mask = torch.zeros([1, *self.input_size])
else:
mask = Image.open(mask)
mask = self.transform_mask(mask)
return x, y, mask
def __len__(self):
return len(self.x)
def load_dataset_folder(self):
phase = 'train' if self.is_train else 'test'
x, y, mask = [], [], []
img_dir = os.path.join(self.dataset_path, self.class_name, phase)
gt_dir = os.path.join(self.dataset_path, self.class_name, 'ground_truth')
img_types = sorted(os.listdir(img_dir))
for img_type in img_types:
# load images
img_type_dir = os.path.join(img_dir, img_type)
if not os.path.isdir(img_type_dir):
continue
img_fpath_list = sorted([os.path.join(img_type_dir, f)
for f in os.listdir(img_type_dir)])
x.extend(img_fpath_list)
# load gt labels
if img_type == 'good':
y.extend([0] * len(img_fpath_list))
mask.extend([None] * len(img_fpath_list))
else:
y.extend([1] * len(img_fpath_list))
gt_type_dir = os.path.join(gt_dir, img_type)
img_fname_list = [os.path.splitext(os.path.basename(f))[0] for f in img_fpath_list]
gt_fpath_list = [os.path.join(gt_type_dir, img_fname + '.png')
for img_fname in img_fname_list]
mask.extend(gt_fpath_list)
assert len(x) == len(y), 'number of x and y should be same'
return list(x), list(y), list(mask)