forked from mit-han-lab/temporal-shift-module
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gen_label_kinetics.py
executable file
·62 lines (55 loc) · 2.7 KB
/
gen_label_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
# Code for "TSM: Temporal Shift Module for Efficient Video Understanding"
# arXiv:1811.08383
# Ji Lin*, Chuang Gan, Song Han
# {jilin, songhan}@mit.edu, [email protected]
# ------------------------------------------------------
# Code adapted from https://github.com/metalbubble/TRN-pytorch/blob/master/process_dataset.py
import os
dataset_path = '/ssd/video/kinetics/images256/'
label_path = '/ssd/video/kinetics/labels'
if __name__ == '__main__':
with open('kinetics_label_map.txt') as f:
categories = f.readlines()
categories = [c.strip().replace(' ', '_').replace('"', '').replace('(', '').replace(')', '').replace("'", '') for c in categories]
assert len(set(categories)) == 400
dict_categories = {}
for i, category in enumerate(categories):
dict_categories[category] = i
print(dict_categories)
files_input = ['kinetics_val.csv', 'kinetics_train.csv']
files_output = ['val_videofolder.txt', 'train_videofolder.txt']
for (filename_input, filename_output) in zip(files_input, files_output):
count_cat = {k: 0 for k in dict_categories.keys()}
with open(os.path.join(label_path, filename_input)) as f:
lines = f.readlines()[1:]
folders = []
idx_categories = []
categories_list = []
for line in lines:
line = line.rstrip()
items = line.split(',')
folders.append(items[1] + '_' + items[2])
this_catergory = items[0].replace(' ', '_').replace('"', '').replace('(', '').replace(')', '').replace("'", '')
categories_list.append(this_catergory)
idx_categories.append(dict_categories[this_catergory])
count_cat[this_catergory] += 1
print(max(count_cat.values()))
assert len(idx_categories) == len(folders)
missing_folders = []
output = []
for i in range(len(folders)):
curFolder = folders[i]
curIDX = idx_categories[i]
# counting the number of frames in each video folders
img_dir = os.path.join(dataset_path, categories_list[i], curFolder)
if not os.path.exists(img_dir):
missing_folders.append(img_dir)
# print(missing_folders)
else:
dir_files = os.listdir(img_dir)
output.append('%s %d %d'%(os.path.join(categories_list[i], curFolder), len(dir_files), curIDX))
print('%d/%d, missing %d'%(i, len(folders), len(missing_folders)))
with open(os.path.join(label_path, filename_output),'w') as f:
f.write('\n'.join(output))
with open(os.path.join(label_path, 'missing_' + filename_output),'w') as f:
f.write('\n'.join(missing_folders))