Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
cf7fb3c5
P
PaddleSlim
项目概览
PaddlePaddle
/
PaddleSlim
1 年多 前同步成功
通知
51
Star
1434
Fork
344
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
53
列表
看板
标记
里程碑
合并请求
16
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleSlim
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
53
Issue
53
列表
看板
标记
里程碑
合并请求
16
合并请求
16
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
cf7fb3c5
编写于
12月 04, 2019
作者:
W
wanghaoshuang
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'develop' into 'develop'
Refine dist.merge(), remove some redundant interface See merge request !62
上级
b0972527
4e8c0f4c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
31 addition
and
41 deletion
+31
-41
demo/distillation/train.py
demo/distillation/train.py
+26
-29
paddleslim/dist/single_distiller.py
paddleslim/dist/single_distiller.py
+5
-12
未找到文件。
demo/distillation/train.py
浏览文件 @
cf7fb3c5
...
@@ -140,41 +140,38 @@ def compress(args):
...
@@ -140,41 +140,38 @@ def compress(args):
# define teacher program
# define teacher program
teacher_program
=
fluid
.
Program
()
teacher_program
=
fluid
.
Program
()
t_startup
=
fluid
.
Program
()
t_startup
=
fluid
.
Program
()
teacher_scope
=
fluid
.
Scope
()
with
fluid
.
program_guard
(
teacher_program
,
t_startup
):
with
fluid
.
scope_guard
(
teacher_scope
):
with
fluid
.
unique_name
.
guard
():
with
fluid
.
program_guard
(
teacher_program
,
t_startup
):
image
=
fluid
.
layers
.
data
(
with
fluid
.
unique_name
.
guard
():
name
=
'image'
,
shape
=
image_shape
,
dtype
=
'float32'
)
image
=
fluid
.
layers
.
data
(
predict
=
teacher_model
.
net
(
image
,
class_dim
=
class_dim
)
name
=
'image'
,
shape
=
image_shape
,
dtype
=
'float32'
)
predict
=
teacher_model
.
net
(
image
,
class_dim
=
class_dim
)
#print("="*50+"teacher_model_params"+"="*50)
#for v in teacher_program.list_vars():
#print("="*50+"teacher_model_params"+"="*50)
# print(v.name, v.shape)
#for v in teacher_program.list_vars():
# print(v.name, v.shape)
exe
.
run
(
t_startup
)
assert
args
.
teacher_pretrained_model
and
os
.
path
.
exists
(
exe
.
run
(
t_startup
)
args
.
teacher_pretrained_model
assert
args
.
teacher_pretrained_model
and
os
.
path
.
exists
(
),
"teacher_pretrained_model should be set when teacher_model is not None."
args
.
teacher_pretrained_model
),
"teacher_pretrained_model should be set when teacher_model is not None."
def
if_exist
(
var
):
return
os
.
path
.
exists
(
def
if_exist
(
var
):
os
.
path
.
join
(
args
.
teacher_pretrained_model
,
var
.
name
)
return
os
.
path
.
exists
(
)
and
var
.
name
!=
'conv1_weights'
and
var
.
name
!=
'fc_0.w_0'
and
var
.
name
!=
'fc_0.b_0'
os
.
path
.
join
(
args
.
teacher_pretrained_model
,
var
.
name
)
)
and
var
.
name
!=
'conv1_weights'
and
var
.
name
!=
'fc_0.w_0'
and
var
.
name
!=
'fc_0.b_0'
fluid
.
io
.
load_vars
(
exe
,
fluid
.
io
.
load_vars
(
args
.
teacher_pretrained_model
,
exe
,
main_program
=
teacher_program
,
args
.
teacher_pretrained_model
,
predicate
=
if_exist
)
main_program
=
teacher_program
,
predicate
=
if_exist
)
data_name_map
=
{
'image'
:
'image'
}
data_name_map
=
{
'image'
:
'image'
}
main
=
merge
(
main
=
merge
(
teacher_program
,
teacher_program
,
student_program
,
student_program
,
data_name_map
,
data_name_map
,
place
,
place
)
teacher_scope
=
teacher_scope
)
#print("="*50+"teacher_vars"+"="*50)
#print("="*50+"teacher_vars"+"="*50)
#for v in teacher_program.list_vars():
#for v in teacher_program.list_vars():
...
...
paddleslim/dist/single_distiller.py
浏览文件 @
cf7fb3c5
...
@@ -20,8 +20,7 @@ def merge(teacher_program,
...
@@ -20,8 +20,7 @@ def merge(teacher_program,
student_program
,
student_program
,
data_name_map
,
data_name_map
,
place
,
place
,
teacher_scope
=
fluid
.
global_scope
(),
scope
=
fluid
.
global_scope
(),
student_scope
=
fluid
.
global_scope
(),
name_prefix
=
'teacher_'
):
name_prefix
=
'teacher_'
):
"""
"""
Merge teacher program into student program and add a uniform prefix to the
Merge teacher program into student program and add a uniform prefix to the
...
@@ -33,8 +32,7 @@ def merge(teacher_program,
...
@@ -33,8 +32,7 @@ def merge(teacher_program,
and the student var name
and the student var name
place(fluid.CPUPlace()|fluid.CUDAPlace(N)): This parameter represents
place(fluid.CPUPlace()|fluid.CUDAPlace(N)): This parameter represents
paddle run on which device.
paddle run on which device.
student_scope(Scope): The input student scope
scope(Scope): The input scope
teacher_scope(Scope): The input teacher scope
name_prefix(str): Name prefix added for all vars of the teacher program.
name_prefix(str): Name prefix added for all vars of the teacher program.
Return(Program): Merged program.
Return(Program): Merged program.
"""
"""
...
@@ -50,9 +48,9 @@ def merge(teacher_program,
...
@@ -50,9 +48,9 @@ def merge(teacher_program,
new_name
=
name_prefix
+
teacher_var
.
name
new_name
=
name_prefix
+
teacher_var
.
name
if
not
skip_rename
:
if
not
skip_rename
:
# scope var rename
# scope var rename
scope_var
=
teacher_
scope
.
var
(
teacher_var
.
name
).
get_tensor
()
old_var
=
scope
.
var
(
teacher_var
.
name
).
get_tensor
()
renamed_
scope_var
=
teacher_
scope
.
var
(
new_name
).
get_tensor
()
renamed_
var
=
scope
.
var
(
new_name
).
get_tensor
()
renamed_
scope_var
.
set
(
np
.
array
(
scope
_var
),
place
)
renamed_
var
.
set
(
np
.
array
(
old
_var
),
place
)
# program var rename
# program var rename
renamed_var
=
teacher_program
.
global_block
().
_rename_var
(
renamed_var
=
teacher_program
.
global_block
().
_rename_var
(
...
@@ -60,11 +58,6 @@ def merge(teacher_program,
...
@@ -60,11 +58,6 @@ def merge(teacher_program,
for
teacher_var
in
teacher_program
.
list_vars
():
for
teacher_var
in
teacher_program
.
list_vars
():
if
teacher_var
.
name
!=
'fetch'
and
teacher_var
.
name
!=
'feed'
:
if
teacher_var
.
name
!=
'fetch'
and
teacher_var
.
name
!=
'feed'
:
# student scope add var
student_scope_var
=
student_scope
.
var
(
teacher_var
.
name
).
get_tensor
()
teacher_scope_var
=
teacher_scope
.
var
(
teacher_var
.
name
).
get_tensor
()
student_scope_var
.
set
(
np
.
array
(
teacher_scope_var
),
place
)
# student program add var
# student program add var
new_var
=
student_program
.
global_block
().
_clone_variable
(
new_var
=
student_program
.
global_block
().
_clone_variable
(
teacher_var
,
force_persistable
=
False
)
teacher_var
,
force_persistable
=
False
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录