-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
180 lines (140 loc) · 6.72 KB
/
data.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
import pickle
import h5py
import torch
import torch.utils.data as data
import numpy as np
import random
from utils.args import train_assist_path,anchor_path
class TrainDataset(data.Dataset):
def __init__(self, feature_h5_path, sim_path):
with h5py.File(feature_h5_path,'r') as h5_file:
self.video_feats = h5_file['feats'][:]
with h5py.File(sim_path,'r') as h5_file:
self.v_feats = h5_file['adj'][:]
with h5py.File(train_assist_path,'r') as h5_file:
self.neighbor = h5_file['pos'][:]
with h5py.File(anchor_path,'r') as h5_file:
self.achors = h5_file['feats'][:]
def random_frame(self, video):
output_label = np.zeros((video.shape),dtype=np.float32)
for i, token in enumerate(video):
prob = random.uniform(0,1)
if prob < 0.15:
prob /= 0.15
# 80% randomly change token to mask token
if prob < 0.8:
video[i] = video[i]*0
# 10% randomly change token to random token
elif prob < 0.9:
xx = np.random.randint(0,self.video_feats.shape[0],size=1)
yy = np.random.randint(0,self.video_feats.shape[1],size=1)
video[i] = self.video_feats[xx[0],yy[0],:]
output_label[i] = token
return video, output_label
def random_video(self, index):
if random.random() > 0.5:
# get a similar video
index2 = self.get_neighbor_video(index)
return index2, 1.0
else:
# get a dissimilar video
index2 = self.get_random_video(index)
return index2, -1.0
def get_neighbor_video(self, index):
# similar videos have closest distance from the query video (tag is 1)
neighbors = np.where(self.v_feats[index]==1)
neighbors=neighbors[0]
ranind = np.random.randint(0,len(neighbors),size=1)
return neighbors[ranind[0]]
def get_random_video(self, index):
# dissimilar videos have middle distance from the query video (tag is 2)
others = np.where(self.v_feats[index]==2)
others=others[0]
# If there is no tag 2 video, we get dissimilar videos with tag 0
if len(others)==0:
others = np.where(self.v_feats[index]==0)
others=others[0]
ranind = np.random.randint(0,len(others),size=1)
return others[ranind[0]]
def __getitem__(self, item):
item2, is_similar = self.random_video(item)
t2 = self.video_feats[item2]
t1 = self.video_feats[item]
t1_random, t1_label = self.random_frame(t1)
t2_random, t2_label = self.random_frame(t2)
neighbor1 = torch.from_numpy(self.achors[self.neighbor[item][0]])
neighbor2 = torch.from_numpy(self.achors[self.neighbor[item2][0]])
visual_word = np.concatenate((t1,t2),0)
mask_input = np.concatenate((t1_random,t2_random),0)
output = {"mask_input": mask_input,
"visual_word": visual_word,
"is_similar": is_similar,
'n1':neighbor1,
'n2':neighbor2}
return {key: torch.tensor(value) for key, value in output.items()}
def __len__(self):
return len(self.video_feats)
class TestDataset(data.Dataset):
def __init__(self, feature_h5_path):
with h5py.File(feature_h5_path,'r') as h5_file:
self.video_feats = h5_file['feats'][:]
def __getitem__(self, item):
visual_word = self.video_feats[item]
output = {"visual_word": visual_word}
return {key: torch.tensor(value) for key, value in output.items()}
def __len__(self):
return len(self.video_feats)
class TestDataset_Mask(data.Dataset):
def __init__(self, feature_h5_path):
with h5py.File(feature_h5_path,'r') as h5_file:
self.video_feats = h5_file['feats'][:]
def random_frame(self, video):
output_label = np.zeros((video.shape),dtype=np.float32)
for i, token in enumerate(video):
prob = random.uniform(0,1)
if prob < 0.15:
prob /= 0.15
# 80% randomly change token to mask token
if prob < 0.8:
video[i] = video[i]*0
# 10% randomly change token to random token
elif prob < 0.9:
xx = np.random.randint(0,self.video_feats.shape[0],size=1)
yy = np.random.randint(0,self.video_feats.shape[1],size=1)
video[i] = self.video_feats[xx[0],yy[0],:]
output_label[i] = token
return video, output_label
def __getitem__(self, item):
visual_word = self.video_feats[item]
visual_word_random, visual_word_label = self.random_frame(visual_word)
output = {"mask_input": visual_word_random,"visual_word": visual_word}
return {key: torch.tensor(value) for key, value in output.items()}
def __len__(self):
return len(self.video_feats)
def get_train_loader(feature_h5_path, sim_path,batch_size=10, shuffle=True, num_workers=1, pin_memory=True):
v = TrainDataset(feature_h5_path,sim_path)
data_loader = torch.utils.data.DataLoader(dataset=v,
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
#collate_fn=train_collate_fn,
pin_memory=pin_memory)
return data_loader
def get_eval_loader(feature_h5_path, batch_size=256, shuffle=False, num_workers=1, pin_memory=False):
vd = TestDataset(feature_h5_path)
data_loader = torch.utils.data.DataLoader(dataset=vd,
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
#collate_fn=eval_collate_fn,
pin_memory=pin_memory)
return data_loader
def get_eval_mask_loader(feature_h5_path, batch_size=256, shuffle=True, num_workers=1, pin_memory=False):
vd = TestDataset_Mask(feature_h5_path)
data_loader = torch.utils.data.DataLoader(dataset=vd,
batch_size=batch_size,
shuffle=shuffle,
num_workers=num_workers,
#collate_fn=eval_collate_fn,
pin_memory=pin_memory)
return data_loader