-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsad_dataset.py
503 lines (460 loc) · 19.1 KB
/
wsad_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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
from __future__ import print_function
import h5py
import numpy as np
import torch
import utils.wsad_utils as utils
import random
import os
import options
import pdb
class SampleDataset:
def __init__(self, args, mode="both",sampling='random'):
self.dataset_name = args.dataset_name
self.num_class = args.num_class
self.sampling=sampling
self.num_segments = args.max_seqlen
self.feature_size = args.feature_size
self.path_to_features = os.path.join(args.path_dataset,self.dataset_name + "-I3D-JOINTFeatures.npy")
self.path_to_annotations = os.path.join(args.path_dataset,self.dataset_name + "-Annotations/")
self.features = np.load(
self.path_to_features, encoding="bytes", allow_pickle=True
)
self.segments = np.load(
self.path_to_annotations + "segments.npy", allow_pickle=True
)
self.labels = np.load(
self.path_to_annotations + "labels_all.npy", allow_pickle=True
)
# Specific to Thumos14
self._labels = np.load(
self.path_to_annotations + "labels.npy", allow_pickle=True
)
self.classlist = np.load(
self.path_to_annotations + "classlist.npy", allow_pickle=True
)
self.subset = np.load(
self.path_to_annotations + "subset.npy", allow_pickle=True
)
self.videonames = np.load(
self.path_to_annotations + "videoname.npy", allow_pickle=True
)
self.batch_size = args.batch_size
self.trainidx = []
self.testidx = []
self.classwiseidx = []
self.currenttestidx = 0
self.labels_multihot = [
utils.strlist2multihot(labs, self.classlist)
for labs in self.labels
]
try:
ambilist = self.path_to_annotations + "/Ambiguous_test.txt"
ambilist = list(open(ambilist, "r"))
ambilist = [a.strip("\n").split(" ")[0] for a in ambilist]
except:
ambilist = []
self.train_test_idx()
self.classwise_feature_mapping()
self.normalize = False
self.mode = mode
if mode == "rgb" or mode == "flow":
self.feature_size = 1024
def train_test_idx(self):
for i, s in enumerate(self.subset):
if s.decode("utf-8") == "validation": # Specific to Thumos14
self.trainidx.append(i)
elif s.decode("utf-8") == "test":
self.testidx.append(i)
def classwise_feature_mapping(self):
for category in self.classlist:
idx = []
for i in self.trainidx:
for label in self.labels[i]:
if label == category.decode("utf-8"):
idx.append(i)
break
self.classwiseidx.append(idx)
def load_data(self, n_similar=0, is_training=True, similar_size=2):
if is_training:
labels = []
idx = []
# Load similar pairs
if n_similar != 0:
rand_classid = np.random.choice(
len(self.classwiseidx), size=n_similar
)
for rid in rand_classid:
rand_sampleid = np.random.choice(
len(self.classwiseidx[rid]),
size=similar_size,
replace=False,
)
for k in rand_sampleid:
idx.append(self.classwiseidx[rid][k])
# Load rest pairs
if self.batch_size - similar_size * n_similar < 0:
self.batch_size = similar_size * n_similar
rand_sampleid = np.random.choice(
len(self.trainidx),
size=self.batch_size - similar_size * n_similar,
)
for r in rand_sampleid:
idx.append(self.trainidx[r])
feat = []
for i in idx:
ifeat = self.features[i]
if self.sampling == 'random':
sample_idx = self.random_perturb(ifeat.shape[0])
elif self.sampling == 'uniform':
sample_idx = self.uniform_sampling(ifeat.shape[0])
elif self.sampling == "all":
sample_idx = np.arange(ifeat.shape[0])
else:
raise AssertionError('Not supported sampling !')
ifeat = ifeat[sample_idx]
feat.append(ifeat)
feat = np.array(feat)
labels = np.array([self.labels_multihot[i] for i in idx])
if self.mode == "rgb":
feat = feat[..., : self.feature_size]
elif self.mode == "flow":
feat = feat[..., self.feature_size :]
return feat, labels,rand_sampleid
else:
labs = self.labels_multihot[self.testidx[self.currenttestidx]]
feat = self.features[self.testidx[self.currenttestidx]]
# feat = utils.process_feat(feat, normalize=self.normalize)
# feature = feature[sample_idx]
vn = self.videonames[self.testidx[self.currenttestidx]]
if self.currenttestidx == len(self.testidx) - 1:
done = True
self.currenttestidx = 0
else:
done = False
self.currenttestidx += 1
feat = np.array(feat)
if self.mode == "rgb":
feat = feat[..., : self.feature_size]
elif self.mode == "flow":
feat = feat[..., self.feature_size :]
# return feat, np.array(labs),vn, done
output_dict = {
'feat': feat,
'lab': np.array(labs),
'vn': vn,
'done': done,
'mode': 'All'
# 'sample_idx': sample_idx,
}
return output_dict # feat, np.array(labs),vn, done
def random_avg(self, x, segm=None):
if len(x) < self.num_segments:
ind = self.random_perturb(len(x))
x_n = x[ind]
segm = segm[ind] if segm is not None else None
return x_n, segm
else:
inds = np.array_split(np.arange(len(x)), self.num_segments)
x_n = np.zeros((self.num_segments, x.shape[-1])).astype(x.dtype)
segm_n = np.zeros(
(self.num_segments, segm.shape[-1])).astype(x.dtype)
for i, ind in enumerate(inds):
x_n[i] = np.mean(x[ind], axis=0)
if segm is not None:
segm_n[i] = segm[(ind[0] + ind[-1]) // 2]
return x_n, segm_n if segm is not None else None
def random_pad(self, x, segm=None):
length = self.num_segments
if x.shape[0] > length:
strt = np.random.randint(0, x.shape[0] - length)
x_ret = x[strt:strt + length]
if segm is not None:
segm = segm[strt:strt + length]
return x_ret, segm
elif x.shape[0] == length:
return x, segm
else:
pad_len = length - x.shape[0]
x_ret = np.pad(x, ((0, pad_len), (0, 0)), mode='constant')
if segm is not None:
segm = np.pad(segm, ((0, pad_len), (0, 0)), mode='constant')
return x_ret, segm
def random_perturb(self, length):
if self.num_segments == length:
return np.arange(self.num_segments).astype(int)
samples = np.arange(self.num_segments) * length / self.num_segments
for i in range(self.num_segments):
if i < self.num_segments - 1:
if int(samples[i]) != int(samples[i + 1]):
samples[i] = np.random.choice(
range(int(samples[i]),
int(samples[i + 1]) + 1))
else:
samples[i] = int(samples[i])
else:
if int(samples[i]) < length - 1:
samples[i] = np.random.choice(
range(int(samples[i]), length))
else:
samples[i] = int(samples[i])
return samples.astype(int)
def uniform_sampling(self, length):
if self.num_segments == length:
return np.arange(self.num_segments).astype(int)
samples = np.arange(self.num_segments) * length / self.num_segments
samples = np.floor(samples)
return samples.astype(int)
class Ant13_SampleDataset:
def __init__(self, args, mode="both",sampling='random'):
self.dataset_name = args.dataset_name
self.num_class = args.num_class
self.sampling=sampling
self.num_segments = args.max_seqlen
self.feature_size = args.feature_size
# self.path_to_features = os.path.join(args.path_dataset,self.dataset_name + "-I3D-JOINTFeatures.npy")
self.train_features,self.test_features={},{}
self.train_features['rgb']=self.preload_features(h5py.File(args.path_dataset+'feature_rgb_train.h5','r'))
self.test_features['rgb']=self.preload_features(h5py.File(args.path_dataset+'feature_rgb_val.h5','r'))
self.train_features['flow']=self.preload_features(h5py.File(args.path_dataset+'feature_flow_train.h5','r'))
self.test_features['flow']=self.preload_features(h5py.File(args.path_dataset+'feature_flow_val.h5','r'))
self.path_to_annotations = os.path.join(args.path_dataset,self.dataset_name + "-Annotations/")
# self.features = np.load(
# self.path_to_features, encoding="bytes", allow_pickle=True
# )
self.segments = np.load(
self.path_to_annotations + "segments.npy", allow_pickle=True
)
self.labels = np.load(
self.path_to_annotations + "labels_all.npy", allow_pickle=True
)
# Specific to Thumos14
self._labels = np.load(
self.path_to_annotations + "labels.npy", allow_pickle=True
)
self.classlist = np.load(
self.path_to_annotations + "classlist.npy", allow_pickle=True
)
self.subset = np.load(
self.path_to_annotations + "subset.npy", allow_pickle=True
)
self.videonames = np.load(
self.path_to_annotations + "videoname.npy", allow_pickle=True
)
self.durations=np.load(self.path_to_annotations+"duration.npy",allow_pickle=True)
self.batch_size = args.batch_size
self.t_max = args.max_seqlen
self.trainidx = []
self.testidx = []
self.classwiseidx = []
self.currenttestidx = 0
self.labels_multihot = [
utils.strlist2multihot(labs, self.classlist)
for labs in self.labels
]
try:
ambilist = self.path_to_annotations + "/Ambiguous_test.txt"
ambilist = list(open(ambilist, "r"))
ambilist = [a.strip("\n").split(" ")[0] for a in ambilist]
except:
ambilist = []
self.train_test_idx()
self.classwise_feature_mapping()
self.normalize = False
self.mode = mode
if mode == "rgb" or mode == "flow":
self.feature_size = 1024
self.filter()
def filter(self):
new_testidx = []
for idx in self.testidx:
feat = self.test_features['rgb'][self.videonames[idx]][:]
if len(feat)>10:
new_testidx.append(idx)
self.testidx = new_testidx
new_trainidx = []
for idx in self.trainidx:
feat = self.train_features['rgb'][self.videonames[idx]][:]
if len(feat)>10:
new_trainidx.append(idx)
self.trainidx = new_trainidx
def train_test_idx(self):
for i, s in enumerate(self.subset):
if s== "train": # Specific to Thumos14
self.trainidx.append(i)
elif s == "val":
self.testidx.append(i)
def classwise_feature_mapping(self):
for category in self.classlist:
idx = []
for i in self.trainidx:
if self.train_features['rgb'][self.videonames[i]].shape[0]==0:
continue
for label in self.labels[i]:
if label == category.decode("utf-8"):
idx.append(i)
break
self.classwiseidx.append(idx)
def preload_features(self,feat_dict):
feature_dict={}
for key in feat_dict.keys():
feature_dict[key]=feat_dict[key][:]
return feature_dict
def load_data(self, n_similar=0, is_training=True, similar_size=2):
if is_training:
labels = []
idx = []
# Load similar pairs
if n_similar != 0:
rand_classid = np.random.choice(
len(self.classwiseidx), size=n_similar
)
# import pdb
# pdb.set_trace()
for rid in rand_classid:
rand_sampleid = np.random.choice(
len(self.classwiseidx[rid]),
size=similar_size,
replace=False,
)
for k in rand_sampleid:
idx.append(self.classwiseidx[rid][k])
# Load rest pairs
if self.batch_size - similar_size * n_similar < 0:
self.batch_size = similar_size * n_similar
rand_sampleid = np.random.choice(
len(self.trainidx),
size=self.batch_size - similar_size * n_similar,
)
for r in rand_sampleid:
idx.append(self.trainidx[r])
feat = []
for i in idx:
# ifeat = self.features[i]
ifeat=np.concatenate([self.train_features['rgb'][self.videonames[i]],
self.train_features['flow'][self.videonames[i]]],axis=-1)
if self.sampling == 'random':
sample_idx = self.random_perturb(ifeat.shape[0])
ifeat = ifeat[sample_idx]
elif self.sampling == 'uniform':
sample_idx = self.uniform_sampling(ifeat.shape[0])
ifeat = ifeat[sample_idx]
elif self.sampling == "all":
sample_idx = np.arange(ifeat.shape[0])
ifeat = ifeat[sample_idx]
elif self.sampling =='average':
ifeat=utils.average_to_fixed_length(ifeat,self.num_segments)
else:
raise AssertionError('Not supported sampling !')
feat.append(ifeat)
feat = np.array(feat)
labels = np.array([self.labels_multihot[i] for i in idx])
if self.mode == "rgb":
feat = feat[..., : self.feature_size]
elif self.mode == "flow":
feat = feat[..., self.feature_size :]
return feat, labels,rand_sampleid
else:
idx=self.testidx[self.currenttestidx]
labs = self.labels_multihot[idx]
# feat = self.features[self.testidx[self.currenttestidx]]
vn = self.videonames[idx]
feat = np.concatenate([self.test_features['rgb'][vn],
self.test_features['flow'][vn]], axis=-1)
dur=self.durations[idx]
# feat = utils.process_feat(feat, normalize=self.normalize)
# feature = feature[sample_idx]
if self.currenttestidx == len(self.testidx) - 1:
done = True
self.currenttestidx = 0
else:
done = False
self.currenttestidx += 1
feat = np.array(feat)
if self.mode == "rgb":
feat = feat[..., : self.feature_size]
elif self.mode == "flow":
feat = feat[..., self.feature_size :]
output_dict={
'feat':feat,
'lab':np.array(labs),
'vn':vn,
'done':done,
'mode':'All',
'dur':dur
# 'sample_idx':sample_idx,
}
return output_dict#feat, np.array(labs),vn, done def random_avg(self, x, segm=None):
def verify_dataset(self):
not_exist_vns=[]
for i in self.testidx:
vn=self.videonames[i]
try:
a=self.test_features['rgb'][vn]
b=self.test_features['flow'][vn]
except:
not_exist_vns.append(vn)
print(not_exist_vns)
def random_avg(self, x, segm=None):
if len(x) < self.num_segments:
ind = self.random_perturb(len(x))
x_n = x[ind]
segm = segm[ind] if segm is not None else None
return x_n, segm
else:
inds = np.array_split(np.arange(len(x)), self.num_segments)
x_n = np.zeros((self.num_segments, x.shape[-1])).astype(x.dtype)
segm_n = np.zeros(
(self.num_segments, segm.shape[-1])).astype(x.dtype)
for i, ind in enumerate(inds):
x_n[i] = np.mean(x[ind], axis=0)
if segm is not None:
segm_n[i] = segm[(ind[0] + ind[-1]) // 2]
return x_n, segm_n if segm is not None else None
def random_pad(self, x, segm=None):
length = self.num_segments
if x.shape[0] > length:
strt = np.random.randint(0, x.shape[0] - length)
x_ret = x[strt:strt + length]
if segm is not None:
segm = segm[strt:strt + length]
return x_ret, segm
elif x.shape[0] == length:
return x, segm
else:
pad_len = length - x.shape[0]
x_ret = np.pad(x, ((0, pad_len), (0, 0)), mode='constant')
if segm is not None:
segm = np.pad(segm, ((0, pad_len), (0, 0)), mode='constant')
return x_ret, segm
def random_perturb(self, length):
if self.num_segments == length:
return np.arange(self.num_segments).astype(int)
samples = np.arange(self.num_segments) * length / self.num_segments
for i in range(self.num_segments):
if i < self.num_segments - 1:
if int(samples[i]) != int(samples[i + 1]):
samples[i] = np.random.choice(
range(int(samples[i]),
int(samples[i + 1]) + 1))
else:
samples[i] = int(samples[i])
else:
if int(samples[i]) < length - 1:
samples[i] = np.random.choice(
range(int(samples[i]), length))
else:
samples[i] = int(samples[i])
return samples.astype(int)
def uniform_sampling(self, length):
if self.num_segments == length:
return np.arange(self.num_segments).astype(int)
samples = np.arange(self.num_segments) * length / self.num_segments
samples = np.floor(samples)
return samples.astype(int)
if __name__ == '__main__':
args = options.parser.parse_args()
dt = SampleDataset(args)
data = dt.load_data()
print(data)
pdb.set_trace()
print(dt)