-
Notifications
You must be signed in to change notification settings - Fork 10
/
thumos_features.py
162 lines (121 loc) · 5.54 KB
/
thumos_features.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
import torch.utils.data as data
import os
import csv
import json
import numpy as np
import torch
import pdb
import time
import random
import utils
import config
class ThumosFeature(data.Dataset):
def __init__(self, data_path, mode, modal, feature_fps, num_segments, sampling, seed=-1, supervision='weak'):
if seed >= 0:
utils.set_seed(seed)
self.mode = mode
self.modal = modal
self.feature_fps = feature_fps
self.num_segments = num_segments
if self.modal == 'all':
self.feature_path = []
for _modal in ['rgb', 'flow']:
self.feature_path.append(os.path.join(data_path, 'features', self.mode, _modal))
else:
self.feature_path = os.path.join(data_path, 'features', self.mode, self.modal)
split_path = os.path.join(data_path, 'split_{}.txt'.format(self.mode))
split_file = open(split_path, 'r')
self.vid_list = []
for line in split_file:
self.vid_list.append(line.strip())
split_file.close()
anno_path = os.path.join(data_path, 'gt.json')
anno_file = open(anno_path, 'r')
self.anno = json.load(anno_file)
anno_file.close()
self.class_name_to_idx = dict((v, k) for k, v in config.class_dict.items())
self.num_classes = len(self.class_name_to_idx.keys())
self.supervision = supervision
self.sampling = sampling
def __len__(self):
return len(self.vid_list)
def __getitem__(self, index):
data, vid_num_seg, sample_idx = self.get_data(index)
label, temp_anno = self.get_label(index, vid_num_seg, sample_idx)
return data, label, temp_anno, self.vid_list[index], vid_num_seg
def get_data(self, index):
vid_name = self.vid_list[index]
vid_num_seg = 0
if self.modal == 'all':
rgb_feature = np.load(os.path.join(self.feature_path[0],
vid_name + '.npy')).astype(np.float32)
flow_feature = np.load(os.path.join(self.feature_path[1],
vid_name + '.npy')).astype(np.float32)
vid_num_seg = rgb_feature.shape[0]
if self.sampling == 'random':
sample_idx = self.random_perturb(rgb_feature.shape[0])
elif self.sampling == 'uniform':
sample_idx = self.uniform_sampling(rgb_feature.shape[0])
else:
raise AssertionError('Not supported sampling !')
rgb_feature = rgb_feature[sample_idx]
flow_feature = flow_feature[sample_idx]
feature = np.concatenate((rgb_feature, flow_feature), axis=1)
else:
feature = np.load(os.path.join(self.feature_path,
vid_name + '.npy')).astype(np.float32)
vid_num_seg = feature.shape[0]
if self.sampling == 'random':
sample_idx = self.random_perturb(feature.shape[0])
elif self.sampling == 'uniform':
sample_idx = self.uniform_sampling(feature.shape[0])
else:
raise AssertionError('Not supported sampling !')
feature = feature[sample_idx]
return torch.from_numpy(feature), vid_num_seg, sample_idx
def get_label(self, index, vid_num_seg, sample_idx):
vid_name = self.vid_list[index]
anno_list = self.anno['database'][vid_name]['annotations']
label = np.zeros([self.num_classes], dtype=np.float32)
classwise_anno = [[]] * self.num_classes
for _anno in anno_list:
label[self.class_name_to_idx[_anno['label']]] = 1
classwise_anno[self.class_name_to_idx[_anno['label']]].append(_anno)
if self.supervision == 'weak':
return label, torch.Tensor(0)
else:
temp_anno = np.zeros([vid_num_seg, self.num_classes])
t_factor = self.feature_fps / 16
for class_idx in range(self.num_classes):
if label[class_idx] != 1:
continue
for _anno in classwise_anno[class_idx]:
tmp_start_sec = float(_anno['segment'][0])
tmp_end_sec = float(_anno['segment'][1])
tmp_start = round(tmp_start_sec * t_factor)
tmp_end = round(tmp_end_sec * t_factor)
temp_anno[tmp_start:tmp_end+1, class_idx] = 1
temp_anno = temp_anno[sample_idx, :]
return label, torch.from_numpy(temp_anno)
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 length <= self.num_segments:
return np.arange(length).astype(int)
samples = np.arange(self.num_segments) * length / self.num_segments
samples = np.floor(samples)
return samples.astype(int)