-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
222 lines (149 loc) · 7.85 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
import numpy as np
import os
from torch.utils.data import Dataset
import torch
from utils import is_png_file, load_img, Augment_RGB_torch
import torch.nn.functional as F
import random
augment = Augment_RGB_torch()
transforms_aug = [method for method in dir(augment) if callable(getattr(augment, method)) if not method.startswith('_')]
##################################################################################################
class DataLoaderTrain(Dataset):
def __init__(self, rgb_dir, img_options=None, target_transform=None):
super(DataLoaderTrain, self).__init__()
self.target_transform = target_transform
gt_dir = 'groundtruth'
input_dir = 'input'
clean_files = sorted(os.listdir(os.path.join(rgb_dir, gt_dir)))
noisy_files = sorted(os.listdir(os.path.join(rgb_dir, input_dir)))
self.clean_filenames = [os.path.join(rgb_dir, gt_dir, x) for x in clean_files if is_png_file(x)]
self.noisy_filenames = [os.path.join(rgb_dir, input_dir, x) for x in noisy_files if is_png_file(x)]
self.img_options=img_options
self.tar_size = len(self.clean_filenames) # get the size of target
def __len__(self):
return self.tar_size
def __getitem__(self, index):
tar_index = index % self.tar_size
clean = torch.from_numpy(np.float32(load_img(self.clean_filenames[tar_index])))
noisy = torch.from_numpy(np.float32(load_img(self.noisy_filenames[tar_index])))
clean = clean.permute(2,0,1)
noisy = noisy.permute(2,0,1)
clean_filename = os.path.split(self.clean_filenames[tar_index])[-1]
noisy_filename = os.path.split(self.noisy_filenames[tar_index])[-1]
#Crop Input and Target
ps = self.img_options['patch_size']
H = clean.shape[1]
W = clean.shape[2]
# r = np.random.randint(0, H - ps) if not H-ps else 0
# c = np.random.randint(0, W - ps) if not H-ps else 0
if H-ps==0:
r=0
c=0
else:
r = np.random.randint(0, H - ps)
c = np.random.randint(0, W - ps)
clean = clean[:, r:r + ps, c:c + ps]
noisy = noisy[:, r:r + ps, c:c + ps]
apply_trans = transforms_aug[random.getrandbits(3)]
clean = getattr(augment, apply_trans)(clean)
noisy = getattr(augment, apply_trans)(noisy)
return clean, noisy, clean_filename, noisy_filename
##################################################################################################
class DataLoaderTrain_Gaussian(Dataset):
def __init__(self, rgb_dir, noiselevel=5, img_options=None, target_transform=None):
super(DataLoaderTrain_Gaussian, self).__init__()
self.target_transform = target_transform
#pdb.set_trace()
clean_files = sorted(os.listdir(rgb_dir))
#noisy_files = sorted(os.listdir(os.path.join(rgb_dir, 'input')))
#clean_files = clean_files[0:83000]
#noisy_files = noisy_files[0:83000]
self.clean_filenames = [os.path.join(rgb_dir, x) for x in clean_files if is_png_file(x)]
#self.noisy_filenames = [os.path.join(rgb_dir, 'input', x) for x in noisy_files if is_png_file(x)]
self.noiselevel = noiselevel
self.img_options=img_options
self.tar_size = len(self.clean_filenames) # get the size of target
print(self.tar_size)
def __len__(self):
return self.tar_size
def __getitem__(self, index):
tar_index = index % self.tar_size
#print(self.clean_filenames[tar_index])
clean = np.float32(load_img(self.clean_filenames[tar_index]))
#noisy = torch.from_numpy(np.float32(load_img(self.noisy_filenames[tar_index])))
# noiselevel = random.randint(5,20)
noisy = clean + np.float32(np.random.normal(0, self.noiselevel, np.array(clean).shape)/255.)
noisy = np.clip(noisy,0.,1.)
clean = torch.from_numpy(clean)
noisy = torch.from_numpy(noisy)
clean = clean.permute(2,0,1)
noisy = noisy.permute(2,0,1)
clean_filename = os.path.split(self.clean_filenames[tar_index])[-1]
noisy_filename = os.path.split(self.clean_filenames[tar_index])[-1]
#Crop Input and Target
ps = self.img_options['patch_size']
H = clean.shape[1]
W = clean.shape[2]
r = np.random.randint(0, H - ps)
c = np.random.randint(0, W - ps)
clean = clean[:, r:r + ps, c:c + ps]
noisy = noisy[:, r:r + ps, c:c + ps]
apply_trans = transforms_aug[random.getrandbits(3)]
clean = getattr(augment, apply_trans)(clean)
noisy = getattr(augment, apply_trans)(noisy)
return clean, noisy, clean_filename, noisy_filename
##################################################################################################
class DataLoaderVal(Dataset):
def __init__(self, rgb_dir, target_transform=None):
super(DataLoaderVal, self).__init__()
self.target_transform = target_transform
gt_dir = 'groundtruth'
input_dir = 'input'
clean_files = sorted(os.listdir(os.path.join(rgb_dir, gt_dir)))
noisy_files = sorted(os.listdir(os.path.join(rgb_dir, input_dir)))
self.clean_filenames = [os.path.join(rgb_dir, gt_dir, x) for x in clean_files if is_png_file(x)]
self.noisy_filenames = [os.path.join(rgb_dir, input_dir, x) for x in noisy_files if is_png_file(x)]
self.tar_size = len(self.clean_filenames)
def __len__(self):
return self.tar_size
def __getitem__(self, index):
tar_index = index % self.tar_size
clean = torch.from_numpy(np.float32(load_img(self.clean_filenames[tar_index])))
noisy = torch.from_numpy(np.float32(load_img(self.noisy_filenames[tar_index])))
clean_filename = os.path.split(self.clean_filenames[tar_index])[-1]
noisy_filename = os.path.split(self.noisy_filenames[tar_index])[-1]
clean = clean.permute(2,0,1)
noisy = noisy.permute(2,0,1)
return clean, noisy, clean_filename, noisy_filename
##################################################################################################
class DataLoaderTest(Dataset):
def __init__(self, rgb_dir, target_transform=None):
super(DataLoaderTest, self).__init__()
self.target_transform = target_transform
noisy_files = sorted(os.listdir(os.path.join(rgb_dir, 'input')))
self.noisy_filenames = [os.path.join(rgb_dir, 'input', x) for x in noisy_files if is_png_file(x)]
self.tar_size = len(self.noisy_filenames)
def __len__(self):
return self.tar_size
def __getitem__(self, index):
tar_index = index % self.tar_size
noisy = torch.from_numpy(np.float32(load_img(self.noisy_filenames[tar_index])))
noisy_filename = os.path.split(self.noisy_filenames[tar_index])[-1]
noisy = noisy.permute(2,0,1)
return noisy, noisy_filename
##################################################################################################
class DataLoaderTestSR(Dataset):
def __init__(self, rgb_dir, target_transform=None):
super(DataLoaderTestSR, self).__init__()
self.target_transform = target_transform
LR_files = sorted(os.listdir(os.path.join(rgb_dir)))
self.LR_filenames = [os.path.join(rgb_dir, x) for x in LR_files if is_png_file(x)]
self.tar_size = len(self.LR_filenames)
def __len__(self):
return self.tar_size
def __getitem__(self, index):
tar_index = index % self.tar_size
LR = torch.from_numpy(np.float32(load_img(self.LR_filenames[tar_index])))
LR_filename = os.path.split(self.LR_filenames[tar_index])[-1]
LR = LR.permute(2,0,1)
return LR, LR_filename