-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
executable file
·418 lines (333 loc) · 18.6 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
import numpy as np
import os
from pathlib import Path
from torch.utils.data import Dataset
import torch
from utils import is_png_file, load_img, load_val_img, load_mask, load_diff, load_val_mask, Augment_RGB_torch
import torch.nn.functional as F
import torchvision
from torchvision.transforms import transforms
import random
import cv2
from utils.dataset_utils import CutShadow
from mtmt_model.utils.util import cal_subitizing
augment = Augment_RGB_torch()
transforms_aug = [method for method in dir(augment) if callable(getattr(augment, method)) if not method.startswith('_')]
##################################################################################################
class DataLoaderTrain(Dataset):
def __init__(self, rgb_dir, img_options=None, target_transform=None):
super(DataLoaderTrain, self).__init__()
self.target_transform = target_transform
if 'ISTD' in rgb_dir:
gt_dir = 'train_C'
input_dir = 'train_A'
mask_dir = 'train_B'
elif 'official' in rgb_dir:
gt_dir = 'gt'
input_dir = 'input'
mask_dir = 'mask'
else:
assert False, rgb_dir
clean_files = sorted(os.listdir(os.path.join(rgb_dir, gt_dir)))
noisy_files = sorted(os.listdir(os.path.join(rgb_dir, input_dir)))
mask_files = sorted(os.listdir(os.path.join(rgb_dir, mask_dir)))
self.clean_filenames = [os.path.join(rgb_dir, gt_dir, x) for x in clean_files if is_png_file(x)]
self.noisy_filenames = [os.path.join(rgb_dir, input_dir, x) for x in noisy_files if is_png_file(x)]
self.mask_filenames = [os.path.join(rgb_dir, mask_dir, x) for x in mask_files if is_png_file(x)]
self.img_options = img_options
self.tar_size = len(self.clean_filenames) # get the size of target
def __len__(self):
return self.tar_size
def __getitem__(self, index):
tar_index = index % self.tar_size
clean = torch.from_numpy(np.float32(load_img(self.clean_filenames[tar_index])))
noisy = torch.from_numpy(np.float32(load_img(self.noisy_filenames[tar_index])))
mask = load_mask(self.mask_filenames[tar_index])
mask = torch.from_numpy(np.float32(mask))
clean = clean.permute(2,0,1)
noisy = noisy.permute(2,0,1)
clean_filename = os.path.split(self.clean_filenames[tar_index])[-1]
noisy_filename = os.path.split(self.noisy_filenames[tar_index])[-1]
mask_filename = os.path.split(self.mask_filenames[tar_index])[-1]
#Crop Input and Target
ps = self.img_options['patch_size']
H = clean.shape[1]
W = clean.shape[2]
# r = np.random.randint(0, H - ps) if not H-ps else 0
# c = np.random.randint(0, W - ps) if not H-ps else 0
if H-ps==0:
r=0
c=0
else:
r = np.random.randint(0, H - ps)
c = np.random.randint(0, W - ps)
clean = clean[:, r:r + ps, c:c + ps]
noisy = noisy[:, r:r + ps, c:c + ps]
mask = mask[r:r + ps, c:c + ps]
apply_trans = transforms_aug[random.getrandbits(3)]
clean = getattr(augment, apply_trans)(clean)
noisy = getattr(augment, apply_trans)(noisy)
mask = getattr(augment, apply_trans)(mask)
mask = torch.unsqueeze(mask, dim=0)
return clean, noisy, mask, clean_filename, noisy_filename
##################################################################################################
class DataLoaderTrainOfficialWarped(Dataset):
def __init__(self, rgb_dir, img_options=None, target_transform=None, color_space='rgb', mask_dir='mask', opt=None):
super(DataLoaderTrainOfficialWarped, self).__init__()
enable_list = ['rgb', 'bray', 'hsv', 'lab', 'luv', 'hls', 'yuv', 'xyz', 'ycrcb']
assert color_space in enable_list, color_space
self.color_space = color_space
self.target_transform = target_transform
assert 'official_warped' in rgb_dir
gt_dir = 'gt'
input_dir = 'input'
# mask_dir = 'mask'
diff_dir = 'diff'
self.clean_filenames = sorted(list(map(str, (Path(rgb_dir) / gt_dir).iterdir())))
self.noisy_filenames = sorted(list(map(str, (Path(rgb_dir) / input_dir).iterdir())))
self.mask_filenames = sorted(list(map(str, (Path(rgb_dir) / mask_dir).iterdir())))
self.diff_filenames = sorted(list(map(str, (Path(rgb_dir) / diff_dir).iterdir())))
if opt.w_val:
val_dir = Path(rgb_dir).parent / 'val'
self.clean_filenames.extend(sorted(list(map(str, (val_dir / gt_dir).iterdir()))))
self.noisy_filenames.extend(sorted(list(map(str, (val_dir / input_dir).iterdir()))))
self.mask_filenames.extend(sorted(list(map(str, (val_dir / mask_dir).iterdir()))))
self.diff_filenames.extend(sorted(list(map(str, (val_dir / diff_dir).iterdir()))))
# それぞれのリストが一致するかどうか確認
for clean, noisy, mask, diff in zip(self.clean_filenames, self.noisy_filenames, self.mask_filenames, self.diff_filenames):
assert clean.split('/')[-1] == noisy.split('/')[-1] == mask.split('/')[-1] == diff.split('/')[-1], \
(clean.split('/')[-1], noisy.split('/')[-1], mask.split('/')[-1], diff.split('/')[-1])
assert len(self.clean_filenames) == len(self.noisy_filenames) == len(self.mask_filenames) == len(self.diff_filenames), \
(len(self.clean_filenames), len(self.noisy_filenames), len(self.mask_filenames), len(self.diff_filenames))
self.img_options = img_options
self.tar_size = len(self.clean_filenames) # get the size of target
self.opt=opt
for key, value in self.opt.color_aug_condition.items():
self.opt.color_aug_condition[key]= float(value)
self.cut_shadow = CutShadow(p = self.opt.cut_shadow_ratio,
ns_s_ratio = self.opt.cut_shadow_ns_s_ratio,
sample_from_s = self.opt.sample_from_s,
visualize = self.opt.visualize)
def __len__(self):
return self.tar_size
def __getitem__(self, index):
tar_index = index % self.tar_size
clean = torch.from_numpy(np.float32(load_img(self.clean_filenames[tar_index], color_space=self.color_space)))
noisy = torch.from_numpy(np.float32(load_img(self.noisy_filenames[tar_index], color_space=self.color_space)))
mask = load_mask(self.mask_filenames[tar_index])
mask = torch.from_numpy(np.float32(mask))
diff = load_diff(self.mask_filenames[tar_index])
diff = torch.from_numpy(np.float32(diff))
clean = clean.permute(2,0,1)
noisy = noisy.permute(2,0,1)
diff = diff.permute(2,0,1)
clean_filename = os.path.split(self.clean_filenames[tar_index])[-1]
noisy_filename = os.path.split(self.noisy_filenames[tar_index])[-1]
# mask_filename = os.path.split(self.mask_filenames[tar_index])[-1]
#Crop Input and Target
ps = self.img_options['patch_size']
H = clean.shape[1]
W = clean.shape[2]
# r = np.random.randint(0, H - ps) if not H-ps else 0
# c = np.random.randint(0, W - ps) if not H-ps else 0
if H-ps==0:
r=0
c=0
else:
r = np.random.randint(0, H - ps)
c = np.random.randint(0, W - ps)
if self.opt.visualize:
r, c = 0, 500
clean = clean[:, r:r + ps, c:c + ps]
noisy = noisy[:, r:r + ps, c:c + ps]
mask = mask[r:r + ps, c:c + ps]
diff = diff[:, r:r + ps, c:c + ps]
# colorjitter
if self.opt.color_aug:
to_pil = transforms.ToPILImage()
to_tensor = transforms.ToTensor()
color_jitter = transforms.ColorJitter(**self.opt.color_aug_condition)
def apply_color_jitter(tensor):
pil_image = to_pil(tensor)
jittered_image = color_jitter(pil_image)
return to_tensor(jittered_image)
concatenated_tensor = torch.cat((clean, noisy), dim=2)
concatenated_tensor = apply_color_jitter(concatenated_tensor)
clean, noisy = torch.chunk(concatenated_tensor, 2, dim=2)
if self.opt.cut_shadow_ratio:
noisy_, mask = self.cut_shadow(clean, noisy, mask) # cut shadow
apply_trans = transforms_aug[random.getrandbits(3)]
if not self.opt.visualize:
clean = getattr(augment, apply_trans)(clean)
noisy_ = getattr(augment, apply_trans)(noisy_)
mask = getattr(augment, apply_trans)(mask)
mask = torch.unsqueeze(mask, dim=0)
diff = getattr(augment, apply_trans)(diff)
# diff = torch.unsqueeze(diff, dim=0)
return clean, noisy_, mask, diff, clean_filename, noisy#noisy_filename
##################################################################################################
class DataLoaderTrainOfficialWarpedJointLearning(Dataset):
def __init__(self, rgb_dir, img_options=None, target_transform=None, color_space='rgb', mask_dir='mask', opt=None):
super(DataLoaderTrainOfficialWarpedJointLearning, self).__init__()
enable_list = ['rgb', 'bray', 'hsv', 'lab', 'luv', 'hls', 'yuv', 'xyz', 'ycrcb']
assert color_space in enable_list, color_space
self.color_space = color_space
self.mtmt_subitizing_threshold = 8
self.mtmt_subitizing_min_size_per = 0.005
self.target_transform = target_transform
assert 'official_warped' in rgb_dir
gt_dir = 'gt'
input_dir = 'input'
# mask_dir = 'mask'
mask_gt_dir = 'mask_v'
diff_dir = 'diff'
self.clean_filenames = sorted(list(map(str, (Path(rgb_dir) / gt_dir).iterdir())))
self.noisy_filenames = sorted(list(map(str, (Path(rgb_dir) / input_dir).iterdir())))
self.mask_filenames = sorted(list(map(str, (Path(rgb_dir) / mask_dir).iterdir())))
self.diff_filenames = sorted(list(map(str, (Path(rgb_dir) / diff_dir).iterdir())))
if opt.w_val:
val_dir = Path(rgb_dir).parent / 'val'
self.clean_filenames.extend(sorted(list(map(str, (val_dir / gt_dir).iterdir()))))
self.noisy_filenames.extend(sorted(list(map(str, (val_dir / input_dir).iterdir()))))
self.mask_filenames.extend(sorted(list(map(str, (val_dir / mask_dir).iterdir()))))
self.diff_filenames.extend(sorted(list(map(str, (val_dir / diff_dir).iterdir()))))
# それぞれのリストが一致するかどうか確認
for clean, noisy, mask, diff in zip(self.clean_filenames, self.noisy_filenames, self.mask_filenames, self.diff_filenames):
assert clean.split('/')[-1] == noisy.split('/')[-1] == mask.split('/')[-1] == diff.split('/')[-1], \
(clean.split('/')[-1], noisy.split('/')[-1], mask.split('/')[-1], diff.split('/')[-1])
assert len(self.clean_filenames) == len(self.noisy_filenames) == len(self.mask_filenames) == len(self.diff_filenames), \
(len(self.clean_filenames), len(self.noisy_filenames), len(self.mask_filenames), len(self.diff_filenames))
self.img_options = img_options
self.tar_size = len(self.clean_filenames) # get the size of target
self.opt=opt
self.cut_shadow = CutShadow(p = self.opt.cut_shadow_ratio,
ns_s_ratio = self.opt.cut_shadow_ns_s_ratio,
sample_from_s = self.opt.sample_from_s)
def __len__(self):
return self.tar_size
def __getitem__(self, index):
tar_index = index % self.tar_size
clean = torch.from_numpy(np.float32(load_img(self.clean_filenames[tar_index], color_space=self.color_space)))
noisy = torch.from_numpy(np.float32(load_img(self.noisy_filenames[tar_index], color_space=self.color_space)))
mask = load_mask(self.mask_filenames[tar_index])
number_per, percentage = cal_subitizing(mask, threshold=self.mtmt_subitizing_threshold, min_size_per=self.mtmt_subitizing_min_size_per)
number_per = torch.Tensor([number_per]) #to Tensor
mask = torch.from_numpy(np.float32(mask))
diff = load_diff(self.mask_filenames[tar_index])
diff = torch.from_numpy(np.float32(diff))
clean = clean.permute(2,0,1)
noisy = noisy.permute(2,0,1)
diff = diff.permute(2,0,1)
clean_filename = os.path.split(self.clean_filenames[tar_index])[-1]
noisy_filename = os.path.split(self.noisy_filenames[tar_index])[-1]
# mask_filename = os.path.split(self.mask_filenames[tar_index])[-1]
#Crop Input and Target
ps = self.img_options['patch_size']
H = clean.shape[1]
W = clean.shape[2]
# r = np.random.randint(0, H - ps) if not H-ps else 0
# c = np.random.randint(0, W - ps) if not H-ps else 0
if H-ps==0:
r=0
c=0
else:
r = np.random.randint(0, H - ps)
c = np.random.randint(0, W - ps)
clean = clean[:, r:r + ps, c:c + ps]
noisy = noisy[:, r:r + ps, c:c + ps]
mask = mask[r:r + ps, c:c + ps]
diff = diff[:, r:r + ps, c:c + ps]
if self.opt.cut_shadow_ratio:
noisy, mask = self.cut_shadow(clean, noisy, mask) # cut shadow
apply_trans = transforms_aug[random.getrandbits(3)]
clean = getattr(augment, apply_trans)(clean)
noisy = getattr(augment, apply_trans)(noisy)
mask = getattr(augment, apply_trans)(mask)
mask = torch.unsqueeze(mask, dim=0)
diff = getattr(augment, apply_trans)(diff)
# diff = torch.unsqueeze(diff, dim=0)
return clean, noisy, mask, diff, number_per, clean_filename, noisy_filename
##################################################################################################
class DataLoaderVal(Dataset):
def __init__(self, rgb_dir, target_transform=None, color_space='rgb', mask_dir='mask', opt=None):
super(DataLoaderVal, self).__init__()
enable_list = ['rgb', 'bray', 'hsv', 'lab', 'luv', 'hls', 'yuv', 'xyz', 'ycrcb']
assert color_space in enable_list, color_space
self.color_space = color_space
self.opt = opt
self.target_transform = target_transform
if 'ISTD' in rgb_dir:
gt_dir = 'train_C'
input_dir = 'train_A'
mask_dir = 'train_B'
elif 'official' in rgb_dir:
gt_dir = 'gt'
input_dir = 'input'
# mask_dir = 'mask'
else:
assert False, rgb_dir
clean_files = sorted(os.listdir(os.path.join(rgb_dir, gt_dir)))
noisy_files = sorted(os.listdir(os.path.join(rgb_dir, input_dir)))
self.clean_filenames = [os.path.join(rgb_dir, gt_dir, x) for x in clean_files if is_png_file(x)]
self.noisy_filenames = [os.path.join(rgb_dir, input_dir, x) for x in noisy_files if is_png_file(x)]
if not self.opt.joint_learning_alpha or True:
mask_files = sorted(os.listdir(os.path.join(rgb_dir, mask_dir)))
self.mask_filenames = [os.path.join(rgb_dir, mask_dir, x) for x in mask_files if is_png_file(x)]
self.tar_size = len(self.clean_filenames)
def __len__(self):
return self.tar_size
def __getitem__(self, index):
tar_index = index % self.tar_size
clean = torch.from_numpy(np.float32(load_img(self.clean_filenames[tar_index], color_space=self.color_space)))
noisy = torch.from_numpy(np.float32(load_img(self.noisy_filenames[tar_index], color_space=self.color_space)))
clean_filename = os.path.split(self.clean_filenames[tar_index])[-1]
noisy_filename = os.path.split(self.noisy_filenames[tar_index])[-1]
clean = clean.permute(2,0,1)
noisy = noisy.permute(2,0,1)
if self.opt.joint_learning_alpha and False:
mask = torch.zeros(1)
else:
mask = load_mask(self.mask_filenames[tar_index])
mask = torch.from_numpy(np.float32(mask))
mask_filename = os.path.split(self.mask_filenames[tar_index])[-1]
mask = torch.unsqueeze(mask, dim=0)
return clean, noisy, mask, clean_filename, noisy_filename
##################################################################################################
class DataLoaderTest(Dataset):
def __init__(self, rgb_dir, target_transform=None, color_space='rgb', mask_dir='mask', opt=None):
super(DataLoaderTest, self).__init__()
enable_list = ['rgb', 'bray', 'hsv', 'lab', 'luv', 'hls', 'yuv', 'xyz', 'ycrcb']
assert color_space in enable_list, color_space
self.color_space = color_space
self.opt = opt
self.target_transform = target_transform
if 'ISTD' in rgb_dir:
input_dir = 'train_A'
# mask_dir = 'train_B'
elif 'official' in rgb_dir:
input_dir = 'input'
# mask_dir = 'mask'
else:
assert False, rgb_dir
noisy_files = sorted(os.listdir(os.path.join(rgb_dir, input_dir)))
self.noisy_filenames = [os.path.join(rgb_dir, input_dir, x) for x in noisy_files if is_png_file(x)]
if not self.opt.joint_learning_alpha:
mask_files = sorted(os.listdir(os.path.join(rgb_dir, mask_dir)))
self.mask_filenames = [os.path.join(rgb_dir, mask_dir, x) for x in mask_files if is_png_file(x)]
self.tar_size = len(self.noisy_filenames)
def __len__(self):
return self.tar_size
def __getitem__(self, index):
tar_index = index % self.tar_size
noisy = torch.from_numpy(np.float32(load_img(self.noisy_filenames[tar_index], color_space=self.color_space)))
noisy_filename = os.path.split(self.noisy_filenames[tar_index])[-1]
noisy = noisy.permute(2,0,1)
if self.opt.joint_learning_alpha:
mask = torch.zeros(1)
else:
mask = load_mask(self.mask_filenames[tar_index])
mask = torch.from_numpy(np.float32(mask))
mask_filename = os.path.split(self.mask_filenames[tar_index])[-1]
mask = torch.unsqueeze(mask, dim=0)
_ = 0
return _, noisy, mask, _, noisy_filename