@@ -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+
317341def filter_boxes (boxes , box_confidences , box_class_probs ):
318342 """Filter boxes with object threshold."""
319343 box_confidences = box_confidences .reshape (- 1 )
0 commit comments