-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdataset.py
executable file
·228 lines (205 loc) · 9.25 KB
/
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
import torch
import torch.utils.data as data
import numpy as np
import json
import os.path as osp
import gc
from datasets.dataset_builder import DATA_LAYERS
FEATURE_SIZES = {
'rgb_anet_resnet50': 2048,
'flow_anet_resnet50': 2048,
'rgb_kinetics_bninception': 1024,
'flow_kinetics_bninception': 1024,
'rgb_kinetics_resnet50': 2048,
'flow_kinetics_resnet50': 2048,
'flow_nv_kinetics_bninception': 1024,
'rgb_kinetics_i3d': 2048,
'flow_kinetics_i3d': 2048
}
@DATA_LAYERS.register("THUMOS")
@DATA_LAYERS.register("TVSERIES")
class THUMOSDataset(data.Dataset):
def __init__(self, cfg, mode='train'):
self.root_path = cfg['root_path']
self.mode = mode
self.training = mode == 'train'
self.window_size = cfg['window_size']
self.stride = cfg['stride']
data_name = cfg['data_name']
self.vids = json.load(open(cfg['video_list_path']))[data_name][mode + '_session_set'] # list of video names
self.num_classes = cfg['num_classes']
self.inputs = []
self._load_features(cfg)
self._init_features()
def _load_features(self, cfg):
self.annotation_type = cfg['annotation_type']
self.rgb_type = cfg['rgb_type']
self.flow_type = cfg['flow_type']
self.target_all = {}
self.rgb_inputs = {}
self.flow_inputs = {}
dummy_target = np.zeros((self.window_size-1, self.num_classes))
dummy_rgb = np.zeros((self.window_size-1, FEATURE_SIZES[cfg['rgb_type']]))
dummy_flow = np.zeros((self.window_size-1, FEATURE_SIZES[cfg['flow_type']]))
for vid in self.vids:
target = np.load(osp.join(self.root_path, self.annotation_type, vid + '.npy'))
rgb = np.load(osp.join(self.root_path, self.rgb_type, vid + '.npy'))
flow = np.load(osp.join(self.root_path, self.flow_type, vid + '.npy'))
# concatting dummy target at the front
if self.training:
self.target_all[vid] = np.concatenate((dummy_target, target), axis=0)
self.rgb_inputs[vid] = np.concatenate((dummy_rgb, rgb), axis=0)
self.flow_inputs[vid] = np.concatenate((dummy_flow, flow), axis=0)
else:
self.target_all[vid] = target
self.rgb_inputs[vid] = rgb
self.flow_inputs[vid] = flow
def _init_features(self):
del self.inputs
gc.collect()
self.inputs = []
for vid in self.vids:
target = self.target_all[vid]
if self.training:
seed = np.random.randint(self.stride)
for start, end in zip(range(seed, target.shape[0], self.stride),
range(seed + self.window_size, target.shape[0]+1, self.stride)):
self.inputs.append([
vid, start, end, target[start:end]
])
else:
start = 0
end = target.shape[0]
self.inputs.append([
vid, start, end, target[start:end]
])
def __getitem__(self, index):
vid, start, end, target = self.inputs[index]
rgb_input = self.rgb_inputs[vid][start:end]
flow_input = self.flow_inputs[vid][start:end]
rgb_input = torch.tensor(rgb_input.astype(np.float32))
flow_input = torch.tensor(flow_input.astype(np.float32))
target = torch.tensor(target.astype(np.float32))
return rgb_input, flow_input, target
def __len__(self):
return len(self.inputs)
@DATA_LAYERS.register("THUMOS_ANTICIPATION")
@DATA_LAYERS.register("TVSERIES_ANTICIPATION")
class THUMOSDataset(data.Dataset):
def __init__(self, cfg, mode='train'):
self.root_path = cfg['root_path']
self.mode = mode
self.training = mode == 'train'
self.window_size = cfg['window_size']
self.stride = cfg['stride']
self.anticipation_length = cfg['anticipation_length']
data_name = cfg["data_name"].split('_')[0]
self.vids = json.load(open(cfg['video_list_path']))[data_name][mode + '_session_set'] # list of video names
self.num_classes = cfg['num_classes']
self.inputs = []
self._load_features(cfg)
self._init_features()
def _load_features(self, cfg):
self.annotation_type = cfg['annotation_type']
self.rgb_type = cfg['rgb_type']
self.flow_type = cfg['flow_type']
self.target_all = {}
self.rgb_inputs = {}
self.flow_inputs = {}
dummy_target = np.zeros((self.window_size-1, self.num_classes))
dummy_rgb = np.zeros((self.window_size-1, FEATURE_SIZES[cfg['rgb_type']]))
dummy_flow = np.zeros((self.window_size-1, FEATURE_SIZES[cfg['flow_type']]))
for vid in self.vids:
target = np.load(osp.join(self.root_path, self.annotation_type, vid + '.npy'))
rgb = np.load(osp.join(self.root_path, self.rgb_type, vid + '.npy'))
flow = np.load(osp.join(self.root_path, self.flow_type, vid + '.npy'))
if self.training:
self.target_all[vid] = np.concatenate((dummy_target, target), axis=0)
self.rgb_inputs[vid] = np.concatenate((dummy_rgb, rgb), axis=0)
self.flow_inputs[vid] = np.concatenate((dummy_flow, flow), axis=0)
else:
self.target_all[vid] = target
self.rgb_inputs[vid] = rgb
self.flow_inputs[vid] = flow
def _init_features(self):
del self.inputs
gc.collect()
self.inputs = []
for vid in self.vids:
target = self.target_all[vid]
if self.training:
seed = np.random.randint(self.stride)
for start, end in zip(range(seed, target.shape[0], self.stride),
range(seed + self.window_size, target.shape[0]-self.anticipation_length, self.stride)):
self.inputs.append([
vid, start, end, target[start:end], target[end:end+self.anticipation_length]
])
else:
start = 0
end = target.shape[0] - self.anticipation_length
ant_target = []
for s in range(0, target.shape[0]-self.anticipation_length):
ant_target.append(target[s:s+self.anticipation_length])
self.inputs.append([
vid, start, end, target[start:end], np.array(ant_target)
])
def __getitem__(self, index):
vid, start, end, target, ant_target = self.inputs[index]
rgb_input = self.rgb_inputs[vid][start:end]
flow_input = self.flow_inputs[vid][start:end]
rgb_input = torch.tensor(rgb_input.astype(np.float32))
flow_input = torch.tensor(flow_input.astype(np.float32))
target = torch.tensor(target.astype(np.float32))
ant_target = torch.tensor(ant_target.astype(np.float32))
return rgb_input, flow_input, target, ant_target
def __len__(self):
return len(self.inputs)
@DATA_LAYERS.register("FINEACTION")
class FINEACTIONDataset(data.Dataset):
def __init__(self, cfg, mode='train'):
self.root_path = cfg['root_path']
self.mode = mode
self.training = mode == 'train'
self.window_size = cfg['window_size']
self.stride = cfg['stride']
data_name = cfg['data_name']
self.vids = json.load(open(cfg['video_list_path']))[data_name][mode + '_session_set'] # list of video names
self.num_classes = cfg['num_classes']
self.inputs = []
self._load_features(cfg)
self._init_features()
def _load_features(self, cfg):
self.annotation_type = cfg['annotation_type']
self.rgb_type = cfg['rgb_type']
self.flow_type = cfg['flow_type']
def _init_features(self, seed=0):
# self.inputs = []
del self.inputs
gc.collect()
self.inputs = []
for vid in self.vids:
target = np.load(osp.join(self.root_path, self.annotation_type, vid + '.npy'))
if self.training:
seed = np.random.randint(self.stride)
for start, end in zip(range(seed, target.shape[0], self.stride),
range(seed + self.window_size, target.shape[0]+1, self.stride)):
self.inputs.append([
vid, start, end
])
else:
start = 0
end = target.shape[0]
self.inputs.append([
vid, start, end
])
def __getitem__(self, index):
vid, start, end = self.inputs[index]
rgb_input = np.load(osp.join(self.root_path, self.rgb_type, vid + '.npy'), mmap_mode='r')[start:end]
flow_input = np.load(osp.join(self.root_path, self.flow_type, vid + '.npy'), mmap_mode='r')[start:end]
target = np.load(osp.join(self.root_path, self.annotation_type, vid + '.npy'), mmap_mode='r')[start:end]
rgb_input = torch.tensor(rgb_input.astype(np.float32))
flow_input = torch.tensor(flow_input.astype(np.float32))
target = torch.tensor(target.astype(np.float32))
return rgb_input, flow_input, target
def __len__(self):
return len(self.inputs)