-
Notifications
You must be signed in to change notification settings - Fork 9
/
dataloader_charades.py
330 lines (269 loc) · 15.3 KB
/
dataloader_charades.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
" Dataloader of charades-STA dataset for RL algorithm"
import torch
import torch.utils.data
import os
import pickle
import numpy as np
import math
from utils import *
import random
import glob
class Charades_Train_dataset(torch.utils.data.Dataset):
def __init__(self):
self.unit_size = 16
self.feats_dimen = 4096
self.context_num = 1
self.context_size = 128
self.visual_feature_dim = 4096
self.sent_vec_dim = 4800
self.sliding_clip_path = "./Dataset/Charades/Charades_localfeature_npy/"#"./Dataset/Charades/all_fc6_unit16_overlap0.5/"
self.clip_sentence_pairs_iou_all = pickle.load(open("./Dataset/Charades/ref_info/charades_rl_train_feature_glove_12408.pkl"))
self.num_samples_iou = len(self.clip_sentence_pairs_iou_all)
print(self.num_samples_iou, "iou clip-sentence pairs are readed") # 49442
self.movie_name_id = []
self.global_feature_all = []
self.original_feats_all = []
self.initial_feature_all = []
self.ten_unit_all = []
self.initial_offset_start_all = []
self.initial_offset_end_all = []
self.initial_offset_start_norm_all = []
self.initial_offset_end_norm_all = []
self.num_units_all = []
movie_name_list = []
for i in range(self.num_samples_iou):
if (i+1) % 1000 == 0:
print("%d/%d read" %((i+1), self.num_samples_iou))
movie_name = self.clip_sentence_pairs_iou_all[i]['video']
end = self.clip_sentence_pairs_iou_all[i]['frames_num'] +1
if movie_name not in movie_name_list:
global_feature, original_feats, initial_feature, ten_unit, initial_offset_start, initial_offset_end, initial_offset_start_norm, \
initial_offset_end_norm, num_units = self.read_video_level_feats(movie_name, end)
self.global_feature_all.append(global_feature)
self.original_feats_all.append(original_feats)
self.initial_feature_all.append(initial_feature)
self.ten_unit_all.append(ten_unit)
self.initial_offset_start_all.append(initial_offset_start)
self.initial_offset_end_all.append(initial_offset_end)
self.initial_offset_start_norm_all.append(initial_offset_start_norm)
self.initial_offset_end_norm_all.append(initial_offset_end_norm)
self.num_units_all.append(num_units)
movie_name_list.append(movie_name)
self.movie_name_id.append(len(movie_name_list)-1)
self.movie_length_dict = {}
with open("./Dataset/Charades/ref_info/charades_movie_length_info.txt") as f:
for l in f:
self.movie_length_dict[l.rstrip().split(" ")[0]] = float(l.rstrip().split(" ")[1])
def read_video_level_feats(self, movie_name, end):
# read unit level feats by just passing the start and end number
unit_size = 16
feats_dimen = 4096
start = 1
num_units = (end - start) / unit_size
# print(start, end, num_units)
curr_start = 1
ten_unit = num_units / 10
four_unit = num_units / 4
oneinfour_unit = four_unit
threeinfour_unit = num_units - four_unit
start_end_list = []
while (curr_start + unit_size <= end):
start_end_list.append((curr_start, curr_start + unit_size))
curr_start += unit_size
original_feats = np.zeros([300, feats_dimen*2], dtype=np.float32)
original_feats_1 = np.zeros([num_units, feats_dimen*2], dtype=np.float32)
for k, (curr_s, curr_e) in enumerate(start_end_list):
one_feat = np.load(self.sliding_clip_path + movie_name + "_" + str(curr_s) + ".0_" + str(curr_e) + ".0.npy")
original_feats[k] = one_feat[feats_dimen:]
original_feats_1[k] = one_feat[feats_dimen:]
# print(np.shape(original_feats))
feature_length = len(original_feats_1)
idx = choose_ten_frame(feature_length)
global_feature_1 = original_feats_1[idx[0]]
global_feature_2 = original_feats_1[idx[1]]
global_feature_3 = original_feats_1[idx[2]]
global_feature_4 = original_feats_1[idx[3]]
global_feature_5 = original_feats_1[idx[4]]
global_feature_6 = original_feats_1[idx[5]]
global_feature_7 = original_feats_1[idx[6]]
global_feature_8 = original_feats_1[idx[7]]
global_feature_9 = original_feats_1[idx[8]]
global_feature_10 = original_feats_1[idx[9]]
global_feature_concate = np.concatenate([global_feature_1, global_feature_2, global_feature_3, global_feature_4, global_feature_5, \
global_feature_6, global_feature_7, global_feature_8, global_feature_9, global_feature_10], axis=0)
global_feature = global_feature_concate
initial_feature = original_feats[(oneinfour_unit-1):(threeinfour_unit)]
feature_length = len(initial_feature)
idx = choose_ten_frame(feature_length)
initial_feature_1 = initial_feature[idx[0]]
initial_feature_2 = initial_feature[idx[1]]
initial_feature_3 = initial_feature[idx[2]]
initial_feature_4 = initial_feature[idx[3]]
initial_feature_5 = initial_feature[idx[4]]
initial_feature_6 = initial_feature[idx[5]]
initial_feature_7 = initial_feature[idx[6]]
initial_feature_8 = initial_feature[idx[7]]
initial_feature_9 = initial_feature[idx[8]]
initial_feature_10 = initial_feature[idx[9]]
initial_feature_concate = np.concatenate([initial_feature_1, initial_feature_2, initial_feature_3, initial_feature_4, initial_feature_5, \
initial_feature_6, initial_feature_7, initial_feature_8, initial_feature_9, initial_feature_10], axis=0)
initial_feature = initial_feature_concate
initial_offset_start = oneinfour_unit-1
initial_offset_end = threeinfour_unit - 1
initial_offset_start_norm = initial_offset_start / float(num_units-1)
initial_offset_end_norm = initial_offset_end / float(num_units-1)
return global_feature, original_feats, initial_feature, ten_unit, initial_offset_start, initial_offset_end, initial_offset_start_norm, initial_offset_end_norm, num_units
def __getitem__(self, index):
# print(index)
offset = np.zeros(2, dtype=np.float32)
offset_norm = np.zeros(2, dtype=np.float32)
initial_offset = np.zeros(2, dtype=np.float32)
initial_offset_norm = np.zeros(2, dtype=np.float32)
samples = self.clip_sentence_pairs_iou_all[index]
proposal_or_sliding_window = samples['proposal_or_sliding_window']
movie_name_id = self.movie_name_id[index]
global_feature = self.global_feature_all[movie_name_id]
original_feats = self.original_feats_all[movie_name_id]
initial_feature = self.initial_feature_all[movie_name_id]
ten_unit = self.ten_unit_all[movie_name_id]
initial_offset_start = self.initial_offset_start_all[movie_name_id]
initial_offset_end = self.initial_offset_end_all[movie_name_id]
initial_offset_start_norm = self.initial_offset_start_norm_all[movie_name_id]
initial_offset_end_norm = self.initial_offset_end_norm_all[movie_name_id]
num_units = self.num_units_all[movie_name_id]
sentence = samples['sent_skip_thought_vec'][0][0]
# print(np.shape(sentence))
offset_start = samples['offset_start']
offset_end = samples['offset_end']
offset_start_norm = samples['offset_start_norm']
offset_end_norm = samples['offset_end_norm']
# offest
offset[0] = offset_start
offset[1] = offset_end
offset_norm[0] = offset_start_norm
offset_norm[1] = offset_end_norm
initial_offset[0] = initial_offset_start
initial_offset[1] = initial_offset_end
initial_offset_norm[0] = initial_offset_start_norm
initial_offset_norm[1] = initial_offset_end_norm
return global_feature, original_feats, initial_feature, sentence, offset_norm, initial_offset, initial_offset_norm, ten_unit, num_units
def __len__(self):
return self.num_samples_iou
class Charades_Test_dataset(torch.utils.data.Dataset):
def __init__(self):
# il_path: image_label_file path
self.context_num = 1
self.context_size = 128
self.visual_feature_dim = 4096
self.feats_dimen = 4096
self.unit_size = 16
self.semantic_size = 4800
self.sliding_clip_path = "./Dataset/Charades/Charades_localfeature_npy/"#"./Dataset/Charades/all_fc6_unit16_overlap0.5/"
self.index_in_epoch = 0
self.spacy_vec_dim = 300
self.sent_vec_dim = 4800
self.epochs_completed = 0
self.test_swin_txt_path = "./Dataset/Charades/ref_info/charades_sta_test_swin_props_num_36364.txt"
self.clip_sentence_pairs = pickle.load(open("./Dataset/Charades/ref_info/charades_sta_test_semantic_sentence_VP_sub_obj.pkl"))
self.num_samples_iou = len(self.clip_sentence_pairs)
print str(len(self.clip_sentence_pairs)) + " test videos are readed" # 1334
movie_names_set = set()
for ii in self.clip_sentence_pairs:
for iii in self.clip_sentence_pairs[ii]:
clip_name = iii
movie_name = ii
if not movie_name in movie_names_set:
movie_names_set.add(movie_name)
self.movie_names = list(movie_names_set)
self.sliding_clip_names = []
with open(self.test_swin_txt_path) as f:
for l in f:
self.sliding_clip_names.append(l.rstrip().replace(" ", "_"))
print "sliding clips number for test: " + str(len(self.sliding_clip_names)) # 36364
self.movie_length_dict = {}
with open("./Dataset/Charades/ref_info/charades_movie_length_info.txt") as f:
for l in f:
self.movie_length_dict[l.rstrip().split(" ")[0]] = float(l.rstrip().split(" ")[1])
def read_video_level_feats(self, movie_name, end):
# read unit level feats by just passing the start and end number
unit_size = 16
feats_dimen = 4096
start = 1
num_units = (end - start) / unit_size
# print(start, end, num_units)
curr_start = 1
ten_unit = num_units / 10
four_unit = num_units / 4
oneinfour_unit = four_unit
threeinfour_unit = num_units - four_unit
start_end_list = []
while (curr_start + unit_size <= end):
start_end_list.append((curr_start, curr_start + unit_size))
curr_start += unit_size
# original_feats = np.zeros([num_units, feats_dimen], dtype=np.float32)
original_feats = np.zeros([num_units, feats_dimen*2], dtype=np.float32)
for k, (curr_s, curr_e) in enumerate(start_end_list):
one_feat = np.load(self.sliding_clip_path + movie_name + "_" + str(curr_s) + ".0_" + str(curr_e) + ".0.npy")
original_feats[k] = one_feat[feats_dimen:]
# print(np.shape(original_feats))
feature_length = len(original_feats)
idx = choose_ten_frame(feature_length)
global_feature_1 = original_feats[idx[0]]
global_feature_2 = original_feats[idx[1]]
global_feature_3 = original_feats[idx[2]]
global_feature_4 = original_feats[idx[3]]
global_feature_5 = original_feats[idx[4]]
global_feature_6 = original_feats[idx[5]]
global_feature_7 = original_feats[idx[6]]
global_feature_8 = original_feats[idx[7]]
global_feature_9 = original_feats[idx[8]]
global_feature_10 = original_feats[idx[9]]
global_feature_concate = np.concatenate([global_feature_1, global_feature_2, global_feature_3, global_feature_4, global_feature_5, \
global_feature_6, global_feature_7, global_feature_8, global_feature_9, global_feature_10], axis=0)
global_feature = global_feature_concate
initial_feature = original_feats[(oneinfour_unit-1):(threeinfour_unit)]
feature_length = len(initial_feature)
idx = choose_ten_frame(feature_length)
initial_feature_1 = initial_feature[idx[0]]
initial_feature_2 = initial_feature[idx[1]]
initial_feature_3 = initial_feature[idx[2]]
initial_feature_4 = initial_feature[idx[3]]
initial_feature_5 = initial_feature[idx[4]]
initial_feature_6 = initial_feature[idx[5]]
initial_feature_7 = initial_feature[idx[6]]
initial_feature_8 = initial_feature[idx[7]]
initial_feature_9 = initial_feature[idx[0]]
initial_feature_10 = initial_feature[idx[9]]
initial_feature_concate = np.concatenate([initial_feature_1, initial_feature_2, initial_feature_3, initial_feature_4, initial_feature_5, \
initial_feature_6, initial_feature_7, initial_feature_8, initial_feature_9, initial_feature_10], axis=0)
initial_feature = initial_feature_concate
initial_offset_start = oneinfour_unit-1
initial_offset_end = threeinfour_unit - 1
initial_offset_start_norm = initial_offset_start / float(num_units-1)
initial_offset_end_norm = initial_offset_end / float(num_units-1)
return global_feature, original_feats, initial_feature, ten_unit, initial_offset_start, initial_offset_end, initial_offset_start_norm, initial_offset_end_norm, num_units
def __getitem__(self, index):
movie_name = self.movie_names[index]
# load unit level feats and sentence vector
initial_offset = np.zeros(2, dtype=np.float32)
initial_offset_norm = np.zeros(2, dtype=np.float32)
movie_clip_sentences = []
checkpoint_paths = glob.glob(self.sliding_clip_path + movie_name + "_*")
checkpoint_file_name_ints = [int(float(x.split('/')[-1].split('.npy')[0].split('_')[-1]))
for x in checkpoint_paths]
end = max(checkpoint_file_name_ints)
global_feature, original_feats, initial_feature, ten_unit, initial_offset_start, initial_offset_end, initial_offset_start_norm, initial_offset_end_norm, num_units\
= self.read_video_level_feats(movie_name, end)
ten_unit = np.array(ten_unit)
num_units = np.array(num_units)
for dict_2nd in self.clip_sentence_pairs[movie_name]:
for dict_3rd in self.clip_sentence_pairs[movie_name][dict_2nd]:
sentence_vec_ = dict_3rd['sent_skip_thought_vec'][0][0, :self.sent_vec_dim]
movie_clip_sentences.append((dict_2nd, sentence_vec_))
initial_offset[0] = initial_offset_start
initial_offset[1] = initial_offset_end
initial_offset_norm[0] = initial_offset_start_norm
initial_offset_norm[1] = initial_offset_end_norm
return movie_clip_sentences, global_feature, original_feats, initial_feature, initial_offset, initial_offset_norm, ten_unit, num_units
def __len__(self):
return self.num_samples_iou