import numpy as np
def soft_nms(boxes, sigma, Nt, threshold, method=1): N = boxes.shape[0] pos, maxpos, maxscore = 0, 0, 0
for i in range(N): maxscore = boxes[i, 4] maxpos = i
tx1 = boxes[i, 0] ty1 = boxes[i, 1] tx2 = boxes[i, 2] ty2 = boxes[i, 3] ts = boxes[i, 4]
pos = i + 1
while pos < N: if maxscore < boxes[pos, 4]: maxscore = boxes[pos, 4] maxpos = pos pos += 1
boxes[i, 0] = boxes[maxpos, 0] boxes[i, 1] = boxes[maxpos, 1] boxes[i, 2] = boxes[maxpos, 2] boxes[i, 3] = boxes[maxpos, 3] boxes[i, 4] = boxes[maxpos, 4]
boxes[maxpos, 0] = tx1 boxes[maxpos, 1] = ty1 boxes[maxpos, 2] = tx2 boxes[maxpos, 3] = ty2 boxes[maxpos, 4] = ts
tx1 = boxes[i, 0] ty1 = boxes[i, 1] tx2 = boxes[i, 2] ty2 = boxes[i, 3] ts = boxes[i, 4]
pos = i + 1
while pos < N: x1 = boxes[pos, 0] y1 = boxes[pos, 1] x2 = boxes[pos, 2] y2 = boxes[pos, 3] s = boxes[pos, 4]
area = (x2 - x1 + 1) * (y2 - y1 + 1) iw = min(tx2, x2) - max(tx1, x1) + 1 if iw > 0: ih = min(ty2, y2) - max(ty1, y1) + 1 if ih > 0: ovr = iw * ih / float((tx2 - tx1 + 1) * (ty2 - ty1 + 1) + area - iw * ih)
if method == 1: if ovr > Nt: weight = 1 - ovr else: weight = 1 elif method == 2: weight = np.exp(-(ovr * ovr) / sigma) else: if ovr > Nt: weight = 0 else: weight = 1
boxes[pos, 4] = weight * boxes[pos, 4]
if boxes[pos, 4] < threshold: boxes[pos, 0] = boxes[N - 1, 0] boxes[pos, 1] = boxes[N - 1, 1] boxes[pos, 2] = boxes[N - 1, 2] boxes[pos, 3] = boxes[N - 1, 3] boxes[pos, 4] = boxes[N - 1, 4] N -= 1 pos -= 1 pos += 1
keep = [i for i in range(N)] return keep
if __name__ == '__main__': boxes = np.array([ [204, 102, 358, 250, 0.5], [257, 118, 380, 250, 0.7], [280, 135, 400, 250, 0.6], [255, 118, 360, 235, 0.7]]) thresh = 0.3 threshold = 0.25 Nt = 0.3 sigma = 0.5 res = soft_nms(boxes, sigma, Nt, threshold) print(box[res])
|