-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_loader.py
379 lines (312 loc) · 14 KB
/
data_loader.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import os
import os.path
import random
from PIL import Image
import csv
import scipy.io
import numpy as np
import torch
import torchvision
import torch.utils.data as data
class DataLoader(object):
"""Dataset class for IQA databases"""
def __init__(self, config, dataset, path, patch_num, img_indx=None, istrain=True):
# config.dataset = dataset
self.train_bs = config.train_bs
self.eval_bs = config.eval_bs
self.istrain = istrain
self.num_workers = config.num_workers
transforms_rgb = torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225))
])
transforms_ycbcr = torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize(mean=[0.448, 0.483, 0.491],
std=[0.248, 0.114, 0.106])
])
if dataset == 'live':
self.data = LIVEFolder(
root=path, index=img_indx, transforms=TwoTransform(transforms_rgb, transforms_ycbcr), patch_num=patch_num, img_crop=True)
elif dataset=='csiq':
self.data = CSIQFolder(
root=path, index=img_indx, transforms=TwoTransform(transforms_rgb, transforms_ycbcr), patch_num=patch_num, img_crop=True)
elif dataset == 'tid2013':
self.data = TID2013Folder(
root=path, index=img_indx, transforms=TwoTransform(transforms_rgb, transforms_ycbcr), patch_num=patch_num, img_crop=False)
elif dataset == 'kadid-10k':
self.data = KADID10KFolder(
root=path, index=img_indx, transforms=TwoTransform(transforms_rgb, transforms_ycbcr), patch_num=patch_num, img_crop=False)
elif dataset == 'livec':
self.data = LIVEChallengeFolder(
root=path, index=img_indx, transforms=TwoTransform(transforms_rgb, transforms_ycbcr), patch_num=patch_num, img_crop=True)
elif dataset == 'koniq-10k':
self.data = Koniq_10kFolder(
root=path, index=img_indx, transforms=TwoTransform(transforms_rgb, transforms_ycbcr), patch_num=patch_num, img_crop=True)
else:
print('Invalid dataset were provided.')
def get_data(self):
if self.istrain:
dataloader = torch.utils.data.DataLoader(self.data, batch_size=self.train_bs, shuffle=True, num_workers=self.num_workers,pin_memory=False)
else:
dataloader = torch.utils.data.DataLoader( self.data, batch_size=self.eval_bs,num_workers=self.num_workers, shuffle=False,pin_memory=False)
return dataloader
class TwoTransform:
"""Create two crops of the same image"""
def __init__(self, transforms_rgb, transforms_ycbcr):
self.transforms_rgb = transforms_rgb
self.transforms_ycbcr = transforms_ycbcr
def __call__(self, x, y):
return [self.transforms_rgb(x), self.transforms_ycbcr(y)]
class LIVEFolder(data.Dataset):
def __init__(self, root, index, transforms, patch_num, img_crop):
refpath = os.path.join(root, 'refimgs')
refname = getFileName(refpath, '.bmp')
jp2kroot = os.path.join(root, 'jp2k')
jp2kname = self.getDistortionTypeFileName(jp2kroot, 227)
jpegroot = os.path.join(root, 'jpeg')
jpegname = self.getDistortionTypeFileName(jpegroot, 233)
wnroot = os.path.join(root, 'wn')
wnname = self.getDistortionTypeFileName(wnroot, 174)
gblurroot = os.path.join(root, 'gblur')
gblurname = self.getDistortionTypeFileName(gblurroot, 174)
fastfadingroot = os.path.join(root, 'fastfading')
fastfadingname = self.getDistortionTypeFileName(fastfadingroot, 174)
imgpath = jp2kname + jpegname + wnname + gblurname + fastfadingname
dmos = scipy.io.loadmat(os.path.join(root, 'dmos_realigned.mat'))
labels = dmos['dmos_new'].astype(np.float32)
orgs = dmos['orgs']
refnames_all = scipy.io.loadmat(os.path.join(root, 'refnames_all.mat'))
refnames_all = refnames_all['refnames_all']
sample = []
for i in range(0, len(index)):
train_sel = (refname[index[i]] == refnames_all)
train_sel = train_sel * ~orgs.astype(np.bool_)
train_sel = np.where(train_sel == True)
train_sel = train_sel[1].tolist()
for j, item in enumerate(train_sel):
for aug in range(patch_num):
sample.append((imgpath[item], labels[0][item]))
self.img_crop = img_crop
self.samples = sample
self.transforms = transforms
def __getitem__(self, index):
path, target = self.samples[index]
sample_rgb = imgread(path, is_rgb=True)
sample_ycbcr = imgread(path, is_rgb=False)
sample_rgb, sample_ycbcr = imgprocess(sample_rgb, sample_ycbcr, patch_size=[288, 384], is_crop=self.img_crop)
sample = self.transforms(sample_rgb, sample_ycbcr)
return sample, target
def __len__(self):
length = len(self.samples)
return length
def getDistortionTypeFileName(self, path, num):
filename = []
index = 1
for i in range(0, num):
name = '%s%s%s' % ('img', str(index), '.bmp')
filename.append(os.path.join(path, name))
index = index + 1
return filename
class CSIQFolder(data.Dataset):
def __init__(self, root, index, transforms, patch_num, img_crop):
refpath = os.path.join(root, 'src_imgs')
refname = getFileName(refpath, '.png')
txtpath = os.path.join(root, 'csiq_label.txt')
fh = open(txtpath, 'r')
imgnames = []
target = []
refnames_all = []
for line in fh:
line = line.split('\n')
words = line[0].split()
imgnames.append((words[0]))
target.append(words[1])
ref_temp = words[0].split(".")
refnames_all.append(ref_temp[0] + '.' + ref_temp[-1])
labels = np.array(target).astype(np.float32)
refnames_all = np.array(refnames_all)
sample = []
for i, item in enumerate(index):
train_sel = (refname[index[i]] == refnames_all)
train_sel = np.where(train_sel == True)
train_sel = train_sel[0].tolist()
for j, item in enumerate(train_sel):
for aug in range(patch_num):
# Create a folder 'all_imgs' and copy all distorted images into it.
sample.append((os.path.join(root, 'all_imgs', imgnames[item]), labels[item]))
self.img_crop = img_crop
self.samples = sample
self.transforms = transforms
def __getitem__(self, index):
path, target = self.samples[index]
sample_rgb = imgread(path, is_rgb=True)
sample_ycbcr = imgread(path, is_rgb=False)
sample_rgb, sample_ycbcr = imgprocess(sample_rgb, sample_ycbcr, patch_size=[288, 384], is_crop=self.img_crop)
sample = self.transforms(sample_rgb,sample_ycbcr)
return sample, target
def __len__(self):
length = len(self.samples)
return length
class TID2013Folder(data.Dataset):
def __init__(self, root, index, transforms, patch_num, img_crop):
refpath = os.path.join(root, 'reference_images')
refname = sorted(getTIDFileName(refpath, '.bmp.BMP'))
txtpath = os.path.join(root, 'mos_with_names.txt')
fh = open(txtpath, 'r')
imgnames = []
target = []
refnames_all = []
for line in fh:
line = line.split('\n')
words = line[0].split()
imgnames.append((words[1]))
target.append(words[0])
ref_temp = words[1].split("_")
refnames_all.append(ref_temp[0][1:])
labels = np.array(target).astype(np.float32)
refnames_all = np.array(refnames_all)
sample = []
for i, item in enumerate(index):
train_sel = (refname[index[i]] == refnames_all)
train_sel = np.where(train_sel == True)
train_sel = train_sel[0].tolist()
for j, item in enumerate(train_sel):
for aug in range(patch_num):
sample.append((os.path.join(root, 'distorted_images', imgnames[item]), labels[item]))
self.img_crop = img_crop
self.samples = sample
self.transforms = transforms
def __getitem__(self, index):
path, target = self.samples[index]
sample_rgb = imgread(path, is_rgb=True)
sample_ycbcr = imgread(path, is_rgb=False)
sample_rgb, sample_ycbcr = imgprocess(sample_rgb, sample_ycbcr, patch_size=[288, 384],is_crop=self.img_crop)
sample = self.transforms(sample_rgb, sample_ycbcr)
return sample, target
def __len__(self):
length = len(self.samples)
return length
class KADID10KFolder(data.Dataset):
def __init__(self, root, index, transforms, patch_num, img_crop):
imgname = []
refnames_all = []
labels = []
csv_file = os.path.join(root, 'dmos.csv')
with open(csv_file) as f:
reader = csv.DictReader(f)
for row in reader:
imgname.append(row['dist_img'])
refnames_all.append(row['ref_img'])
mos = np.array(float(row['dmos']))
labels.append(mos)
im_ref = np.unique(refnames_all)
refnames_all = np.array(refnames_all)
sample = []
for i, item in enumerate(index):
train_sel = (im_ref[index[i]] == refnames_all)
train_sel = np.where(train_sel == True)
train_sel = train_sel[0].tolist()
for j, item in enumerate(train_sel):
for aug in range(patch_num):
sample.append((os.path.join(root, 'images', imgname[item]), labels[item]))
self.img_crop = img_crop
self.samples = sample
self.transforms = transforms
def __getitem__(self, index):
path, target = self.samples[index]
sample_rgb = imgread(path, is_rgb=True)
sample_ycbcr = imgread(path, is_rgb=False)
sample_rgb, sample_ycbcr = imgprocess(sample_rgb, sample_ycbcr, patch_size=[288, 384], is_crop=self.img_crop)
sample = self.transforms(sample_rgb, sample_ycbcr)
return sample, target
def __len__(self):
length = len(self.samples)
return length
class LIVEChallengeFolder(data.Dataset):
def __init__(self, root, index, transforms, patch_num, img_crop):
imgpath = scipy.io.loadmat(os.path.join(root, 'Data', 'AllImages_release.mat'))
imgpath = imgpath['AllImages_release']
imgpath = imgpath[7:]
mos = scipy.io.loadmat(os.path.join(root, 'Data', 'AllMOS_release.mat'))
labels = mos['AllMOS_release'].astype(np.float32)
labels = labels[0][7:]
sample = []
for i, item in enumerate(index):
for aug in range(patch_num):
sample.append((os.path.join(root, 'Images', imgpath[item][0][0]), labels[item]))
self.img_crop = img_crop
self.samples = sample
self.transforms = transforms
def __getitem__(self, index):
path, target = self.samples[index]
sample_rgb = imgread(path, is_rgb=True)
sample_ycbcr = imgread(path, is_rgb=False)
sample_rgb, sample_ycbcr = imgprocess(sample_rgb, sample_ycbcr, patch_size=[288, 384], is_crop=self.img_crop)
sample = self.transforms(sample_rgb, sample_ycbcr)
return sample, target
def __len__(self):
length = len(self.samples)
return length
class Koniq_10kFolder(data.Dataset):
def __init__(self, root, index, transforms, patch_num, img_crop):
imgname = []
labels = []
csv_file = os.path.join(root, 'koniq10k_scores_and_distributions.csv')
with open(csv_file) as f:
reader = csv.DictReader(f)
for row in reader:
imgname.append(row['image_name'])
mos = np.array(float(row['MOS_zscore'])).astype(np.float32)
labels.append(mos)
sample = []
for i, item in enumerate(index):
for aug in range(patch_num):
sample.append((os.path.join(root, '512x384', imgname[item]), labels[item]))
self.img_crop = img_crop
self.samples = sample
self.transforms = transforms
def __getitem__(self, index):
path, target = self.samples[index]
sample_rgb = imgread(path, is_rgb=True)
sample_ycbcr = imgread(path, is_rgb=False)
sample_rgb, sample_ycbcr = imgprocess(sample_rgb, sample_ycbcr, patch_size=[288, 384], is_crop=self.img_crop)
sample = self.transforms(sample_rgb, sample_ycbcr)
return sample, target
def __len__(self):
length = len(self.samples)
return length
def getFileName(path, suffix):
filename = []
f_list = os.listdir(path)
for i in f_list:
if os.path.splitext(i)[1] == suffix:
filename.append(i)
return filename
def getTIDFileName(path, suffix):
filename = []
f_list = os.listdir(path)
for i in f_list:
if suffix.find(os.path.splitext(i)[1]) != -1:
filename.append(i[1:3])
return filename
def imgread(path, is_rgb=True):
with open(path, 'rb') as f:
img = Image.open(f)
return img.convert('RGB') if is_rgb else img.convert('YCbCr')
def imgprocess(img_rgb, img_ycbcr, patch_size=[288, 384],is_crop=False):
w, h = img_rgb.size
w_ = np.random.randint(low=0, high=w - patch_size[1] + 1)
h_ = np.random.randint(low=0, high=h - patch_size[0] + 1)
img_rgb = img_rgb.crop((w_, h_, w_ + patch_size[1], h_ + patch_size[0]))
if is_crop:
img_ycbcr = img_ycbcr.crop((w_, h_, w_ + patch_size[1], h_ + patch_size[0]))
else:
img_ycbcr = img_ycbcr.resize([384, 288])
if random.random() < 0.5:
img_rgb = img_rgb.transpose(Image.FLIP_LEFT_RIGHT)
img_ycbcr = img_ycbcr.transpose(Image.FLIP_LEFT_RIGHT)
return img_rgb, img_ycbcr
if __name__ == '__main__':
pass