/
postprocess.py
239 lines (191 loc) · 8.48 KB
/
postprocess.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import numpy as np
from utils import detect_peaks
from utils.mappings import confmap2ra
from utils.ols import get_ols_btw_objects
from utils.visualization import visualize_postprocessing
from config import class_ids, class_table, n_class
from config import rodnet_configs, radar_configs
def search_surround(peak_conf, row, col, conf_valu, search_size):
height = peak_conf.shape[0]
width = peak_conf.shape[1]
half_size = int((search_size - 1) / 2)
row_start = max(half_size, row - half_size)
row_end = min(height - half_size - 1, row + half_size)
col_start = max(half_size, col - half_size)
col_end = min(width - half_size - 1, col + half_size)
# print(row_start)
No_bigger = True
for i in range(row_start, row_end + 1):
for j in range(col_start, col_end + 1):
if peak_conf[i, j] > conf_valu:
# current conf is not big enough, skip this peak
No_bigger = False
break
return No_bigger, [row_start, row_end, col_start, col_end]
def peak_mapping(peak_conf, peak_class, list_row, list_col, confmap, search_size, o_class):
for i in range(len(list_col)):
row_id = list_row[i]
col_id = list_col[i]
conf_valu = confmap[row_id, col_id]
flag, indices = search_surround(peak_conf, row_id, col_id, conf_valu, search_size)
if flag:
# clear all detections in search window
search_width = indices[1] - indices[0] + 1
search_height = indices[3] - indices[2] + 1
peak_conf[indices[0]:indices[1]+1, indices[2]:indices[3]+1] = np.zeros((search_width, search_height))
peak_class[indices[0]:indices[1]+1, indices[2]:indices[3]+1] = - np.ones((search_width, search_height))
# write the detected objects to matrix
peak_conf[row_id, col_id] = conf_valu
peak_class[row_id, col_id] = class_ids[o_class]
return peak_conf, peak_class
def find_greatest_points(peak_conf, peak_class):
detect_mat = - np.ones((rodnet_configs['max_dets'], 4))
height = peak_conf.shape[0]
width = peak_conf.shape[1]
peak_flatten = peak_conf.flatten()
indic = np.argsort(peak_flatten)
ind_len = indic.shape[0]
if ind_len >= rodnet_configs['max_dets']:
choos_ind = np.flip(indic[-rodnet_configs['max_dets']:ind_len])
else:
choos_ind = np.flip(indic)
for count, ele_ind in enumerate(choos_ind):
row = ele_ind // width
col = ele_ind % width
if peak_conf[row, col] > 0:
detect_mat[count, 0] = peak_class[row, col]
detect_mat[count, 1] = row
detect_mat[count, 2] = col
detect_mat[count, 3] = peak_conf[row, col]
return detect_mat
def post_processing(confmaps, peak_thres=0.1):
"""
Post-processing for RODNet
:param confmaps: predicted confidence map [B, n_class, win_size, ramap_r, ramap_a]
:param search_size: search other detections within this window (resolution of our system)
:param peak_thres: peak threshold
:return: [B, win_size, max_dets, 4]
"""
# print(confmaps.shape)
batch_size = confmaps.shape[0]
class_size = confmaps.shape[1]
win_size = confmaps.shape[2]
height = confmaps.shape[3]
width = confmaps.shape[4]
if class_size != n_class:
raise TypeError("Wrong class number setting. ")
max_dets = rodnet_configs['max_dets']
rng_grid = confmap2ra(radar_configs, 'range')
agl_grid = confmap2ra(radar_configs, 'angle')
res_final = - np.ones((batch_size, win_size, max_dets, 4))
for b in range(batch_size):
for w in range(win_size):
detect_mat = []
for c in range(class_size):
obj_dicts_in_class = []
confmap = np.squeeze(confmaps[b, c, w, :, :])
# detect peak
rowids, colids = detect_peaks(confmap, threshold=peak_thres)
for ridx, aidx in zip(rowids, colids):
rng = rng_grid[ridx]
agl = agl_grid[aidx]
conf = confmap[ridx, aidx]
obj_dict = {'frameid': None, 'range': rng, 'angle': agl, 'ridx': ridx, 'aidx': aidx,
'classid': c, 'score': conf}
obj_dicts_in_class.append(obj_dict)
detect_mat_in_class = lnms(obj_dicts_in_class)
detect_mat.append(detect_mat_in_class)
detect_mat = np.array(detect_mat)
detect_mat = np.reshape(detect_mat, (class_size*max_dets, 4))
detect_mat = detect_mat[detect_mat[:, 3].argsort(kind='mergesort')[::-1]]
res_final[b, w, :, :] = detect_mat[:max_dets]
return res_final
def post_processing_single_timestamp(confmaps, peak_thres=0.1):
"""
Post-processing for RODNet
:param confmaps: predicted confidence map [B, n_class, win_size, ramap_r, ramap_a]
:param search_size: search other detections within this window (resolution of our system)
:param peak_thres: peak threshold
:return: [B, win_size, max_dets, 4]
"""
class_size = confmaps.shape[0]
height = confmaps.shape[1]
width = confmaps.shape[2]
if class_size != n_class:
raise TypeError("Wrong class number setting. ")
max_dets = rodnet_configs['max_dets']
rng_grid = confmap2ra(radar_configs, 'range')
agl_grid = confmap2ra(radar_configs, 'angle')
res_final = - np.ones((max_dets, 4))
detect_mat = []
for c in range(class_size):
obj_dicts_in_class = []
confmap = confmaps[c, :, :]
# detect peak
rowids, colids = detect_peaks(confmap, threshold=peak_thres)
for ridx, aidx in zip(rowids, colids):
rng = rng_grid[ridx]
agl = agl_grid[aidx]
conf = confmap[ridx, aidx]
obj_dict = {'frameid': None, 'range': rng, 'angle': agl, 'ridx': ridx, 'aidx': aidx,
'classid': c, 'score': conf}
obj_dicts_in_class.append(obj_dict)
detect_mat_in_class = lnms(obj_dicts_in_class)
detect_mat.append(detect_mat_in_class)
detect_mat = np.array(detect_mat)
detect_mat = np.reshape(detect_mat, (class_size*max_dets, 4))
detect_mat = detect_mat[detect_mat[:, 3].argsort(kind='mergesort')[::-1]]
res_final[:, :] = detect_mat[:max_dets]
return res_final
def lnms(obj_dicts_in_class):
detect_mat = - np.ones((rodnet_configs['max_dets'], 4))
cur_det_id = 0
# sort peaks by confidence score
inds = np.argsort([-d['score'] for d in obj_dicts_in_class], kind='mergesort')
dts = [obj_dicts_in_class[i] for i in inds]
while len(dts) != 0:
if cur_det_id >= rodnet_configs['max_dets']:
break
p_star = dts[0]
detect_mat[cur_det_id, 0] = p_star['classid']
detect_mat[cur_det_id, 1] = p_star['ridx']
detect_mat[cur_det_id, 2] = p_star['aidx']
detect_mat[cur_det_id, 3] = p_star['score']
cur_det_id += 1
del dts[0]
for pid, pi in enumerate(dts):
ols = get_ols_btw_objects(p_star, pi)
if ols > rodnet_configs['ols_thres']:
del dts[pid]
return detect_mat
def write_dets_results(res, data_id, save_path):
# batch_size = 1 when testing
batch_size = 1
with open(save_path, 'a+') as f:
for b in range(batch_size):
for w in range(rodnet_configs['win_size']):
for d in range(rodnet_configs['max_dets']):
cla_id = int(res[b, w, d, 0])
if cla_id == -1:
continue
row_id = res[b, w, d, 1]
col_id = res[b, w, d, 2]
conf = res[b, w, d, 3]
f.write("%010d %s %d %d %s\n" % (data_id+w, class_table[cla_id], row_id, col_id, conf))
def write_dets_results_single_timestamp(res, data_id, save_path):
with open(save_path, 'a+') as f:
for d in range(rodnet_configs['max_dets']):
cla_id = int(res[d, 0])
if cla_id == -1:
continue
row_id = res[d, 1]
col_id = res[d, 2]
conf = res[d, 3]
f.write("%010d %s %d %d %s\n" % (data_id, class_table[cla_id], row_id, col_id, conf))
if __name__ == "__main__":
input_test = np.random.random_sample((1, 3, 16, 122, 91))
res_final = post_processing(input_test)
for b in range(1):
for w in range(16):
confmaps = np.squeeze(input_test[b, :, w, :, :])
visualize_postprocessing(confmaps, res_final[b, w, :, :])