-
Notifications
You must be signed in to change notification settings - Fork 13
/
kinetics.py
242 lines (199 loc) · 8.1 KB
/
kinetics.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
import torch
import torchvision
import torch.utils.data as data
from PIL import Image
import os
import math
import functools
import json
import copy
import numpy as np
def load_value_file(file_path):
with open(file_path, 'r') as input_file:
value = float(input_file.read().rstrip('\n\r'))
return value
def pil_loader(path):
with open(path, 'rb') as f:
with Image.open(f) as img:
return img.convert('RGB')
def accimage_loader(path):
try:
import accimage
return accimage.Image(path)
except IOError:
# Potentially a decoding problem, fall back to PIL.Image
return pil_loader(path)
def get_default_image_loader():
torchvision.set_image_backend('accimage')
from torchvision import get_image_backend
if get_image_backend() == 'accimage':
return accimage_loader
else:
return pil_loader
def video_loader(video_dir_path, frame_indices, image_loader):
video = []
for i in frame_indices:
image_path = os.path.join(video_dir_path, 'frame_{:05d}.jpg'.format(i))
if os.path.exists(image_path):
video.append(image_loader(image_path))
else:
return video
return video
def get_default_video_loader():
image_loader = get_default_image_loader()
return functools.partial(video_loader, image_loader=image_loader)
def load_annotation_data(data_file_path):
with open(data_file_path, 'r') as data_file:
return json.load(data_file)
def get_class_labels(data):
class_labels_map = {}
index = 0
data = open(data).read().splitlines()
for class_label in data: #['labels']
class_labels_map[class_label] = index
index += 1
return class_labels_map
def get_video_names_and_annotations(data, subset):
video_names = []
annotations = []
for key, value in data.items():
this_subset = value['subset']
if this_subset == subset:
if subset == 'testing':
video_names.append('test/{}'.format(key))
elif subset == 'train':
st = int(value['annotations']['segment'][0])
end = int(value['annotations']['segment'][1])
label = value['annotations']['label'].replace(' ','_')
video_names.append('{}/{}_{}_{}'.format(label, key, str(st).zfill(6), str(end).zfill(6)))
annotations.append(value['annotations'])
else:
label = value['annotations']['label'].replace(' ','_')
video_names.append('{}/{}'.format(label, key))
annotations.append(value['annotations'])
return video_names, annotations
def make_dataset(root_path, annotation_path, class_labels, subset, n_samples_for_each_video, sample_duration):
data = load_annotation_data(annotation_path)
video_names, annotations = get_video_names_and_annotations(data, subset)
class_to_idx = get_class_labels(class_labels)
idx_to_class = {}
for name, label in class_to_idx.items():
idx_to_class[label] = name
pre_saved_dataset = os.path.join(root_path, 'labeldata_80.npy')
if os.path.exists(pre_saved_dataset):
print('{} exists'.format(pre_saved_dataset))
dataset = np.load(pre_saved_dataset, allow_pickle=True)
else:
dataset = []
na = 0
for i in range(len(video_names)):
if i % 1000 == 0:
print('dataset loading [{}/{}] N/A {}'.format(i, len(video_names), na))
video_path = os.path.join(root_path, video_names[i])
if not os.path.exists(video_path):
na += 1
continue
n_frames = len(os.listdir(video_path))
if n_frames <= 80+1:
na += 1
continue
begin_t = 1
end_t = n_frames
sample = {
'video': video_path,
'segment': [begin_t, end_t],
'n_frames': n_frames,
'video_id': video_names[i].split('/')[1]
}
if len(annotations) != 0:
sample['label'] = class_to_idx[annotations[i]['label']]
else:
sample['label'] = -1
if n_samples_for_each_video == 1:
sample['frame_indices'] = list(range(1, n_frames + 1))
dataset.append(sample)
else:
if n_samples_for_each_video > 1:
step = max(1,
math.ceil((n_frames - 1 - sample_duration) /
(n_samples_for_each_video - 1)))
else:
step = sample_duration
for j in range(1, n_frames, step):
sample_j = copy.deepcopy(sample)
sample_j['frame_indices'] = list(
range(j, min(n_frames + 1, j + sample_duration)))
dataset.append(sample_j)
np.save(pre_saved_dataset, dataset)
return dataset, idx_to_class
class Kinetics(data.Dataset):
"""
Args:
root (string): Root directory path.
spatial_transform (callable, optional): A function/transform that takes in an PIL image
and returns a transformed version. E.g, ``transforms.RandomCrop``
temporal_transform (callable, optional): A function/transform that takes in a list of frame indices
and returns a transformed version
target_transform (callable, optional): A function/transform that takes in the
target and transforms it.
loader (callable, optional): A function to load an video given its path and frame indices.
Attributes:
classes (list): List of the class names.
class_to_idx (dict): Dict with items (class_name, class_index).
imgs (list): List of (image path, class_index) tuples
"""
def __init__(self,
root_path,
annotation_path,
class_labels,
subset,
n_samples_for_each_video=1,
spatial_transform=None,
temporal_transform=None,
target_transform=None,
sample_duration=16,
gamma_tau=5,
crops=10,
get_loader=get_default_video_loader):
self.data, self.class_names = make_dataset(
root_path, annotation_path, class_labels, subset, n_samples_for_each_video,
sample_duration)
self.spatial_transform = spatial_transform
self.temporal_transform = temporal_transform
self.target_transform = target_transform
self.loader = get_loader()
self.gamma_tau = gamma_tau
self.crops = crops
self.sample_duration = sample_duration
self.frames = sample_duration//gamma_tau
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is class_index of the target class.
"""
path = self.data[index]['video']
frame_indices = self.data[index]['frame_indices']
#if self.temporal_transform is not None:
# frame_indices = self.temporal_transform(frame_indices)
# FOR MULTI-CROP TESTING
frame_indices = frame_indices[::self.gamma_tau]
step = int((len(frame_indices) - 1 - self.frames)//(self.crops-1))
clip = self.loader(path, frame_indices)
if self.spatial_transform is not None:
self.spatial_transform.randomize_parameters()
clip = [self.spatial_transform(img) for img in clip]
clip = torch.stack(clip, 0).permute(1, 0, 2, 3) # T C H W --> C T H W
if step == 0:
clips = [clip[:,:self.frames,...] for i in range(self.crops)]
clips = torch.stack(clips, 0)
else:
clips = [clip[:,i:i+self.frames,...] for i in range(0, step*self.crops, step)]
clips = torch.stack(clips, 0)
target = self.data[index]
if self.target_transform is not None:
target = self.target_transform(target)
return clips, target
def __len__(self):
return len(self.data)