Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
7b107160
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看板
提交
7b107160
编写于
11月 22, 2019
作者:
W
wanghaoshuang
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'sen_ck' into 'develop'
Add save checkpoint function to sensitive pruner. See merge request
!41
上级
0e2b3039
09954701
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
60 addition
and
6 deletion
+60
-6
demo/sensitive_prune/train.py
demo/sensitive_prune/train.py
+14
-5
paddleslim/prune/sensitive_pruner.py
paddleslim/prune/sensitive_pruner.py
+46
-1
未找到文件。
demo/sensitive_prune/train.py
浏览文件 @
7b107160
...
@@ -35,6 +35,7 @@ add_arg('config_file', str, None, "The config file for comp
...
@@ -35,6 +35,7 @@ add_arg('config_file', str, None, "The config file for comp
add_arg
(
'data'
,
str
,
"mnist"
,
"Which data to use. 'mnist' or 'imagenet'"
)
add_arg
(
'data'
,
str
,
"mnist"
,
"Which data to use. 'mnist' or 'imagenet'"
)
add_arg
(
'log_period'
,
int
,
10
,
"Log period in batches."
)
add_arg
(
'log_period'
,
int
,
10
,
"Log period in batches."
)
add_arg
(
'test_period'
,
int
,
10
,
"Test period in epoches."
)
add_arg
(
'test_period'
,
int
,
10
,
"Test period in epoches."
)
add_arg
(
'checkpoints'
,
str
,
"./checkpoints"
,
"Checkpoints path."
)
# yapf: enable
# yapf: enable
model_list
=
[
m
for
m
in
dir
(
models
)
if
"__"
not
in
m
]
model_list
=
[
m
for
m
in
dir
(
models
)
if
"__"
not
in
m
]
...
@@ -188,17 +189,25 @@ def compress(args):
...
@@ -188,17 +189,25 @@ def compress(args):
def
eval_func
(
program
):
def
eval_func
(
program
):
return
test
(
0
,
program
)
return
test
(
0
,
program
)
pruner
=
SensitivePruner
(
place
,
eval_func
)
if
args
.
data
==
"mnist"
:
if
args
.
data
==
"mnist"
:
train
(
0
,
fluid
.
default_main_program
())
train
(
0
,
fluid
.
default_main_program
())
pruned_program
=
fluid
.
default_main_program
()
pruned_val_program
=
val_program
pruner
=
SensitivePruner
(
place
,
eval_func
,
checkpoints
=
args
.
checkpoints
)
for
iter
in
range
(
6
):
pruned_program
,
pruned_val_program
,
iter
=
pruner
.
restore
()
if
pruned_program
is
None
:
pruned_program
=
fluid
.
default_main_program
()
if
pruned_val_program
is
None
:
pruned_val_program
=
val_program
start
=
iter
end
=
6
for
iter
in
range
(
start
,
end
):
pruned_program
,
pruned_val_program
=
pruner
.
prune
(
pruned_program
,
pruned_val_program
=
pruner
.
prune
(
pruned_program
,
pruned_val_program
,
params
,
0.1
)
pruned_program
,
pruned_val_program
,
params
,
0.1
)
train
(
iter
,
pruned_program
)
train
(
iter
,
pruned_program
)
test
(
iter
,
pruned_val_program
)
test
(
iter
,
pruned_val_program
)
pruner
.
save_checkpoint
(
pruned_program
,
pruned_val_program
)
print
(
"before flops: {}"
.
format
(
flops
(
fluid
.
default_main_program
())))
print
(
"before flops: {}"
.
format
(
flops
(
fluid
.
default_main_program
())))
print
(
"after flops: {}"
.
format
(
flops
(
pruned_val_program
)))
print
(
"after flops: {}"
.
format
(
flops
(
pruned_val_program
)))
...
...
paddleslim/prune/sensitive_pruner.py
浏览文件 @
7b107160
...
@@ -12,6 +12,7 @@
...
@@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# See the License for the specific language governing permissions and
# limitations under the License.
# limitations under the License.
import
os
import
logging
import
logging
import
copy
import
copy
from
scipy.optimize
import
leastsq
from
scipy.optimize
import
leastsq
...
@@ -28,7 +29,7 @@ _logger = get_logger(__name__, level=logging.INFO)
...
@@ -28,7 +29,7 @@ _logger = get_logger(__name__, level=logging.INFO)
class
SensitivePruner
(
object
):
class
SensitivePruner
(
object
):
def
__init__
(
self
,
place
,
eval_func
,
scope
=
None
):
def
__init__
(
self
,
place
,
eval_func
,
scope
=
None
,
checkpoints
=
None
):
"""
"""
Pruner used to prune parameters iteratively according to sensitivities of parameters in each step.
Pruner used to prune parameters iteratively according to sensitivities of parameters in each step.
Args:
Args:
...
@@ -41,6 +42,50 @@ class SensitivePruner(object):
...
@@ -41,6 +42,50 @@ class SensitivePruner(object):
self
.
_place
=
place
self
.
_place
=
place
self
.
_scope
=
fluid
.
global_scope
()
if
scope
is
None
else
scope
self
.
_scope
=
fluid
.
global_scope
()
if
scope
is
None
else
scope
self
.
_pruner
=
Pruner
()
self
.
_pruner
=
Pruner
()
self
.
_checkpoints
=
checkpoints
def
save_checkpoint
(
self
,
train_program
,
eval_program
):
checkpoint
=
os
.
path
.
join
(
self
.
_checkpoints
,
str
(
self
.
_iter
-
1
))
exe
=
fluid
.
Executor
(
self
.
_place
)
fluid
.
io
.
save_persistables
(
exe
,
checkpoint
,
main_program
=
train_program
,
filename
=
"__params__"
)
with
open
(
checkpoint
+
"/main_program"
,
"wb"
)
as
f
:
f
.
write
(
train_program
.
desc
.
serialize_to_string
())
with
open
(
checkpoint
+
"/eval_program"
,
"wb"
)
as
f
:
f
.
write
(
eval_program
.
desc
.
serialize_to_string
())
def
restore
(
self
,
checkpoints
=
None
):
exe
=
fluid
.
Executor
(
self
.
_place
)
checkpoints
=
self
.
_checkpoints
if
checkpoints
is
None
else
checkpoints
print
(
"check points: {}"
.
format
(
checkpoints
))
main_program
=
None
eval_program
=
None
if
checkpoints
is
not
None
:
cks
=
[
dir
for
dir
in
os
.
listdir
(
checkpoints
)]
if
len
(
cks
)
>
0
:
latest
=
max
([
int
(
ck
)
for
ck
in
cks
])
latest_ck_path
=
os
.
path
.
join
(
checkpoints
,
str
(
latest
))
self
.
_iter
+=
1
with
open
(
latest_ck_path
+
"/main_program"
,
"rb"
)
as
f
:
program_desc_str
=
f
.
read
()
main_program
=
fluid
.
Program
.
parse_from_string
(
program_desc_str
)
print
main_program
with
open
(
latest_ck_path
+
"/eval_program"
,
"rb"
)
as
f
:
program_desc_str
=
f
.
read
()
eval_program
=
fluid
.
Program
.
parse_from_string
(
program_desc_str
)
with
fluid
.
scope_guard
(
self
.
_scope
):
fluid
.
io
.
load_persistables
(
exe
,
latest_ck_path
,
main_program
,
"__params__"
)
print
(
"load checkpoint from: {}"
.
format
(
latest_ck_path
))
print
(
"flops of eval program: {}"
.
format
(
flops
(
eval_program
)))
return
main_program
,
eval_program
,
self
.
_iter
def
prune
(
self
,
train_program
,
eval_program
,
params
,
pruned_flops
):
def
prune
(
self
,
train_program
,
eval_program
,
params
,
pruned_flops
):
"""
"""
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录