-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_dataloader.py
377 lines (276 loc) · 14.7 KB
/
my_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
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
import os
import pandas as pd
from PIL import Image
import torch
from torch.utils import data
import numpy as np
import scipy.io as scio
import cv2
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
class VideoDataset_images(data.Dataset):
"""Read data from the original dataset for feature extraction"""
def __init__(self, data_dir, filename_path, transform, database_name):
super(VideoDataset_images, self).__init__()
if database_name == 'vqa_train':
column_names = ['name','mos']
dataInfo = pd.read_csv(filename_path, header=0, sep=',', names=column_names, index_col=False,
encoding="utf-8-sig")
self.video_names = dataInfo['name'].tolist()
self.score = dataInfo['mos'].tolist()
elif database_name == 'vqa_test':
column_names = ['name','mos']
dataInfo = pd.read_csv(filename_path, header=0, sep=',', names=column_names, index_col=False,
encoding="utf-8-sig")
self.video_names = dataInfo['name'].tolist()
self.score = dataInfo['mos'].tolist()
self.videos_dir = data_dir
self.transform = transform
self.length = len(self.video_names)
self.database_name = database_name
def __len__(self):
return self.length
def __getitem__(self, idx):
if self.database_name == 'vqa_train' or self.database_name == 'vqa_test':
video_name = self.video_names[idx]
video_name_str = video_name.split('.')[0]
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 = 720
video_width_crop = 1280
video_length_read = 8
key_frames = 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)
key_frames[i] = read_frame
return key_frames, video_score, video_name
class VideoDataset_temporal_feature(data.Dataset):
def __init__(self, temporal_feature, filename_path, database_name, feature_type):
super(VideoDataset_temporal_feature, self).__init__()
if database_name == 'vqa_train':
column_names = ['name','mos']
dataInfo = pd.read_csv(filename_path, header=0, sep=',', names=column_names, index_col=False,
encoding="utf-8-sig")
self.video_names = dataInfo['name'].tolist()
self.score = dataInfo['mos'].tolist()
elif database_name == 'vqa_test':
column_names = ['name','mos']
dataInfo = pd.read_csv(filename_path, header=0, sep=',', names=column_names, index_col=False,
encoding="utf-8-sig")
self.video_names = dataInfo['name'].tolist()
self.score = dataInfo['mos'].tolist()
self.temporal_feature = temporal_feature
self.length = len(self.video_names)
self.feature_type = feature_type
self.database_name = database_name
def __len__(self):
return self.length
def __getitem__(self, idx):
if self.database_name == 'vqa_train' or self.database_name == 'vqa_test':
video_name = self.video_names[idx]
video_name_str = video_name.split('.')[0]
video_score = torch.FloatTensor(np.array(float(self.score[idx])))
video_length_read = 8
# read temporal features
if self.feature_type == 'SlowFast':
feature_folder_name = os.path.join(self.temporal_feature, video_name_str)
temporal_feature = torch.zeros([video_length_read, 2048 + 256])
for i in range(video_length_read):
i_index = i
feature_3D_slow = np.load(
os.path.join(feature_folder_name, 'feature_' + str(i_index) + '_slow_feature.npy'))
feature_3D_slow = torch.from_numpy(feature_3D_slow)
feature_3D_slow = feature_3D_slow.squeeze()
feature_3D_fast = np.load(
os.path.join(feature_folder_name, 'feature_' + str(i_index) + '_fast_feature.npy'))
feature_3D_fast = torch.from_numpy(feature_3D_fast)
feature_3D_fast = feature_3D_fast.squeeze()
feature_3D = torch.cat([feature_3D_slow, feature_3D_fast])
temporal_feature[i] = feature_3D
return temporal_feature, video_score, video_name
class VideoDataset_extract_temporal_feature(data.Dataset):
"""Read data from the original dataset for feature extraction"""
def __init__(self, data_dir, filename_path, transform, resize):
super(VideoDataset_extract_temporal_feature, self).__init__()
column_names = ['name','mos']
dataInfo = pd.read_csv(filename_path, header=0, sep=',', names=column_names, index_col=False,
encoding="utf-8-sig")
self.video_names = dataInfo['name']
self.score = dataInfo['mos']
self.videos_dir = data_dir
self.transform = transform
self.resize = resize
self.length = len(self.video_names)
def __len__(self):
return self.length
def __getitem__(self, idx):
video_name = self.video_names.iloc[idx]
video_score = torch.FloatTensor(np.array(float(self.score.iloc[idx]))) / 20
filename = os.path.join(self.videos_dir, video_name)
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_score, video_name
class VideoDataset_images_with_temporal_features(data.Dataset):
"""Read data from the original dataset for feature extraction"""
def __init__(self, data_dir, data_dir_3D, filename_path, transform, database_name, feature_type):
super(VideoDataset_images_with_temporal_features, self).__init__()
if database_name == 'vqa_train':
column_names = ['name','mos']
dataInfo = pd.read_csv(filename_path, header=0, sep=',', names=column_names, index_col=False,
encoding="utf-8-sig")
self.video_names = dataInfo['name'].tolist()
self.score = dataInfo['mos'].tolist()
elif database_name == 'vqa_test':
column_names = ['name','mos']
dataInfo = pd.read_csv(filename_path, header=0, sep=',', names=column_names, index_col=False,
encoding="utf-8-sig")
self.video_names = dataInfo['name'].tolist()
self.score = dataInfo['mos'].tolist()
self.videos_dir = data_dir
self.data_dir_3D = data_dir_3D
self.transform = transform
self.length = len(self.video_names)
self.feature_type = feature_type
self.database_name = database_name
def __len__(self):
return self.length
def __getitem__(self, idx):
if self.database_name == 'vqa_train' or self.database_name == 'vqa_test':
video_name = self.video_names[idx]
video_name_str = video_name.split('.')[0]
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 = 720
video_width_crop = 1280
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 temporal features
if self.feature_type == 'SlowFast':
feature_folder_name = os.path.join(self.data_dir_3D, video_name_str)
transformed_feature = torch.zeros([video_length_read, 2048 + 256])
for i in range(video_length_read):
i_index = i
feature_3D_slow = np.load(
os.path.join(feature_folder_name, 'feature_' + str(i_index) + '_slow_feature.npy'))
feature_3D_slow = torch.from_numpy(feature_3D_slow)
feature_3D_slow = feature_3D_slow.squeeze()
feature_3D_fast = np.load(
os.path.join(feature_folder_name, 'feature_' + str(i_index) + '_fast_feature.npy'))
feature_3D_fast = torch.from_numpy(feature_3D_fast)
feature_3D_fast = feature_3D_fast.squeeze()
feature_3D = torch.cat([feature_3D_slow, feature_3D_fast])
transformed_feature[i] = feature_3D
return transformed_video, transformed_feature, video_score, video_name
class VideoDataset_spatio_temporal_brightness(data.Dataset):
"""Read data from the original dataset for feature extraction"""
def __init__(self, data_dir, data_dir_3D, filename_path, transform, database_name, feature_type):
super(VideoDataset_spatio_temporal_brightness, self).__init__()
if database_name == 'vqa_train':
column_names = ['name','mos']
dataInfo = pd.read_csv(filename_path, header=0, sep=',', names=column_names, index_col=False,
encoding="utf-8-sig")
self.video_names = dataInfo['name'].tolist()
self.score = dataInfo['mos'].tolist()
elif database_name == 'vqa_test':
column_names = ['name','mos']
dataInfo = pd.read_csv(filename_path, header=0, sep=',', names=column_names, index_col=False,
encoding="utf-8-sig")
self.video_names = dataInfo['name'].tolist()
self.score = dataInfo['mos'].tolist()
self.videos_dir = data_dir
self.data_dir_3D = data_dir_3D
self.transform = transform
self.length = len(self.video_names)
self.feature_type = feature_type
self.database_name = database_name
def __len__(self):
return self.length
def __getitem__(self, idx):
if self.database_name == 'vqa_train' or self.database_name == 'vqa_test':
video_name = self.video_names[idx]
video_name_str = video_name.split('.')[0]
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 = 672
video_width_crop = 1120
video_length_read = 8
BNS_feature = np.load(
os.path.join('BN_Features', video_name_str+ '_BNF.npy')
)
BNS_feature = torch.from_numpy(BNS_feature)
transformed_video = torch.zeros([video_length_read, video_channel, video_height_crop, video_width_crop])
for i in range(8):
imge_name = os.path.join(path_name, f'00{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 temporal features
BCT_feature = np.load(
os.path.join('BC_Features', video_name_str+ '_BC.npy')
)
BCT_feature = torch.from_numpy(BCT_feature)
if self.feature_type == 'SlowFast':
feature_folder_name = os.path.join(self.data_dir_3D, video_name_str)
transformed_feature = torch.zeros([video_length_read, 2048 + 256 + 300])
for i in range(video_length_read):
i_index = i
feature_3D_slow = np.load(
os.path.join(feature_folder_name, 'feature_' + str(i_index) + '_slow_feature.npy'))
feature_3D_slow = torch.from_numpy(feature_3D_slow)
feature_3D_slow = feature_3D_slow.squeeze()
feature_3D_fast = np.load(
os.path.join(feature_folder_name, 'feature_' + str(i_index) + '_fast_feature.npy'))
feature_3D_fast = torch.from_numpy(feature_3D_fast)
feature_3D_fast = feature_3D_fast.squeeze()
feature_3D = torch.cat([feature_3D_slow, feature_3D_fast, BCT_feature[i]])
transformed_feature[i] = feature_3D
return transformed_video, transformed_feature, BNS_feature, video_score, video_name