-
Notifications
You must be signed in to change notification settings - Fork 2
/
datasets.py
259 lines (222 loc) · 10.5 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
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
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
import cv2
import itertools
# Định sẵn đường đẫn đến các dataset
DEFAULT_LIVESTOCK_DIR = "./data/livestock/part_III_cropped"
DEFAULT_MVTEC_DIR = "E:/UnitWTF/lab ai/mvtec_anomaly_detection/wood"
DEFAULT_MIAD_DIR = "E:/UnitWTF/dataset/photovoltaic_module"
# Traning Dataset for livestock
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") # set image dir
else:
self.img_dir = UNDEFINE # set undefine if not found
#get a list of image path
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)
)
# Tuỳ chỉnh độ dài data
self.fake_dataset_size = fake_dataset_size # needed otherwise there are
# 125000 images, and this is too much
#Augmentation setting
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.)
])
#number of image
self.nb_img = len(self.img_files)
#number of (color) channel
self.nb_channels = 3
#get length of data
def __len__(self):
return max(self.nb_img, self.fake_dataset_size)
#get specific item in the dataset via index
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)
class MVTecTrainDataset(Dataset):
def __init__(self, img_size, fake_dataset_size):
if os.path.isdir(DEFAULT_MVTEC_DIR):
self.img_dir = os.path.join(DEFAULT_MVTEC_DIR, "train", "good")
else:
self.img_dir = UNDEFINE
self.img_files = list(
[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('png'))]
)
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]).convert("RGB")
# img = Image.open(self.img_files[index])
return self.transform(img), 1 # one if the ground truth if there is one
class MVTecTestDataset(Dataset):
def __init__(self, img_size, fake_dataset_size):
if os.path.isdir(DEFAULT_MVTEC_DIR):
self.default_dir = os.path.join(DEFAULT_MVTEC_DIR, "test")
self.img_dir = list(os.path.join(self.default_dir, img_fol) for img_fol in os.listdir(self.default_dir) if not img_fol.endswith("good"))
# self.img_dir = os.path.join(DEFAULT_MVTEC_DIR, "test", "hole")
else:
self.img_dir = UNDEFINE
self.img_files = list(
list(itertools.chain.from_iterable([[os.path.join(img_fol, img)
for img in os.listdir(img_fol)
if (os.path.isfile(os.path.join(img_fol, img))
and img.endswith('.png'))] for img_fol in self.img_dir]))
)
# self.img_files = list(
# [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('.png'))]
# )
self.fake_dataset_size = fake_dataset_size # needed otherwise there are
self.gt_files = [s.replace(".png", "_mask.png").replace("test","ground_truth") 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.nb_img
def __getitem__(self, index):
img = Image.open(self.img_files[index]).convert("RGB") #to turn binary image to RGB
# img = Image.open(self.img_files[index])
gt = Image.open(self.gt_files[index])
return self.transform(img), self.transform(gt)
class MIADTestDataset(Dataset):
def __init__(self, img_size, fake_dataset_size):
if os.path.isdir(DEFAULT_MIAD_DIR):
self.img_dir = os.path.join(DEFAULT_MIAD_DIR, "test", "broken")
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('.png'))],
size=fake_dataset_size)
)
self.fake_dataset_size = fake_dataset_size # needed otherwise there are
self.gt_files = [s.replace(".png", "_mask.png").replace("test","ground_truth") 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)
class MIADTrainDataset(Dataset):
def __init__(self, img_size, fake_dataset_size, all_in = False):
if os.path.isdir(DEFAULT_MIAD_DIR):
self.img_dir = os.path.join(DEFAULT_MIAD_DIR, "train", "good")
else:
self.img_dir = UNDEFINE
print("all_in")
# if not all_in:
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('png'))],
size=fake_dataset_size)# needed otherwise there are
# 125000 images, and this is too much
)
#UNCOMMENT TO RUN FULL DATASET
# else:
# self.img_files = list(
# [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('png'))]
# )
# self.fake_dataset_size = fake_dataset_size
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)
print("the number of image is:", self.nb_img)
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