Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
c9f2c474
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看板
提交
c9f2c474
编写于
6月 04, 2021
作者:
W
weishengyu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update metric
上级
b56237dc
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
151 addition
and
0 deletion
+151
-0
ppcls/metric/__init__.py
ppcls/metric/__init__.py
+0
-0
ppcls/metric/metrics.py
ppcls/metric/metrics.py
+151
-0
未找到文件。
ppcls/metric/__init__.py
0 → 100644
浏览文件 @
c9f2c474
ppcls/metric/metrics.py
0 → 100644
浏览文件 @
c9f2c474
# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
numpy
as
np
import
paddle
import
paddle.nn
as
nn
# TODO: fix the format
class
Topk
(
nn
.
Layer
):
def
__init__
(
self
,
topk
=
(
1
,
5
)):
super
().
__init__
()
assert
isinstance
(
topk
,
(
int
,
list
,
tuple
))
if
isinstance
(
topk
,
int
):
topk
=
[
topk
]
self
.
topk
=
topk
def
forward
(
self
,
x
,
label
):
if
isinstance
(
x
,
dict
):
x
=
x
[
"logits"
]
metric_dict
=
dict
()
for
k
in
self
.
topk
:
metric_dict
[
"top{}"
.
format
(
k
)]
=
paddle
.
metric
.
accuracy
(
x
,
label
,
k
=
k
)
return
metric_dict
class
mAP
(
nn
.
Layer
):
def
__init__
(
self
,
max_rank
=
50
):
super
().
__init__
()
self
.
max_rank
=
max_rank
def
forward
(
self
,
similarities_matrix
,
query_img_id
,
gallery_img_id
):
metric_dict
=
dict
()
num_q
,
num_g
=
similarities_matrix
.
shape
q_pids
=
query_img_id
.
numpy
().
reshape
((
query_img_id
.
shape
[
0
]))
g_pids
=
gallery_img_id
.
numpy
().
reshape
((
gallery_img_id
.
shape
[
0
]))
if
num_g
<
self
.
max_rank
:
self
.
max_rank
=
num_g
print
(
'Note: number of gallery samples is quite small, got {}'
.
format
(
num_g
))
indices
=
paddle
.
argsort
(
similarities_matrix
,
axis
=
1
,
descending
=
True
).
numpy
()
_
,
all_AP
,
_
=
get_metrics
(
indices
,
num_q
,
num_g
,
q_pids
,
g_pids
,
self
.
max_rank
)
mAP
=
np
.
mean
(
all_AP
)
metric_dict
[
"mAP"
]
=
mAP
return
metric_dict
class
mINP
(
nn
.
Layer
):
def
__init__
(
self
,
max_rank
=
50
):
super
().
__init__
()
self
.
max_rank
=
max_rank
def
forward
(
self
,
similarities_matrix
,
query_img_id
,
gallery_img_id
):
metric_dict
=
dict
()
num_q
,
num_g
=
similarities_matrix
.
shape
q_pids
=
query_img_id
.
numpy
().
reshape
((
query_img_id
.
shape
[
0
]))
g_pids
=
gallery_img_id
.
numpy
().
reshape
((
gallery_img_id
.
shape
[
0
]))
if
num_g
<
self
.
max_rank
:
max_rank
=
num_g
print
(
'Note: number of gallery samples is quite small, got {}'
.
format
(
num_g
))
indices
=
paddle
.
argsort
(
similarities_matrix
,
axis
=
1
,
descending
=
True
).
numpy
()
_
,
_
,
all_INP
=
get_metrics
(
indices
,
num_q
,
num_g
,
q_pids
,
g_pids
,
self
.
max_rank
)
mINP
=
np
.
mean
(
all_INP
)
metric_dict
[
"mINP"
]
=
mINP
return
metric_dict
class
Recallk
(
nn
.
Layer
):
def
__init__
(
self
,
max_rank
=
50
,
topk
=
(
1
,
5
)):
super
().
__init__
()
self
.
max_rank
=
max_rank
assert
isinstance
(
topk
,
(
int
,
list
))
if
isinstance
(
topk
,
int
):
topk
=
[
topk
]
self
.
topk
=
topk
def
forward
(
self
,
similarities_matrix
,
query_img_id
,
gallery_img_id
):
metric_dict
=
dict
()
num_q
,
num_g
=
similarities_matrix
.
shape
q_pids
=
query_img_id
.
numpy
().
reshape
((
query_img_id
.
shape
[
0
]))
g_pids
=
gallery_img_id
.
numpy
().
reshape
((
gallery_img_id
.
shape
[
0
]))
if
num_g
<
self
.
max_rank
:
max_rank
=
num_g
print
(
'Note: number of gallery samples is quite small, got {}'
.
format
(
num_g
))
indices
=
paddle
.
argsort
(
similarities_matrix
,
axis
=
1
,
descending
=
True
).
numpy
()
all_cmc
,
_
,
_
=
get_metrics
(
indices
,
num_q
,
num_g
,
q_pids
,
g_pids
,
self
.
max_rank
)
for
k
in
self
.
topk
:
metric_dict
[
"recall{}"
.
format
(
k
)]
=
all_cmc
[
k
-
1
]
return
metric_dict
def
get_metrics
(
indices
,
num_q
,
num_g
,
q_pids
,
g_pids
,
max_rank
=
50
):
all_cmc
=
[]
all_AP
=
[]
all_INP
=
[]
num_valid_q
=
0
matches
=
(
g_pids
[
indices
]
==
q_pids
[:,
np
.
newaxis
]).
astype
(
np
.
int32
)
for
q_idx
in
range
(
num_q
):
q_pid
=
q_pids
[
q_idx
]
order
=
indices
[
q_idx
]
remove
=
g_pids
[
order
]
==
q_pid
keep
=
np
.
invert
(
remove
)
raw_cmc
=
matches
[
q_idx
][
keep
]
if
not
np
.
any
(
raw_cmc
):
continue
cmc
=
raw_cmc
.
cumsum
()
pos_idx
=
np
.
where
(
raw_cmc
==
1
)
max_pos_idx
=
np
.
max
(
pos_idx
)
inp
=
cmc
[
max_pos_idx
]
/
(
max_pos_idx
+
1.0
)
all_INP
.
append
(
inp
)
cmc
[
cmc
>
1
]
=
1
all_cmc
.
append
(
cmc
[:
max_rank
])
num_valid_q
+=
1.
num_rel
=
raw_cmc
.
sum
()
tmp_cmc
=
raw_cmc
.
cumsum
()
tmp_cmc
=
[
x
/
(
i
+
1.
)
for
i
,
x
in
enumerate
(
tmp_cmc
)]
tmp_cmc
=
np
.
asarray
(
tmp_cmc
)
*
raw_cmc
AP
=
tmp_cmc
.
sum
()
/
num_rel
all_AP
.
append
(
AP
)
assert
num_valid_q
>
0
,
'Error: all query identities do not appear in gallery'
all_cmc
=
np
.
asarray
(
all_cmc
).
astype
(
np
.
float32
)
all_cmc
=
all_cmc
.
sum
(
0
)
/
num_valid_q
return
all_cmc
,
all_AP
,
all_INP
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录