forked from Luciferbobo/TTSR_b2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataloader.py
198 lines (161 loc) · 7 KB
/
dataloader.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
from torch.utils.data import DataLoader
from importlib import import_module
import os
from imageio import imread
from PIL import Image
import numpy as np
import glob
import random
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset
from torchvision import transforms
# Ignore warnings
import warnings
warnings.filterwarnings("ignore")
def get_dataloader(args):
### import module
m = import_module(args.dataset.lower())
if (args.dataset == 'CUFED'):
data_train = getattr(m, 'TrainSet')(args)
dataloader_train = DataLoader(data_train, batch_size=args.batch_size, shuffle=True, num_workers=0)
dataloader_test = {}
for i in range(5):
data_test = getattr(m, 'TestSet')(args=args, ref_level=str(i+1))
dataloader_test[str(i+1)] = DataLoader(data_test, batch_size=1, shuffle=False, num_workers=0)
dataloader = {'train': dataloader_train, 'test': dataloader_test}
else:
raise SystemExit('Error: no such type of dataset!')
return dataloader
class RandomRotate(object):
def __call__(self, sample):
k1 = np.random.randint(0, 4)
sample['LR'] = np.rot90(sample['LR'], k1).copy()
sample['HR'] = np.rot90(sample['HR'], k1).copy()
sample['LR_sr'] = np.rot90(sample['LR_sr'], k1).copy()
k2 = np.random.randint(0, 4)
sample['Ref'] = np.rot90(sample['Ref'], k2).copy()
sample['Ref_sr'] = np.rot90(sample['Ref_sr'], k2).copy()
return sample
class RandomFlip(object):
def __call__(self, sample):
if (np.random.randint(0, 2) == 1):
sample['LR'] = np.fliplr(sample['LR']).copy()
sample['HR'] = np.fliplr(sample['HR']).copy()
sample['LR_sr'] = np.fliplr(sample['LR_sr']).copy()
if (np.random.randint(0, 2) == 1):
sample['Ref'] = np.fliplr(sample['Ref']).copy()
sample['Ref_sr'] = np.fliplr(sample['Ref_sr']).copy()
if (np.random.randint(0, 2) == 1):
sample['LR'] = np.flipud(sample['LR']).copy()
sample['HR'] = np.flipud(sample['HR']).copy()
sample['LR_sr'] = np.flipud(sample['LR_sr']).copy()
if (np.random.randint(0, 2) == 1):
sample['Ref'] = np.flipud(sample['Ref']).copy()
sample['Ref_sr'] = np.flipud(sample['Ref_sr']).copy()
return sample
class ToTensor(object):
def __call__(self, sample):
LR, LR_sr, HR, Ref, Ref_sr = sample['LR'], sample['LR_sr'], sample['HR'], sample['Ref'], sample['Ref_sr']
LR = LR.transpose((2,0,1))
LR_sr = LR_sr.transpose((2,0,1))
HR = HR.transpose((2,0,1))
Ref = Ref.transpose((2,0,1))
Ref_sr = Ref_sr.transpose((2,0,1))
return {'LR': torch.from_numpy(LR).float(),
'LR_sr': torch.from_numpy(LR_sr).float(),
'HR': torch.from_numpy(HR).float(),
'Ref': torch.from_numpy(Ref).float(),
'Ref_sr': torch.from_numpy(Ref_sr).float()}
class TrainSet(Dataset):
def __init__(self, args, transform=transforms.Compose([RandomFlip(), RandomRotate(), ToTensor()]) ):
self.input_list = sorted([os.path.join(args.dataset_dir, 'train/input', name) for name in
os.listdir( os.path.join(args.dataset_dir, 'train/input') )])
self.ref_list = sorted([os.path.join(args.dataset_dir, 'train/ref', name) for name in
os.listdir( os.path.join(args.dataset_dir, 'train/ref') )])
self.transform = transform
def __len__(self):
return len(self.input_list)
def __getitem__(self, idx):
### HR
HR = imread(self.input_list[idx])
h,w = HR.shape[:2]
#HR = HR[:h//4*4, :w//4*4, :]
### LR and LR_sr
LR = np.array(Image.fromarray(HR).resize((w//4, h//4), Image.BICUBIC))
LR_sr = np.array(Image.fromarray(LR).resize((w, h), Image.BICUBIC))
### Ref and Ref_sr
Ref_sub = imread(self.ref_list[idx])
h2, w2 = Ref_sub.shape[:2]
Ref_sr_sub = np.array(Image.fromarray(Ref_sub).resize((w2//4, h2//4), Image.BICUBIC))
Ref_sr_sub = np.array(Image.fromarray(Ref_sr_sub).resize((w2, h2), Image.BICUBIC))
### complete ref and ref_sr to the same size, to use batch_size > 1
Ref = np.zeros((160, 160, 3))
Ref_sr = np.zeros((160, 160, 3))
Ref[:h2, :w2, :] = Ref_sub
Ref_sr[:h2, :w2, :] = Ref_sr_sub
### change type
LR = LR.astype(np.float32)
LR_sr = LR_sr.astype(np.float32)
HR = HR.astype(np.float32)
Ref = Ref.astype(np.float32)
Ref_sr = Ref_sr.astype(np.float32)
### rgb range to [-1, 1]
LR = LR / 127.5 - 1.
LR_sr = LR_sr / 127.5 - 1.
HR = HR / 127.5 - 1.
Ref = Ref / 127.5 - 1.
Ref_sr = Ref_sr / 127.5 - 1.
sample = {'LR': LR,
'LR_sr': LR_sr,
'HR': HR,
'Ref': Ref,
'Ref_sr': Ref_sr}
if self.transform:
sample = self.transform(sample)
return sample
class TestSet(Dataset):
def __init__(self, args, ref_level='1', transform=transforms.Compose([ToTensor()])):
self.input_list = sorted(glob.glob(os.path.join(args.dataset_dir, 'test/CUFED5', '*_0.png')))
self.ref_list = sorted(glob.glob(os.path.join(args.dataset_dir, 'test/CUFED5',
'*_' + ref_level + '.png')))
self.transform = transform
def __len__(self):
return len(self.input_list)
def __getitem__(self, idx):
### HR
HR = imread(self.input_list[idx])
h, w = HR.shape[:2]
h, w = h//4*4, w//4*4
HR = HR[:h, :w, :] ### crop to the multiple of 4
### LR and LR_sr
LR = np.array(Image.fromarray(HR).resize((w//4, h//4), Image.BICUBIC))
LR_sr = np.array(Image.fromarray(LR).resize((w, h), Image.BICUBIC))
### Ref and Ref_sr
Ref = imread(self.ref_list[idx])
h2, w2 = Ref.shape[:2]
h2, w2 = h2//4*4, w2//4*4
Ref = Ref[:h2, :w2, :]
Ref_sr = np.array(Image.fromarray(Ref).resize((w2//4, h2//4), Image.BICUBIC))
Ref_sr = np.array(Image.fromarray(Ref_sr).resize((w2, h2), Image.BICUBIC))
### change type
LR = LR.astype(np.float32)
LR_sr = LR_sr.astype(np.float32)
HR = HR.astype(np.float32)
Ref = Ref.astype(np.float32)
Ref_sr = Ref_sr.astype(np.float32)
### rgb range to [-1, 1]
LR = LR / 127.5 - 1.
LR_sr = LR_sr / 127.5 - 1.
HR = HR / 127.5 - 1.
Ref = Ref / 127.5 - 1.
Ref_sr = Ref_sr / 127.5 - 1.
sample = {'LR': LR,
'LR_sr': LR_sr,
'HR': HR,
'Ref': Ref,
'Ref_sr': Ref_sr}
if self.transform:
sample = self.transform(sample)
return sample