-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathdataloader_charades_RL.py
217 lines (164 loc) · 8.87 KB
/
dataloader_charades_RL.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
" Dataloader of charades-STA dataset for Reinforcenments Learning based methods"
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/all_fc6_unit16_overlap0.5/"
self.clip_sentence_pairs_iou_all = pickle.load(open("./Dataset/Charades/ref_info/charades_rl_train_feature.pkl"))
self.num_samples_iou = len(self.clip_sentence_pairs_iou_all)
print(self.num_samples_iou, "iou clip-sentence pairs are readed") # 49442
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], dtype=np.float32)
original_feats_1 = np.zeros([num_units, feats_dimen], 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
original_feats_1[k] = one_feat
# print(np.shape(original_feats))
global_feature = np.mean(original_feats_1, axis=0)
initial_feature = original_feats[(oneinfour_unit-1):(threeinfour_unit)]
initial_feature = np.mean(initial_feature, axis=0)
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']
end = samples['frames_num'] + 1
movie_name = proposal_or_sliding_window.split("_")[0]
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)
# print(np.shape(global_feature), np.shape(original_feats), np.shape(initial_feature))
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/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.clip_sentence_pairs = pickle.load(open("./Dataset/Charades/ref_info/charades_sta_test_semantic_sentence_VP_sub_obj.pkl"))
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.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], 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
# print(np.shape(original_feats))
global_feature = np.mean(original_feats, axis=0)
initial_feature = original_feats[(oneinfour_unit-1):(threeinfour_unit)]
initial_feature = np.mean(initial_feature, axis=0)
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 load_movie_slidingclip(self, movie_name):
# 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