-
Notifications
You must be signed in to change notification settings - Fork 1
/
make_index.py
173 lines (134 loc) · 5.57 KB
/
make_index.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
import argparse
import json
from pathlib import Path
import numpy as np
from PIL import Image
from tqdm import tqdm
DAVIS16_TRAIN_VAL_ROOT = '../datasets/DAVIS2016/'
DAVIS17_TRAIN_VAL_ROOT = '../datasets/DAVIS2017/'
DAVIS17_TEST_ROOT = '../datasets/DAVIS2017_test/'
YOUTUBE_ROOT = '../datasets/YouTubeVOS/'
def index_youtube(split):
assert split in ['train', 'test']
if split == 'train':
dataset_root = Path(YOUTUBE_ROOT) / 'train'
else:
dataset_root = Path(YOUTUBE_ROOT) / 'valid'
assert dataset_root.is_dir()
video_root = dataset_root / 'JPEGImages'
assert video_root.is_dir()
label_root = dataset_root / 'Annotations'
assert label_root.is_dir()
meta_path = dataset_root / 'meta.json'
assert meta_path.is_file()
with open(meta_path) as f:
meta = json.load(f)
split_info = []
for video_name, video in tqdm(meta['videos'].items()):
for object_index, obj in video['objects'].items():
mask_path = None
for frame_index, frame in enumerate(obj['frames']):
object_index = int(object_index)
image_path = video_root / video_name / f'{frame}.jpg'
if split != 'test' or frame_index == 0:
mask_path = label_root / video_name / f'{frame}.png'
num_frames = len(obj['frames'])
video_info = dict(image_path=str(image_path.resolve()),
mask_path=str(mask_path.resolve()),
video_name=video_name,
num_frames=num_frames,
frame_index=frame_index,
object_index=object_index)
split_info.append(video_info)
return split_info
def index_davis(video_root, label_root, split, video_names):
split_info = []
for video_name in tqdm(video_names):
video_dir = video_root / video_name
assert video_dir.is_dir()
label_dir = label_root / video_name
assert label_dir.is_dir()
img_paths = sorted(video_dir.glob('*.jpg'))
mask_paths = sorted(label_dir.glob('*.png'))
if split == 'test':
assert len(mask_paths) == 1
mask_paths *= len(img_paths)
else:
assert len(img_paths) == len(mask_paths)
assert [f.stem for f in img_paths] == [f.stem for f in mask_paths]
mask_first = np.array(Image.open(mask_paths[0]), dtype=np.uint8)
object_indices = [int(x) for x in np.unique(mask_first) if x != 0]
num_frames = len(img_paths)
for object_index in object_indices:
video_info = [dict(image_path=str(image_path.resolve()),
mask_path=str(mask_path.resolve()),
video_name=video_name,
num_frames=num_frames,
frame_index=frame_index,
object_index=object_index)
for frame_index, (image_path, mask_path) in enumerate(zip(img_paths, mask_paths))]
split_info += video_info
return split_info
def index_davis17(split):
assert split in ['train', 'val', 'test']
if split == 'test':
dataset_root = Path(DAVIS17_TEST_ROOT)
else:
dataset_root = Path(DAVIS17_TRAIN_VAL_ROOT)
assert dataset_root.is_dir()
meta_name_map = {
'train': 'train.txt',
'val': 'val.txt',
'test': 'test-dev.txt'
}
meta_path = dataset_root / 'ImageSets/2017' / meta_name_map[split]
with open(meta_path) as f:
video_names = f.read().split()
video_root = dataset_root / 'JPEGImages/480p'
label_root = dataset_root / 'Annotations/480p'
return index_davis(video_root, label_root, split, video_names)
def index_davis16(split):
assert split in ['train', 'val']
dataset_root = Path(DAVIS16_TRAIN_VAL_ROOT)
assert dataset_root.is_dir()
video_root = dataset_root / 'JPEGImages' / '480p'
assert video_root.is_dir()
label_root = dataset_root / 'Annotations' / '480p'
assert label_root.is_dir()
meta_path = dataset_root / 'ImageSets' / '480p' / f'{split}.txt'
assert meta_path.is_file()
with open(meta_path) as f:
lines = f.readlines()
video_names = set()
for line in lines:
img_path, _ = line.split()
video_names.add(Path(img_path).parent.name)
return index_davis(video_root, label_root, split, video_names)
def index_dataset(dataset, split, out_dir):
assert dataset in ['davis2016', 'davis2017', 'youtube']
print(f'Indexing {dataset}.{split}')
if dataset == 'davis2016':
split_info = index_davis16(split)
elif dataset == 'davis2017':
split_info = index_davis17(split)
elif dataset == 'youtube':
split_info = index_youtube(split)
else:
assert False
out_dir = Path(out_dir)
out_dir.mkdir(parents=True, exist_ok=True)
with open(out_dir / f'{dataset}_{split}.json', 'w') as f:
json.dump(split_info, f, indent=4)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--out-dir', type=str, default='../splits')
args = parser.parse_args()
index_dataset('davis2016', 'train', args.out_dir)
index_dataset('davis2016', 'val', args.out_dir)
index_dataset('davis2017', 'train', args.out_dir)
index_dataset('davis2017', 'val', args.out_dir)
index_dataset('davis2017', 'test', args.out_dir)
index_dataset('youtube', 'train', args.out_dir)
index_dataset('youtube', 'test', args.out_dir)
if __name__ == '__main__':
main()