-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_loader.py
435 lines (313 loc) · 15.9 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import os
import pandas as pd
from PIL import Image
import torch
from torch.utils import data
import numpy as np
import cv2
import random
class BVQA_VideoDataset(data.Dataset):
"""Read data from the original dataset for feature extraction"""
def __init__(self, data_dir, data_dir_3D, data_dir_LLM, data_dir_LIQE, data_dir_FASTVQA, filename_path, transform, crop_size):
super(BVQA_VideoDataset, self).__init__()
dataInfo = pd.read_csv(filename_path)
video = dataInfo['filename'].tolist()
score = None
n = len(video)
video_names = []
for i in range(n):
video_names.append(video[i])
self.video_names = video_names
self.score = score
self.crop_size = crop_size
self.videos_dir = data_dir
self.data_dir_3D = data_dir_3D
self.data_dir_LLM = data_dir_LLM
self.data_dir_LIQE = data_dir_LIQE
self.data_dir_FASTVQA = data_dir_FASTVQA
self.transform = transform
self.length = len(self.video_names)
def __len__(self):
return self.length
def __getitem__(self, idx):
video_name = self.video_names[idx]
video_name_str = video_name[:-4]
path_name = os.path.join(self.videos_dir, video_name_str)
video_channel = 3
video_height_crop = self.crop_size
video_width_crop = self.crop_size
video_length_read = 8
transformed_video = torch.zeros([video_length_read, video_channel, video_height_crop, video_width_crop])
for i in range(video_length_read):
imge_name = os.path.join(path_name, '{:03d}'.format(i) + '.png')
read_frame = Image.open(imge_name)
read_frame = read_frame.convert('RGB')
read_frame = self.transform(read_frame)
transformed_video[i] = read_frame
# read 3D features
feature_folder_name = os.path.join(self.data_dir_3D, video_name_str)
transformed_feature = torch.zeros([video_length_read, 256])
for i in range(video_length_read):
feature_3D = np.load(os.path.join(feature_folder_name, 'feature_' + str(int(i)) + '_fast_feature.npy'))
feature_3D = torch.from_numpy(feature_3D)
feature_3D = feature_3D.squeeze()
transformed_feature[i] = feature_3D
# read LLM features
feature_folder_name = os.path.join(self.data_dir_LLM, video_name_str)
transformed_LLM_feature = torch.zeros([video_length_read, 4096])
for i in range(video_length_read):
feature_LLM = np.load(os.path.join(feature_folder_name, '{:03d}'.format(i) + '.npy'))
feature_LLM = torch.from_numpy(feature_LLM)
feature_LLM = feature_LLM.squeeze()
transformed_LLM_feature[i] = feature_LLM
# read LIQE features
feature_folder_name = os.path.join(self.data_dir_LIQE, video_name_str)
transformed_LIQE_feature = torch.zeros([video_length_read, 495])
for i in range(video_length_read):
feature_LIQE = np.load(os.path.join(feature_folder_name, 'feature_' + str(int(i)) + '_LIQE_feature.npy'))
feature_LIQE = torch.from_numpy(feature_LIQE)
feature_LIQE = feature_LIQE.squeeze()
transformed_LIQE_feature[i] = feature_LIQE
# read FAST-VQA features
feature_folder_name = os.path.join(self.data_dir_FASTVQA, video_name_str+'.npy')
feature_FASTVQA = np.load(feature_folder_name)
feature_FASTVQA = torch.from_numpy(feature_FASTVQA)
transformed_FASTVQA_feature = torch.zeros([video_length_read, 768])
for i in range(video_length_read):
transformed_FASTVQA_feature[i] = feature_FASTVQA
return transformed_video, transformed_feature, transformed_LLM_feature, transformed_LIQE_feature, transformed_FASTVQA_feature, video_name
class BVQA_VideoDataset_RQ_VQA(data.Dataset):
"""Read data from the original dataset for feature extraction"""
def __init__(self, data_dir, data_dir_3D, data_dir_LLM, data_dir_LIQE, data_dir_FASTVQA, filename_path, transform, database_name, crop_size, seed=0):
super(BVQA_VideoDataset_RQ_VQA, self).__init__()
column_names = ['filename','score']
dataInfo = pd.read_csv(filename_path, header = 0, sep=',', names=column_names, index_col=False, encoding="utf-8-sig")
video = dataInfo['filename'].tolist()
score = dataInfo['score'].tolist()
n = len(video)
video_names = []
for i in range(n):
video_names.append(video[i])
if database_name == 'NTIREVideo':
self.video_names = video_names
self.score = score
else:
dataInfo = pd.DataFrame(video_names)
dataInfo['score'] = score
dataInfo.columns = ['file_names', 'MOS']
random.seed(seed)
np.random.seed(seed)
length = 418
index_rd = np.random.permutation(length)
train_index_ref = index_rd[0:int(length * 0.8)]
# do not use the validation set
val_index_ref = index_rd[int(length * 0.8):]
train_index = []
for i_ref in train_index_ref:
for i_dis in range(7):
train_index.append(i_ref*7+i_dis)
val_index = []
for i_ref in val_index_ref:
for i_dis in range(7):
val_index.append(i_ref*7+i_dis)
print('train_index')
print(train_index)
print('val_index')
print(val_index)
if database_name == 'NTIREVideo_train':
self.video_names = dataInfo.iloc[train_index]['file_names'].tolist()
self.score = dataInfo.iloc[train_index]['MOS'].tolist()
elif database_name == 'NTIREVideo_val':
self.video_names = dataInfo.iloc[val_index]['file_names'].tolist()
self.score = dataInfo.iloc[val_index]['MOS'].tolist()
self.crop_size = crop_size
self.videos_dir = data_dir
self.data_dir_3D = data_dir_3D
self.data_dir_LLM = data_dir_LLM
self.data_dir_LIQE = data_dir_LIQE
self.data_dir_FASTVQA = data_dir_FASTVQA
self.transform = transform
self.length = len(self.video_names)
self.database_name = database_name
def __len__(self):
return self.length
def __getitem__(self, idx):
video_name = self.video_names[idx]
video_name_str = video_name[:-4]
video_score = torch.FloatTensor(np.array(float(self.score[idx])))
path_name = os.path.join(self.videos_dir, video_name_str)
video_channel = 3
video_height_crop = self.crop_size
video_width_crop = self.crop_size
video_length_read = 8
transformed_video = torch.zeros([video_length_read, video_channel, video_height_crop, video_width_crop])
for i in range(video_length_read):
imge_name = os.path.join(path_name, '{:03d}'.format(i) + '.png')
read_frame = Image.open(imge_name)
read_frame = read_frame.convert('RGB')
read_frame = self.transform(read_frame)
transformed_video[i] = read_frame
feature_folder_name = os.path.join(self.data_dir_3D, video_name_str)
transformed_feature = torch.zeros([video_length_read, 256])
for i in range(video_length_read):
feature_3D = np.load(os.path.join(feature_folder_name, 'feature_' + str(int(i)) + '_fast_feature.npy'))
feature_3D = torch.from_numpy(feature_3D)
feature_3D = feature_3D.squeeze()
transformed_feature[i] = feature_3D
feature_folder_name = os.path.join(self.data_dir_LLM, video_name_str)
transformed_LLM_feature = torch.zeros([video_length_read, 4096])
for i in range(video_length_read):
feature_LLM = np.load(os.path.join(feature_folder_name, '{:03d}'.format(i) + '.npy'))
feature_LLM = torch.from_numpy(feature_LLM)
feature_LLM = feature_LLM.squeeze()
transformed_LLM_feature[i] = feature_LLM
feature_folder_name = os.path.join(self.data_dir_LIQE, video_name_str)
transformed_LIQE_feature = torch.zeros([video_length_read, 495])
for i in range(video_length_read):
feature_LIQE = np.load(os.path.join(feature_folder_name, 'feature_' + str(int(i)) + '_LIQE_feature.npy'))
feature_LIQE = torch.from_numpy(feature_LIQE)
feature_LIQE = feature_LIQE.squeeze()
transformed_LIQE_feature[i] = feature_LIQE
feature_folder_name = os.path.join(self.data_dir_FASTVQA, video_name_str+'.npy')
feature_FASTVQA = np.load(feature_folder_name)
feature_FASTVQA = torch.from_numpy(feature_FASTVQA)
transformed_FASTVQA_feature = torch.zeros([video_length_read, 768])
for i in range(video_length_read):
transformed_FASTVQA_feature[i] = feature_FASTVQA
return transformed_video, transformed_feature, transformed_LLM_feature, transformed_LIQE_feature, transformed_FASTVQA_feature, video_score, video_name
class BVQA_VideoDataset_RQ_VQA_base_model(data.Dataset):
"""Read data from the original dataset for feature extraction"""
def __init__(self, data_dir, data_dir_3D, filename_path, transform, database_name, crop_size, seed=0):
super(BVQA_VideoDataset_RQ_VQA_base_model, self).__init__()
column_names = ['filename','score']
dataInfo = pd.read_csv(filename_path, header = 0, sep=',', names=column_names, index_col=False, encoding="utf-8-sig")
video = dataInfo['filename'].tolist()
score = dataInfo['score'].tolist()
n = len(video)
video_names = []
for i in range(n):
video_names.append(video[i])
if database_name == 'NTIREVideo':
self.video_names = video_names
self.score = score
else:
dataInfo = pd.DataFrame(video_names)
dataInfo['score'] = score
dataInfo.columns = ['file_names', 'MOS']
random.seed(seed)
np.random.seed(seed)
length = 418
index_rd = np.random.permutation(length)
train_index_ref = index_rd[0:int(length * 0.8)]
# do not use the validation set
val_index_ref = index_rd[int(length * 0.8):]
train_index = []
for i_ref in train_index_ref:
for i_dis in range(7):
train_index.append(i_ref*7+i_dis)
val_index = []
for i_ref in val_index_ref:
for i_dis in range(7):
val_index.append(i_ref*7+i_dis)
print('train_index')
print(train_index)
print('val_index')
print(val_index)
if database_name == 'NTIREVideo_train':
self.video_names = dataInfo.iloc[train_index]['file_names'].tolist()
self.score = dataInfo.iloc[train_index]['MOS'].tolist()
elif database_name == 'NTIREVideo_val':
self.video_names = dataInfo.iloc[val_index]['file_names'].tolist()
self.score = dataInfo.iloc[val_index]['MOS'].tolist()
self.crop_size = crop_size
self.videos_dir = data_dir
self.data_dir_3D = data_dir_3D
self.transform = transform
self.length = len(self.video_names)
self.database_name = database_name
def __len__(self):
return self.length
def __getitem__(self, idx):
video_name = self.video_names[idx]
video_name_str = video_name[:-4]
video_score = torch.FloatTensor(np.array(float(self.score[idx])))
path_name = os.path.join(self.videos_dir, video_name_str)
video_channel = 3
video_height_crop = self.crop_size
video_width_crop = self.crop_size
video_length_read = 8
transformed_video = torch.zeros([video_length_read, video_channel, video_height_crop, video_width_crop])
for i in range(video_length_read):
imge_name = os.path.join(path_name, '{:03d}'.format(i) + '.png')
read_frame = Image.open(imge_name)
read_frame = read_frame.convert('RGB')
read_frame = self.transform(read_frame)
transformed_video[i] = read_frame
feature_folder_name = os.path.join(self.data_dir_3D, video_name_str)
transformed_feature = torch.zeros([video_length_read, 256])
for i in range(video_length_read):
feature_3D = np.load(os.path.join(feature_folder_name, 'feature_' + str(int(i)) + '_fast_feature.npy'))
feature_3D = torch.from_numpy(feature_3D)
feature_3D = feature_3D.squeeze()
transformed_feature[i] = feature_3D
return transformed_video, transformed_feature, video_score, video_name
class BVQA_VideoDataset_SlowFast(data.Dataset):
"""Read data from the original dataset for feature extraction"""
def __init__(self, data_dir, filename_path, transform, resize):
super(BVQA_VideoDataset_SlowFast, self).__init__()
dataInfo = pd.read_csv(filename_path)
video = dataInfo['filename'].tolist()
video_names = []
for video_i in video:
video_names.append(video_i)
self.video_names = video_names
self.transform = transform
self.videos_dir = data_dir
self.resize = resize
self.length = len(self.video_names)
def __len__(self):
return self.length
def __getitem__(self, idx):
video_name = self.video_names[idx]
video_name_str = video_name[:-4]
filename=os.path.join(self.videos_dir, 'test', video_name) # for test
print(filename)
video_capture = cv2.VideoCapture()
video_capture.open(filename)
cap=cv2.VideoCapture(filename)
video_channel = 3
video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
video_frame_rate = int(round(cap.get(cv2.CAP_PROP_FPS)))
if video_frame_rate == 0:
video_clip = 10
else:
video_clip = int(video_length/video_frame_rate)
video_clip_min = 8
video_length_clip = 32
transformed_frame_all = torch.zeros([video_length, video_channel, self.resize, self.resize])
transformed_video_all = []
video_read_index = 0
for i in range(video_length):
has_frames, frame = video_capture.read()
if has_frames:
read_frame = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
read_frame = self.transform(read_frame)
transformed_frame_all[video_read_index] = read_frame
video_read_index += 1
if video_read_index < video_length:
for i in range(video_read_index, video_length):
transformed_frame_all[i] = transformed_frame_all[video_read_index - 1]
video_capture.release()
for i in range(video_clip):
transformed_video = torch.zeros([video_length_clip, video_channel, self.resize, self.resize])
if (i*video_frame_rate + video_length_clip) <= video_length:
transformed_video = transformed_frame_all[i*video_frame_rate : (i*video_frame_rate + video_length_clip)]
else:
transformed_video[:(video_length - i*video_frame_rate)] = transformed_frame_all[i*video_frame_rate :]
for j in range((video_length - i*video_frame_rate), video_length_clip):
transformed_video[j] = transformed_video[video_length - i*video_frame_rate - 1]
transformed_video_all.append(transformed_video)
if video_clip < video_clip_min:
for i in range(video_clip, video_clip_min):
transformed_video_all.append(transformed_video_all[video_clip - 1])
return transformed_video_all, video_name_str