Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
Y
yolov5
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Administrator
yolov5
Commits
36d12a50
Unverified
提交
36d12a50
authored
11月 20, 2021
作者:
Glenn Jocher
提交者:
GitHub
11月 20, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Explicitly compute TP, FP in val.py (#5727)
上级
eb51ffdc
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
16 行增加
和
7 行删除
+16
-7
metrics.py
utils/metrics.py
+15
-6
val.py
val.py
+1
-1
没有找到文件。
utils/metrics.py
浏览文件 @
36d12a50
...
...
@@ -18,7 +18,7 @@ def fitness(x):
return
(
x
[:,
:
4
]
*
w
)
.
sum
(
1
)
def
ap_per_class
(
tp
,
conf
,
pred_cls
,
target_cls
,
plot
=
False
,
save_dir
=
'.'
,
names
=
()):
def
ap_per_class
(
tp
,
conf
,
pred_cls
,
target_cls
,
plot
=
False
,
save_dir
=
'.'
,
names
=
()
,
eps
=
1e-16
):
""" Compute the average precision, given the recall and precision curves.
Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.
# Arguments
...
...
@@ -37,7 +37,7 @@ def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names
tp
,
conf
,
pred_cls
=
tp
[
i
],
conf
[
i
],
pred_cls
[
i
]
# Find unique classes
unique_classes
=
np
.
unique
(
target_cls
)
unique_classes
,
nt
=
np
.
unique
(
target_cls
,
return_counts
=
True
)
nc
=
unique_classes
.
shape
[
0
]
# number of classes, number of detections
# Create Precision-Recall curve and compute AP for each class
...
...
@@ -45,7 +45,7 @@ def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names
ap
,
p
,
r
=
np
.
zeros
((
nc
,
tp
.
shape
[
1
])),
np
.
zeros
((
nc
,
1000
)),
np
.
zeros
((
nc
,
1000
))
for
ci
,
c
in
enumerate
(
unique_classes
):
i
=
pred_cls
==
c
n_l
=
(
target_cls
==
c
)
.
sum
()
# number of labels
n_l
=
nt
[
ci
]
# number of labels
n_p
=
i
.
sum
()
# number of predictions
if
n_p
==
0
or
n_l
==
0
:
...
...
@@ -56,7 +56,7 @@ def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names
tpc
=
tp
[
i
]
.
cumsum
(
0
)
# Recall
recall
=
tpc
/
(
n_l
+
1e-16
)
# recall curve
recall
=
tpc
/
(
n_l
+
eps
)
# recall curve
r
[
ci
]
=
np
.
interp
(
-
px
,
-
conf
[
i
],
recall
[:,
0
],
left
=
0
)
# negative x, xp because xp decreases
# Precision
...
...
@@ -70,7 +70,7 @@ def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names
py
.
append
(
np
.
interp
(
px
,
mrec
,
mpre
))
# precision at mAP@0.5
# Compute F1 (harmonic mean of precision and recall)
f1
=
2
*
p
*
r
/
(
p
+
r
+
1e-16
)
f1
=
2
*
p
*
r
/
(
p
+
r
+
eps
)
names
=
[
v
for
k
,
v
in
names
.
items
()
if
k
in
unique_classes
]
# list: only classes that have data
names
=
{
i
:
v
for
i
,
v
in
enumerate
(
names
)}
# to dict
if
plot
:
...
...
@@ -80,7 +80,10 @@ def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names
plot_mc_curve
(
px
,
r
,
Path
(
save_dir
)
/
'R_curve.png'
,
names
,
ylabel
=
'Recall'
)
i
=
f1
.
mean
(
0
)
.
argmax
()
# max F1 index
return
p
[:,
i
],
r
[:,
i
],
ap
,
f1
[:,
i
],
unique_classes
.
astype
(
'int32'
)
p
,
r
,
f1
=
p
[:,
i
],
r
[:,
i
],
f1
[:,
i
]
tp
=
(
r
*
nt
)
.
round
()
# true positives
fp
=
(
tp
/
(
p
+
eps
)
-
tp
)
.
round
()
# false positives
return
tp
,
fp
,
p
,
r
,
f1
,
ap
,
unique_classes
.
astype
(
'int32'
)
def
compute_ap
(
recall
,
precision
):
...
...
@@ -162,6 +165,12 @@ class ConfusionMatrix:
def
matrix
(
self
):
return
self
.
matrix
def
tp_fp
(
self
):
tp
=
self
.
matrix
.
diagonal
()
# true positives
fp
=
self
.
matrix
.
sum
(
1
)
-
tp
# false positives
# fn = self.matrix.sum(0) - tp # false negatives (missed detections)
return
tp
[:
-
1
],
fp
[:
-
1
]
# remove background class
def
plot
(
self
,
normalize
=
True
,
save_dir
=
''
,
names
=
()):
try
:
import
seaborn
as
sn
...
...
val.py
浏览文件 @
36d12a50
...
...
@@ -237,7 +237,7 @@ def run(data,
# Compute metrics
stats
=
[
np
.
concatenate
(
x
,
0
)
for
x
in
zip
(
*
stats
)]
# to numpy
if
len
(
stats
)
and
stats
[
0
]
.
any
():
p
,
r
,
ap
,
f1
,
ap_class
=
ap_per_class
(
*
stats
,
plot
=
plots
,
save_dir
=
save_dir
,
names
=
names
)
tp
,
fp
,
p
,
r
,
f1
,
ap
,
ap_class
=
ap_per_class
(
*
stats
,
plot
=
plots
,
save_dir
=
save_dir
,
names
=
names
)
ap50
,
ap
=
ap
[:,
0
],
ap
.
mean
(
1
)
# AP@0.5, AP@0.5:0.95
mp
,
mr
,
map50
,
map
=
p
.
mean
(),
r
.
mean
(),
ap50
.
mean
(),
ap
.
mean
()
nt
=
np
.
bincount
(
stats
[
3
]
.
astype
(
np
.
int64
),
minlength
=
nc
)
# number of targets per class
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论