-
Notifications
You must be signed in to change notification settings - Fork 0
/
ManualDataset.py
403 lines (310 loc) · 14 KB
/
ManualDataset.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
import torch.utils.data as data
import torchvision.transforms as transforms
from PIL import Image
import os, random, json
import os.path
from utils import is_png_file, load_img, Augment_RGB_torch
from collections import OrderedDict
import sys
import numpy as np
import skimage.io as io
import pdb
import torch
augment = Augment_RGB_torch()
transforms_aug = [method for method in dir(augment) if callable(getattr(augment, method)) if not method.startswith('_')]
def _mod_crop(im, scala):
w, h = im.size
return im.crop((0, 0, w - w % scala, h - h % scala))
def get_crop(img, r1, r2, c1, c2):
im_raw = img[:, r1:r2, c1:c2]
return im_raw
# manual datasets
class ManualDatasets(torch.utils.data.Dataset):
""" Real-world burst super-resolution dataset. """
def __init__(self, root, crop_sz=64, burst_size=14, center_crop=False, random_flip=False, sift_lr=False,
split='train'):
"""
args:
root : path of the root directory
burst_size : Burst size. Maximum allowed burst size is 14.
crop_sz: Size of the extracted crop. Maximum allowed crop size is 80
center_crop: Whether to extract a random crop, or a centered crop.
random_flip: Whether to apply random horizontal and vertical flip
split: Can be 'train' or 'val'
"""
assert burst_size <= 14, 'burst_sz must be less than or equal to 14'
# assert crop_sz <= 80, 'crop_sz must be less than or equal to 80'
assert split in ['train', 'val']
super().__init__()
self.transform = transforms.Compose([transforms.ToTensor()])
self.burst_size = burst_size
self.crop_sz = crop_sz
self.split = split
self.center_crop = center_crop
self.random_flip = random_flip
self.sift_lr = sift_lr
self.root = root
# split trainset and testset in one dir
if self.split == 'val':
root = root + '/test'
else:
root = root + '/train'
self.hrdir = root + '/' + 'HR'
self.lrdir = root + '/' + 'LR_aligned'
print(self.lrdir)
self.substract_black_level = True
self.white_balance = False
self.burst_list = self._get_burst_list()
self.data_length = len(self.burst_list)
# self.data_length = 20
def _get_burst_list(self):
burst_list = sorted(os.listdir(self.lrdir))
# print(burst_list)
return burst_list
def _get_raw_image(self, burst_id, im_id):
# Manual_dataset/train/LR/109_28/109_MFSR_Sony_0028_x4_00.png
burst_number = self.burst_list[burst_id].split('_')[0]
burst_number2 = int(self.burst_list[burst_id].split('_')[-1])
path = '{}/{}/{}_MFSR_Sony_{:04d}_x1_{:02d}.png'.format(self.lrdir, self.burst_list[burst_id], burst_number,
burst_number2, im_id)
image = Image.open(path) # RGB,W, H, C
image = self.transform(image)
# print(image.shape)
# image = cv2.imread(path, cv2.COLOR_BGR2RGB)
return image
def _get_gt_image(self, burst_id):
# 000_MFSR_Sony_0001_x4.png
burst_number = self.burst_list[burst_id].split('_')[0]
burst_nmber2 = int(self.burst_list[burst_id].split('_')[-1])
path = '{}/{}/{}_MFSR_Sony_{:04d}_x4.png'.format(self.hrdir, self.burst_list[burst_id], burst_number, burst_nmber2)
image = Image.open(path) # RGB,W, H, C
image = self.transform(image)
return image
def get_burst(self, burst_id, im_ids):
frames = [self._get_raw_image(burst_id, i) for i in im_ids]
# pic = self._get_raw_image(burst_id, 0)
gt = self._get_gt_image(burst_id)
return frames, gt
def _sample_images(self):
burst_size = self.burst_size
ids = random.sample(range(1, burst_size), k=self.burst_size - 1)
ids = [0, ] + ids
return ids
def get_burst_info(self, burst_id):
burst_info = {'burst_size': 14, 'burst_name': self.burst_list[burst_id]}
return burst_info
def __len__(self):
return self.data_length
def __getitem__(self, index):
# Sample the images in the burst, in case a burst_size < 14 is used.
im_ids = self._sample_images()
frames, gt = self.get_burst(index, im_ids)
info = self.get_burst_info(index)
# Extract crop if needed
if frames[0].shape[-1] != self.crop_sz:
r1 = random.randint(0, frames[0].shape[-2] - self.crop_sz)
c1 = random.randint(0, frames[0].shape[-1] - self.crop_sz)
r2 = r1 + self.crop_sz
c2 = c1 + self.crop_sz
scale_factor = gt.shape[-1] // frames[0].shape[-1]
# print(scale_factor)
frames = [get_crop(im, r1, r2, c1, c2) for im in frames]
gt = get_crop(gt, scale_factor * r1, scale_factor * r2, scale_factor * c1, scale_factor * c2)
apply_trans = transforms_aug[random.getrandbits(3)]
frames = [getattr(augment, apply_trans)(im) for im in frames]
gt = getattr(augment, apply_trans)(gt)
burst = torch.stack(frames, dim=0)
burst = burst.float()
frame_gt = gt.float()
data = {}
data['LR'] = burst
data['HR'] = frame_gt
data['burst_name'] = info['burst_name']
return data
class ManualDatasets_validation(torch.utils.data.Dataset):
""" Real-world burst super-resolution dataset. """
def __init__(self, root, crop_sz=64, burst_size=14, center_crop=False, random_flip=False, sift_lr=False,
split='train'):
"""
args:
root : path of the root directory
burst_size : Burst size. Maximum allowed burst size is 14.
crop_sz: Size of the extracted crop. Maximum allowed crop size is 80
center_crop: Whether to extract a random crop, or a centered crop.
random_flip: Whether to apply random horizontal and vertical flip
split: Can be 'train' or 'val'
"""
assert burst_size <= 14, 'burst_sz must be less than or equal to 14'
# assert crop_sz <= 80, 'crop_sz must be less than or equal to 80'
assert split in ['train', 'val']
super().__init__()
self.transform = transforms.Compose([transforms.ToTensor()])
self.burst_size = burst_size
self.crop_sz = crop_sz
self.split = split
self.center_crop = center_crop
self.random_flip = random_flip
self.sift_lr = sift_lr
self.root = root
# split trainset and testset in one dir
if self.split == 'val':
root = root + '/test'
else:
root = root + '/train'
self.hrdir = root + '/' + 'HR'
self.lrdir = root + '/' + 'LR_aligned'
print(self.lrdir)
self.substract_black_level = True
self.white_balance = False
self.burst_list = self._get_burst_list()
self.data_length = len(self.burst_list)
# self.data_length = 20
def _get_burst_list(self):
burst_list = sorted(os.listdir(self.lrdir))
# print(burst_list)
return burst_list
def _get_raw_image(self, burst_id, im_id):
# Manual_dataset/train/LR/109_28/109_MFSR_Sony_0028_x4_00.png
burst_number = self.burst_list[burst_id].split('_')[0]
burst_number2 = int(self.burst_list[burst_id].split('_')[-1])
path = '{}/{}/{}_MFSR_Sony_{:04d}_x1_{:02d}.png'.format(self.lrdir, self.burst_list[burst_id], burst_number,
burst_number2, im_id)
image = Image.open(path) # RGB,W, H, C
image = self.transform(image)
# print(image.shape)
# image = cv2.imread(path, cv2.COLOR_BGR2RGB)
return image
def _get_gt_image(self, burst_id):
# 000_MFSR_Sony_0001_x4.png
burst_number = self.burst_list[burst_id].split('_')[0]
burst_nmber2 = int(self.burst_list[burst_id].split('_')[-1])
path = '{}/{}/{}_MFSR_Sony_{:04d}_x4.png'.format(self.hrdir, self.burst_list[burst_id], burst_number, burst_nmber2)
image = Image.open(path) # RGB,W, H, C
image = self.transform(image)
return image
def get_burst(self, burst_id, im_ids):
frames = [self._get_raw_image(burst_id, i) for i in im_ids]
# pic = self._get_raw_image(burst_id, 0)
gt = self._get_gt_image(burst_id)
return frames, gt
def _sample_images(self):
burst_size = self.burst_size
ids = random.sample(range(1, burst_size), k=self.burst_size - 1)
ids = [0, ] + ids
return ids
def get_burst_info(self, burst_id):
burst_info = {'burst_size': 14, 'burst_name': self.burst_list[burst_id]}
return burst_info
def __len__(self):
return self.data_length
def __getitem__(self, index):
# Sample the images in the burst, in case a burst_size < 14 is used.
im_ids = self._sample_images()
frames, gt = self.get_burst(index, im_ids)
info = self.get_burst_info(index)
# Extract crop if needed
if frames[0].shape[-1] != self.crop_sz:
r1 = random.randint(0, frames[0].shape[-2] - self.crop_sz)
c1 = random.randint(0, frames[0].shape[-1] - self.crop_sz)
r2 = r1 + self.crop_sz
c2 = c1 + self.crop_sz
scale_factor = gt.shape[-1] // frames[0].shape[-1]
# print(scale_factor)
frames = [get_crop(im, r1, r2, c1, c2) for im in frames]
gt = get_crop(gt, scale_factor * r1, scale_factor * r2, scale_factor * c1, scale_factor * c2)
burst = torch.stack(frames, dim=0)
burst = burst.float()
frame_gt = gt.float()
data = {}
data['LR'] = burst
data['HR'] = frame_gt
data['burst_name'] = info['burst_name']
return data
class ManualDatasets_test(torch.utils.data.Dataset):
""" Real-world burst super-resolution dataset. """
def __init__(self, root, burst_size=14, center_crop=False, random_flip=False, sift_lr=False,
split='train'):
"""
args:
root : path of the root directory
burst_size : Burst size. Maximum allowed burst size is 14.
crop_sz: Size of the extracted crop. Maximum allowed crop size is 80
center_crop: Whether to extract a random crop, or a centered crop.
random_flip: Whether to apply random horizontal and vertical flip
split: Can be 'train' or 'val'
"""
# assert burst_size <= 14, 'burst_sz must be less than or equal to 14'
# assert crop_sz <= 80, 'crop_sz must be less than or equal to 80'
assert split in ['train', 'val']
super().__init__()
self.transform = transforms.Compose([transforms.ToTensor()])
self.burst_size = burst_size
self.split = split
self.center_crop = center_crop
self.random_flip = random_flip
self.sift_lr = sift_lr
self.root = root
# split trainset and testset in one dir
if self.split == 'val':
root = root + '/test'
else:
root = root + '/train'
self.hrdir = root + '/' + 'HR'
self.lrdir = root + '/' + 'LR_aligned'
print(self.lrdir)
self.substract_black_level = True
self.white_balance = False
self.burst_list = self._get_burst_list()
self.data_length = len(self.burst_list)
# self.data_length = 20
def _get_burst_list(self):
burst_list = sorted(os.listdir(self.lrdir))
# print(burst_list)
return burst_list
def _get_raw_image(self, burst_id, im_id):
burst_number2 = int(self.burst_list[burst_id])
path = '{}/{}/MFSR_Sony_{:04d}_x1_{:02d}.png'.format(self.lrdir, self.burst_list[burst_id], burst_number2, im_id)
image = Image.open(path) # RGB,W, H, C
image = self.transform(image)
# print(image.shape)
# image = cv2.imread(path, cv2.COLOR_BGR2RGB)
return image
# def _get_gt_image(self, burst_id):
# # 000_MFSR_Sony_0001_x4.png
# burst_number = self.burst_list[burst_id].split('_')[0]
# burst_nmber2 = int(self.burst_list[burst_id].split('_')[-1])
# path = '{}/{}/{}_MFSR_Sony_{:04d}_x4.png'.format(self.hrdir, self.burst_list[burst_id], burst_number, burst_nmber2)
# image = Image.open(path) # RGB,W, H, C
# image = self.transform(image)
# return image
def get_burst(self, burst_id, im_ids):
frames = [self._get_raw_image(burst_id, i) for i in im_ids]
# pic = self._get_raw_image(burst_id, 0)
# gt = self._get_gt_image(burst_id)
return frames
def _sample_images(self):
burst_size = self.burst_size
ids = random.sample(range(1, burst_size), k=self.burst_size - 1)
ids = [0, ] + ids
# ids = []
# for i in range(0, 14):
# ids.append(0)
return ids
def get_burst_info(self, burst_id):
burst_info = {'burst_size': 14, 'burst_name': self.burst_list[burst_id]}
return burst_info
def __len__(self):
return self.data_length
def __getitem__(self, index):
# Sample the images in the burst, in case a burst_size < 14 is used.
im_ids = self._sample_images()
frames = self.get_burst(index, im_ids)
info = self.get_burst_info(index)
burst = torch.stack(frames, dim=0)
burst = burst.float()
# frame_gt = gt.float()
data = {}
data['LR'] = burst
# data['HR'] = frame_gt
data['burst_name'] = info['burst_name']
return data