-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdataset.py
145 lines (115 loc) · 5.86 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
import os.path as osp
import pickle
import torch
import numpy as np
from tqdm import tqdm
import torch.utils.data as data
START_INDEX = 2
class TRNTHUMOSDataLayer(data.Dataset):
def __init__(self, args, phase):
self.pickle_root = args.pickle_root
self.numclass = args.numclass
self.training = phase == 'train'
self.feature_pretrain = args.feature
self.inputs = []
self.history_desision = args.history_desision
self.history_feature = args.history_feature
assert self.history_desision == self.history_feature
if self.training:
self.num_videoframes = args.train_num_videoframes
self.stride = args.train_stride
else:
self.num_videoframes = args.test_num_videoframes
self.stride = args.test_stride
self.skip_videoframes = 1
self.subnet = 'val' if phase == 'train' else 'test'
print(f'loading {self.subnet} dataset.......................')
target_all = pickle.load(
open(osp.join(self.pickle_root, f'thumos_{self.subnet}_anno_multiclass.pickle'), 'rb'))
self.sessions = sorted(list(set(target_all.keys())))
feature_file = osp.join(
self.pickle_root, f'thumos_all_feature_{self.subnet}_V3.pickle')
if osp.exists(feature_file):
self.feature_All = pickle.load(open(feature_file, 'rb'))
print(f'load THUMOS feature {feature_file}')
else:
NotImplementedError
self.origin_video_length_dict = dict()
self.track_session = []
self.track_start = []
self.track_indices = []
for session in tqdm(self.sessions):
video_feature_rgb = self.feature_All[session]['rgb']
video_feature_flow = self.feature_All[session]['flow']
video_feature = np.concatenate(
[video_feature_rgb, video_feature_flow], -1)
target = target_all[session]['anno']
# pad feature and target
if len(video_feature) % self.stride != 0:
tmp_data = np.zeros(
(self.stride-len(video_feature) % self.stride, args.dim_feature))
video_feature = np.concatenate(
(video_feature, tmp_data), axis=0)
# extend gt annotation
tmp_data_anno = np.full(
(self.stride-len(video_feature) % self.stride, target.shape[-1]), -1)
target = np.concatenate((target, tmp_data_anno), axis=0)
num_snippet = video_feature.shape[0]
# save original length of video
self.origin_video_length_dict[session] = num_snippet
num_windows = int(
(num_snippet + self.stride - self.num_videoframes) / self.stride)
windows_start = [i * self.stride for i in range(num_windows)]
if num_snippet < self.num_videoframes:
windows_start = [0]
# Add on a bunch of zero data if there aren't enough windows.
tmp_data = np.zeros(
(self.num_videoframes - num_snippet, args.dim_feature))
video_feature = np.concatenate(
(video_feature, tmp_data), axis=0)
# extend gt annotation
tmp_data_anno = np.full(
(self.num_videoframes - num_snippet, target.shape[-1]), -1)
target = np.concatenate((target, tmp_data_anno), axis=0)
elif num_snippet - windows_start[-1] - self.num_videoframes > int(self.num_videoframes / self.skip_videoframes):
windows_start.append(num_snippet - self.num_videoframes)
for start in windows_start:
obser_data = video_feature[start:start + self.num_videoframes]
obser_data = np.array(obser_data).astype(np.float32)
obser_target = target[start:start + self.num_videoframes]
self.inputs.append([obser_data, obser_target, session, start])
self.track_session.append([session]*self.num_videoframes)
self.track_start.append(start)
self.track_indices.append(
np.arange(start, start+self.num_videoframes))
self.sos_cls = torch.full(
(1, self.numclass), 1/(self.numclass-1)).to(torch.float32)
def __getitem__(self, index):
return self.prepare_imgs(index)
def prepare_imgs(self, index):
obser_data, obser_target, session, start = self.inputs[index]
class_h_target = torch.from_numpy(obser_target)
camera_inputs = torch.from_numpy(obser_data)
return (camera_inputs, class_h_target, torch.tensor([index]))
def prepare_training_data(self, index,
all_video_camera_inputs,
all_video_class_h_target):
taken_decision_indices = np.arange(
max(0, index-self.history_desision-1), index+1)
is_start = False
if len(taken_decision_indices) < self.history_feature + 2:
is_start = True
re_camera_inputs = all_video_camera_inputs[:, torch.from_numpy(
taken_decision_indices)]
re_class_h_target = all_video_class_h_target[:, torch.from_numpy(
taken_decision_indices)]
if is_start:
re_camera_inputs = torch.cat(
(re_camera_inputs[:, 0:1], re_camera_inputs), 1)
# the first class is dummy and not used
re_class_h_target = torch.cat((self.sos_cls[None].repeat(
len(re_class_h_target), 1, 1), re_class_h_target), 1)
return (re_camera_inputs, re_class_h_target,
torch.tensor([is_start]*len(all_video_camera_inputs)))
def __len__(self):
return len(self.inputs)