forked from facebookresearch/classifier-balancing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_lists.py
executable file
·39 lines (31 loc) · 1.04 KB
/
gen_lists.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
"""Copyright (c) Facebook, Inc. and its affiliates.
All rights reserved.
This source code is licensed under the license found in the
LICENSE file in the root directory of this source tree.
Usage:
1. Change "root" to your data path
2. python gen_lists.py
"""
import os
import json
from tqdm import tqdm
root = '/checkpoint/bykang/iNaturalist18'
json2txt = {
'train2018.json': 'iNaturalist18_train.txt',
'val2018.json': 'iNaturalist18_val.txt'
}
def convert(json_file, txt_file):
with open(json_file, 'r') as f:
data = json.load(f)
lines = []
for i in tqdm(range(len(data['images']))):
assert data['images'][i]['id'] == data['annotations'][i]['id']
img_name = data['images'][i]['file_name']
label = data['annotations'][i]['category_id']
lines.append(img_name + ' ' + str(label) + '\n')
with open(txt_file, 'w') as ftxt:
ftxt.writelines(lines)
for k, v in json2txt.items():
print('===> Converting {} to {}'.format(k, v))
srcfile = os.path.join(root, k)
convert(srcfile, v)