Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
8ffb5bff
M
models
项目概览
PaddlePaddle
/
models
1 年多 前同步成功
通知
222
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看板
未验证
提交
8ffb5bff
编写于
11月 03, 2020
作者:
L
littletomatodonkey
提交者:
GitHub
11月 03, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix load in metric learning (#4928)
* fix load in metric learning * fix load in metric learning * fix typo * fix typo
上级
2f52d598
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
63 addition
and
4 deletion
+63
-4
PaddleCV/metric_learning/train_elem.py
PaddleCV/metric_learning/train_elem.py
+2
-2
PaddleCV/metric_learning/train_pair.py
PaddleCV/metric_learning/train_pair.py
+2
-2
PaddleCV/metric_learning/utility.py
PaddleCV/metric_learning/utility.py
+59
-0
未找到文件。
PaddleCV/metric_learning/train_elem.py
浏览文件 @
8ffb5bff
...
...
@@ -33,6 +33,7 @@ from losses import SoftmaxLoss
from
losses
import
ArcMarginLoss
from
utility
import
add_arguments
,
print_arguments
from
utility
import
fmt_time
,
recall_topk
,
get_gpu_num
,
check_cuda
from
utility
import
load_params
parser
=
argparse
.
ArgumentParser
(
description
=
__doc__
)
add_arg
=
functools
.
partial
(
add_arguments
,
argparser
=
parser
)
...
...
@@ -190,8 +191,7 @@ def train_async(args):
fluid
.
load
(
program
=
train_prog
,
model_path
=
checkpoint
,
executor
=
exe
)
if
pretrained_model
:
fluid
.
load
(
program
=
train_prog
,
model_path
=
pretrained_model
,
executor
=
exe
)
load_params
(
exe
,
train_prog
,
pretrained_model
)
if
args
.
use_gpu
:
devicenum
=
get_gpu_num
()
...
...
PaddleCV/metric_learning/train_pair.py
浏览文件 @
8ffb5bff
...
...
@@ -35,6 +35,7 @@ from losses import EmlLoss
from
losses
import
NpairsLoss
from
utility
import
add_arguments
,
print_arguments
from
utility
import
fmt_time
,
recall_topk
,
get_gpu_num
,
check_cuda
from
utility
import
load_params
parser
=
argparse
.
ArgumentParser
(
description
=
__doc__
)
add_arg
=
functools
.
partial
(
add_arguments
,
argparser
=
parser
)
...
...
@@ -188,8 +189,7 @@ def train_async(args):
fluid
.
load
(
program
=
train_prog
,
model_path
=
checkpoint
,
executor
=
exe
)
if
pretrained_model
:
fluid
.
load
(
program
=
train_prog
,
model_path
=
pretrained_model
,
executor
=
exe
)
load_params
(
exe
,
train_prog
,
pretrained_model
)
if
args
.
use_gpu
:
devicenum
=
get_gpu_num
()
...
...
PaddleCV/metric_learning/utility.py
浏览文件 @
8ffb5bff
...
...
@@ -176,3 +176,62 @@ def check_cuda(use_cuda, err = \
sys
.
exit
(
1
)
except
Exception
as
e
:
pass
def
_load_state
(
path
):
if
os
.
path
.
exists
(
path
+
'.pdopt'
):
# XXX another hack to ignore the optimizer state
tmp
=
tempfile
.
mkdtemp
()
dst
=
os
.
path
.
join
(
tmp
,
os
.
path
.
basename
(
os
.
path
.
normpath
(
path
)))
shutil
.
copy
(
path
+
'.pdparams'
,
dst
+
'.pdparams'
)
state
=
fluid
.
io
.
load_program_state
(
dst
)
shutil
.
rmtree
(
tmp
)
else
:
state
=
fluid
.
io
.
load_program_state
(
path
)
return
state
def
load_params
(
exe
,
prog
,
path
,
ignore_params
=
None
):
"""
Load model from the given path.
Args:
exe (fluid.Executor): The fluid.Executor object.
prog (fluid.Program): load weight to which Program object.
path (string): local model path.
ignore_params (list): ignore variable to load when finetuning.
"""
if
not
(
os
.
path
.
isdir
(
path
)
or
os
.
path
.
exists
(
path
+
'.pdparams'
)):
raise
ValueError
(
"Model pretrain path {} does not "
"exists."
.
format
(
path
))
print
(
'Loading parameters from {}...'
.
format
(
path
))
ignore_set
=
set
()
state
=
_load_state
(
path
)
# ignore the parameter which mismatch the shape
# between the model and pretrain weight.
all_var_shape
=
{}
for
block
in
prog
.
blocks
:
for
param
in
block
.
all_parameters
():
all_var_shape
[
param
.
name
]
=
param
.
shape
ignore_set
.
update
([
name
for
name
,
shape
in
all_var_shape
.
items
()
if
name
in
state
and
shape
!=
state
[
name
].
shape
])
if
ignore_params
:
all_var_names
=
[
var
.
name
for
var
in
prog
.
list_vars
()]
ignore_list
=
filter
(
lambda
var
:
any
([
re
.
match
(
name
,
var
)
for
name
in
ignore_params
]),
all_var_names
)
ignore_set
.
update
(
list
(
ignore_list
))
if
len
(
ignore_set
)
>
0
:
for
k
in
ignore_set
:
if
k
in
state
:
print
(
'warning: variable {} is already excluded automatically'
.
format
(
k
))
del
state
[
k
]
fluid
.
io
.
set_program_state
(
prog
,
state
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录