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
| import numpy as np def soft_nms(dets, method, thresh=0.001, Nt=0.1, sigma=0.5): x1 = dets[:, 0] y1 = dets[:, 1] x2 = dets[:, 2] y2 = dets[:, 3] scores = dets[:, 4]
areas = (y2 - y1 + 1.) * (x2 - x1 + 1.) orders = scores.argsort()[::-1] keep = [] while orders.size > 0: i = orders[0] keep.append(i) for j in orders[1:]: xx1 = np.maximum(x1[i], x1[j]) yy1 = np.maximum(y1[i], y1[j]) xx2 = np.minimum(x2[i], x2[j]) yy2 = np.minimum(y2[i], y2[j])
w = np.maximum( xx2 - xx1 + 1., 0. ) h = np.maximum( yy2 - yy1 + 1., 0. ) inter = w * h IoU = inter / (areas[i] + areas[j] - inter)
if method == 1: if IoU > Nt: weight = 1 - IoU else: weight = 1 elif method == 2: weight = np.exp(-(IoU * IoU)/sigma) else: if IoU > Nt: weight = 0 else: weight = 1 scores[j] = weight * scores[j]
if scores[j] < thresh: orders = np.delete(orders, np.where(orders == j))
orders = np.delete(orders, 0)
return keep
if __name__ == '__main__':
dets = np.array([[100,120,170,200,0.98], [20,40,80,90,0.94], [20,38,82,88,0.99], [200,380,282,488,0.9], [19,38,75,91, 0.98]])
print(soft_nms(dets, 2))
|