Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
5ea771d9
Unverified
提交
5ea771d9
authored
6月 29, 2021
作者:
Glenn Jocher
提交者:
GitHub
6月 29, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move IoU functions to metrics.py (#3820)
上级
3213d871
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
82 行增加
和
83 行删除
+82
-83
general.py
utils/general.py
+1
-79
loss.py
utils/loss.py
+1
-1
metrics.py
utils/metrics.py
+80
-3
没有找到文件。
utils/general.py
浏览文件 @
5ea771d9
...
...
@@ -25,7 +25,7 @@ import torchvision
import
yaml
from
utils.google_utils
import
gsutil_getsize
from
utils.metrics
import
fitness
from
utils.metrics
import
box_iou
,
fitness
from
utils.torch_utils
import
init_torch_seeds
# Settings
...
...
@@ -469,84 +469,6 @@ def clip_coords(boxes, img_shape):
boxes
[:,
3
]
.
clip
(
0
,
img_shape
[
0
],
out
=
boxes
[:,
3
])
# y2
def
bbox_iou
(
box1
,
box2
,
x1y1x2y2
=
True
,
GIoU
=
False
,
DIoU
=
False
,
CIoU
=
False
,
eps
=
1e-7
):
# Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
box2
=
box2
.
T
# Get the coordinates of bounding boxes
if
x1y1x2y2
:
# x1, y1, x2, y2 = box1
b1_x1
,
b1_y1
,
b1_x2
,
b1_y2
=
box1
[
0
],
box1
[
1
],
box1
[
2
],
box1
[
3
]
b2_x1
,
b2_y1
,
b2_x2
,
b2_y2
=
box2
[
0
],
box2
[
1
],
box2
[
2
],
box2
[
3
]
else
:
# transform from xywh to xyxy
b1_x1
,
b1_x2
=
box1
[
0
]
-
box1
[
2
]
/
2
,
box1
[
0
]
+
box1
[
2
]
/
2
b1_y1
,
b1_y2
=
box1
[
1
]
-
box1
[
3
]
/
2
,
box1
[
1
]
+
box1
[
3
]
/
2
b2_x1
,
b2_x2
=
box2
[
0
]
-
box2
[
2
]
/
2
,
box2
[
0
]
+
box2
[
2
]
/
2
b2_y1
,
b2_y2
=
box2
[
1
]
-
box2
[
3
]
/
2
,
box2
[
1
]
+
box2
[
3
]
/
2
# Intersection area
inter
=
(
torch
.
min
(
b1_x2
,
b2_x2
)
-
torch
.
max
(
b1_x1
,
b2_x1
))
.
clamp
(
0
)
*
\
(
torch
.
min
(
b1_y2
,
b2_y2
)
-
torch
.
max
(
b1_y1
,
b2_y1
))
.
clamp
(
0
)
# Union Area
w1
,
h1
=
b1_x2
-
b1_x1
,
b1_y2
-
b1_y1
+
eps
w2
,
h2
=
b2_x2
-
b2_x1
,
b2_y2
-
b2_y1
+
eps
union
=
w1
*
h1
+
w2
*
h2
-
inter
+
eps
iou
=
inter
/
union
if
GIoU
or
DIoU
or
CIoU
:
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
if
CIoU
or
DIoU
:
# Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
c2
=
cw
**
2
+
ch
**
2
+
eps
# convex diagonal squared
rho2
=
((
b2_x1
+
b2_x2
-
b1_x1
-
b1_x2
)
**
2
+
(
b2_y1
+
b2_y2
-
b1_y1
-
b1_y2
)
**
2
)
/
4
# center distance squared
if
DIoU
:
return
iou
-
rho2
/
c2
# DIoU
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
)
with
torch
.
no_grad
():
alpha
=
v
/
(
v
-
iou
+
(
1
+
eps
))
return
iou
-
(
rho2
/
c2
+
v
*
alpha
)
# CIoU
else
:
# GIoU https://arxiv.org/pdf/1902.09630.pdf
c_area
=
cw
*
ch
+
eps
# convex area
return
iou
-
(
c_area
-
union
)
/
c_area
# GIoU
else
:
return
iou
# IoU
def
box_iou
(
box1
,
box2
):
# https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py
"""
Return intersection-over-union (Jaccard index) of boxes.
Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
Arguments:
box1 (Tensor[N, 4])
box2 (Tensor[M, 4])
Returns:
iou (Tensor[N, M]): the NxM matrix containing the pairwise
IoU values for every element in boxes1 and boxes2
"""
def
box_area
(
box
):
# box = 4xn
return
(
box
[
2
]
-
box
[
0
])
*
(
box
[
3
]
-
box
[
1
])
area1
=
box_area
(
box1
.
T
)
area2
=
box_area
(
box2
.
T
)
# inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2)
inter
=
(
torch
.
min
(
box1
[:,
None
,
2
:],
box2
[:,
2
:])
-
torch
.
max
(
box1
[:,
None
,
:
2
],
box2
[:,
:
2
]))
.
clamp
(
0
)
.
prod
(
2
)
return
inter
/
(
area1
[:,
None
]
+
area2
-
inter
)
# iou = inter / (area1 + area2 - inter)
def
wh_iou
(
wh1
,
wh2
):
# Returns the nxm IoU matrix. wh1 is nx2, wh2 is mx2
wh1
=
wh1
[:,
None
]
# [N,1,2]
wh2
=
wh2
[
None
]
# [1,M,2]
inter
=
torch
.
min
(
wh1
,
wh2
)
.
prod
(
2
)
# [N,M]
return
inter
/
(
wh1
.
prod
(
2
)
+
wh2
.
prod
(
2
)
-
inter
)
# iou = inter / (area1 + area2 - inter)
def
non_max_suppression
(
prediction
,
conf_thres
=
0.25
,
iou_thres
=
0.45
,
classes
=
None
,
agnostic
=
False
,
multi_label
=
False
,
labels
=
(),
max_det
=
300
):
"""Runs Non-Maximum Suppression (NMS) on inference results
...
...
utils/loss.py
浏览文件 @
5ea771d9
...
...
@@ -3,7 +3,7 @@
import
torch
import
torch.nn
as
nn
from
utils.
general
import
bbox_iou
from
utils.
metrics
import
bbox_iou
from
utils.torch_utils
import
is_parallel
...
...
utils/metrics.py
浏览文件 @
5ea771d9
# Model validation metrics
import
math
import
warnings
from
pathlib
import
Path
...
...
@@ -7,8 +8,6 @@ import matplotlib.pyplot as plt
import
numpy
as
np
import
torch
from
.
import
general
def
fitness
(
x
):
# Model fitness as a weighted combination of metrics
...
...
@@ -128,7 +127,7 @@ class ConfusionMatrix:
detections
=
detections
[
detections
[:,
4
]
>
self
.
conf
]
gt_classes
=
labels
[:,
0
]
.
int
()
detection_classes
=
detections
[:,
5
]
.
int
()
iou
=
general
.
box_iou
(
labels
[:,
1
:],
detections
[:,
:
4
])
iou
=
box_iou
(
labels
[:,
1
:],
detections
[:,
:
4
])
x
=
torch
.
where
(
iou
>
self
.
iou_thres
)
if
x
[
0
]
.
shape
[
0
]:
...
...
@@ -184,6 +183,84 @@ class ConfusionMatrix:
print
(
' '
.
join
(
map
(
str
,
self
.
matrix
[
i
])))
def
bbox_iou
(
box1
,
box2
,
x1y1x2y2
=
True
,
GIoU
=
False
,
DIoU
=
False
,
CIoU
=
False
,
eps
=
1e-7
):
# Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
box2
=
box2
.
T
# Get the coordinates of bounding boxes
if
x1y1x2y2
:
# x1, y1, x2, y2 = box1
b1_x1
,
b1_y1
,
b1_x2
,
b1_y2
=
box1
[
0
],
box1
[
1
],
box1
[
2
],
box1
[
3
]
b2_x1
,
b2_y1
,
b2_x2
,
b2_y2
=
box2
[
0
],
box2
[
1
],
box2
[
2
],
box2
[
3
]
else
:
# transform from xywh to xyxy
b1_x1
,
b1_x2
=
box1
[
0
]
-
box1
[
2
]
/
2
,
box1
[
0
]
+
box1
[
2
]
/
2
b1_y1
,
b1_y2
=
box1
[
1
]
-
box1
[
3
]
/
2
,
box1
[
1
]
+
box1
[
3
]
/
2
b2_x1
,
b2_x2
=
box2
[
0
]
-
box2
[
2
]
/
2
,
box2
[
0
]
+
box2
[
2
]
/
2
b2_y1
,
b2_y2
=
box2
[
1
]
-
box2
[
3
]
/
2
,
box2
[
1
]
+
box2
[
3
]
/
2
# Intersection area
inter
=
(
torch
.
min
(
b1_x2
,
b2_x2
)
-
torch
.
max
(
b1_x1
,
b2_x1
))
.
clamp
(
0
)
*
\
(
torch
.
min
(
b1_y2
,
b2_y2
)
-
torch
.
max
(
b1_y1
,
b2_y1
))
.
clamp
(
0
)
# Union Area
w1
,
h1
=
b1_x2
-
b1_x1
,
b1_y2
-
b1_y1
+
eps
w2
,
h2
=
b2_x2
-
b2_x1
,
b2_y2
-
b2_y1
+
eps
union
=
w1
*
h1
+
w2
*
h2
-
inter
+
eps
iou
=
inter
/
union
if
GIoU
or
DIoU
or
CIoU
:
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
if
CIoU
or
DIoU
:
# Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
c2
=
cw
**
2
+
ch
**
2
+
eps
# convex diagonal squared
rho2
=
((
b2_x1
+
b2_x2
-
b1_x1
-
b1_x2
)
**
2
+
(
b2_y1
+
b2_y2
-
b1_y1
-
b1_y2
)
**
2
)
/
4
# center distance squared
if
DIoU
:
return
iou
-
rho2
/
c2
# DIoU
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
)
with
torch
.
no_grad
():
alpha
=
v
/
(
v
-
iou
+
(
1
+
eps
))
return
iou
-
(
rho2
/
c2
+
v
*
alpha
)
# CIoU
else
:
# GIoU https://arxiv.org/pdf/1902.09630.pdf
c_area
=
cw
*
ch
+
eps
# convex area
return
iou
-
(
c_area
-
union
)
/
c_area
# GIoU
else
:
return
iou
# IoU
def
box_iou
(
box1
,
box2
):
# https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py
"""
Return intersection-over-union (Jaccard index) of boxes.
Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
Arguments:
box1 (Tensor[N, 4])
box2 (Tensor[M, 4])
Returns:
iou (Tensor[N, M]): the NxM matrix containing the pairwise
IoU values for every element in boxes1 and boxes2
"""
def
box_area
(
box
):
# box = 4xn
return
(
box
[
2
]
-
box
[
0
])
*
(
box
[
3
]
-
box
[
1
])
area1
=
box_area
(
box1
.
T
)
area2
=
box_area
(
box2
.
T
)
# inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2)
inter
=
(
torch
.
min
(
box1
[:,
None
,
2
:],
box2
[:,
2
:])
-
torch
.
max
(
box1
[:,
None
,
:
2
],
box2
[:,
:
2
]))
.
clamp
(
0
)
.
prod
(
2
)
return
inter
/
(
area1
[:,
None
]
+
area2
-
inter
)
# iou = inter / (area1 + area2 - inter)
def
wh_iou
(
wh1
,
wh2
):
# Returns the nxm IoU matrix. wh1 is nx2, wh2 is mx2
wh1
=
wh1
[:,
None
]
# [N,1,2]
wh2
=
wh2
[
None
]
# [1,M,2]
inter
=
torch
.
min
(
wh1
,
wh2
)
.
prod
(
2
)
# [N,M]
return
inter
/
(
wh1
.
prod
(
2
)
+
wh2
.
prod
(
2
)
-
inter
)
# iou = inter / (area1 + area2 - inter)
# Plots ----------------------------------------------------------------------------------------------------------------
def
plot_pr_curve
(
px
,
py
,
ap
,
save_dir
=
'pr_curve.png'
,
names
=
()):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论