Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
bac761fb
M
models
项目概览
PaddlePaddle
/
models
接近 2 年 前同步成功
通知
230
Star
6828
Fork
2962
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
602
列表
看板
标记
里程碑
合并请求
255
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
models
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
602
Issue
602
列表
看板
标记
里程碑
合并请求
255
合并请求
255
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
bac761fb
编写于
10月 11, 2019
作者:
W
wopeizl
提交者:
qingqing01
10月 11, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix the out of memory issue test=develop (#3486)
上级
7dcc3890
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
66 addition
and
5 deletion
+66
-5
PaddleCV/metric_learning/utility.py
PaddleCV/metric_learning/utility.py
+66
-5
未找到文件。
PaddleCV/metric_learning/utility.py
浏览文件 @
bac761fb
...
...
@@ -26,6 +26,7 @@ import numpy as np
import
sys
import
paddle.fluid
as
fluid
from
paddle.fluid
import
core
import
multiprocessing
as
mp
def
print_arguments
(
args
):
...
...
@@ -68,21 +69,23 @@ def add_arguments(argname, type, default, help, argparser, **kwargs):
help
=
help
+
' Default: %(default)s.'
,
**
kwargs
)
def
fmt_time
():
""" get formatted time for now
"""
now_str
=
time
.
strftime
(
'%Y-%m-%d %H:%M:%S'
,
time
.
localtime
(
time
.
time
()))
return
now_str
def
recall_topk
(
fea
,
lab
,
k
=
1
):
def
recall_topk_ori
(
fea
,
lab
,
k
):
fea
=
np
.
array
(
fea
)
fea
=
fea
.
reshape
(
fea
.
shape
[
0
],
-
1
)
n
=
np
.
sqrt
(
np
.
sum
(
fea
**
2
,
1
)).
reshape
(
-
1
,
1
)
fea
=
fea
/
n
a
=
np
.
sum
(
fea
**
2
,
1
).
reshape
(
-
1
,
1
)
a
=
np
.
sum
(
fea
**
2
,
1
).
reshape
(
-
1
,
1
)
b
=
a
.
T
ab
=
np
.
dot
(
fea
,
fea
.
T
)
d
=
a
+
b
-
2
*
ab
d
=
a
+
b
-
2
*
ab
d
=
d
+
np
.
eye
(
len
(
fea
))
*
1e8
sorted_index
=
np
.
argsort
(
d
,
1
)
res
=
0
...
...
@@ -95,13 +98,72 @@ def recall_topk(fea, lab, k = 1):
res
=
res
/
len
(
fea
)
return
res
def
func
(
param
):
sharedlist
,
s
,
e
=
param
fea
,
a
,
b
=
sharedlist
ab
=
np
.
dot
(
fea
[
s
:
e
],
fea
.
T
)
d
=
a
[
s
:
e
]
+
b
-
2
*
ab
for
i
in
range
(
e
-
s
):
d
[
i
][
s
+
i
]
+=
1e8
sorted_index
=
np
.
argsort
(
d
,
1
)[:,
:
10
]
return
sorted_index
def
recall_topk_parallel
(
fea
,
lab
,
k
):
fea
=
np
.
array
(
fea
)
fea
=
fea
.
reshape
(
fea
.
shape
[
0
],
-
1
)
n
=
np
.
sqrt
(
np
.
sum
(
fea
**
2
,
1
)).
reshape
(
-
1
,
1
)
fea
=
fea
/
n
a
=
np
.
sum
(
fea
**
2
,
1
).
reshape
(
-
1
,
1
)
b
=
a
.
T
sharedlist
=
mp
.
Manager
().
list
()
sharedlist
.
append
(
fea
)
sharedlist
.
append
(
a
)
sharedlist
.
append
(
b
)
N
=
100
L
=
fea
.
shape
[
0
]
/
N
params
=
[]
for
i
in
xrange
(
N
):
if
i
==
N
-
1
:
s
,
e
=
int
(
i
*
L
),
int
(
fea
.
shape
[
0
])
else
:
s
,
e
=
int
(
i
*
L
),
int
((
i
+
1
)
*
L
)
params
.
append
([
sharedlist
,
s
,
e
])
pool
=
mp
.
Pool
(
processes
=
4
)
sorted_index_list
=
pool
.
map
(
func
,
params
)
pool
.
close
()
pool
.
join
()
sorted_index
=
np
.
vstack
(
sorted_index_list
)
res
=
0
for
i
in
range
(
len
(
fea
)):
for
j
in
range
(
k
):
pred
=
lab
[
sorted_index
[
i
][
j
]]
if
lab
[
i
]
==
pred
:
res
+=
1.0
break
res
=
res
/
len
(
fea
)
return
res
def
recall_topk
(
fea
,
lab
,
k
=
1
):
if
fea
.
shape
[
0
]
<
20
:
return
recall_topk_ori
(
fea
,
lab
,
k
)
else
:
return
recall_topk_parallel
(
fea
,
lab
,
k
)
def
get_gpu_num
():
visibledevice
=
os
.
getenv
(
'CUDA_VISIBLE_DEVICES'
)
if
visibledevice
:
devicenum
=
len
(
visibledevice
.
split
(
','
))
else
:
devicenum
=
subprocess
.
check_output
(
[
str
.
encode
(
'nvidia-smi'
),
str
.
encode
(
'-L'
)]).
decode
(
'utf-8'
).
count
(
'
\n
'
)
[
str
.
encode
(
'nvidia-smi'
),
str
.
encode
(
'-L'
)]).
decode
(
'utf-8'
).
count
(
'
\n
'
)
return
devicenum
def
check_cuda
(
use_cuda
,
err
=
\
...
...
@@ -114,4 +176,3 @@ def check_cuda(use_cuda, err = \
sys
.
exit
(
1
)
except
Exception
as
e
:
pass
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录