文章作者:Tyan
博客:noahsnail.com ?|? CSDN ?|? 簡(jiǎn)書(shū)
1. 什么是非極大值抑制
非極大值抑制,簡(jiǎn)稱(chēng)為NMS算法俱两,英文為Non-Maximum Suppression冲茸。其思想是搜素局部最大值通孽,抑制極大值仲义。NMS算法在不同應(yīng)用中的具體實(shí)現(xiàn)不太一樣齐蔽,但思想是一樣的产弹。非極大值抑制派歌,在計(jì)算機(jī)視覺(jué)任務(wù)中得到了廣泛的應(yīng)用,例如邊緣檢測(cè)痰哨、人臉檢測(cè)胶果、目標(biāo)檢測(cè)(DPM,YOLO作谭,SSD稽物,F(xiàn)aster R-CNN)等。
2. 為什么要用非極大值抑制
以目標(biāo)檢測(cè)為例:目標(biāo)檢測(cè)的過(guò)程中在同一目標(biāo)的位置上會(huì)產(chǎn)生大量的候選框折欠,這些候選框相互之間可能會(huì)有重疊贝或,此時(shí)我們需要利用非極大值抑制找到最佳的目標(biāo)邊界框,消除冗余的邊界框锐秦。Demo如下圖:
左圖是人臉檢測(cè)的候選框結(jié)果咪奖,每個(gè)邊界框有一個(gè)置信度得分(confidence score),如果不使用非極大值抑制酱床,就會(huì)有多個(gè)候選框出現(xiàn)羊赵。右圖是使用非極大值抑制之后的結(jié)果,符合我們?nèi)四槞z測(cè)的預(yù)期結(jié)果扇谣。
3. 如何使用非極大值抑制
前提:目標(biāo)邊界框列表及其對(duì)應(yīng)的置信度得分列表昧捷,設(shè)定閾值,閾值用來(lái)刪除重疊較大的邊界框罐寨。
IoU:intersection-over-union靡挥,即兩個(gè)邊界框的交集部分除以它們的并集。
非極大值抑制的流程如下:
根據(jù)置信度得分進(jìn)行排序
選擇置信度最高的比邊界框添加到最終輸出列表中鸯绿,將其從邊界框列表中刪除
計(jì)算所有邊界框的面積
計(jì)算置信度最高的邊界框與其它候選框的IoU跋破。
刪除IoU大于閾值的邊界框
重復(fù)上述過(guò)程簸淀,直至邊界框列表為空。
Python代碼如下:
#!/usr/bin/env python
# _*_ coding: utf-8 _*_
import cv2
import numpy as np
"""
Non-max Suppression Algorithm
@param list Object candidate bounding boxes
@param list Confidence score of bounding boxes
@param float IoU threshold
@return Rest boxes after nms operation
"""
def nms(bounding_boxes, confidence_score, threshold):
# If no bounding boxes, return empty list
if len(bounding_boxes) == 0:
return [], []
# Bounding boxes
boxes = np.array(bounding_boxes)
# coordinates of bounding boxes
start_x = boxes[:, 0]
start_y = boxes[:, 1]
end_x = boxes[:, 2]
end_y = boxes[:, 3]
# Confidence scores of bounding boxes
score = np.array(confidence_score)
# Picked bounding boxes
picked_boxes = []
picked_score = []
# Compute areas of bounding boxes
areas = (end_x - start_x + 1) * (end_y - start_y + 1)
# Sort by confidence score of bounding boxes
order = np.argsort(score)
# Iterate bounding boxes
while order.size > 0:
# The index of largest confidence score
index = order[-1]
# Pick the bounding box with largest confidence score
picked_boxes.append(bounding_boxes[index])
picked_score.append(confidence_score[index])
# Compute ordinates of intersection-over-union(IOU)
x1 = np.maximum(start_x[index], start_x[order[:-1]])
x2 = np.minimum(end_x[index], end_x[order[:-1]])
y1 = np.maximum(start_y[index], start_y[order[:-1]])
y2 = np.minimum(end_y[index], end_y[order[:-1]])
# Compute areas of intersection-over-union
w = np.maximum(0.0, x2 - x1 + 1)
h = np.maximum(0.0, y2 - y1 + 1)
intersection = w * h
# Compute the ratio between intersection and union
ratio = intersection / (areas[index] + areas[order[:-1]] - intersection)
left = np.where(ratio < threshold)
order = order[left]
return picked_boxes, picked_score
# Image name
image_name = 'nms.jpg'
# Bounding boxes
bounding_boxes = [(187, 82, 337, 317), (150, 67, 305, 282), (246, 121, 368, 304)]
confidence_score = [0.9, 0.75, 0.8]
# Read image
image = cv2.imread(image_name)
# Copy image as original
org = image.copy()
# Draw parameters
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 1
thickness = 2
# IoU threshold
threshold = 0.4
# Draw bounding boxes and confidence score
for (start_x, start_y, end_x, end_y), confidence in zip(bounding_boxes, confidence_score):
(w, h), baseline = cv2.getTextSize(str(confidence), font, font_scale, thickness)
cv2.rectangle(org, (start_x, start_y - (2 * baseline + 5)), (start_x + w, start_y), (0, 255, 255), -1)
cv2.rectangle(org, (start_x, start_y), (end_x, end_y), (0, 255, 255), 2)
cv2.putText(org, str(confidence), (start_x, start_y), font, font_scale, (0, 0, 0), thickness)
# Run non-max suppression algorithm
picked_boxes, picked_score = nms(bounding_boxes, confidence_score, threshold)
# Draw bounding boxes and confidence score after non-maximum supression
for (start_x, start_y, end_x, end_y), confidence in zip(picked_boxes, picked_score):
(w, h), baseline = cv2.getTextSize(str(confidence), font, font_scale, thickness)
cv2.rectangle(image, (start_x, start_y - (2 * baseline + 5)), (start_x + w, start_y), (0, 255, 255), -1)
cv2.rectangle(image, (start_x, start_y), (end_x, end_y), (0, 255, 255), 2)
cv2.putText(image, str(confidence), (start_x, start_y), font, font_scale, (0, 0, 0), thickness)
# Show image
cv2.imshow('Original', org)
cv2.imshow('NMS', image)
cv2.waitKey(0)
源碼下載地址:https://github.com/SnailTyan/deep-learning-tools/blob/master/nms.py
記得給個(gè)Star毒返。Demo原圖在README.md
里租幕。
實(shí)驗(yàn)結(jié)果:
- 閾值為0.6
- 閾值為0.5
- 閾值為0.4