当前位置:网站首页>Basis of target detection (NMS)
Basis of target detection (NMS)
2022-07-01 14:05:00 【victor_ gx】
Fundamentals of target detection (NMS)

What is non maximum suppression
Non maximum suppression is a technique mainly used for target detection , To select the best bounding box from a set of overlapping boxes . In the following illustration , The purpose of non maximum suppression is to delete the yellow and blue boxes , In this way, we only have the green box as the final prediction result .

Calculation NMS Steps for
To understand what is boundingbox, as well as IOU The meaning of , I published a previous article on IOU The article . The terms described in the previous article will continue in this article .
Let's first describe NMS The working process in this particular example , Then explain a more general algorithm , Extend it to different scenarios .
1、 Definition of terms
The format of each bounding box we will use is as follows :
b o x _ l i s t = [ x 1 , y 1 , x 2 , y 2 , c l a s s c o n f i d e n c e ] box\_list=[x1,\ \ y1,\ \ x2,\ \ y2,\ \ class\ \ confidence] box_list=[x1, y1, x2, y2, class confidence]
Let's assume that , For this particular image, we have 3 A bounding box , namely :
b b o x _ l i s t = [ b l u e _ b o x , y e l l o w _ b o x , g r e e n _ b o x ] bbox\_list=[blue\_box,yellow\_box,green\_box] bbox_list=[blue_box,yellow_box,green_box]
For each box , Related definitions are as follows
b l u e _ b o x = [ x 3 , y 3 , x 4 , y 4 , " C a t " 0.85 ] y e l l o w _ b o x = [ x 5 , y 5 , x 6 , y 6 , " C a t " 0.75 ] g r e e n _ b o x = [ x 1 , y 1 , x 2 , y 2 , " C a t " 0.9 ] blue\_box=[x3,\ \ y3,\ \ x4,\ \ y4,\ \ "Cat"\ \ 0.85] \\yellow\_box=[x5,\ \ y5,\ \ x6,\ \ y6,\ \ "Cat"\ \ 0.75] \\green\_box=[x1,\ \ y1,\ \ x2,\ \ y2,\ \ "Cat"\ \ 0.9] blue_box=[x3, y3, x4, y4, "Cat" 0.85]yellow_box=[x5, y5, x6, y6, "Cat" 0.75]green_box=[x1, y1, x2, y2, "Cat" 0.9]
2、 Filter the candidate boxes according to the confidence level
As NMS The first step in , We sort the boxes in descending order of confidence . After sorting, we get the result as :
b b o x _ l i s t = [ g r e e n _ b o x , b l u e _ b o x , y e l l o w _ b o x ] bbox\_list=[green\_box,blue\_box,yellow\_box] bbox_list=[green_box,blue_box,yellow_box]
Then we define a confidence threshold . Any boxes with confidence below this threshold will be deleted . For this example , False set the confidence threshold to 0.8. Use this threshold , We will delete the yellow box , Because of its confidence <0.8. This leaves us :
b b o x _ l i s t = [ g r e e n _ b o x , b l u e _ b o x ] bbox\_list=[green\_box,blue\_box] bbox_list=[green_box,blue_box]
The result of this operation is shown below :

3、 according to IOU Filter
Because the confidence of the box is in descending order , We know that the first box in the list has the highest confidence . We delete the first box from the list , And add it to the new list . In our case , We will delete the green box , And put it in a new list , such as bbox_list_new.
At this stage , We are IOU An additional threshold is defined . This threshold is used to delete boxes with high overlap . The reasons are as follows : If the two boxes overlap a lot , And they belong to the same category , It is likely that both boxes cover the same object ( We can verify this from the figure above ). Because the reality is that each object has only one box , Therefore, we try to delete the boxes with low confidence .
In the example above , Suppose our IOU The threshold for 0.5
Let's start calculating the green box IOU, among bbox_list Each of the remaining boxes in also has the same class . In our case , We will only use the blue box to calculate the green box's IOU.
If green and blue IOU Greater than the threshold we define 0.5, We will delete the blue box , Because of its low confidence , And there is obvious overlap .
Repeat this process for each box in the image , In the above example, only unique boxes with high confidence are finally generated . As shown below :

NMS Algorithm
Sum up the above process , We can get NMS Of The calculation process as follows :
- Define confidence thresholds and IOU Threshold value .
- Arrange the bounding boxes in descending order of confidence bounding_box
- from bbox_list Delete the prediction box with confidence less than the threshold
- Loop through the remaining boxes , First, select the box with the highest confidence as the candidate box .
- Then, the values of all prediction frames and current candidate frames belonging to the same class as the candidate frames are calculated IOU.
- If any of the above two boxes IOU The value is greater than IOU threshold , So from box_list Remove the prediction box with low confidence
- Repeat this operation , Until all prediction boxes in the list are traversed .
Code implementation
def nms(boxes, conf_threshold = 0.7, iou_threshold = 0.4):
bbox_list_thresholded = []
bbox_list_new = []
boxes_sorted = sorted(boxes, reverse = True, key = lambda x: x[5])
for box in boxes_sorted:
if box[5] > conf_threshold:
bbox_list_thresholded.append(box)
else:
pass
while len(bbox_list_thresholded) > 0:
current_box = bbox_list_thresholded.pop(0)
bbox_list_new.append(current_box)
for box in bbox_list_thresholded:
if current_box[4] == box[4]:
iou = IOU(current_box[:4], box[:4])
if iou > iou_threshold:
bbox_list_thresholded.remove(box)
return bbox_list_new
The explanation is as follows :
def nms(boxes, conf_threshold=0.7, iou_threshold=0.4):
This function lists the image candidate boxes 、 Confidence thresholds and iou Threshold as input .( The corresponding default values are set to 0.7 and 0.4)
bbox_list_thresholded = []
bbox_list_new = []
Then we created two named bbox_list_threshold and bbox_list_new A list of .
- bbox_list_threshold: Contains a list of new boxes after filtering low confidence boxes
- bbox_list_new: Include execution NMS The final box list after
boxes_sorted = sorted(boxes, reverse=True, key = lambda x : x[5])
In the above definition of terms , Sort the list of boxes in descending order of confidence , And store the new list in the variable boxes_sorted in .
Here we use python The built-in sorted Function to sort it , This function is based on key Field specifies the collation .
In our case , We specify a keyword reverse=True To sort the list in descending order , At the same time, specify the constraint of the second keyword for sorting . Here we use lambda The function provides a mapping , Returns the... Of each bounding box 5 Elements ( Degree of confidence ).
After setting the above two parameters , When traversing each box , The sorting function will sort the candidate boxes in descending order according to confidence .
for box in boxes_sorted:
if box[5] > conf_threshold:
bbox_list_thresholded.append(box)
else:
pass
We traverse all sorted boxes , And remove confidence below the threshold we set (conf_threshold=0.7) Box of
while len(bbox_list_thresholded) > 0:
current_box = bbox_list_thresholded.pop(0)
bbox_list_new.append(current_box)
In the above filter candidate box based on confidence , We iterate through the threshold box list one by one (bbox_list_threshold) All boxes in , Until the list is empty .
Let's first remove... From this list ( eject ) The first box ( Current box ), Because it has the highest credibility , Then attach it to our final list (bbox_list_new).
for box in bbox_list_thresholded:
if current_box[4] == box[4]:
iou = IOU(current_box[:4], box[:4])
if iou > iou_threshold:
bbox_list_thresholded.remove(box)
then , Let's iterate over the list bbox_list_threshold All remaining boxes in , And check whether they are the same as the current box category .(box[4] Corresponding to category )
If two boxes belong to the same class , We calculate the distance between these boxes IOU, If IOU>IOU_threshold, We will start from the list bbox_list_thresholded Remove the box with low confidence .
return bbox_list_new
After non maximum transplantation , We return to the list of updated boxes bbox_list_new.
边栏推荐
- el-form-item 正则验证
- 佩服,阿里女程序卧底 500 多个黑产群……
- 学会使用LiveData和ViewModel,我相信会让你在写业务时变得轻松
- 我们该如何保护自己的密码?
- 自定义注解实现验证信息的功能
- Introduction to distributed transactions (Seata)
- Use lambda function URL + cloudfront to realize S3 image back to source
- Summary of 20 practical typescript single line codes
- 小程序- view中多个text换行
- Animesr: learnable degradation operator and new real world animation VSR dataset
猜你喜欢

Etcd summary mechanism and usage scenarios

A new book by teacher Zhang Yujin of Tsinghua University: 2D vision system and image technology (five copies will be sent at the end of the article)

Using CMD to repair and recover virus infected files

QT学习管理系统

Station B was scolded on the hot search..
![[NLP] pre training model - gpt1](/img/bd/9803ad946b33159de51b93106a2151.png)
[NLP] pre training model - gpt1

Introduction to distributed transactions (Seata)

算网融合赋能行业转型,移动云点亮数智未来新路标

开源实习经验分享:openEuler软件包加固测试

Play with mongodb - build a mongodb cluster
随机推荐
Summary of 20 practical typescript single line codes
Explain IO multiplexing, select, poll, epoll in detail
Summary of interview questions (1) HTTPS man in the middle attack, the principle of concurrenthashmap, serialVersionUID constant, redis single thread,
使用 Lambda 函数URL + CloudFront 实现S3镜像回源
Applet - applet chart Library (F2 chart Library)
Introduction to distributed transactions (Seata)
Après avoir été licencié pendant trois mois, l'entrevue s'est effondrée et l'état d'esprit a commencé à s'effondrer.
Uni app realizes advertisement scroll bar
[flask] flask starts and implements a minimal application based on flask
WebSocket(简单体验版)
AnimeSR:可学习的降质算子与新的真实世界动漫VSR数据集
2022 · 让我带你Jetpack架构组件从入门到精通 — Lifecycle
Scheme of printing statistical information in log
建立自己的网站(21)
Fiori applications are shared through the enhancement of adaptation project
算网融合赋能行业转型,移动云点亮数智未来新路标
使用CMD修复和恢复病毒感染文件
Dragon lizard community open source coolbpf, BPF program development efficiency increased 100 times
【修复版】仿我爱看电影网站模板/海洋CMS影视系统模板
Logic is a good thing