-
Notifications
You must be signed in to change notification settings - Fork 43
/
data_utils.py
202 lines (160 loc) · 7.41 KB
/
data_utils.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
from PIL import Image
from torch.utils.data.dataset import Dataset
from modules import optical_flow_warp
import numpy as np
import os
import torch
import random
class TrainsetLoader(Dataset):
def __init__(self, cfg):
super(TrainsetLoader).__init__()
self.trainset_dir = cfg.trainset_dir
self.scale = cfg.scale
self.patch_size = cfg.patch_size
self.n_iters = cfg.n_iters * cfg.batch_size
self.video_list = os.listdir(cfg.trainset_dir)
self.degradation = cfg.degradation
def __getitem__(self, idx):
idx_video = random.randint(0, len(self.video_list)-1)
idx_frame = random.randint(0, 28) # #frames of training videos is 31, 31-3=28
lr_dir = self.trainset_dir + '/' + self.video_list[idx_video] + '/lr_x' + str(self.scale) + '_' + self.degradation
hr_dir = self.trainset_dir + '/' + self.video_list[idx_video] + '/hr'
# read HR & LR frames
LR0 = Image.open(lr_dir + '/lr' + str(idx_frame) + '.png')
LR1 = Image.open(lr_dir + '/lr' + str(idx_frame + 1) + '.png')
LR2 = Image.open(lr_dir + '/lr' + str(idx_frame + 2) + '.png')
HR0 = Image.open(hr_dir + '/hr' + str(idx_frame) + '.png')
HR1 = Image.open(hr_dir + '/hr' + str(idx_frame + 1) + '.png')
HR2 = Image.open(hr_dir + '/hr' + str(idx_frame + 2) + '.png')
LR0 = np.array(LR0, dtype=np.float32) / 255.0
LR1 = np.array(LR1, dtype=np.float32) / 255.0
LR2 = np.array(LR2, dtype=np.float32) / 255.0
HR0 = np.array(HR0, dtype=np.float32) / 255.0
HR1 = np.array(HR1, dtype=np.float32) / 255.0
HR2 = np.array(HR2, dtype=np.float32) / 255.0
# extract Y channel for LR inputs
HR0 = rgb2y(HR0)
HR1 = rgb2y(HR1)
HR2 = rgb2y(HR2)
LR0 = rgb2y(LR0)
LR1 = rgb2y(LR1)
LR2 = rgb2y(LR2)
# crop patchs randomly
HR0, HR1, HR2, LR0, LR1, LR2 = random_crop(HR0, HR1, HR2, LR0, LR1, LR2, self.patch_size, self.scale)
HR0 = HR0[:, :, np.newaxis]
HR1 = HR1[:, :, np.newaxis]
HR2 = HR2[:, :, np.newaxis]
LR0 = LR0[:, :, np.newaxis]
LR1 = LR1[:, :, np.newaxis]
LR2 = LR2[:, :, np.newaxis]
HR = np.concatenate((HR0, HR1, HR2), axis=2)
LR = np.concatenate((LR0, LR1, LR2), axis=2)
# data augmentation
LR, HR = augmentation()(LR, HR)
return toTensor(LR), toTensor(HR)
def __len__(self):
return self.n_iters
class TestsetLoader(Dataset):
def __init__(self, cfg, video_name):
super(TestsetLoader).__init__()
self.dataset_dir = cfg.testset_dir + '/' + video_name
self.degradation = cfg.degradation
self.scale = cfg.scale
self.frame_list = os.listdir(self.dataset_dir + '/lr_x' + str(self.scale) + '_' + self.degradation)
def __getitem__(self, idx):
dir = self.dataset_dir + '/lr_x' + str(self.scale) + '_' + self.degradation
LR0 = Image.open(dir + '/' + 'lr_' + str(idx+1).rjust(2, '0') + '.png')
LR1 = Image.open(dir + '/' + 'lr_' + str(idx+2).rjust(2, '0') + '.png')
LR2 = Image.open(dir + '/' + 'lr_' + str(idx+3).rjust(2, '0') + '.png')
W, H = LR1.size
# H and W should be divisible by 2
W = int(W // 2) * 2
H = int(H // 2) * 2
LR0 = LR0.crop([0, 0, W, H])
LR1 = LR1.crop([0, 0, W, H])
LR2 = LR2.crop([0, 0, W, H])
LR1_bicubic = LR1.resize((W*self.scale, H*self.scale), Image.BICUBIC)
LR1_bicubic = np.array(LR1_bicubic, dtype=np.float32) / 255.0
LR0 = np.array(LR0, dtype=np.float32) / 255.0
LR1 = np.array(LR1, dtype=np.float32) / 255.0
LR2 = np.array(LR2, dtype=np.float32) / 255.0
# extract Y channel for LR inputs
LR0_y, _, _ = rgb2ycbcr(LR0)
LR1_y, _, _ = rgb2ycbcr(LR1)
LR2_y, _, _ = rgb2ycbcr(LR2)
LR0_y = LR0_y[:, :, np.newaxis]
LR1_y = LR1_y[:, :, np.newaxis]
LR2_y = LR2_y[:, :, np.newaxis]
LR = np.concatenate((LR0_y, LR1_y, LR2_y), axis=2)
LR = toTensor(LR)
# generate Cr, Cb channels using bicubic interpolation
_, SR_cb, SR_cr = rgb2ycbcr(LR1_bicubic)
return LR, SR_cb, SR_cr
def __len__(self):
return len(self.frame_list) - 2
class augmentation(object):
def __call__(self, input, target):
if random.random()<0.5:
input = input[:, ::-1, :]
target = target[:, ::-1, :]
if random.random()<0.5:
input = input[::-1, :, :]
target = target[::-1, :, :]
if random.random()<0.5:
input = input.transpose(1, 0, 2)
target = target.transpose(1, 0, 2)
return np.ascontiguousarray(input), np.ascontiguousarray(target)
def random_crop(HR0, HR1, HR2, LR0, LR1, LR2, patch_size_lr, scale):
h_hr, w_hr = HR0.shape
h_lr = h_hr // scale
w_lr = w_hr // scale
idx_h = random.randint(10, h_lr - patch_size_lr - 10)
idx_w = random.randint(10, w_lr - patch_size_lr - 10)
h_start_hr = (idx_h - 1) * scale
h_end_hr = (idx_h - 1 + patch_size_lr) * scale
w_start_hr = (idx_w - 1) * scale
w_end_hr = (idx_w - 1 + patch_size_lr) * scale
h_start_lr = idx_h - 1
h_end_lr = idx_h - 1 + patch_size_lr
w_start_lr = idx_w - 1
w_end_lr = idx_w - 1 + patch_size_lr
HR0 = HR0[h_start_hr:h_end_hr, w_start_hr:w_end_hr]
HR1 = HR1[h_start_hr:h_end_hr, w_start_hr:w_end_hr]
HR2 = HR2[h_start_hr:h_end_hr, w_start_hr:w_end_hr]
LR0 = LR0[h_start_lr:h_end_lr, w_start_lr:w_end_lr]
LR1 = LR1[h_start_lr:h_end_lr, w_start_lr:w_end_lr]
LR2 = LR2[h_start_lr:h_end_lr, w_start_lr:w_end_lr]
return HR0, HR1, HR2, LR0, LR1, LR2
def toTensor(img):
img = torch.from_numpy(img.transpose((2, 0, 1)))
return img
def rgb2ycbcr(img_rgb):
## the range of img_rgb should be (0, 1)
img_y = 0.257 * img_rgb[:, :, 0] + 0.504 * img_rgb[:, :, 1] + 0.098 * img_rgb[:, :, 2] + 16 / 255.0
img_cb = -0.148 * img_rgb[:, :, 0] - 0.291 * img_rgb[:, :, 1] + 0.439 * img_rgb[:, :, 2] + 128 / 255.0
img_cr = 0.439 * img_rgb[:, :, 0] - 0.368 * img_rgb[:, :, 1] - 0.071 * img_rgb[:, :, 2] + 128 / 255.0
return img_y, img_cb, img_cr
def ycbcr2rgb(img_ycbcr):
## the range of img_ycbcr should be (0, 1)
img_r = 1.164 * (img_ycbcr[:, :, 0] - 16 / 255.0) + 1.596 * (img_ycbcr[:, :, 2] - 128 / 255.0)
img_g = 1.164 * (img_ycbcr[:, :, 0] - 16 / 255.0) - 0.392 * (img_ycbcr[:, :, 1] - 128 / 255.0) - 0.813 * (img_ycbcr[:, :, 2] - 128 / 255.0)
img_b = 1.164 * (img_ycbcr[:, :, 0] - 16 / 255.0) + 2.017 * (img_ycbcr[:, :, 1] - 128 / 255.0)
img_r = img_r[:, :, np.newaxis]
img_g = img_g[:, :, np.newaxis]
img_b = img_b[:, :, np.newaxis]
img_rgb = np.concatenate((img_r, img_g, img_b), 2)
return img_rgb
def rgb2y(img_rgb):
## the range of img_rgb should be (0, 1)
image_y = 0.257 * img_rgb[:, :, 0] + 0.504 * img_rgb[:, :, 1] + 0.098 * img_rgb[:, :, 2] +16 / 255.0
return image_y
def OFR_loss(x0, x1, optical_flow):
warped = optical_flow_warp(x0, optical_flow)
loss = torch.mean(torch.abs(x1 - warped)) + 0.1 * L1_regularization(optical_flow)
return loss
def L1_regularization(image):
b, _, h, w = image.size()
reg_x_1 = image[:, :, 0:h-1, 0:w-1] - image[:, :, 1:, 0:w-1]
reg_y_1 = image[:, :, 0:h-1, 0:w-1] - image[:, :, 0:h-1, 1:]
reg_L1 = torch.abs(reg_x_1) + torch.abs(reg_y_1)
return torch.sum(reg_L1) / (b*(h-1)*(w-1))