soft-nms代码如下:
def soft_nms(np.ndarray[float, ndim=2] boxes, float sigma=0.5, float Nt=0.3, float threshold=0.001, unsigned int method=0):
cdef unsigned int N = boxes.shape[0]
cdef float iw, ih, box_area
cdef float ua
cdef int pos = 0
cdef float maxscore = 0
cdef int maxpos = 0
cdef float x1,x2,y1,y2,tx1,tx2,ty1,ty2,ts,area,weight,ov
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
# get max box
while pos < N:
if maxscore < boxes[pos, 4]:
maxscore = boxes[pos, 4]
maxpos = pos
pos = pos + 1
# add max box as a detection
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]
# swap ith box with position of max box
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
# NMS iterations, note that N changes if detection boxes fall below threshold
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:
ua = float((tx2 - tx1 + 1) * (ty2 - ty1 + 1) + area - iw * ih)
ov = iw * ih / ua #iou between max box and detection box
if method == 1: # linear
if ov > Nt:
weight = 1 - ov
else:
weight = 1
elif method == 2: # gaussian
weight = np.exp(-(ov * ov)/sigma)
else: # original NMS
if ov > Nt:
weight = 0
else:
weight = 1
boxes[pos, 4] = weight*boxes[pos, 4]
# if box score falls below threshold, discard the box by swapping with last box
# update N
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 = N - 1
pos = pos - 1
pos = pos + 1
keep = [i for i in range(N)]
return keep
调用例子为:
soft_nms(boxes, Nt=0.5, method=2)
当时很迷惑,为什么返回时一个index,直接返回boxes不好吗?
然后当时想,可能因为传入的boxes在函数中会被修改,所以就不需要再return回来了
为了验证这个想法,我记录了soft-nms前与soft-nms后的score值
score_before = boxes[:, 4]
soft_nms(boxes, Nt=0.5, method=2)
score_after = boxes[:, 4]
diff = score_before - score_after
然后看他们的差值diff,如果不为0的话,说明我的boxes的值确实是发生了变化的。但是发现diff居然一直为零,然后就在soft-nms上耗了一个下午。。。。
先说结论吧:
- 我验证的方式是错误的!!!!
- 调用soft-nms的时候,boxes确实是有变化的,所以不需要再return
原理
上面两个结论用到的原理都一样,就是:在python中,变量赋值的时候是以引用
的方式赋值的,例如:
a = [1]
b = a
print(id(a))
print(id(b))
你会发现a
和b
的id是一样的!因为他们就是指向的是同一个地址!如果你改变了a
,b
也会跟着变,例如
a.append(2)
print(a)
print(b)
b虽然没有append,但是结果也会变为[1,2]
为什么验证的方式是错误
这就是了为什么我的验证方法是错误的,因为他们指向的是同一个地址,所以结果两个scores当然会相同!Orz
为什么不用return boxes呢?
因为boxes在传入函数后,不是传一个值进去,而是传入了boxes的地址,所以在里面的修改,会对原boxes地址上的值进行直接修改
当我们在后续再读取boxes的时候,因为是读boxes地址的值,而地址上的值已经被改过了,所以我们读到的就是修改后的值了,所以函数中不需要再return boxes回来