forked from ermongroup/tile2vec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprep_labels.py
61 lines (43 loc) · 1.44 KB
/
prep_labels.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
# !/usr/bin/env python
"""
prep_labels.py
!! Seems to be correct, compared to third_party scripts
"""
import os
import re
import json
import pickle
from glob import glob
from tqdm import tqdm
# --
# Load lookups
label_indices = json.load(open('/home/bjohnson/projects/benet/third_party/bigearthnet-19-models/label_indices.json'))
label_conversion = label_indices['label_conversion']
new2idx = label_indices['BigEarthNet-19_labels']
old2idx = label_indices['original_labels']
idx2new = {v: k for k, v in new2idx.items()}
old2new = {}
for new, olds in enumerate(label_conversion):
for old in olds:
old2new[old] = new
# --
# Helpers
def convert_labels(patch_dir):
patch_name = os.path.basename(patch_dir)
meta_path = os.path.join(patch_dir, patch_name + '_labels_metadata.json')
meta = json.load(open(meta_path))
labs = meta['labels']
old_idxs = [old2idx[l] for l in labs]
new_idxs = [old2new[i] for i in old_idxs if i in old2new]
return sorted(list(set(new_idxs)))
# --
# Run
indir = '/raid/users/bjohnson/data/bigearthnet/bigearthnet/'
outpath = '/raid/users/ebarnett/bigearthnet/'
patch_dirs = sorted(glob(os.path.join(indir, '*')))
patch2labs = {}
for patch_dir in tqdm(patch_dirs):
patch_name = os.path.basename(patch_dir)
patch2labs[patch_name] = convert_labels(patch_dir)
if not os.path.isdir(outpath): os.mkdir(outpath)
pickle.dump(patch2labs, open(os.path.join(outpath, 'labels_ben_19.pkl'), 'wb'))