File size: 1,585 Bytes
4f005f7
 
14aae6f
 
4f005f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14aae6f
 
 
 
4f005f7
14aae6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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')