forked from eezkni/UEGAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_loader.py
590 lines (475 loc) · 21.2 KB
/
data_loader.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#-*-coding:utf-8-*-
import random
import numpy as np
from pathlib import Path
from itertools import chain
import os
import io
import imageio
import copy
import cv2
import warnings
from numpy.core.fromnumeric import argmin
from skimage.util.dtype import img_as_int
from imresize import imresize
from munch import Munch
from PIL import Image
from utils import list_files_in_dir
import torch
from torch.utils import data
from torchvision import transforms
import torchvision.transforms.functional as TF
def listdir(dname):
fnames = list(chain(*[list(Path(dname).rglob('*.' + ext))
for ext in ['png', 'jpg', 'jpeg', 'JPG']]))
return fnames
class DefaultDataset(data.Dataset):
def __init__(self, root, transform=None):
self.samples = listdir(root)
self.samples.sort()
self.transform = transform
self.targets = None
def __getitem__(self, index):
fname = self.samples[index]
img = Image.open(fname).convert('RGB')
if self.transform is not None:
img = self.transform(img)
return img
def __len__(self):
return len(self.samples)
class ReferenceDataset(data.Dataset):
def __init__(self, root, img_size, resize_size, config=None):
# TODO: fix the warning with immutable Image numpy array
warnings.filterwarnings('ignore')
self.config = config
self.img_size = img_size
self.resize_size = resize_size
self.exp_samples, self.raw_samples = self._make_dataset(root)
self.transform = transforms.Compose([
transforms.RandomCrop(img_size),
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomVerticalFlip(p=0.5),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5],
std=[0.5, 0.5, 0.5]),
])
def _make_dataset(self, root):
domains = os.listdir(root)
fnames, fnames2 = [], []
# by sorting, exp is fnames and raw is fnames2
for idx, domain in enumerate(sorted(domains)):
class_dir = os.path.join(root, domain)
cls_fnames = listdir(class_dir)
if idx == 0:
fnames += cls_fnames
elif idx == 1:
fnames2 += cls_fnames
random.shuffle(fnames)
random.shuffle(fnames2)
return fnames, fnames2
def __getitem__(self, index):
exp_fname, raw_fname = self.exp_samples[index], self.raw_samples[index]
name = str(raw_fname)
img_name, _ = name.split('.', 1)
_, img_name = img_name.rsplit('/', 1)
exp_img = Image.open(exp_fname).convert('RGB')
raw_img = Image.open(raw_fname).convert('RGB')
if self.transform is not None:
exp_img = self.transform(exp_img)
raw_img = self.transform(raw_img)
return raw_img, exp_img, img_name, raw_img
def __len__(self):
return len(self.exp_samples)
class NoiseAugmentDataset(ReferenceDataset):
def __init__(self, root, img_size, resize_size, config=None):
super().__init__(root, img_size, resize_size, config=config)
self.do_jpeg_aug = True if 'jpeg_aug' in self.config else False
if self.do_jpeg_aug:
self.jpeg_min_qual = self.config['jpeg_aug'][0]
self.jpeg_max_qual = self.config['jpeg_aug'][1]
self.do_scale_up_down = True if 'scale_up_down_prob' in self.config else False
if self.do_scale_up_down:
self.scale_up_down_prob = self.config['scale_up_down_prob']
self.AUGMENT_TYPE = ["none", "jpg", "scale_up_down", "blur", \
"jpg-scale_up_down", "jpg-blur", \
"scale_up_down-blur", \
"all"]
assert len(self.config['aug_prob']) == len(self.AUGMENT_TYPE), \
"Not enough probs. for all augmentations"
self.pst_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5],
std=[0.5, 0.5, 0.5]),
])
def _twin_transform(self, img1, img2=None):
# Random crop
i, j, h, w = transforms.RandomCrop.get_params(
img1,
output_size=(self.img_size, self.img_size))
img1 = TF.crop(img1, i, j, h, w)
if img2 is not None:
img2 = TF.crop(img2, i, j, h, w)
# Random horizontal flipping
if random.random() < 0.5:
img1 = TF.hflip(img1)
if img2 is not None:
img2 = TF.hflip(img2)
# Random horizontal flipping
if random.random() < 0.5:
img1 = TF.vflip(img1)
if img2 is not None:
img2 = TF.vflip(img2)
img1 = self.pst_transform(img1)
img2 = self.pst_transform(img2)
return img1, img2
def _add_scale_up_down_aug(self, img):
#from debugpy_util import debug
#debug(address='10.42.96.4:5678')
h, w = img.size[:2]
# TODO: try 2, 3 scale as well
img = img.resize((h // 2, w // 2), Image.BICUBIC)
img = img.resize((h, w), Image.BICUBIC)
return img
def _add_jpeg_aug(self, img):
buf = io.BytesIO()
quality = random.randint(self.jpeg_min_qual, self.jpeg_max_qual)
# TODO: optimize it by removing imageio
imageio.imwrite(buf, img, format='JPEG', quality=quality)
img = imageio.imread(buf.getvalue())
np_img = np.asarray(img)
img = Image.fromarray(np_img)
return img
def _add_blur_aug(self, img):
img = np.asarray(img)
#TODO: tune values here
rand_val = random.randint(0, 99)
blur_types = ['Average', 'Bilateral', 'Box', 'Gaussian', 'Median']
blur_type = random.choices(blur_types)[0]
kernel_size = 3
if rand_val % 3 == 0:
kernel_size = 5
if blur_type == 'Average':
img = cv2.blur(img, (kernel_size, kernel_size))
elif blur_type == 'Bilateral':
img = cv2.bilateralFilter(img, 9, 75, 75)
elif blur_type == 'Box':
img = cv2.boxFilter(img, -1, (kernel_size, kernel_size))
elif blur_type == 'Gaussian':
img = cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
elif blur_type == 'Median':
img = cv2.medianBlur(img, kernel_size)
if len(img.shape) == 2:
img = np.expand_dims(img, axis=2)
img = Image.fromarray(img)
return img
def _img_noise_augment(self, img):
aug_type = random.choices(self.AUGMENT_TYPE, self.config['aug_prob'])[0]
if aug_type == "none":
return img
elif aug_type == "jpg":
img = self._add_jpeg_aug(img)
elif aug_type == "scale_up_down":
img = self._add_scale_up_down_aug(img)
elif aug_type == "blur":
img = self._add_blur_aug(img)
elif aug_type == "jpg-scale_up_down":
img = self._add_scale_up_down_aug(img)
img = self._add_jpeg_aug(img)
elif aug_type == "jpg-blur":
img = self._add_blur_aug(img)
img = self._add_jpeg_aug(img)
elif aug_type == "scale_up_down-blur":
img = self._add_scale_up_down_aug(img)
img = self._add_blur_aug(img)
elif "all":
img = self._add_scale_up_down_aug(img)
img = self._add_blur_aug(img)
img = self._add_jpeg_aug(img)
return img
def _get_img(self, index):
exp_fname, raw_fname = self.exp_samples[index], self.raw_samples[index]
name = str(raw_fname)
img_name, _ = name.split('.', 1)
_, img_name = img_name.rsplit('/', 1)
raw_img = Image.open(raw_fname).convert('RGB')
exp_img = Image.open(exp_fname).convert('RGB')
return exp_img, raw_img, img_name
def __getitem__(self, index):
exp_img, raw_img, img_name = self._get_img(index)
orig_raw_img = raw_img
raw_img = self._img_noise_augment(raw_img)
# orig_raw_img.save('/tmp/img/' + img_name.split('.')[0] + '_a_pre.png')
if self.transform is not None:
raw_img, orig_raw_img = self._twin_transform(raw_img, orig_raw_img)
exp_img = self.transform(exp_img)
# orig_img_rgb = transforms.ToPILImage()(orig_raw_img)
# orig_img_rgb.save('/tmp/img/' + img_name.split('.')[0] + '_b_orig.png')
# img_rgb = transforms.ToPILImage()(raw_img)
# img_rgb.save('/tmp/img/' + img_name.split('.')[0] + '_c_in.png')
# exp_rgb = transforms.ToPILImage()(exp_img)
# #img_rgb.save('/tmp/img/' + img_name.split('.')[0] + '_e_in.png')
# exp_rgb.save('/tmp/img/' + img_name.split('.')[0] + '_lbl_' + str(self.exp_source_choice) + '.png')
# print("saved")
return raw_img, exp_img, img_name, orig_raw_img
class NonExpNoiseAugmentDataset(NoiseAugmentDataset):
def __init__(self, root, img_size, resize_size, config=None):
super().__init__(root, img_size, resize_size, config=config)
def _make_dataset(self, root):
fnames, fnames2 = [], []
filenames = list_files_in_dir(root)
for fn in filenames:
file_path = os.path.join(root, fn)
fnames.append(file_path)
fnames2 = copy.deepcopy(fnames)
random.shuffle(fnames)
random.shuffle(fnames2)
return fnames, fnames2
class MultiSourceNoiseAugmentDataset(NoiseAugmentDataset):
def __init__(self, root, img_size, resize_size, config=None):
super().__init__(root, img_size, resize_size, config=config)
root = [x.strip() for x in root.split(',')]
assert config['nb_train_datasets'] == len(root)
raw_root = [x.strip() for x in self.config['raw_train_img_dir'].split(',')]
assert config['raw_nb_train_datasets'] == len(raw_root)
self.exp_total_data_sources = len(root)
self.exp_datasets_probs = config['datasets_probs']
self.raw_total_data_sources = len(raw_root)
self.raw_datasets_probs = config['raw_datasets_probs']
# TODO: Need to revist this
smallest_exp_set = argmin(self.exp_datasets_len)
self.dataset_len = int(self.exp_datasets_len[smallest_exp_set] / self.exp_datasets_probs[smallest_exp_set])
def _get_img_paths(self, root):
# if cached
cache_filename = "_".join(root.split('/')) + '.txt'
cache_file_path = os.path.join(self.config['cache_dir'], cache_filename)
if os.path.isfile(cache_file_path):
with open(cache_file_path, 'r', encoding='utf-8') as fi:
filenames = fi.readlines()
filenames = [x.strip() for x in filenames]
else:
filenames = list_files_in_dir(root)
try:
with open(cache_file_path, 'w', encoding='utf-8') as fi:
fi.write('\n'.join(filenames))
except Exception as e:
# clean by deleting the file
os.remove(cache_file_path)
print(e)
raise e
file_paths = []
for fn in filenames:
file_path = os.path.join(root, fn)
file_paths.append(file_path)
return file_paths
def _read_data(self, data_paths):
samples = []
datasets_len = []
for folder in data_paths:
filenames = self._get_img_paths(folder)
datasets_len.append(len(filenames))
samples.append(filenames)
return samples, datasets_len
def _make_dataset(self, root):
# expert data
root = root.split(',')
exp_samples, self.exp_datasets_len = self._read_data(root)
# raw data
raw_dataset_paths = self.config['raw_train_img_dir'].split(',')
raw_samples, self.raw_datasets_len = self._read_data(raw_dataset_paths)
return exp_samples, raw_samples
def _get_img(self, index):
exp_data_source_idx = random.choices(range(0, self.exp_total_data_sources),
weights=self.exp_datasets_probs, k=1)[0]
raw_data_source_idx = random.choices(range(0, self.raw_total_data_sources),
weights=self.raw_datasets_probs, k=1)[0]
exp_index = index % self.exp_datasets_len[exp_data_source_idx]
exp_fname = self.exp_samples[exp_data_source_idx][exp_index]
raw_index = index % self.raw_datasets_len[raw_data_source_idx]
raw_fname = self.raw_samples[raw_data_source_idx][raw_index]
name = str(raw_fname)
img_name, _ = name.split('.', 1)
_, img_name = img_name.rsplit('/', 1)
raw_img = Image.open(raw_fname).convert('RGB')
exp_img = Image.open(exp_fname).convert('RGB')
return exp_img, raw_img, img_name
def __len__(self):
return self.dataset_len
class TestDataset(data.Dataset):
def __init__(self, root, label_root, config=None):
# TODO: fix the warning with immutable Image numpy array
warnings.filterwarnings('ignore')
self.samples, self.label_samples = self._make_dataset(root, label_root)
self.transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5],
std=[0.5, 0.5, 0.5]),
])
def _make_dataset(self, root, label_root):
filenames = os.listdir(root)
fnames, label_fnames = [], []
for _, filename in enumerate(filenames):
img_path = os.path.join(root, filename)
fnames.append(img_path)
if label_root is not None:
label_img_path = os.path.join(label_root, filename)
label_fnames.append(label_img_path)
return fnames, label_fnames
def _get_img(self, path):
img = Image.open(path).convert('RGB')
img = np.asarray(img)
H, W = img.shape[:2]
H, W = int(H/16), int(W/16)
# mod crop
img = img[: H * 16, : W * 16, :]
img = Image.fromarray(img)
return img
def __getitem__(self, index):
fname_path = self.samples[index]
img = self._get_img(fname_path)
img = self.transform(img)
file_name = os.path.basename(fname_path)
# for dummy return if there is no label img
label_img = img
if len(self.label_samples) > 0:
label_fname = self.label_samples[index]
label_img = self._get_img(label_fname)
label_img = self.transform(label_img)
#from debugpy_util import debug
#debug(address='10.33.72.4:5678')
# img_rgb = transforms.ToPILImage()(img)
# label_img_rgb = transforms.ToPILImage()(label_img)
# img_rgb.save('/tmp/img/' + file_name.split('.')[0] + '_in.png')
# label_img_rgb.save('/tmp/img/' + file_name.split('.')[0] + '_lbl.png')
# print("saved")
return img, label_img, file_name, img
def __len__(self):
return len(self.samples)
class TestNonExpNoiseAugmentDataset(NonExpNoiseAugmentDataset):
'''
Test dataset class without "expert" (label) images. Input images
are noise augmented on the fly.
'''
def __init__(self, root, label_root=None, img_size=None, resize_size=None, config=None):
super().__init__(root, img_size, resize_size, config=config)
self.transform = transforms.Compose([
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomVerticalFlip(p=0.5),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5],
std=[0.5, 0.5, 0.5]),
])
def _twin_transform(self, img1, img2=None):
img1 = self.pst_transform(img1)
img2 = self.pst_transform(img2)
return img1, img2
def _make_dataset(self, root):
fnames, fnames2 = [], []
filenames = list_files_in_dir(root)
for fn in filenames:
file_path = os.path.join(root, fn)
fnames.append(file_path)
fnames2 = copy.deepcopy(fnames)
return fnames, fnames2
def _mod_crop_img(self, img):
img = np.asarray(img)
H, W = img.shape[:2]
H, W = int(H/16), int(W/16)
# mod crop
img = img[: H * 16, : W * 16, :]
img = Image.fromarray(img)
return img
def _get_img(self, index):
raw_fname = self.raw_samples[index]
name = str(raw_fname)
img_name, _ = name.split('.', 1)
_, img_name = img_name.rsplit('/', 1)
raw_img = Image.open(raw_fname).convert('RGB')
raw_img = self._mod_crop_img(raw_img)
return _, raw_img, img_name
def __getitem__(self, index):
# from debugpy_util import debug
# debug('10.36.64.1:5678')
_, raw_img, img_name = self._get_img(index)
orig_raw_img = raw_img
raw_img = self._img_noise_augment(raw_img)
# orig_raw_img.save('/tmp/img/' + img_name.split('.')[0] + '_a_pre.png')
if self.transform is not None:
raw_img, orig_raw_img = self._twin_transform(raw_img, orig_raw_img)
# img_rgb = transforms.ToPILImage()(raw_img)
# img_rgb.save('/tmp/img/' + img_name.split('.')[0] + '_c_in.png')
# orig_img_rgb = transforms.ToPILImage()(orig_raw_img)
# exp_rgb = transforms.ToPILImage()(exp_img)
# orig_img_rgb.save('/tmp/img/' + img_name.split('.')[0] + '_b_orig.png')
# #img_rgb.save('/tmp/img/' + img_name.split('.')[0] + '_e_in.png')
# exp_rgb.save('/tmp/img/' + img_name.split('.')[0] + '_lbl.png')
# print("saved")
return raw_img, orig_raw_img, img_name, orig_raw_img
def get_train_loader(root, config, img_size=512, \
resize_size=256, batch_size=8, shuffle=True, \
num_workers=8, drop_last=True, parallel_mode="ddp"):
if config['dataset_type'] == "ref":
D = ReferenceDataset
elif config['dataset_type'] == "noise_aug":
D = NoiseAugmentDataset
elif config['dataset_type'] == "multi_noise_aug":
D = MultiSourceNoiseAugmentDataset
elif config['dataset_type'] == "non_exp_noise_aug":
D = NonExpNoiseAugmentDataset
elif config['dataset_type'] == "debug":
D = DebugDataset
else:
raise NotImplementedError("Unrecoganized dataset type!")
dataset = D(root, img_size, resize_size, config)
sampler = None
if parallel_mode == "ddp":
world_size = torch.distributed.get_world_size()
assert batch_size % world_size == 0
batch_size = batch_size // world_size
shuffle = False
sampler = torch.utils.data.distributed.DistributedSampler(dataset)
data_loader = data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
pin_memory=True,
drop_last=drop_last,
sampler=sampler)
return data_loader, sampler
def get_test_loader(root, config, dataset_type=None, label_root=None, \
img_size=512, batch_size=8, shuffle=False, num_workers=4, parallel_mode="non_ddp"):
if dataset_type is None or dataset_type == "test":
D = TestDataset
elif dataset_type == "test_non_exp_noise_aug":
D = TestNonExpNoiseAugmentDataset
else:
raise NotImplementedError("Unrecoganized dataset type!")
dataset = D(root, label_root=label_root, config=config)
sampler = None
if parallel_mode == "ddp":
world_size = torch.distributed.get_world_size()
assert batch_size % world_size == 0
batch_size = batch_size // world_size
shuffle = False
sampler = torch.utils.data.distributed.DistributedSampler(dataset)
return data.DataLoader(dataset=dataset,
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
pin_memory=True,
sampler=sampler)
class InputFetcher:
def __init__(self, loader):
self.loader = loader
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def _fetch_refs(self):
try:
raw_img, exp_img, name, orig_raw_img = next(self.iter)
except (AttributeError, StopIteration):
self.iter = iter(self.loader)
raw_img, exp_img, name, orig_raw_img = next(self.iter)
return raw_img, exp_img, name, orig_raw_img
def __next__(self):
raw_img, exp_img, img_name, orig_raw_img = self._fetch_refs()
raw_img, exp_img, orig_raw_img = raw_img.to(self.device), exp_img.to(self.device), orig_raw_img.to(self.device)
inputs = Munch(img_exp=exp_img, img_raw=raw_img, img_orig_raw=orig_raw_img, img_name=img_name)
return inputs