-
Notifications
You must be signed in to change notification settings - Fork 68
/
dataset.py
261 lines (218 loc) · 10.4 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
import torch.utils.data as data
import torch
import numpy as np
import os
from os import listdir
from os.path import join
from PIL import Image, ImageOps
import random
import pyflow
from skimage import img_as_float
from random import randrange
import os.path
def is_image_file(filename):
return any(filename.endswith(extension) for extension in [".png", ".jpg", ".jpeg"])
def load_img(filepath, nFrames, scale, other_dataset, upscale_only):
seq = [i for i in range(1, nFrames)]
#random.shuffle(seq) #if random sequence
if other_dataset:
if upscale_only:
target = Image.open(filepath).convert('RGB')
input=target
else:
target = modcrop(Image.open(filepath).convert('RGB'),scale)
input=target.resize((int(target.size[0]/scale),int(target.size[1]/scale)), Image.BICUBIC)
char_len = len(filepath)
neigbor=[]
for i in seq:
index = int(filepath[char_len-7:char_len-4])-i
file_name=filepath[0:char_len-7]+'{0:03d}'.format(index)+'.png'
if os.path.exists(file_name):
if upscale_only:
temp = Image.open(filepath[0:char_len-7]+'{0:03d}'.format(index)+'.png').convert('RGB')
else:
temp = modcrop(Image.open(filepath[0:char_len-7]+'{0:03d}'.format(index)+'.png').convert('RGB'),scale).resize((int(target.size[0]/scale),int(target.size[1]/scale)), Image.BICUBIC)
neigbor.append(temp)
else:
# print('neigbor frame is not exist')
temp = input
neigbor.append(temp)
else:
if upscale_only:
target = Image.open(join(filepath,'im'+str(nFrames)+'.png')).convert('RGB')
input = target
neigbor = [Image.open(filepath+'/im'+str(j)+'.png').convert('RGB') for j in reversed(seq)]
else:
target = modcrop(Image.open(join(filepath,'im'+str(nFrames)+'.png')).convert('RGB'), scale)
input = target.resize((int(target.size[0]/scale), int(target.size[1]/scale)), Image.BICUBIC)
neigbor = [modcrop(Image.open(filepath+'/im'+str(j)+'.png').convert('RGB'), scale).resize((int(target.size[0]/scale),int(target.size[1]/scale)), Image.BICUBIC) for j in reversed(seq)]
return target, input, neigbor
def load_img_future(filepath, nFrames, scale, other_dataset, upscale_only):
tt = int(nFrames/2)
if other_dataset:
if upscale_only:
target = Image.open(filepath).convert('RGB')
input = target
else:
target = modcrop(Image.open(filepath).convert('RGB'),scale)
input = target.resize((int(target.size[0]/scale),int(target.size[1]/scale)), Image.BICUBIC)
char_len = len(filepath)
neigbor=[]
if nFrames%2 == 0:
seq = [x for x in range(-tt,tt) if x!=0] # or seq = [x for x in range(-tt+1,tt+1) if x!=0]
else:
seq = [x for x in range(-tt,tt+1) if x!=0]
#random.shuffle(seq) #if random sequence
for i in seq:
index1 = int(filepath[char_len-7:char_len-4])+i
file_name1=filepath[0:char_len-7]+'{0:03d}'.format(index1)+'.png'
if os.path.exists(file_name1):
if upscale_only:
temp = Image.open(file_name1).convert('RGB')
else:
temp = modcrop(Image.open(file_name1).convert('RGB'), scale).resize((int(target.size[0]/scale),int(target.size[1]/scale)), Image.BICUBIC)
neigbor.append(temp)
else:
# print('neigbor frame- is not exist')
temp=input
neigbor.append(temp)
else:
if upscale_only:
target = Image.open(join(filepath,'im4.png')).convert('RGB')
input = target
else:
target = modcrop(Image.open(join(filepath,'im4.png')).convert('RGB'),scale)
input = target.resize((int(target.size[0]/scale),int(target.size[1]/scale)), Image.BICUBIC)
neigbor = []
seq = [x for x in range(4-tt,5+tt) if x!=4]
#random.shuffle(seq) #if random sequence
for j in seq:
neigbor.append(modcrop(Image.open(filepath+'/im'+str(j)+'.png').convert('RGB'), scale).resize((int(target.size[0]/scale),int(target.size[1]/scale)), Image.BICUBIC))
return target, input, neigbor
def get_flow(im1, im2):
im1 = np.array(im1)
im2 = np.array(im2)
im1 = im1.astype(float) / 255.
im2 = im2.astype(float) / 255.
# Flow Options:
alpha = 0.012
ratio = 0.75
minWidth = 20
nOuterFPIterations = 7
nInnerFPIterations = 1
nSORIterations = 30
colType = 0 # 0 or default:RGB, 1:GRAY (but pass gray image with shape (h,w,1))
u, v, im2W = pyflow.coarse2fine_flow(im1, im2, alpha, ratio, minWidth, nOuterFPIterations, nInnerFPIterations,nSORIterations, colType)
flow = np.concatenate((u[..., None], v[..., None]), axis=2)
#flow = rescale_flow(flow,0,1)
return flow
def rescale_flow(x,max_range,min_range):
max_val = np.max(x)
min_val = np.min(x)
return (max_range-min_range)/(max_val-min_val)*(x-max_val)+max_range
def modcrop(img, modulo):
(ih, iw) = img.size
ih = ih - (ih%modulo)
iw = iw - (iw%modulo)
img = img.crop((0, 0, ih, iw))
return img
def get_patch(img_in, img_tar, img_nn, patch_size, scale, nFrames, ix=-1, iy=-1):
(ih, iw) = img_in.size
(th, tw) = (scale * ih, scale * iw)
patch_mult = scale #if len(scale) > 1 else 1
tp = patch_mult * patch_size
ip = tp // scale
if ix == -1:
ix = random.randrange(0, iw - ip + 1)
if iy == -1:
iy = random.randrange(0, ih - ip + 1)
(tx, ty) = (scale * ix, scale * iy)
img_in = img_in.crop((iy,ix,iy + ip, ix + ip))#[:, iy:iy + ip, ix:ix + ip]
img_tar = img_tar.crop((ty,tx,ty + tp, tx + tp))#[:, ty:ty + tp, tx:tx + tp]
img_nn = [j.crop((iy,ix,iy + ip, ix + ip)) for j in img_nn] #[:, iy:iy + ip, ix:ix + ip]
info_patch = {
'ix': ix, 'iy': iy, 'ip': ip, 'tx': tx, 'ty': ty, 'tp': tp}
return img_in, img_tar, img_nn, info_patch
def augment(img_in, img_tar, img_nn, flip_h=True, rot=True):
info_aug = {'flip_h': False, 'flip_v': False, 'trans': False}
if random.random() < 0.5 and flip_h:
img_in = ImageOps.flip(img_in)
img_tar = ImageOps.flip(img_tar)
img_nn = [ImageOps.flip(j) for j in img_nn]
info_aug['flip_h'] = True
if rot:
if random.random() < 0.5:
img_in = ImageOps.mirror(img_in)
img_tar = ImageOps.mirror(img_tar)
img_nn = [ImageOps.mirror(j) for j in img_nn]
info_aug['flip_v'] = True
if random.random() < 0.5:
img_in = img_in.rotate(180)
img_tar = img_tar.rotate(180)
img_nn = [j.rotate(180) for j in img_nn]
info_aug['trans'] = True
return img_in, img_tar, img_nn, info_aug
def rescale_img(img_in, scale):
size_in = img_in.size
new_size_in = tuple([int(x * scale) for x in size_in])
img_in = img_in.resize(new_size_in, resample=Image.BICUBIC)
return img_in
class DatasetFromFolder(data.Dataset):
def __init__(self, image_dir,nFrames, upscale_factor, data_augmentation, file_list, other_dataset, patch_size, future_frame, transform=None):
super(DatasetFromFolder, self).__init__()
alist = [line.rstrip() for line in open(join(image_dir,file_list))]
self.image_filenames = [join(image_dir,x) for x in alist]
self.nFrames = nFrames
self.upscale_factor = upscale_factor
self.transform = transform
self.data_augmentation = data_augmentation
self.other_dataset = other_dataset
self.patch_size = patch_size
self.future_frame = future_frame
def __getitem__(self, index):
if self.future_frame:
target, input, neigbor = load_img_future(self.image_filenames[index], self.nFrames, self.upscale_factor, self.other_dataset)
else:
target, input, neigbor = load_img(self.image_filenames[index], self.nFrames, self.upscale_factor, self.other_dataset)
if self.patch_size != 0:
input, target, neigbor, _ = get_patch(input,target,neigbor,self.patch_size, self.upscale_factor, self.nFrames)
if self.data_augmentation:
input, target, neigbor, _ = augment(input, target, neigbor)
flow = [get_flow(input,j) for j in neigbor]
bicubic = rescale_img(input, self.upscale_factor)
if self.transform:
target = self.transform(target)
input = self.transform(input)
bicubic = self.transform(bicubic)
neigbor = [self.transform(j) for j in neigbor]
flow = [torch.from_numpy(j.transpose(2,0,1)) for j in flow]
return input, target, neigbor, flow, bicubic
def __len__(self):
return len(self.image_filenames)
class DatasetFromFolderTest(data.Dataset):
def __init__(self, image_dir, nFrames, upscale_factor, file_list, other_dataset, future_frame, transform=None, upscale_only=False):
super(DatasetFromFolderTest, self).__init__()
alist = [line.rstrip() for line in open(join(image_dir,file_list))]
self.image_filenames = [join(image_dir,x) for x in alist]
self.nFrames = nFrames
self.upscale_factor = upscale_factor
self.transform = transform
self.other_dataset = other_dataset
self.future_frame = future_frame
self.upscale_only = upscale_only
def __getitem__(self, index):
if self.future_frame:
target, input, neigbor = load_img_future(self.image_filenames[index], self.nFrames, self.upscale_factor, self.other_dataset, self.upscale_only)
else:
target, input, neigbor = load_img(self.image_filenames[index], self.nFrames, self.upscale_factor, self.other_dataset, self.upscale_only)
flow = [get_flow(input,j) for j in neigbor]
bicubic = rescale_img(input, self.upscale_factor)
if self.transform:
target = self.transform(target)
input = self.transform(input)
bicubic = self.transform(bicubic)
neigbor = [self.transform(j) for j in neigbor]
flow = [torch.from_numpy(j.transpose(2,0,1)) for j in flow]
return input, target, neigbor, flow, bicubic
def __len__(self):
return len(self.image_filenames)