Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleX
提交
3905ef2c
P
PaddleX
项目概览
PaddlePaddle
/
PaddleX
通知
138
Star
4
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
43
列表
看板
标记
里程碑
合并请求
5
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleX
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
43
Issue
43
列表
看板
标记
里程碑
合并请求
5
合并请求
5
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
3905ef2c
编写于
9月 22, 2020
作者:
J
jiangjiajun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix scope problem for slim
上级
f32753d2
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
21 addition
and
13 deletion
+21
-13
paddlex/cv/models/slim/prune.py
paddlex/cv/models/slim/prune.py
+12
-8
paddlex/cv/models/slim/prune_config.py
paddlex/cv/models/slim/prune_config.py
+8
-4
paddlex/cv/models/slim/visualize.py
paddlex/cv/models/slim/visualize.py
+1
-1
未找到文件。
paddlex/cv/models/slim/prune.py
浏览文件 @
3905ef2c
...
@@ -104,7 +104,7 @@ def sensitivity(program,
...
@@ -104,7 +104,7 @@ def sensitivity(program,
return
sensitivities
return
sensitivities
def
channel_prune
(
program
,
prune_names
,
prune_ratios
,
place
,
only_graph
=
False
):
def
channel_prune
(
program
,
prune_names
,
prune_ratios
,
place
,
only_graph
=
False
,
scope
=
None
):
"""通道裁剪。
"""通道裁剪。
Args:
Args:
...
@@ -134,6 +134,7 @@ def channel_prune(program, prune_names, prune_ratios, place, only_graph=False):
...
@@ -134,6 +134,7 @@ def channel_prune(program, prune_names, prune_ratios, place, only_graph=False):
pruned_num
=
int
(
round
(
origin_num
*
(
ratio
)))
pruned_num
=
int
(
round
(
origin_num
*
(
ratio
)))
prune_ratios
[
index
]
=
ratio
prune_ratios
[
index
]
=
ratio
index
+=
1
index
+=
1
if
scope
is
None
:
scope
=
fluid
.
global_scope
()
scope
=
fluid
.
global_scope
()
pruner
=
Pruner
()
pruner
=
Pruner
()
program
,
_
,
_
=
pruner
.
prune
(
program
,
_
,
_
=
pruner
.
prune
(
...
@@ -175,12 +176,12 @@ def prune_program(model, prune_params_ratios=None):
...
@@ -175,12 +176,12 @@ def prune_program(model, prune_params_ratios=None):
prune_params_ratios
[
prune_name
]
for
prune_name
in
prune_names
prune_params_ratios
[
prune_name
]
for
prune_name
in
prune_names
]
]
model
.
train_prog
=
channel_prune
(
train_prog
,
prune_names
,
prune_ratios
,
model
.
train_prog
=
channel_prune
(
train_prog
,
prune_names
,
prune_ratios
,
place
)
place
,
scope
=
model
.
scope
)
model
.
test_prog
=
channel_prune
(
model
.
test_prog
=
channel_prune
(
eval_prog
,
prune_names
,
prune_ratios
,
place
,
only_graph
=
True
)
eval_prog
,
prune_names
,
prune_ratios
,
place
,
only_graph
=
True
,
scope
=
model
.
scope
)
def
update_program
(
program
,
model_dir
,
place
):
def
update_program
(
program
,
model_dir
,
place
,
scope
=
None
):
"""根据裁剪信息更新Program和参数。
"""根据裁剪信息更新Program和参数。
Args:
Args:
...
@@ -197,10 +198,12 @@ def update_program(program, model_dir, place):
...
@@ -197,10 +198,12 @@ def update_program(program, model_dir, place):
shapes
=
yaml
.
load
(
f
.
read
(),
Loader
=
yaml
.
Loader
)
shapes
=
yaml
.
load
(
f
.
read
(),
Loader
=
yaml
.
Loader
)
for
param
,
shape
in
shapes
.
items
():
for
param
,
shape
in
shapes
.
items
():
graph
.
var
(
param
).
set_shape
(
shape
)
graph
.
var
(
param
).
set_shape
(
shape
)
if
scope
is
None
:
scope
=
fluid
.
global_scope
()
for
block
in
program
.
blocks
:
for
block
in
program
.
blocks
:
for
param
in
block
.
all_parameters
():
for
param
in
block
.
all_parameters
():
if
param
.
name
in
shapes
:
if
param
.
name
in
shapes
:
param_tensor
=
fluid
.
global_scope
()
.
find_var
(
param_tensor
=
scope
.
find_var
(
param
.
name
).
get_tensor
()
param
.
name
).
get_tensor
()
param_tensor
.
set
(
param_tensor
.
set
(
np
.
zeros
(
list
(
shapes
[
param
.
name
])).
astype
(
'float32'
),
np
.
zeros
(
list
(
shapes
[
param
.
name
])).
astype
(
'float32'
),
...
@@ -293,7 +296,7 @@ def get_params_ratios(sensitivities_file, eval_metric_loss=0.05):
...
@@ -293,7 +296,7 @@ def get_params_ratios(sensitivities_file, eval_metric_loss=0.05):
return
params_ratios
return
params_ratios
def
cal_model_size
(
program
,
place
,
sensitivities_file
,
eval_metric_loss
=
0.05
):
def
cal_model_size
(
program
,
place
,
sensitivities_file
,
eval_metric_loss
=
0.05
,
scope
=
None
):
"""在可容忍的精度损失下,计算裁剪后模型大小相对于当前模型大小的比例。
"""在可容忍的精度损失下,计算裁剪后模型大小相对于当前模型大小的比例。
Args:
Args:
...
@@ -326,7 +329,8 @@ def cal_model_size(program, place, sensitivities_file, eval_metric_loss=0.05):
...
@@ -326,7 +329,8 @@ def cal_model_size(program, place, sensitivities_file, eval_metric_loss=0.05):
list
(
prune_params_ratios
.
keys
()),
list
(
prune_params_ratios
.
keys
()),
list
(
prune_params_ratios
.
values
()),
list
(
prune_params_ratios
.
values
()),
place
,
place
,
only_graph
=
True
)
only_graph
=
True
,
scope
=
scope
)
origin_size
=
0
origin_size
=
0
new_size
=
0
new_size
=
0
for
var
in
program
.
list_vars
():
for
var
in
program
.
list_vars
():
...
...
paddlex/cv/models/slim/prune_config.py
浏览文件 @
3905ef2c
...
@@ -171,10 +171,14 @@ def get_prune_params(model):
...
@@ -171,10 +171,14 @@ def get_prune_params(model):
model_type
.
startswith
(
'ShuffleNetV2'
):
model_type
.
startswith
(
'ShuffleNetV2'
):
for
block
in
program
.
blocks
:
for
block
in
program
.
blocks
:
for
param
in
block
.
all_parameters
():
for
param
in
block
.
all_parameters
():
pd_var
=
fluid
.
global_scope
().
find_var
(
param
.
name
)
pd_var
=
model
.
scope
.
find_var
(
param
.
name
)
try
:
pd_param
=
pd_var
.
get_tensor
()
pd_param
=
pd_var
.
get_tensor
()
if
len
(
np
.
array
(
pd_param
).
shape
)
==
4
:
if
len
(
np
.
array
(
pd_param
).
shape
)
==
4
:
prune_names
.
append
(
param
.
name
)
prune_names
.
append
(
param
.
name
)
except
Exception
as
e
:
print
(
"None Tensor Name: "
,
param
.
name
)
print
(
"Error message: {}"
.
format
(
e
))
if
model_type
==
'AlexNet'
:
if
model_type
==
'AlexNet'
:
prune_names
.
remove
(
'conv5_weights'
)
prune_names
.
remove
(
'conv5_weights'
)
if
model_type
==
'ShuffleNetV2'
:
if
model_type
==
'ShuffleNetV2'
:
...
...
paddlex/cv/models/slim/visualize.py
浏览文件 @
3905ef2c
...
@@ -42,7 +42,7 @@ def visualize(model, sensitivities_file, save_dir='./'):
...
@@ -42,7 +42,7 @@ def visualize(model, sensitivities_file, save_dir='./'):
y
=
list
()
y
=
list
()
for
loss_thresh
in
tqdm
.
tqdm
(
list
(
np
.
arange
(
0.05
,
1
,
0.05
))):
for
loss_thresh
in
tqdm
.
tqdm
(
list
(
np
.
arange
(
0.05
,
1
,
0.05
))):
prune_ratio
=
1
-
cal_model_size
(
prune_ratio
=
1
-
cal_model_size
(
program
,
place
,
sensitivities_file
,
eval_metric_loss
=
loss_thresh
)
program
,
place
,
sensitivities_file
,
eval_metric_loss
=
loss_thresh
,
scope
=
model
.
scope
)
x
.
append
(
prune_ratio
)
x
.
append
(
prune_ratio
)
y
.
append
(
loss_thresh
)
y
.
append
(
loss_thresh
)
plt
.
plot
(
x
,
y
,
color
=
'green'
,
linewidth
=
0.5
,
marker
=
'o'
,
markersize
=
3
)
plt
.
plot
(
x
,
y
,
color
=
'green'
,
linewidth
=
0.5
,
marker
=
'o'
,
markersize
=
3
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录