-
Notifications
You must be signed in to change notification settings - Fork 20
/
create_bdd_dataset.py
163 lines (136 loc) · 4.18 KB
/
create_bdd_dataset.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import numpy as np
import scipy
import scipy.io as sio
import scipy.misc
from scipy.misc import imread, imsave
import matplotlib
import matplotlib.pyplot as plt
import json
import os
import os.path
from tqdm import tqdm
# replace the colors with our colors
#a = sio.loadmat("data_ADE/color150.mat")
# print(a)
colors = np.array(
[0, # road
1, #sidewalk
2, # building
3, # wall
4, # fence
5, # pole
6, # traffic light
7, # traffic sign
8, # vegetation
9, # terrain
10, # sky
11, # person
12, # rider
13, # car
14, # truck
15, # bus
16, # train
17, # motorcycle
18, # bicycle
255,]) # other
#swap 255 with -1
# add 2 to whole array
#a["colors"] = colors
# print(a)
#sio.savemat("data/color150.mat", a)
#####
#create the train and val obgt
## To view the structure of their obgt file uncomment
## the lines below
# odgt = "data_ADE/train.odgt"
#
# with open(odgt) as fp:
# a = json.loads(fp.read())
# print(a, type(a))
#
# a = [json.loads(x.rstrip()) for x in open(odgt, 'r')]
# print(a, type(a), type(a[0]), len(a), "\n\n", a[0])
def create_odgt(root_dir, file_dir, ann_dir, out_dir, anom_files=None):
if anom_files is None:
anom_files = []
_files = []
count1 = 0
count2 = 0
img_files = sorted(os.listdir(root_dir+file_dir))
for img in img_files:
#print(img, img[-5])
# this line is because all of train images
# are saved as "type5.png"
#ann_file = img[:-5] + "5" + img[-4:]
ann_file = img[:-4] + "_train_id.png"
#print(ann_file)
ann_file_path = root_dir+ann_dir+ann_file
if os.path.exists(ann_file_path):
#print("exists")
dict_entry = {
"dbName": "BDD100k",
"width": 1280,
"height": 720,
"fpath_img": file_dir+img,
"fpath_segm": ann_dir+ann_file,
}
img = imread(ann_file_path)
cond1 = np.logical_or((img == 18), (img == 19) )
if np.any(np.logical_or( cond1, (img == 20) )):
count2 += 1
anom_files.append(dict_entry)
else:
count1 += 1
_files.append(dict_entry)
print("total images in = {} and out = {}".format(count1, count2))
with open(out_dir, "w") as outfile:
json.dump(_files, outfile)
with open(root_dir + "anom.odgt", "w") as outfile:
json.dump(anom_files, outfile)
# for i in training_files:
# json.dumps(i, outfile)
return anom_files
#do train first
out_dir = "data/train.odgt"
root_dir = "data/"
train_dir = "seg/images/train/"
ann_dir = "seg/train_labels/train/"
anom_files = create_odgt(root_dir, train_dir, ann_dir, out_dir)
out_dir = "data/validation.odgt"
root_dir = "data/"
train_dir = "seg/images/val/"
ann_dir = "seg/train_labels/val/"
create_odgt(root_dir, train_dir, ann_dir, out_dir, anom_files=anom_files)
# sanity check to make sure it can be loaded back
#a = [json.loads(x.rstrip()) for x in open(odgt, 'r')]
# print(a)
# print(a, type(a), type(a[0]), len(a[0]), "\n\n",)
### convert annotation images to correct labels
def convert_cityscapes_to_uint(root_dir, ann_dir):
count = 0
for img_loc in tqdm(os.listdir(root_dir+ann_dir)):
img = imread(root_dir+ann_dir+img_loc)
if img.ndim <= 1:
continue
#img = img[:,:,:3]
#print(img.shape, img[0],)
#swap 255 with -1
# add 2 to whole array
loc = img == 255
img[loc] = -1
img += 2
# plt.imshow(new_img)
# plt.show()
#imsave(root_dir+ann_dir+img_loc, new_img) # SCIPY RESCALES from 0-255 on its own
scipy.misc.toimage(img, cmin=0, cmax=255).save(root_dir+ann_dir+img_loc)
root_dir = "data/"
ann_dir = "seg/train_labels/train/"
# convert the training images
#convert_cityscapes_to_uint(root_dir, ann_dir)
root_dir = "data/"
ann_dir = "seg/train_labels/val/"
# convert the anomaly images
#convert_cityscapes_to_uint(root_dir, ann_dir)
#convert the val images
#ann_dir = "annotations/validation/"
#convert_cityscapes_to_uint(root_dir, ann_dir)