Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
5e0b90de
提交
5e0b90de
authored
8月 20, 2020
作者:
Glenn Jocher
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
CIoU nan bug fix (#736)
上级
fb4fc8cd
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
13 行增加
和
13 行删除
+13
-13
general.py
utils/general.py
+13
-13
没有找到文件。
utils/general.py
浏览文件 @
5e0b90de
import
glob
import
glob
import
logging
import
math
import
math
import
os
import
os
import
platform
import
random
import
random
import
shutil
import
shutil
import
subprocess
import
subprocess
import
time
import
time
import
logging
from
contextlib
import
contextmanager
from
contextlib
import
contextmanager
from
copy
import
copy
from
copy
import
copy
from
pathlib
import
Path
from
pathlib
import
Path
import
platform
import
cv2
import
cv2
import
matplotlib
import
matplotlib
...
@@ -339,19 +339,19 @@ def compute_ap(recall, precision):
...
@@ -339,19 +339,19 @@ def compute_ap(recall, precision):
return
ap
return
ap
def
bbox_iou
(
box1
,
box2
,
x1y1x2y2
=
True
,
GIoU
=
False
,
DIoU
=
False
,
CIoU
=
False
):
def
bbox_iou
(
box1
,
box2
,
x1y1x2y2
=
True
,
GIoU
=
False
,
DIoU
=
False
,
CIoU
=
False
,
eps
=
1e-12
):
# Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
# Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
box2
=
box2
.
T
box2
=
box2
.
T
# Get the coordinates of bounding boxes
# Get the coordinates of bounding boxes
if
x1y1x2y2
:
# x1, y1, x2, y2 = box1
if
x1y1x2y2
:
# x1, y1, x2, y2 = box1
b1_x1
,
b1_y1
,
b1_x2
,
b1_y2
=
box1
[
0
],
box1
[
1
],
box1
[
2
]
,
box1
[
3
]
b1_x1
,
b1_y1
,
b1_x2
,
b1_y2
=
box1
[
0
],
box1
[
1
],
box1
[
2
]
+
eps
,
box1
[
3
]
+
eps
b2_x1
,
b2_y1
,
b2_x2
,
b2_y2
=
box2
[
0
],
box2
[
1
],
box2
[
2
]
,
box2
[
3
]
b2_x1
,
b2_y1
,
b2_x2
,
b2_y2
=
box2
[
0
],
box2
[
1
],
box2
[
2
]
+
eps
,
box2
[
3
]
+
eps
else
:
# transform from xywh to xyxy
else
:
# transform from xywh to xyxy
b1_x1
,
b1_x2
=
box1
[
0
]
-
box1
[
2
]
/
2
,
box1
[
0
]
+
box1
[
2
]
/
2
b1_x1
,
b1_x2
=
box1
[
0
]
-
box1
[
2
]
/
2
,
box1
[
0
]
+
box1
[
2
]
/
2
+
eps
b1_y1
,
b1_y2
=
box1
[
1
]
-
box1
[
3
]
/
2
,
box1
[
1
]
+
box1
[
3
]
/
2
b1_y1
,
b1_y2
=
box1
[
1
]
-
box1
[
3
]
/
2
,
box1
[
1
]
+
box1
[
3
]
/
2
+
eps
b2_x1
,
b2_x2
=
box2
[
0
]
-
box2
[
2
]
/
2
,
box2
[
0
]
+
box2
[
2
]
/
2
b2_x1
,
b2_x2
=
box2
[
0
]
-
box2
[
2
]
/
2
,
box2
[
0
]
+
box2
[
2
]
/
2
+
eps
b2_y1
,
b2_y2
=
box2
[
1
]
-
box2
[
3
]
/
2
,
box2
[
1
]
+
box2
[
3
]
/
2
b2_y1
,
b2_y2
=
box2
[
1
]
-
box2
[
3
]
/
2
,
box2
[
1
]
+
box2
[
3
]
/
2
+
eps
# Intersection area
# Intersection area
inter
=
(
torch
.
min
(
b1_x2
,
b2_x2
)
-
torch
.
max
(
b1_x1
,
b2_x1
))
.
clamp
(
0
)
*
\
inter
=
(
torch
.
min
(
b1_x2
,
b2_x2
)
-
torch
.
max
(
b1_x1
,
b2_x1
))
.
clamp
(
0
)
*
\
...
@@ -360,18 +360,18 @@ def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False):
...
@@ -360,18 +360,18 @@ def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False):
# Union Area
# Union Area
w1
,
h1
=
b1_x2
-
b1_x1
,
b1_y2
-
b1_y1
w1
,
h1
=
b1_x2
-
b1_x1
,
b1_y2
-
b1_y1
w2
,
h2
=
b2_x2
-
b2_x1
,
b2_y2
-
b2_y1
w2
,
h2
=
b2_x2
-
b2_x1
,
b2_y2
-
b2_y1
union
=
(
w1
*
h1
+
1e-16
)
+
w2
*
h2
-
inter
union
=
w1
*
h1
+
w2
*
h2
-
inter
iou
=
inter
/
union
# iou
iou
=
inter
/
union
# iou
if
GIoU
or
DIoU
or
CIoU
:
if
GIoU
or
DIoU
or
CIoU
:
cw
=
torch
.
max
(
b1_x2
,
b2_x2
)
-
torch
.
min
(
b1_x1
,
b2_x1
)
# convex (smallest enclosing box) width
cw
=
torch
.
max
(
b1_x2
,
b2_x2
)
-
torch
.
min
(
b1_x1
,
b2_x1
)
# convex (smallest enclosing box) width
ch
=
torch
.
max
(
b1_y2
,
b2_y2
)
-
torch
.
min
(
b1_y1
,
b2_y1
)
# convex height
ch
=
torch
.
max
(
b1_y2
,
b2_y2
)
-
torch
.
min
(
b1_y1
,
b2_y1
)
# convex height
if
GIoU
:
# Generalized IoU https://arxiv.org/pdf/1902.09630.pdf
if
GIoU
:
# Generalized IoU https://arxiv.org/pdf/1902.09630.pdf
c_area
=
cw
*
ch
+
1e-16
# convex area
c_area
=
cw
*
ch
# convex area
return
iou
-
(
c_area
-
union
)
/
c_area
# GIoU
return
iou
-
(
c_area
-
union
)
/
c_area
# GIoU
if
DIoU
or
CIoU
:
# Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
if
DIoU
or
CIoU
:
# Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
# convex diagonal squared
# convex diagonal squared
c2
=
cw
**
2
+
ch
**
2
+
1e-16
c2
=
cw
**
2
+
ch
**
2
# centerpoint distance squared
# centerpoint distance squared
rho2
=
((
b2_x1
+
b2_x2
)
-
(
b1_x1
+
b1_x2
))
**
2
/
4
+
((
b2_y1
+
b2_y2
)
-
(
b1_y1
+
b1_y2
))
**
2
/
4
rho2
=
((
b2_x1
+
b2_x2
)
-
(
b1_x1
+
b1_x2
))
**
2
/
4
+
((
b2_y1
+
b2_y2
)
-
(
b1_y1
+
b1_y2
))
**
2
/
4
if
DIoU
:
if
DIoU
:
...
@@ -379,7 +379,7 @@ def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False):
...
@@ -379,7 +379,7 @@ def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False):
elif
CIoU
:
# https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
elif
CIoU
:
# https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
v
=
(
4
/
math
.
pi
**
2
)
*
torch
.
pow
(
torch
.
atan
(
w2
/
h2
)
-
torch
.
atan
(
w1
/
h1
),
2
)
v
=
(
4
/
math
.
pi
**
2
)
*
torch
.
pow
(
torch
.
atan
(
w2
/
h2
)
-
torch
.
atan
(
w1
/
h1
),
2
)
with
torch
.
no_grad
():
with
torch
.
no_grad
():
alpha
=
v
/
(
1
-
iou
+
v
+
1e-16
)
alpha
=
v
/
(
(
1
+
eps
)
-
iou
+
v
)
return
iou
-
(
rho2
/
c2
+
v
*
alpha
)
# CIoU
return
iou
-
(
rho2
/
c2
+
v
*
alpha
)
# CIoU
return
iou
return
iou
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论