|
import os |
|
import json |
|
import shutil |
|
import numpy as np |
|
|
|
|
|
dir = 'annotations' |
|
excluded = [ |
|
'task_2021-03-01_09', |
|
'task_2021-03-01_10', |
|
'task_20210526_UPS', |
|
'task_20210611_Lab', |
|
'task_20210612_1_Lab', |
|
'task_20210705-07_balacet', |
|
'task_20211204_Orlu', |
|
'task_21-01-2021', |
|
] |
|
|
|
|
|
def iter_files(path, callback): |
|
if os.path.isfile(path): |
|
callback(path) |
|
elif os.path.isdir(path): |
|
for entry in os.scandir(path): |
|
iter_files(entry.path, callback) |
|
|
|
|
|
def merge_files(path): |
|
global merged |
|
for name in excluded: |
|
if name in path: |
|
return |
|
with open(path, 'r') as f: |
|
data = json.load(f) |
|
merged = merged | data |
|
|
|
|
|
n_images = [10, 20, 50, 100, 250, 500] |
|
for i, n in enumerate(n_images): |
|
merged = {} |
|
counts = {} |
|
|
|
with open('annotations/iNatv1.json', 'r') as f: |
|
data = json.load(f) |
|
items = list(data.items()) |
|
np.random.shuffle(items) |
|
for key, value in items: |
|
to_add = True |
|
for box in value['boxes']: |
|
if box['label'] in counts and counts[box['label']] >= n: |
|
to_add = False |
|
break |
|
if to_add: |
|
merged[key] = value |
|
for box in value['boxes']: |
|
counts[box['label']] = counts.get(box['label'], 0) + 1 |
|
|
|
with open(f'sets/DS9.{i}_train.json', 'w') as f: |
|
json.dump(merged, f) |
|
|
|
shutil.copyfile('sets/DS8_val.json', f'sets/DS9.{i}_val.json') |
|
shutil.copyfile('sets/DS8_test.json', f'sets/DS9.{i}_test.json') |
|
|