-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
157 lines (137 loc) · 4 KB
/
utils.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
import subprocess
import numpy as np
import os
import sys
def get_kitti_attributes():
labels = {
1: "outlier",
10: "car",
11: "bicycle",
13: "bus",
15: "motorcycle",
16: "on-rails",
18: "truck",
20: "other-vehicle",
30: "person",
31: "bicyclist",
32: "motorcyclist",
40: "road",
44: "parking",
48: "sidewalk",
49: "other-ground",
50: "building",
51: "fence",
52: "other-structure",
60: "lane-marking",
70: "vegetation",
71: "trunk",
72: "terrain",
80: "pole",
81: "traffic-sign",
99: "other-object",
252: "moving-car",
253: "moving-bicyclist",
254: "moving-person",
255: "moving-motorcyclist",
256: "moving-on-rails",
257: "moving-bus",
258: "moving-truck",
259: "moving-other-vehicle",
}
color_map = {
0: [0, 0, 0],
1: [0, 0, 255],
10: [245, 150, 100],
11: [245, 230, 100],
13: [250, 80, 100],
15: [150, 60, 30],
16: [255, 0, 0],
18: [180, 30, 80],
20: [255, 0, 0],
30: [30, 30, 255],
31: [200, 40, 255],
32: [90, 30, 150],
40: [255, 0, 255],
44: [255, 150, 255],
48: [75, 0, 75],
49: [75, 0, 175],
50: [0, 200, 255],
51: [50, 120, 255],
52: [0, 150, 255],
60: [170, 255, 150],
70: [0, 175, 0],
71: [0, 60, 135],
72: [80, 240, 150],
80: [150, 240, 255],
81: [0, 0, 255],
99: [255, 255, 50],
252: [245, 150, 100],
256: [255, 0, 0],
253: [200, 40, 255],
254: [30, 30, 255],
255: [90, 30, 150],
257: [250, 80, 100],
258: [180, 30, 80],
259: [255, 0, 0],
}
categories = [
{"id": key, "name": value, "color": color_map[key]}
for (key, value) in labels.items()
]
task_attributes = {
"format_version": "0.1",
"categories": categories,
}
return task_attributes
def run_model(dataset_path, output_path):
dataset_path = os.path.abspath(dataset_path)
output_path = os.path.abspath(output_path)
model_path = os.path.abspath("SSGV3-53")
script_path = os.path.abspath("SqueezeSegV3/src/tasks/semantic/demo.py")
wd_path = os.path.abspath("SqueezeSegV3/src/tasks/semantic")
subprocess.call(
[
"python",
script_path,
"-m",
model_path,
"-l",
output_path,
"-d",
dataset_path,
],
cwd=wd_path,
)
def get_prediction(path):
label = np.fromfile(path, dtype=np.uint32)
label = label.reshape((-1))
return label_kitti_to_segments(label)
def label_kitti_to_segments(label):
instance_ids = (
label >> 16
) # shift 16 bits to the right to get the upper half for instances
category_ids = label & 0xFFFF # get lower half for semantics
unique_cats = np.unique(category_ids)
unique_instances, indices = np.unique(instance_ids, return_index=True)
instances_cats = [category_ids[indices[i]] for i in range(len(unique_instances))]
annotations = []
instance_id = 1
cat_map = {0: 0}
for cat in unique_cats:
if cat == 0:
continue
for i, instances_cat in enumerate(instances_cats):
if instances_cat == cat and unique_instances[i] > 0:
annotations.append(
{"id": int(unique_instances[i]), "category_id": int(instances_cat)}
)
while instance_id in unique_instances:
instance_id += 1
annotations.append({"id": instance_id, "category_id": int(cat)})
cat_map[cat] = instance_id
instance_id += 1
point_annotations = [
cat_map[category_ids[i]] if id == 0 else int(id)
for i, id in enumerate(instance_ids)
]
return annotations, point_annotations