NMS_非极大值抑制的作用
参考链接 :NMS(非极大值抑制)
NMS: non maximum suppression
翻译为“非极大值抑制”,为什么不翻译成最大值抑制呢?maximum可以翻译为“最大值”,也可以翻译成“极大值”,所以翻译成极大值或者最大值一定要看这个值的含义。
极大值和最大值的区别就是,极大值是局部最大值。
NMS的作用:去掉detection任务重复的候选框,只留下预测概率值最大的候选框最为最终预测的结果(非极大值抑制)。
实现代码如下,来自链接:
1 # 非极大值抑制 2 def nms(bboxes, scores, score_thresh, nms_thresh, pre_nms_topk, i=0, c=0): 3 """ 4 nms 5 """ 6 inds = np.argsort(scores) 7 inds = inds[::-1] 8 keep_inds = [] 9 while(len(inds) > 0): 10 cur_ind = inds[0] 11 cur_score = scores[cur_ind] 12 # if score of the box is less than score_thresh, just drop it 13 if cur_score < score_thresh: 14 break 15 16 keep = True 17 for ind in keep_inds: 18 current_box = bboxes[cur_ind] 19 remain_box = bboxes[ind] 20 iou = box_iou_xyxy(current_box, remain_box) 21 if iou > nms_thresh: 22 keep = False 23 break 24 if i == 0 and c == 4 and cur_ind == 951: 25 print('suppressed, ', keep, i, c, cur_ind, ind, iou) 26 if keep: 27 keep_inds.append(cur_ind) 28 inds = inds[1:] 29 30 return np.array(keep_inds) 31 32 # 多分类非极大值抑制 33 def multiclass_nms(bboxes, scores, score_thresh=0.01, nms_thresh=0.45, pre_nms_topk=1000, pos_nms_topk=100): 34 """ 35 This is for multiclass_nms 36 """ 37 batch_size = bboxes.shape[0] 38 class_num = scores.shape[1] 39 rets = [] 40 for i in range(batch_size): 41 bboxes_i = bboxes[i] 42 scores_i = scores[i] 43 ret = [] 44 for c in range(class_num): 45 scores_i_c = scores_i[c] 46 keep_inds = nms(bboxes_i, scores_i_c, score_thresh, nms_thresh, pre_nms_topk, i=i, c=c) 47 if len(keep_inds) < 1: 48 continue 49 keep_bboxes = bboxes_i[keep_inds] 50 keep_scores = scores_i_c[keep_inds] 51 keep_results = np.zeros([keep_scores.shape[0], 6]) 52 keep_results[:, 0] = c 53 keep_results[:, 1] = keep_scores[:] 54 keep_results[:, 2:6] = keep_bboxes[:, :] 55 ret.append(keep_results) 56 if len(ret) < 1: 57 rets.append(ret) 58 continue 59 ret_i = np.concatenate(ret, axis=0) 60 scores_i = ret_i[:, 1] 61 if len(scores_i) > pos_nms_topk: 62 inds = np.argsort(scores_i)[::-1] 63 inds = inds[:pos_nms_topk] 64 ret_i = ret_i[inds] 65 66 rets.append(ret_i) 67 68 return rets