-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_split.py
More file actions
48 lines (41 loc) · 1.47 KB
/
data_split.py
File metadata and controls
48 lines (41 loc) · 1.47 KB
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
import sys
import copy
import json
def _convert(images, annotations):
id2img, id2anno = {}, {}
for image in images:
if image["id"] not in id2img:
id2img[image["id"]] = image
for annotation in annotations:
if annotation["image_id"] not in id2anno:
id2anno[annotation["image_id"]] = []
id2anno[annotation["image_id"]].append(annotation)
return id2img, id2anno
if __name__ == "__main__":
meta_path, num_split = sys.argv[1], sys.argv[2]
num_split = int(num_split)
with open(meta_path, "r") as f:
metas = json.loads(f.readlines()[0])
new_metas = copy.deepcopy(metas)
# images, annotations
images = metas["images"]
annotations = metas["annotations"]
print("Loading meta file done.")
id2img, id2anno = _convert(images, annotations)
print("Converting format done.")
new_images, new_annos = [], []
img_ids = list(id2img.keys())
for idx in range(num_split):
img_id = img_ids[idx]
print(f"Processing {img_id} done.")
if img_id not in id2anno:
continue
new_images.append(id2img[img_id])
new_annos.extend(id2anno[img_id])
new_metas["images"] = new_images
new_metas["annotations"] = new_annos
sv_path = meta_path.split(".")[0] + f"_{num_split}s.json"
with open(sv_path, "w") as f:
new_metas_json = json.dumps(new_metas)
f.write(new_metas_json + "\n")
print(f"Saving {sv_path} done.")