Skip to content

Commit fd185bf

Browse files
committed
[fix] add box_process function define
1 parent 3e00b5c commit fd185bf

4 files changed

Lines changed: 96 additions & 0 deletions

File tree

src/rk3576/realtime_detection.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,30 @@ def nms_boxes(boxes, scores):
351351
order = order[inds + 1]
352352
return np.array(keep)
353353

354+
def dfl(position):
355+
n, c, h, w = position.shape
356+
p_num = 4
357+
mc = c // p_num
358+
y = position.reshape(n, p_num, mc, h, w)
359+
y_exp = np.exp(y - np.max(y, axis=2, keepdims=True))
360+
y_softmax = y_exp / np.sum(y_exp, axis=2, keepdims=True)
361+
acc_metrix = np.arange(mc).reshape(1, 1, mc, 1, 1).astype(np.float32)
362+
y = (y_softmax * acc_metrix).sum(2)
363+
return y
364+
365+
def box_process(position):
366+
grid_h, grid_w = position.shape[2:4]
367+
col, row = np.meshgrid(np.arange(0, grid_w), np.arange(0, grid_h))
368+
col = col.reshape(1, 1, grid_h, grid_w)
369+
row = row.reshape(1, 1, grid_h, grid_w)
370+
grid = np.concatenate((col, row), axis=1)
371+
stride = np.array([IMG_SIZE[1]//grid_h, IMG_SIZE[0]//grid_w]).reshape(1,2,1,1)
372+
position = dfl(position)
373+
box_xy = grid +0.5 -position[:,0:2,:,:]
374+
box_xy2 = grid +0.5 +position[:,2:4,:,:]
375+
xyxy = np.concatenate((box_xy*stride, box_xy2*stride), axis=1)
376+
return xyxy
377+
354378
def post_process(input_data):
355379
obj, nms = det_config.get()
356380
return post_process_with_thresh(input_data, obj, nms)

src/rk3576/web_detection.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,30 @@ def nms_boxes(boxes, scores):
351351
order = order[inds + 1]
352352
return np.array(keep)
353353

354+
def dfl(position):
355+
n, c, h, w = position.shape
356+
p_num = 4
357+
mc = c // p_num
358+
y = position.reshape(n, p_num, mc, h, w)
359+
y_exp = np.exp(y - np.max(y, axis=2, keepdims=True))
360+
y_softmax = y_exp / np.sum(y_exp, axis=2, keepdims=True)
361+
acc_metrix = np.arange(mc).reshape(1, 1, mc, 1, 1).astype(np.float32)
362+
y = (y_softmax * acc_metrix).sum(2)
363+
return y
364+
365+
def box_process(position):
366+
grid_h, grid_w = position.shape[2:4]
367+
col, row = np.meshgrid(np.arange(0, grid_w), np.arange(0, grid_h))
368+
col = col.reshape(1, 1, grid_h, grid_w)
369+
row = row.reshape(1, 1, grid_h, grid_w)
370+
grid = np.concatenate((col, row), axis=1)
371+
stride = np.array([IMG_SIZE[1]//grid_h, IMG_SIZE[0]//grid_w]).reshape(1,2,1,1)
372+
position = dfl(position)
373+
box_xy = grid +0.5 -position[:,0:2,:,:]
374+
box_xy2 = grid +0.5 +position[:,2:4,:,:]
375+
xyxy = np.concatenate((box_xy*stride, box_xy2*stride), axis=1)
376+
return xyxy
377+
354378
def post_process(input_data):
355379
obj, nms = det_config.get()
356380
return post_process_with_thresh(input_data, obj, nms)

src/rk3588/realtime_detection.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,30 @@ def nms_boxes(boxes, scores):
350350
order = order[inds + 1]
351351
return np.array(keep)
352352

353+
def dfl(position):
354+
n, c, h, w = position.shape
355+
p_num = 4
356+
mc = c // p_num
357+
y = position.reshape(n, p_num, mc, h, w)
358+
y_exp = np.exp(y - np.max(y, axis=2, keepdims=True))
359+
y_softmax = y_exp / np.sum(y_exp, axis=2, keepdims=True)
360+
acc_metrix = np.arange(mc).reshape(1, 1, mc, 1, 1).astype(np.float32)
361+
y = (y_softmax * acc_metrix).sum(2)
362+
return y
363+
364+
def box_process(position):
365+
grid_h, grid_w = position.shape[2:4]
366+
col, row = np.meshgrid(np.arange(0, grid_w), np.arange(0, grid_h))
367+
col = col.reshape(1, 1, grid_h, grid_w)
368+
row = row.reshape(1, 1, grid_h, grid_w)
369+
grid = np.concatenate((col, row), axis=1)
370+
stride = np.array([IMG_SIZE[1]//grid_h, IMG_SIZE[0]//grid_w]).reshape(1,2,1,1)
371+
position = dfl(position)
372+
box_xy = grid +0.5 -position[:,0:2,:,:]
373+
box_xy2 = grid +0.5 +position[:,2:4,:,:]
374+
xyxy = np.concatenate((box_xy*stride, box_xy2*stride), axis=1)
375+
return xyxy
376+
353377
def post_process(input_data):
354378
obj, nms = det_config.get()
355379
return post_process_with_thresh(input_data, obj, nms)

src/rk3588/web_detection.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,30 @@ def sp_flatten(_in):
314314

315315
return np.concatenate(nboxes), np.concatenate(nclasses), np.concatenate(nscores)
316316

317+
def dfl(position):
318+
n, c, h, w = position.shape
319+
p_num = 4
320+
mc = c // p_num
321+
y = position.reshape(n, p_num, mc, h, w)
322+
y_exp = np.exp(y - np.max(y, axis=2, keepdims=True))
323+
y_softmax = y_exp / np.sum(y_exp, axis=2, keepdims=True)
324+
acc_metrix = np.arange(mc).reshape(1, 1, mc, 1, 1).astype(np.float32)
325+
y = (y_softmax * acc_metrix).sum(2)
326+
return y
327+
328+
def box_process(position):
329+
grid_h, grid_w = position.shape[2:4]
330+
col, row = np.meshgrid(np.arange(0, grid_w), np.arange(0, grid_h))
331+
col = col.reshape(1, 1, grid_h, grid_w)
332+
row = row.reshape(1, 1, grid_h, grid_w)
333+
grid = np.concatenate((col, row), axis=1)
334+
stride = np.array([IMG_SIZE[1]//grid_h, IMG_SIZE[0]//grid_w]).reshape(1,2,1,1)
335+
position = dfl(position)
336+
box_xy = grid +0.5 -position[:,0:2,:,:]
337+
box_xy2 = grid +0.5 +position[:,2:4,:,:]
338+
xyxy = np.concatenate((box_xy*stride, box_xy2*stride), axis=1)
339+
return xyxy
340+
317341
def filter_boxes(boxes, box_confidences, box_class_probs):
318342
"""Filter boxes with object threshold."""
319343
box_confidences = box_confidences.reshape(-1)

0 commit comments

Comments
 (0)