当前位置:网站首页>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.
边栏推荐
- That hard-working student failed the college entrance examination... Don't panic! You have another chance to counter attack!
- This paper introduces an implementation scheme to enhance the favorite transaction code management tool in SAP GUI
- C语言基础知识
- Play with mongodb - build a mongodb cluster
- leetcode 322. Coin change (medium)
- Halo effect - who says that those with light on their heads are heroes
- Summary of interview questions (1) HTTPS man in the middle attack, the principle of concurrenthashmap, serialVersionUID constant, redis single thread,
- Animesr: learnable degradation operator and new real world animation VSR dataset
- Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security
- 奔涌而来的数字化浪潮,将怎样颠覆未来?
猜你喜欢

我们该如何保护自己的密码?
![[安网杯 2021] REV WP](/img/98/ea5c241e2b8f3ae4c76e1c75c9e0d1.png)
[安网杯 2021] REV WP

After being laid off for three months, the interview ran into a wall everywhere, and the mentality has begun to collapse

建立自己的网站(21)

Scheme of printing statistical information in log

Etcd summary mechanism and usage scenarios

Kongsong (Xintong Institute) - cloud security capacity building and trend in the digital era

“国防七子”经费暴增,清华足足362亿元,甩第二名101亿 |全国高校2022预算大公开...

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

TexStudio使用教程
随机推荐
SAP intelligent robot process automation (IRPA) solution sharing
MySQL log
JVM有哪些类加载机制?
In depth cooperation | Taosi data cooperates with changhongjia Huawei customers in China to provide tdengine with powerful enterprise level products and perfect service guarantee
Use the right scene, get twice the result with half the effort! Full introduction to the window query function and usage scenarios of tdengine
Benefiting from the Internet, the scientific and technological performance of overseas exchange volume has returned to high growth
Distributed dynamic (collaborative) rendering / function runtime based on computing power driven, data and function collaboration
8款最佳实践,保护你的 IaC 安全!
【IoT毕设.下】STM32+机智云AIoT+实验室安全监控系统
Several models of IO blocking, non blocking, IO multiplexing, signal driven and asynchronous IO
[anwangbei 2021] Rev WP
Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales Daniel?
[IOT design. Part I] stm32+ smart cloud aiot+ laboratory security monitoring system
AnimeSR:可学习的降质算子与新的真实世界动漫VSR数据集
What "hard core innovations" does Intel have in the first half of 2022? Just look at this picture!
Use the npoi package of net core 6 C to read excel Pictures in xlsx cells and stored to the specified server
Go integrates logrus to realize log printing
建立自己的网站(21)
Word2vec training Chinese word vector
The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise