目录
有原理步骤:
加注释版:
soft_nms的优点
有原理步骤:
soft-nms详解_笔记大全_设计学院
Soft-nms的实现过程可以分为几个步骤:
1. 输入预测框
输入神经网络预测输出的所有框,每个框有四个坐标和一个类别得分。
2. 对于每个框计算其权重
权重可以使用三种不同的函数:max、linear和Gaussian。
3. 重复以下步骤,直到不再有框被删除
(1)选出最高得分的框,令其权重为1,与第一个框进行交换。
(2)计算当前框与剩余框的重叠率。
(3)根据重叠率和选定的函数计算权重。
(4)根据权重更新每个框的得分。
(5)剔除得分小于设定阈值的框。
4. 输出筛选后的结果
代码:
def soft_nms(dets, sigma=0.5, Nt=0.3, threshold=0.001, method=1):"""PyTorch implementation of SoftNMS algorithm.# Argumentsdets: detections, size[N,5], format[x1,y1,x2,y2,score]sigma: variance of Gaussian function, scalarNt: threshold for box overlap, scalarthreshold: score threshold, scalarmethod: 0=Max, 1=Linear, 2=Gaussian# Returnsdets: detections after SoftNMS, size[K,5]"""# Indexes concatenate detection boxes with the scoreN = dets.shape[0]indexes = np.array([np.arange(N)])dets = np.concatenate((dets, indexes.T), axis=1)for i in range(N):# intermediate parameters for later parameters exchangesi = dets[i, 4]xi = dets[i, :4]area_i = (xi[2] - xi[0] + 1) * (xi[3] - xi[1] + 1)if method == 1: # Linearweight = np.ones((N - i))weight[0] = sielse: # Gaussian# Compute Gaussian weight coefficientsxx = np.arange(i, N).astype(np.float32)if method == 2:sigma = 0.5ii = np.ones((xx.shape[0], 1)) * i# print(sigma)# print((xx - ii).shape)gauss = np.exp(-1.0 * ((xx - ii) ** 2) / (2 * sigma * sigma))if method == 2:weight = gausselse:weight = np.zeros((N - i))weight[0] = 1.0weight[1:] = gauss / np.sum(gauss)# Sort boxes by scoreidx = np.arange(i, N)idx_max = np.argmax(dets[idx, 4])idx_max += i# Swap boxes and scoresdets[i, 4], dets[idx_max, 4] = dets[idx_max, 4], dets[i, 4]dets[i, :4], dets[idx_max, :4] = dets[idx_max, :4], dets[i, :4]dets[i, 5], dets[idx_max, 5] = dets[idx_max, 5], dets[i, 5]# Compute overlap ratiosxx1 = np.maximum(dets[i, 0], dets[idx, 0])yy1 = np.maximum(dets[i, 1], dets[idx, 1])xx2 = np.minimum(dets[i, 2], dets[idx, 2])yy2 = np.minimum(dets[i, 3], dets[idx, 3])w = np.maximum(0.0, xx2 - xx1 + 1)h = np.maximum(0.0, yy2 - yy1 + 1)inter = w * h# Update weightsif method == 0: # Maxweight[idx_max - i + 1:] = np.where(inter > Nt, 0.0, 1.0)else: # Linear / Gaussianweight_matrix = np.zeros((weight.shape[0], weight.shape[0]))weight_matrix[0, :] = weightweight_matrix[1:, :] = np.diag(weight[1:])iou = inter / (area_i + dets[idx, 4] * (1 - inter))weight[idx - i + 1] = np.matmul(weight_matrix, (1.0 - iou).reshape(-1, 1)).reshape(-1)weight[idx_max - i + 1:] = np.where(iou > Nt, 0.0, weight[idx_max - i + 1:])# Apply weightdets[idx, 4] = dets[idx, 4] * weight# Weigh small scoressuppress_small = np.where(dets[idx, 4] < threshold)[0]dets[suppress_small + i, 4] = 0.0# remove boxes lower than thresholdidx_keep = np.where(dets[:, 4] > 0)[0]dets = dets[idx_keep]return dets[:, :5]
加注释版:
soft_nms的优点
1,解决了物体挨得很近导致的漏检问题
2,需要增加的超参数很少,只增加了一个sigma,阈值nms本来也有,iou是算出来的
3,计算复杂度相对于nms没有增加,都是O(n^2),n是bboxes的数量。
import numpy as np# 定义一个nms函数
def soft_nms(dets, thresh=0.3, sigma=0.5): # score大于thresh的才能存留下来,当设定的thresh过低,存留下来的框就很多,所以要根据实际情况调参'''input:dets: dets是(n,5)的ndarray,第0维度的每个元素代码一个框:[x1, y1, x2, y2, score] thresh: floatsigma: flaotoutput:index'''x1 = dets[:, 0] # dets:(n,5) x1:(n,) dets是ndarray, x1是ndarrayy1 = dets[:, 1]x2 = dets[:, 2]y2 = dets[:, 3]scores = dets[:, 4] # scores是ndarray# 每一个候选框的面积areas = (x2 - x1 + 1) * (y2 - y1 + 1) # areas:(n,)# order是按照score降序排序的order = scores.argsort()[::-1] # order:(n,) 降序下标 order是ndarraykeep = []while order.size > 0:i = order[0] # i 是当下分数最高的框的下标# print(i)keep.append(i)# 计算当前概率最大矩形框与其他矩形框的相交框的坐标,会用到numpy的broadcast机制,得到的是向量# 当order只有一个值的时候,order[1]会报错说index out of range,而order[1:]会是[],不报错,[]也可以作为x1的索引,x1[[]]为[]xx1 = np.maximum(x1[i], x1[order[1:]]) # xx1:(n-1,)的ndarray x1[i]:numpy_64浮点数一个,x1[order[1:]]是个ndarray,可以是空的ndarray,如果是空ndarray那么xx1为空ndarray,如果非空,那么x1[order[1:]]有多少个元素,xx1就是有多少个元素的ndarray。x1[]是不是ndarray看中括号内的是不是ndarray,看中括号内的是不是ndarray看中括号内的order[]的中括号内有没有冒号,有冒号的是ndarray,没有的是一个数。yy1 = np.maximum(y1[i], y1[order[1:]])xx2 = np.minimum(x2[i], x2[order[1:]])yy2 = np.minimum(y2[i], y2[order[1:]])# 计算相交框的面积,注意矩形框不相交时w或h算出来会是负数,用0代替w = np.maximum(0.0, xx2 - xx1 + 1) # xx2-xx1是(n-1,)的ndarray,w是(n-1,)的ndarray, n会逐渐减小至1# 当xx2和xx1是空的,那w是空的h = np.maximum(0.0, yy2 - yy1 + 1)inter = w * h # inter是(n,)的ndarray# 当w和h是空的,inter是空的# 计算重叠度IOU:重叠面积/(面积1+面积2-重叠面积)eps = np.finfo(areas.dtype).eps # 除法考虑分母为0的情况,np.finfo(dtype).eps,np.finfo(dtype)是个类,它封装了机器极限浮点类型的数,比如eps,episilon的缩写,表示小正数。ovr = inter / np.maximum(eps, areas[i] + areas[order[1:]] - inter) # n-1 #一旦(面积1+面积2-重叠面积)为0,就用eps进行替换# 当inter为空,areas[i]无论inter空不空都是有值的,那么ovr也为空# 更新分数weight = np.exp(-ovr*ovr/sigma)scores[order[1:]] *= weight# 更新orderscore_order = scores[order[1:]].argsort()[::-1] + 1order = order[score_order]keep_ids = np.where(scores[order]>thresh)[0]order = order[keep_ids]return keepimport numpy as np
import cv2# 读入图片,录入原始人框([x1, y1, x2, y2, score])
image = cv2.imread('w.jpg')boxes = np.array([[5, 52, 171, 270, 0.9999],
[13, 1, 179, 268, 0.9998],
[20, 7, 176, 262, 0.8998],
[7, 5, 169, 272, 0.9687],
[3, 43, 162, 256, 0.9786],
[10, 56, 167, 266, 0.8988]])# 将框绘制在图像上
image_for_nms_box = image.copy()
for box in boxes:x1, y1, x2, y2, score = int(box[0]), int(box[1]), int(box[2]), int(box[3]), box[4] # x:col y:rowimage_for_nms_box = cv2.rectangle(image_for_nms_box, (x1, y1), (x2, y2), (0,255,0), 2)
cv2.imwrite("w_all.jpg", image_for_nms_box)
cv2.imshow('w_all', image_for_nms_box)# 使用soft_nms对框进行筛选
keep = soft_nms(boxes)
soft_nms_boxs = boxes[keep]# 将筛选过后的框绘制在图像上
image_for_nms_box = image.copy()
for box in soft_nms_boxs:x1, y1, x2, y2, score = int(box[0]), int(box[1]), int(box[2]), int(box[3]), box[4]image_for_nms_box = cv2.rectangle(image_for_nms_box, (x1, y1), (x2, y2), (0,255,0), 2)
# Syntax: cv2.imwrite(filename, image)
cv2.imwrite("w_soft_nms.jpg", image_for_nms_box)
cv2.imshow('w_soft_nms', image_for_nms_box)cv2.waitKey()
cv2.destroyAllWindows()