-
Notifications
You must be signed in to change notification settings - Fork 62
/
dataset.py
157 lines (118 loc) · 4.64 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
from __future__ import division
import torchvision
import torchvision.transforms as T
import os
import glob
import scipy.misc
import scipy.ndimage
from PIL import Image, ImageFile
import numpy as np
import h5py
import torch
from skimage import io, transform
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
from skimage import io, transform
from PIL import Image
import matplotlib.pyplot as plt
import random
ImageFile.LOAD_TRUNCATED_IMAGES = True
from torch.utils.data import Dataset
class Dataset(object):
def __init__(self, dir, trans=None, status='train'):
self.dir_HR = os.path.join(dir, "HQ")
self.dir_LR = os.path.join(dir, "LQ")
self.lis = sorted(os.listdir(self.dir_HR))
self.status = status
self.transform = trans
def __len__(self):
return len(self.lis) * 96
def __getitem__(self, idx):
id = idx//96
idx = idx - 96 * id
HR = os.path.join(self.dir_HR, self.lis[id])
LR = os.path.join(self.dir_LR, self.lis[id])
ims = sorted(os.listdir(HR))
# number of the seq
num = len(ims)
i = idx
# get frame size
image = io.imread(os.path.join(HR, ims[i]))
row, col, ch = image.shape
frames_lr = np.zeros((5, int(row/4), int(col/4), ch))
frames_hr = io.imread(os.path.join(HR, ims[i+2]))
for j in range(i, i + 5):
k = j - i
frames_lr[k, :, :, :] = io.imread(os.path.join(LR, ims[j]))
sample = {'lr': frames_lr, 'hr': frames_hr, 'im_name':ims[i]}
if self.transform:
sample = self.transform(sample)
return sample
class DataAug(object):
def __call__(self, sample):
hflip = random.random() < 0.5
vflip = random.random() < 0.5
rot90 = random.random() < 0.5
lr, hr, name = sample['lr'], sample['hr'], sample['im_name']
num, r, c, ch = lr.shape
if hflip:
hr = hr[:, ::-1, :]
for idx in range(num):
lr[idx, :, :, :] = lr[idx, :, ::-1, :]
if vflip:
hr = hr[::-1, :, :]
for idx in range(num):
lr[idx, :, :, :] = lr[idx, ::-1, :, :]
if rot90:
hr= hr.transpose(1, 0, 2)
lr = lr.transpose(0, 2, 1, 3)
return {'lr': lr, 'hr': hr, 'im_name':name}
class CenterCrop(object):
"""crop randomly the image in a sample
Args, output_size:desired output size. If int, square crop is mad
"""
def __init__(self, output_size, scale):
self.scale = scale
assert isinstance(output_size, (int, tuple))
if isinstance(output_size, int):
self.output_size = (output_size, output_size)
else:
assert len(output_size) == 2
self.output_size = output_size
def __call__(self, sample):
lr, hr, name = sample['lr'], sample['hr'], sample['im_name']
h, w = lr.shape[1: 3]
new_h, new_w = self.output_size
top = h//2 - self.output_size[0]//2
left = w//2 - self.output_size[0]//2
new_lr = lr[:,top:top + new_h, left: left + new_w, :]
new_hr = hr[top*self.scale:top*self.scale + new_h*self.scale, left*self.scale: left*self.scale + new_w*self.scale, :]
return {'lr': new_lr, "hr": new_hr, "im_name": name}
class RandomCrop(object):
"""crop randomly the image in a sample
Args, output_size:desired output size. If int, square crop is mad
"""
def __init__(self, output_size, scale):
self.scale = scale
assert isinstance(output_size, (int, tuple))
if isinstance(output_size, int):
self.output_size = (output_size, output_size)
else:
assert len(output_size) == 2
self.output_size = output_size
def __call__(self, sample):
lr, hr, name = sample['lr'], sample['hr'], sample['im_name']
h, w = lr.shape[1: 3]
new_h, new_w = self.output_size
top = np.random.randint(0, h - new_h)
left = np.random.randint(0, w - new_w)
new_lr = lr[:,top:top + new_h, left: left + new_w, :]
new_hr = hr[top*self.scale:top*self.scale + new_h*self.scale, left*self.scale: left*self.scale + new_w*self.scale, :]
return {'lr': new_lr, "hr": new_hr, "im_name": name}
class ToTensor(object):
"""convert ndarrays in sample to Tensors."""
def __call__(self, sample):
lr, hr, name = sample['lr']/255.0 - 0.5, sample['hr']/255.0 - 0.5, sample['im_name']
lr = torch.from_numpy(lr).float()
hr = torch.from_numpy(hr).float()
return {'lr': lr.permute(0, 3, 1, 2), 'hr': hr.permute(2, 0, 1), 'im_name':name}