当前位置:网站首页>Basis of target detection (IOU)
Basis of target detection (IOU)
2022-07-03 16:17:00 【victor_ gx】
Fundamentals of target detection (IoU)

What is? IOU?
IOU( Occurring simultaneously than Intersection over Union) It's a term , Used to describe the degree of overlap between two boxes . The larger the overlap area ,IOU The bigger it is .
IOU It is mainly used in applications related to object detection , In this application , Our training model outputs an external rectangle that completely surrounds the target . for example , In the diagram above , We have a green box and a blue box . The green box represents the real box , The blue box represents the prediction box of our model . The purpose of the training model is to continuously improve the output of its prediction frame , Until the blue and green boxes completely overlap , Between two boxes IOU be equal to 1.
IOU It is often used in non maximum suppression , Multiple prediction frames of the same object are eliminated based on the confidence of the detection frame , To ensure the prediction box with the highest retention reliability .
IOU Calculation formula
In target detection , Let's assume that the box 1 from [x1,y1,x2,y2] Express , Same box 2 from [x3,y3,x4,y4] Express , Here's the picture 1 Shown .

here IOU The calculation formula of is as follows :
I o U = A r e a o f I n t e r s e c t i o n o f t w o b o x e s A r e a o f U n i o n o f t w o b o x e s IoU=\frac{Area\ of\ Intersection\ of\ two\ boxes}{Area\ of\ Union\ of\ two\ boxes} IoU=Area of Union of two boxesArea of Intersection of two boxes
Use pictures 2 Shown by the following :

Next, we calculate the area corresponding to the intersection and union of the corresponding rectangular box step by step .
Calculate the intersection area
Intuitive to see , The intersection of two rectangular boxes has many situations , Let's intuitively observe the following figure 3, Obviously, the lower right two rectangular boxes completely coincide, and the intersection area is the largest .

First, we express the coordinates of the intersecting rectangles as [x_inter1,y_inter1,x_inter2,y_inter2], Represent the coordinates of the upper left corner and the lower right corner of the intersecting rectangle respectively .

Then start by defining the reference coordinate system . We use +X The axis is moving to the right ,+Y Computer graphics Convention for axial downward movement .
In order to calculate the coordinates of the upper left corner of the intersecting rectangle , We compare the upper left corner of each box . We can see from the example above ,x_inter1 You can find which box is closer to the right in the upper left corner . Similarly , You can find... By looking at which box's upper left corner is lower than the other box y_inter1. Mathematically speaking , They can be summarized as : x _ i n t e r 1 = m a x ( x 1 , x 3 ) x\_inter1=max(x1,x3) x_inter1=max(x1,x3), y y y_ i n t e r 1 = m a x ( y 1 , y 3 ) inter1=max(y1,y3) inter1=max(y1,y3)
To calculate the coordinates of the lower right corner of the intersecting rectangle , We compare the lower right corner of each box .
x_inter2 You can find by looking at which box's lower right corner is more left .
Similarly , You can find... By looking at which box's lower right corner is higher than the other box y_inter2.
Similarly , They can be summarized as : x _ i n t e r 2 = m a x ( x 2 , x 4 ) x\_inter2=max(x2,x4) x_inter2=max(x2,x4), y y y_ i n t e r 2 = m a x ( y 2 , y 4 ) inter2=max(y2,y4) inter2=max(y2,y4)
Now we have the coordinates of the intersecting rectangles , Then the area of the intersection is the area of the rectangle .( in application , We will use the absolute values of width and height , To ensure that even if the order of boxes changes , The width and height are still positive ; namely , If x_inter1>x_inter2, We will still get a positive value for the width )
w i d t h _ i n t e r = ( x _ i n t e r 2 − x _ i n t e r 1 ) h e i g h t _ i n t e r = ( y _ i n t e r 2 − y _ i n t e r 1 ) a r e a _ i n t e r = w i d t h _ i n t e r ∗ h e i g h t _ i n t e r width\_inter=(x\_inter2-x\_inter1) \\height\_inter=(y\_inter2-y\_inter1) \\area\_inter=width\_inter*height\_inter width_inter=(x_inter2−x_inter1)height_inter=(y_inter2−y_inter1)area_inter=width_inter∗height_inter
Calculate Union area
The union of boxes is the total area covered by two boxes , Pictured 2 Shown .
To find the total area , We first calculate the area of each rectangular box
w i d t h _ b o x 1 = ( x _ 2 − x _ 1 ) h e i g h t _ b o x 1 = ( y _ 2 − y _ 1 ) w i d t h _ b o x 2 = ( x _ 4 − x _ 3 ) h e i g h t _ b o x 2 = ( y _ 4 − y _ 3 ) a r e a _ b o x 1 = w i d t h _ b o x 1 ∗ h e i g h t _ b o x 1 a r e a _ b o x 2 = w i d t h _ b o x 2 ∗ h e i g h t _ b o x 2 width\_box1=(x\_2-x\_1) \\height\_box1=(y\_2-y\_1) \\width\_box2=(x\_4-x\_3) \\height\_box2=(y\_4-y\_3) \\area\_box1=width\_box1*height\_box1 \\area\_box2=width\_box2*height\_box2 width_box1=(x_2−x_1)height_box1=(y_2−y_1)width_box2=(x_4−x_3)height_box2=(y_4−y_3)area_box1=width_box1∗height_box1area_box2=width_box2∗height_box2
If we look at the total area covered by the two boxes , We will see that both boxes cover the part of the intersecting rectangle , That is, the area of the intersecting rectangle is included in box1 Areas and box2 In the region .
Because we only want to calculate the common intersection area once , We can subtract the intersection area we calculated from the total area of the two boxes . namely :
a r e a _ u n i o n = a r e a _ b o x 1 + a r e a _ b o x 2 − a r e a _ i n t e r s e c t i o n area\_union=area\_box1+area\_box2-area\_intersection area_union=area_box1+area_box2−area_intersection
Calculate the ratio of intersection and union
Through the above calculation formula , We calculate the area of the corresponding intersection and union of two rectangular boxes , At this time, the rectangle is IOU The calculation formula is as follows :
I o U = a r e a _ i n t e r a r e a _ u n i o n IoU=\frac{area\_inter}{area\_union} IoU=area_unionarea_inter
Observe the above formula , It can be concluded as follows :
- Two box IOU Can take 0 To 1 Any value between .
- If the two boxes do not intersect , Then their intersection area will be 0, therefore IOU Will also be 0.
- If two completely overlapping rectangular boxes , Then the area of the intersection will be equal to the area of its Union , therefore IOU Will be for 1
Code implementation
def IOU(box1, box2):
x1, y1, x2, y2 = box1
x3, y3, x4, y4 = box2
x_inter1 = max(x1, x3)
y_inter1 = max(y1, y3)
x_inter2 = min(x2, x4)
y_inter2 = min(y2, y4)
width_inter = abs(x_inter2 - x_inter1)
height_inter = abs(y_inter2 - y_inter1)
area_inter = width_inter * height_inter
width_box1 = abs(x2 - x1)
height_box1 = abs(y2 - y1)
width_box2 = abs(x4 - x3)
height_box2 = abs(y4 - y3)
area_box1 = width_box1 * height_box1
area_box2 = width_box2 * height_box2
area_union = area_box1 + area_box2 - area_inter
iou = area_inter / area_union
return iou
- function IOU Accept two boxes , namely box1 and box2 As input . The data in each box is a containing [x1,y1,x2,y2] A list of , It represents the coordinates of the upper left corner and the lower right corner .
- As mentioned earlier , Let's first calculate the area of intersecting rectangles , Then the area of the Union .
- abs The function is python The built-in function used to calculate the absolute value in . This ensures that we will never calculate the result with negative width or negative height .
- Back to IOU yes float Type value , Be situated between 0 and 1 Between .
The operation results are as follows :
边栏推荐
- [combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
- 1287. Elements that appear more than 25% in an ordered array
- "Everyday Mathematics" serial 56: February 25
- Project -- high concurrency memory pool
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验5 外部中断实验(学习笔记)
- [web security] - [SQL injection] - error detection injection
- Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
- 手机注册股票开户安全吗 开户需要钱吗
- Client does not support authentication protocol requested by server; consider upgrading MySQL client
- 阿飞的期望
猜你喜欢

【声明】关于检索SogK1997而找到诸多网页爬虫结果这件事

Principles of several common IO models

拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。

Unreal_DataTable 实现Id自增与设置RowName

Deep understanding of grouping sets statements in SQL
![[redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)](/img/1f/3dd95522b8d5f03dd763a6779e3db5.jpg)
[redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)

MongoDB 的安装和基本操作

Mixlab编辑团队招募队友啦~~

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
![App mobile terminal test [3] ADB command](/img/f1/4bff6e66b77d0f867bf7237019e982.png)
App mobile terminal test [3] ADB command
随机推荐
App mobile terminal test [3] ADB command
Detailed explanation of four modes of distributed transaction (Seata)
LeetCode1491. Average value of wages after removing the minimum wage and the maximum wage
远程文件包含实操
Unity project optimization case 1
[redis foundation] understand redis persistence mechanism together (rdb+aof graphic explanation)
切入点表达式
Shell script import and export data
Caching mechanism of Hibernate / session level caching mechanism
Brush questions -- sword finger offer
Microservices Seata distributed transactions
Distributed task scheduling XXL job
Three dimensional reconstruction of deep learning
“用Android复刻Apple产品UI”(2)——丝滑的AppStore卡片转场动画
工资3000,靠“视频剪辑”月入40000:会赚钱的人,从不靠拼命!
[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
TCP congestion control details | 3 design space
The difference between calling by value and simulating calling by reference
手机注册股票开户安全吗 开户需要钱吗
[combinatorics] combinatorial identities (sum of variable terms 3 combinatorial identities | sum of variable terms 4 combinatorial identities | binomial theorem + derivation to prove combinatorial ide