forked from hendrycks/anomaly-seg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_dataset.py
161 lines (131 loc) · 4.47 KB
/
create_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
import numpy as np
import scipy
import scipy.io as sio
import scipy.misc
from PIL import Image
import matplotlib
import matplotlib.pyplot as plt
import json
import os
import os.path
from tqdm import tqdm
# Replace the colors with our colors
# This is only used for visualization purposes
#color_mat = sio.loadmat("data_ADE/color150.mat")
#StreetHazards colors
#colors = np.array([[ 0, 0, 0],# // unlabeled = 0,
# [ 70, 70, 70],# // building = 1,
# [190, 153, 153],# // fence = 2,
# [250, 170, 160],# // other = 3,
# [220, 20, 60],# // pedestrian = 4,
# [153, 153, 153],# // pole = 5,
# [157, 234, 50],# // road line = 6,
# [128, 64, 128],# // road = 7,
# [244, 35, 232],# // sidewalk = 8,
# [107, 142, 35],# // vegetation = 9,
# [ 0, 0, 142],# // car = 10,
# [102, 102, 156],# // wall = 11,
# [220, 220, 0],# // traffic sign = 12,
# [ 60, 250, 240],# // anomaly = 13,
#
# ])
#color_mat["colors"] = colors
#sio.savemat("data/color150.mat", color_mat)
#####
#create the train and val obgt
def create_odgt(root_dir, file_dir, ann_dir, out_dir, anom_files=None):
if anom_files is None:
anom_files = []
_files = []
count = total = 0
town_names = sorted(os.listdir(root_dir+file_dir))
for town in town_names:
img_files = sorted(os.listdir(os.path.join(root_dir,file_dir,town)))
total += len(img_files)
for img in img_files:
ann_file = img
ann_file_path = os.path.join(root_dir,ann_dir,town,ann_file)
if os.path.exists(ann_file_path):
dict_entry = {
"dbName": "StreetHazards",
"width": 1280,
"height": 720,
"fpath_img": os.path.join(file_dir,town,img),
"fpath_segm": os.path.join(ann_dir,town,ann_file),
}
# If converting BDD100K uncomment out the following
#img = Image.open(ann_file_path)
#if np.any(np.logical_or( (img == 19), (img == 20) )):
# anom_files.append(dict_entry)
#else:
count += 1
_files.append(dict_entry)
print("total images in = {} and out = {}".format(total, count))
with open(out_dir, "w") as outfile:
json.dump(_files, outfile)
# If converting BDD100K uncomment out the following
#with open(root_dir + "anom.odgt", "w") as outfile:
# json.dump(anom_files, outfile)
return anom_files
out_dir = "data/train.odgt"
#modify root directory to reflect the location of where the streethazards_train was extracted to.
root_dir = "data/train/"
train_dir = "images/training"
ann_dir = "annotations/training/"
anom_files = create_odgt(root_dir, train_dir, ann_dir, out_dir)
out_dir = "data/validation.odgt"
train_dir = "images/validation/"
ann_dir = "annotations/validation/"
create_odgt(root_dir, train_dir, ann_dir, out_dir, anom_files=anom_files)
out_dir = "data/test.odgt"
val_dir = "images/test/"
ann_dir = "annotations/test/"
create_odgt(root_dir, val_dir, ann_dir, out_dir)
# BDD100K label map
#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
### convert BDD100K semantic segmentation images to correct labels
def convert_bdd(root_dir, ann_dir):
count = 0
for img_loc in tqdm(os.listdir(root_dir+ann_dir)):
img = Image.open(root_dir+ann_dir+img_loc)
if img.ndim <= 1:
continue
#swap 255 with -1
#16 -> 19
#18 -> 16
#19 -> 18
# add 1 to whole array
loc = img == 255
img[loc] = -1
loc = img == 16
img[loc] = 19
loc = img == 18
img[loc] = 16
loc = img == 19
img[loc] = 18
img += 1
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 BDD100K semantic segmentation images.
#convert_bdd(root_dir, ann_dir)