-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset.py
299 lines (245 loc) · 15.6 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
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
import math
import random
import os
import collections
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import json
from torch.utils.data import Dataset
from PIL import Image
from math import pi, sqrt
from utils import WarpKeyframe
class Dataset_DNeRV_UVG(Dataset):
def __init__(self, args, transform_rgb, transform_keyframe=None):
self.gt_base_dir = './data/UVG/gt'
self.keyframe_base_dir = './data/UVG/keyframe/q{}'.format(args.keyframe_quality)
self.transform_rgb = transform_rgb
self.transform_keyframe = transform_keyframe
vid_length_dict = collections.OrderedDict()
with open('./data/UVG/annotation/video_length.json', 'r') as fp:
vid_length_dict = json.load(fp)
clip_size = args.clip_size
vid_dict = collections.OrderedDict()
self.frame_count_total = 0
self.frame_path_list = []
for vid_name, vid_length in vid_length_dict.items():
# we divide videos into consecutive video_clips
num_clip = round(math.ceil(vid_length / clip_size))
# rounded up the vid_length, in case the vid_length is not divided by the clip_size
vid_length_round = num_clip * clip_size
for clip_index in range(num_clip):
# the first frame is the start_keyframe, the first frame for the next consecutive clip is the end_keyframe
start_keyframe_index = clip_index * clip_size + 1
end_keyframe_index = min(vid_length, (clip_index + 1) * clip_size + 1)
vid_clip_name = "{}-{}".format(vid_name, clip_index)
vid_dict[vid_clip_name] = {}
vid_dict[vid_clip_name]['vid_name'] = vid_name
vid_dict[vid_clip_name]['keyframe_path'] = ['frame{:06d}.png'.format(start_keyframe_index), 'frame{:06d}.png'.format(end_keyframe_index)]
frame_index_list = list(range(clip_index * clip_size + 1, (clip_index + 1) * clip_size + 1))
# mask out the frame_index which are longer than the actual vid_length
vid_dict[vid_clip_name]['frame_mask'] = [(frame_index <= vid_length) for frame_index in frame_index_list]
frame_index_list = [min(frame_index, vid_length) for frame_index in frame_index_list]
vid_dict[vid_clip_name]['backward_distance'] = [(frame_index - start_keyframe_index) / max(1, end_keyframe_index - start_keyframe_index) for frame_index in frame_index_list]
vid_dict[vid_clip_name]['frame_path'] = ['frame{:06d}.png'.format(frame_index) for frame_index in frame_index_list]
# normalize input_index by the original vid_length to [0, 1]
vid_dict[vid_clip_name]['input_index'] = [(frame_index - 1) / (vid_length - 1) for frame_index in frame_index_list]
self.frame_path_list.append([vid_clip_name, vid_name, vid_dict[vid_clip_name]['frame_path']])
self.frame_count_total += clip_size
self.vid_dict = vid_dict
self.vid_list = sorted(list(vid_dict.keys()))
self.frame_path_list = sorted(self.frame_path_list)
def __len__(self):
return len(self.vid_list)
def __getitem__(self, idx):
vid_clip_name = self.vid_list[idx]
vid_name = self.vid_dict[vid_clip_name]['vid_name']
frame_list = []
for k in range(len(self.vid_dict[vid_clip_name]['frame_path'])):
frame_path = self.vid_dict[vid_clip_name]['frame_path'][k]
frame = Image.open(os.path.join(self.gt_base_dir, vid_name, frame_path)).convert("RGB")
frame_list.append(self.transform_rgb(frame))
video = torch.stack(frame_list, dim=1)
input_index = torch.tensor(self.vid_dict[vid_clip_name]['input_index'])
start_keyframe = self.transform_keyframe(Image.open(os.path.join(self.keyframe_base_dir, vid_name, self.vid_dict[vid_clip_name]['keyframe_path'][0])).convert("RGB"))
end_keyframe = self.transform_keyframe(Image.open(os.path.join(self.keyframe_base_dir, vid_name, self.vid_dict[vid_clip_name]['keyframe_path'][1])).convert("RGB"))
keyframe = torch.stack([start_keyframe, end_keyframe], dim=1)
backward_distance = torch.tensor(self.vid_dict[vid_clip_name]['backward_distance'])
frame_mask = torch.tensor(self.vid_dict[vid_clip_name]['frame_mask'])
return video, input_index, keyframe, backward_distance, frame_mask
class Dataset_NeRV_UVG(Dataset):
def __init__(self, args, transform_rgb, transform_keyframe=None):
self.gt_base_dir = './data/UVG/gt'
self.transform_rgb = transform_rgb
vid_length_dict = collections.OrderedDict()
with open('./data/UVG/annotation/video_length.json', 'r') as fp:
vid_length_dict = json.load(fp)
clip_size = args.clip_size
vid_dict = collections.OrderedDict()
self.frame_count_total = 0
self.frame_path_list = []
for vid_name, vid_length in vid_length_dict.items():
# we divide videos into consecutive video_clips
num_clip = round(math.ceil(vid_length / clip_size))
# rounded up the vid_length, in case the vid_length is not divided by the clip_size
vid_length_round = num_clip * clip_size
for clip_index in range(num_clip):
# the first frame is the start_keyframe, the first frame for the next consecutive clip is the end_keyframe
start_keyframe_index = clip_index * clip_size + 1
end_keyframe_index = min(vid_length, (clip_index + 1) * clip_size + 1)
vid_clip_name = "{}-{}".format(vid_name, clip_index)
vid_dict[vid_clip_name] = {}
vid_dict[vid_clip_name]['vid_name'] = vid_name
vid_dict[vid_clip_name]['keyframe_path'] = ['frame{:06d}.png'.format(start_keyframe_index), 'frame{:06d}.png'.format(end_keyframe_index)]
frame_index_list = list(range(clip_index * clip_size + 1, (clip_index + 1) * clip_size + 1))
# mask out the frame_index which are longer than the actual vid_length
vid_dict[vid_clip_name]['frame_mask'] = [(frame_index <= vid_length) for frame_index in frame_index_list]
frame_index_list = [min(frame_index, vid_length) for frame_index in frame_index_list]
vid_dict[vid_clip_name]['backward_distance'] = [(frame_index - start_keyframe_index) / max(1, end_keyframe_index - start_keyframe_index) for frame_index in frame_index_list]
vid_dict[vid_clip_name]['frame_path'] = ['frame{:06d}.png'.format(frame_index) for frame_index in frame_index_list]
vid_dict[vid_clip_name]['input_index'] = [self.frame_count_total + i for i in range(clip_size)]
self.frame_path_list.append([vid_clip_name, vid_name, vid_dict[vid_clip_name]['frame_path']])
self.frame_count_total += clip_size
self.vid_dict = vid_dict
self.vid_list = sorted(list(vid_dict.keys()))
self.frame_path_list = sorted(self.frame_path_list)
def __len__(self):
return len(self.vid_list)
def __getitem__(self, idx):
vid_clip_name = self.vid_list[idx]
vid_name = self.vid_dict[vid_clip_name]['vid_name']
frame_list = []
for k in range(len(self.vid_dict[vid_clip_name]['frame_path'])):
frame_path = self.vid_dict[vid_clip_name]['frame_path'][k]
frame = Image.open(os.path.join(self.gt_base_dir, vid_name, frame_path)).convert("RGB")
frame_list.append(self.transform_rgb(frame))
video = torch.stack(frame_list, dim=1)
input_index = torch.tensor(self.vid_dict[vid_clip_name]['input_index']) / (self.frame_count_total - 1)
keyframe = torch.zeros(1)
backward_distance = torch.zeros(1)
frame_mask = torch.tensor(self.vid_dict[vid_clip_name]['frame_mask'])
return video, input_index, keyframe, backward_distance, frame_mask
class Dataset_DNeRV_UCF101(Dataset):
def __init__(self, args, transform_rgb, transform_keyframe=None):
self.gt_base_dir = './data/UCF101/gt'
self.keyframe_base_dir = './data/UCF101/keyframe/q{}'.format(args.keyframe_quality)
self.transform_rgb = transform_rgb
self.transform_keyframe = transform_keyframe
vid_length_dict = collections.OrderedDict()
with open('./data/UCF101/annotation/video_length_train.json', 'r') as fp:
vid_length_dict = json.load(fp)
clip_size = args.clip_size
vid_dict = collections.OrderedDict()
self.frame_count_total = 0
self.frame_path_list = []
for vid_name, vid_length in vid_length_dict.items():
action_name = vid_name.split('_')[1]
num_clip = round(math.ceil(vid_length / clip_size))
vid_length_round = num_clip * clip_size
for clip_index in range(num_clip):
start_keyframe_index = clip_index * clip_size + 1
end_keyframe_index = min(vid_length, (clip_index + 1) * clip_size + 1)
vid_clip_name = "{}-{}".format(vid_name, clip_index)
vid_dict[vid_clip_name] = {}
vid_dict[vid_clip_name]['vid_name'] = vid_name
vid_dict[vid_clip_name]['action_name'] = action_name
vid_dict[vid_clip_name]['keyframe_path'] = ['frame{:06d}.png'.format(start_keyframe_index), 'frame{:06d}.png'.format(end_keyframe_index)]
frame_index_list = list(range(clip_index * clip_size + 1, (clip_index + 1) * clip_size + 1))
vid_dict[vid_clip_name]['frame_mask'] = [(frame_index <= vid_length) for frame_index in frame_index_list]
frame_index_list = [min(frame_index, vid_length) for frame_index in frame_index_list]
vid_dict[vid_clip_name]['backward_distance'] = [(frame_index - start_keyframe_index) / max(1, end_keyframe_index - start_keyframe_index) for frame_index in frame_index_list]
vid_dict[vid_clip_name]['frame_path'] = ['frame{:06d}.png'.format(frame_index) for frame_index in frame_index_list]
vid_dict[vid_clip_name]['input_index'] = [(frame_index - 1) / (vid_length - 1) for frame_index in frame_index_list]
self.frame_path_list.append([vid_clip_name, action_name, vid_name, vid_dict[vid_clip_name]['frame_path']])
self.frame_count_total += clip_size
self.vid_dict = vid_dict
self.vid_list = sorted(list(vid_dict.keys()))
self.frame_path_list = sorted(self.frame_path_list)
def __len__(self):
return len(self.vid_list)
def __getitem__(self, idx):
vid_clip_name = self.vid_list[idx]
vid_name = self.vid_dict[vid_clip_name]['vid_name']
action_name = self.vid_dict[vid_clip_name]['action_name']
frame_list = []
for k in range(len(self.vid_dict[vid_clip_name]['frame_path'])):
frame_path = self.vid_dict[vid_clip_name]['frame_path'][k]
frame = Image.open(os.path.join(self.gt_base_dir, action_name, vid_name, frame_path)).convert("RGB")
frame_list.append(self.transform_rgb(frame))
video = torch.stack(frame_list, dim=1)
input_index = torch.tensor(self.vid_dict[vid_clip_name]['input_index'])
start_keyframe = self.transform_keyframe(Image.open(os.path.join(self.keyframe_base_dir, action_name, vid_name, self.vid_dict[vid_clip_name]['keyframe_path'][0])).convert("RGB"))
end_keyframe = self.transform_keyframe(Image.open(os.path.join(self.keyframe_base_dir, action_name, vid_name, self.vid_dict[vid_clip_name]['keyframe_path'][1])).convert("RGB"))
keyframe = torch.stack([start_keyframe, end_keyframe], dim=1)
backward_distance = torch.tensor(self.vid_dict[vid_clip_name]['backward_distance'])
frame_mask = torch.tensor(self.vid_dict[vid_clip_name]['frame_mask'])
return video, input_index, keyframe, backward_distance, frame_mask
class Dataset_NeRV_UCF101(Dataset):
def __init__(self, args, transform_rgb, transform_keyframe=None):
self.gt_base_dir = './data/UCF101/gt'
self.transform_rgb = transform_rgb
vid_length_dict = collections.OrderedDict()
with open('./data/UCF101/annotation/video_length_train.json', 'r') as fp:
vid_length_dict = json.load(fp)
clip_size = args.clip_size
vid_dict = collections.OrderedDict()
self.frame_count_total = 0
self.frame_path_list = []
for vid_name, vid_length in vid_length_dict.items():
action_name = vid_name.split('_')[1]
num_clip = round(math.ceil(vid_length / clip_size))
vid_length_round = num_clip * clip_size
for clip_index in range(num_clip):
start_keyframe_index = clip_index * clip_size + 1
end_keyframe_index = min(vid_length, (clip_index + 1) * clip_size + 1)
vid_clip_name = "{}-{}".format(vid_name, clip_index)
vid_dict[vid_clip_name] = {}
vid_dict[vid_clip_name]['vid_name'] = vid_name
vid_dict[vid_clip_name]['action_name'] = action_name
frame_index_list = list(range(clip_index * clip_size + 1, (clip_index + 1) * clip_size + 1))
vid_dict[vid_clip_name]['frame_mask'] = [(frame_index <= vid_length) for frame_index in frame_index_list]
frame_index_list = [min(frame_index, vid_length) for frame_index in frame_index_list]
vid_dict[vid_clip_name]['frame_path'] = ['frame{:06d}.png'.format(frame_index) for frame_index in frame_index_list]
vid_dict[vid_clip_name]['input_index'] = [self.frame_count_total + i for i in range(clip_size)]
self.frame_path_list.append([vid_clip_name, action_name, vid_name, vid_dict[vid_clip_name]['frame_path']])
self.frame_count_total += clip_size
self.vid_dict = vid_dict
self.vid_list = sorted(list(vid_dict.keys()))
self.frame_path_list = sorted(self.frame_path_list)
def __len__(self):
return len(self.vid_list)
def __getitem__(self, idx):
vid_clip_name = self.vid_list[idx]
vid_name = self.vid_dict[vid_clip_name]['vid_name']
action_name = self.vid_dict[vid_clip_name]['action_name']
frame_list = []
for k in range(len(self.vid_dict[vid_clip_name]['frame_path'])):
frame_path = self.vid_dict[vid_clip_name]['frame_path'][k]
frame = Image.open(os.path.join(self.gt_base_dir, action_name, vid_name, frame_path)).convert("RGB")
frame_list.append(self.transform_rgb(frame))
video = torch.stack(frame_list, dim=1)
input_index = torch.tensor(self.vid_dict[vid_clip_name]['input_index']) / (self.frame_count_total - 1)
keyframe = torch.zeros(1)
backward_distance = torch.zeros(1)
frame_mask = torch.tensor(self.vid_dict[vid_clip_name]['frame_mask'])
return video, input_index, keyframe, backward_distance, frame_mask
def my_collate_fn(batch):
batched_output_list = []
for i in range(len(batch[0])):
if torch.is_tensor(batch[0][i]):
batched_output = torch.stack([single_batch[i] for single_batch in batch], dim=0)
elif type(batch[0][i]) is dict:
batched_output = {}
for k, v in batch[0][i].items():
batched_output[k] = torch.stack([single_batch[i][k] for single_batch in batch], dim=0)
batched_output_list.append(batched_output)
return batched_output_list
def worker_init_fn(worker_id):
"""
Re-seed each worker process to preserve reproducibility
"""
worker_seed = torch.initial_seed() % 2**32
np.random.seed(worker_seed)
random.seed(worker_seed)
return