Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
914e6967
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
1 年多 前同步成功
通知
115
Star
4999
Fork
1114
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
6
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleClas
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
6
合并请求
6
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
914e6967
编写于
6月 22, 2021
作者:
D
dongshuilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix reid recall metric bugs
上级
f40fd3e6
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
64 addition
and
26 deletion
+64
-26
ppcls/engine/trainer.py
ppcls/engine/trainer.py
+3
-1
ppcls/metric/metrics.py
ppcls/metric/metrics.py
+61
-25
未找到文件。
ppcls/engine/trainer.py
浏览文件 @
914e6967
...
...
@@ -442,10 +442,12 @@ class Trainer(object):
keep_mask
=
paddle
.
logical_or
(
query_id_mask
,
image_id_mask
)
similarity_matrix
=
similarity_matrix
*
keep_mask
.
astype
(
"float32"
)
else
:
keep_mask
=
None
metric_tmp
=
self
.
eval_metric_func
(
similarity_matrix
,
image_id_blocks
[
block_idx
],
gallery_img_id
)
gallery_img_id
,
keep_mask
)
for
key
in
metric_tmp
:
if
key
not
in
metric_dict
:
...
...
ppcls/metric/metrics.py
浏览文件 @
914e6967
...
...
@@ -16,6 +16,7 @@ import numpy as np
import
paddle
import
paddle.nn
as
nn
class
TopkAcc
(
nn
.
Layer
):
def
__init__
(
self
,
topk
=
(
1
,
5
)):
super
().
__init__
()
...
...
@@ -34,54 +35,72 @@ class TopkAcc(nn.Layer):
x
,
label
,
k
=
k
)
return
metric_dict
class
mAP
(
nn
.
Layer
):
def
__init__
(
self
):
super
().
__init__
()
def
forward
(
self
,
similarities_matrix
,
query_img_id
,
gallery_img_id
):
def
forward
(
self
,
similarities_matrix
,
query_img_id
,
gallery_img_id
,
*
args
):
metric_dict
=
dict
()
choosen_indices
=
paddle
.
argsort
(
similarities_matrix
,
axis
=
1
,
descending
=
True
)
gallery_labels_transpose
=
paddle
.
transpose
(
gallery_img_id
,
[
1
,
0
])
gallery_labels_transpose
=
paddle
.
broadcast_to
(
gallery_labels_transpose
,
shape
=
[
choosen_indices
.
shape
[
0
],
gallery_labels_transpose
.
shape
[
1
]])
choosen_label
=
paddle
.
index_sample
(
gallery_labels_transpose
,
choosen_indices
)
choosen_indices
=
paddle
.
argsort
(
similarities_matrix
,
axis
=
1
,
descending
=
True
)
gallery_labels_transpose
=
paddle
.
transpose
(
gallery_img_id
,
[
1
,
0
])
gallery_labels_transpose
=
paddle
.
broadcast_to
(
gallery_labels_transpose
,
shape
=
[
choosen_indices
.
shape
[
0
],
gallery_labels_transpose
.
shape
[
1
]
])
choosen_label
=
paddle
.
index_sample
(
gallery_labels_transpose
,
choosen_indices
)
equal_flag
=
paddle
.
equal
(
choosen_label
,
query_img_id
)
equal_flag
=
paddle
.
cast
(
equal_flag
,
'float32'
)
acc_sum
=
paddle
.
cumsum
(
equal_flag
,
axis
=
1
)
div
=
paddle
.
arange
(
acc_sum
.
shape
[
1
]).
astype
(
"float32"
)
+
1
precision
=
paddle
.
divide
(
acc_sum
,
div
)
precision
=
paddle
.
divide
(
acc_sum
,
div
)
#calc map
precision_mask
=
paddle
.
multiply
(
equal_flag
,
precision
)
ap
=
paddle
.
sum
(
precision_mask
,
axis
=
1
)
/
paddle
.
sum
(
equal_flag
,
axis
=
1
)
ap
=
paddle
.
sum
(
precision_mask
,
axis
=
1
)
/
paddle
.
sum
(
equal_flag
,
axis
=
1
)
metric_dict
[
"mAP"
]
=
paddle
.
mean
(
ap
).
numpy
()[
0
]
return
metric_dict
class
mINP
(
nn
.
Layer
):
def
__init__
(
self
):
super
().
__init__
()
def
forward
(
self
,
similarities_matrix
,
query_img_id
,
gallery_img_id
):
def
forward
(
self
,
similarities_matrix
,
query_img_id
,
gallery_img_id
,
*
args
):
metric_dict
=
dict
()
choosen_indices
=
paddle
.
argsort
(
similarities_matrix
,
axis
=
1
,
descending
=
True
)
gallery_labels_transpose
=
paddle
.
transpose
(
gallery_img_id
,
[
1
,
0
])
gallery_labels_transpose
=
paddle
.
broadcast_to
(
gallery_labels_transpose
,
shape
=
[
choosen_indices
.
shape
[
0
],
gallery_labels_transpose
.
shape
[
1
]])
choosen_label
=
paddle
.
index_sample
(
gallery_labels_transpose
,
choosen_indices
)
choosen_indices
=
paddle
.
argsort
(
similarities_matrix
,
axis
=
1
,
descending
=
True
)
gallery_labels_transpose
=
paddle
.
transpose
(
gallery_img_id
,
[
1
,
0
])
gallery_labels_transpose
=
paddle
.
broadcast_to
(
gallery_labels_transpose
,
shape
=
[
choosen_indices
.
shape
[
0
],
gallery_labels_transpose
.
shape
[
1
]
])
choosen_label
=
paddle
.
index_sample
(
gallery_labels_transpose
,
choosen_indices
)
tmp
=
paddle
.
equal
(
choosen_label
,
query_img_id
)
tmp
=
paddle
.
cast
(
tmp
,
'float64'
)
#do accumulative sum
div
=
paddle
.
arange
(
tmp
.
shape
[
1
]).
astype
(
"float64"
)
+
2
minus
=
paddle
.
divide
(
tmp
,
div
)
auxilary
=
paddle
.
subtract
(
tmp
,
minus
)
minus
=
paddle
.
divide
(
tmp
,
div
)
auxilary
=
paddle
.
subtract
(
tmp
,
minus
)
hard_index
=
paddle
.
argmax
(
auxilary
,
axis
=
1
).
astype
(
"float64"
)
all_INP
=
paddle
.
divide
(
paddle
.
sum
(
tmp
,
axis
=
1
),
hard_index
)
mINP
=
paddle
.
mean
(
all_INP
)
metric_dict
[
"mINP"
]
=
mINP
.
numpy
()[
0
]
return
metric_dict
class
Recallk
(
nn
.
Layer
):
def
__init__
(
self
,
topk
=
(
1
,
5
)):
super
().
__init__
()
...
...
@@ -90,25 +109,43 @@ class Recallk(nn.Layer):
topk
=
[
topk
]
self
.
topk
=
topk
def
forward
(
self
,
similarities_matrix
,
query_img_id
,
gallery_img_id
):
def
forward
(
self
,
similarities_matrix
,
query_img_id
,
gallery_img_id
,
keep_mask
):
metric_dict
=
dict
()
#get cmc
choosen_indices
=
paddle
.
argsort
(
similarities_matrix
,
axis
=
1
,
descending
=
True
)
gallery_labels_transpose
=
paddle
.
transpose
(
gallery_img_id
,
[
1
,
0
])
gallery_labels_transpose
=
paddle
.
broadcast_to
(
gallery_labels_transpose
,
shape
=
[
choosen_indices
.
shape
[
0
],
gallery_labels_transpose
.
shape
[
1
]])
choosen_label
=
paddle
.
index_sample
(
gallery_labels_transpose
,
choosen_indices
)
choosen_indices
=
paddle
.
argsort
(
similarities_matrix
,
axis
=
1
,
descending
=
True
)
gallery_labels_transpose
=
paddle
.
transpose
(
gallery_img_id
,
[
1
,
0
])
gallery_labels_transpose
=
paddle
.
broadcast_to
(
gallery_labels_transpose
,
shape
=
[
choosen_indices
.
shape
[
0
],
gallery_labels_transpose
.
shape
[
1
]
])
choosen_label
=
paddle
.
index_sample
(
gallery_labels_transpose
,
choosen_indices
)
equal_flag
=
paddle
.
equal
(
choosen_label
,
query_img_id
)
if
keep_mask
is
not
None
:
keep_mask
=
paddle
.
index_sample
(
keep_mask
.
astype
(
'float32'
),
choosen_indices
)
equal_flag
=
paddle
.
logical_and
(
equal_flag
,
keep_mask
.
astype
(
'bool'
))
equal_flag
=
paddle
.
cast
(
equal_flag
,
'float32'
)
real_query_num
=
paddle
.
sum
(
equal_flag
,
axis
=
1
)
real_query_num
=
paddle
.
sum
(
paddle
.
greater_than
(
real_query_num
,
paddle
.
to_tensor
(
0.
)).
astype
(
"float32"
))
acc_sum
=
paddle
.
cumsum
(
equal_flag
,
axis
=
1
)
mask
=
paddle
.
greater_than
(
acc_sum
,
paddle
.
to_tensor
(
0.
)).
astype
(
"float32"
)
all_cmc
=
paddle
.
mean
(
mask
,
axis
=
0
).
numpy
()
mask
=
paddle
.
greater_than
(
acc_sum
,
paddle
.
to_tensor
(
0.
)).
astype
(
"float32"
)
all_cmc
=
(
paddle
.
sum
(
mask
,
axis
=
0
)
/
real_query_num
).
numpy
()
for
k
in
self
.
topk
:
metric_dict
[
"recall{}"
.
format
(
k
)]
=
all_cmc
[
k
-
1
]
return
metric_dict
class
DistillationTopkAcc
(
TopkAcc
):
def
__init__
(
self
,
model_key
,
feature_key
=
None
,
topk
=
(
1
,
5
)):
super
().
__init__
(
topk
=
topk
)
...
...
@@ -132,4 +169,3 @@ class GoogLeNetTopkAcc(TopkAcc):
def
forward
(
self
,
x
,
label
):
return
super
().
forward
(
x
[
0
],
label
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录