Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
1af11949
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看板
未验证
提交
1af11949
编写于
4月 02, 2019
作者:
Z
Zhen Wang
提交者:
GitHub
4月 02, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1965 from wzzju/add_checkoutpoint
Add checkpoint function for pass.
上级
3765db3d
e7be3bd9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
102 addition
and
12 deletion
+102
-12
fluid/PaddleSlim/quant_low_level_api/quant.py
fluid/PaddleSlim/quant_low_level_api/quant.py
+16
-5
fluid/PaddleSlim/quant_low_level_api/run_quant.sh
fluid/PaddleSlim/quant_low_level_api/run_quant.sh
+7
-7
fluid/PaddleSlim/utility.py
fluid/PaddleSlim/utility.py
+79
-0
未找到文件。
fluid/PaddleSlim/quant_low_level_api/quant.py
浏览文件 @
1af11949
...
...
@@ -20,6 +20,7 @@ sys.path.append('..')
import
reader
import
models
from
utility
import
add_arguments
,
print_arguments
from
utility
import
save_persistable_nodes
,
load_persistable_nodes
parser
=
argparse
.
ArgumentParser
(
description
=
__doc__
)
add_arg
=
functools
.
partial
(
add_arguments
,
argparser
=
parser
)
...
...
@@ -31,7 +32,8 @@ add_arg('num_epochs', int, 120, "number of epochs.")
add_arg
(
'class_dim'
,
int
,
1000
,
"Class number."
)
add_arg
(
'image_shape'
,
str
,
"3,224,224"
,
"input image size"
)
add_arg
(
'model_save_dir'
,
str
,
"output"
,
"model save directory"
)
add_arg
(
'pretrained_model'
,
str
,
None
,
"Whether to use pretrained model."
)
add_arg
(
'pretrained_fp32_model'
,
str
,
None
,
"Whether to use the pretrained float32 model to initialize the weights."
)
add_arg
(
'checkpoint'
,
str
,
None
,
"Whether to resume the training process from the checkpoint."
)
add_arg
(
'lr'
,
float
,
0.1
,
"set learning rate."
)
add_arg
(
'lr_strategy'
,
str
,
"piecewise_decay"
,
"Set the learning rate decay strategy."
)
add_arg
(
'model'
,
str
,
"SE_ResNeXt50_32x4d"
,
"Set the network to use."
)
...
...
@@ -180,7 +182,8 @@ def build_program(is_train, main_prog, startup_prog, args):
def
train
(
args
):
# parameters from arguments
model_name
=
args
.
model
pretrained_model
=
args
.
pretrained_model
pretrained_fp32_model
=
args
.
pretrained_fp32_model
checkpoint
=
args
.
checkpoint
model_save_dir
=
args
.
model_save_dir
data_dir
=
args
.
data_dir
activation_quant_type
=
args
.
act_quant_type
...
...
@@ -210,11 +213,11 @@ def train(args):
main_graph
=
IrGraph
(
core
.
Graph
(
train_prog
.
desc
),
for_test
=
False
)
test_graph
=
IrGraph
(
core
.
Graph
(
test_prog
.
desc
),
for_test
=
True
)
if
pretrained_model
:
if
pretrained_
fp32_
model
:
def
if_exist
(
var
):
return
os
.
path
.
exists
(
os
.
path
.
join
(
pretrained_model
,
var
.
name
))
return
os
.
path
.
exists
(
os
.
path
.
join
(
pretrained_
fp32_
model
,
var
.
name
))
fluid
.
io
.
load_vars
(
exe
,
pretrained_model
,
main_program
=
train_prog
,
predicate
=
if_exist
)
exe
,
pretrained_
fp32_
model
,
main_program
=
train_prog
,
predicate
=
if_exist
)
if
args
.
use_gpu
:
visible_device
=
os
.
getenv
(
'CUDA_VISIBLE_DEVICES'
)
...
...
@@ -248,6 +251,9 @@ def train(args):
transform_pass
.
apply
(
main_graph
)
transform_pass
.
apply
(
test_graph
)
if
checkpoint
:
load_persistable_nodes
(
exe
,
checkpoint
,
main_graph
)
build_strategy
=
fluid
.
BuildStrategy
()
build_strategy
.
memory_optimize
=
False
build_strategy
.
enable_inplace
=
False
...
...
@@ -327,6 +333,11 @@ def train(args):
test_acc1
,
test_acc5
))
sys
.
stdout
.
flush
()
save_checkpoint_path
=
os
.
path
.
join
(
model_save_dir
,
model_name
,
str
(
pass_id
))
if
not
os
.
path
.
isdir
(
save_checkpoint_path
):
os
.
makedirs
(
save_checkpoint_path
)
save_persistable_nodes
(
exe
,
save_checkpoint_path
,
main_graph
)
model_path
=
os
.
path
.
join
(
model_save_dir
,
model_name
,
args
.
act_quant_type
)
float_path
=
os
.
path
.
join
(
model_path
,
'float'
)
int8_path
=
os
.
path
.
join
(
model_path
,
'int8'
)
...
...
fluid/PaddleSlim/quant_low_level_api/run_quant.sh
浏览文件 @
1af11949
#!/usr/bin/env bash
export
CUDA_VISIBLE_DEVICES
=
0
export
CUDA_VISIBLE_DEVICES
=
0
,1,2,3
#MobileNet v1:
python quant.py
\
--model
=
MobileNet
\
--pretrained_model
=
../data/pretrain/MobileNetV1_pretrained
\
--pretrained_
fp32_
model
=
../data/pretrain/MobileNetV1_pretrained
\
--use_gpu
=
True
\
--data_dir
=
../data/ILSVRC2012
\
--batch_size
=
64
\
--batch_size
=
256
\
--total_images
=
1281167
\
--class_dim
=
1000
\
--image_shape
=
3,224,224
\
--model_save_dir
=
output/
\
--lr_strategy
=
piecewise_decay
\
--num_epochs
=
1
0
\
--num_epochs
=
2
0
\
--lr
=
0.0001
\
--act_quant_type
=
abs_max
\
--wt_quant_type
=
abs_max
...
...
@@ -23,16 +23,16 @@ python quant.py \
#ResNet50:
#python quant.py \
# --model=ResNet50 \
# --pretrained_model=../data/pretrain/ResNet50_pretrained \
# --pretrained_
fp32_
model=../data/pretrain/ResNet50_pretrained \
# --use_gpu=True \
# --data_dir=../data/ILSVRC2012 \
# --batch_size=
32
\
# --batch_size=
128
\
# --total_images=1281167 \
# --class_dim=1000 \
# --image_shape=3,224,224 \
# --model_save_dir=output/ \
# --lr_strategy=piecewise_decay \
# --num_epochs=
1
0 \
# --num_epochs=
2
0 \
# --lr=0.0001 \
# --act_quant_type=abs_max \
# --wt_quant_type=abs_max
...
...
fluid/PaddleSlim/utility.py
浏览文件 @
1af11949
...
...
@@ -17,9 +17,13 @@ from __future__ import absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
distutils.util
import
os
import
numpy
as
np
import
six
import
paddle.fluid
as
fluid
import
paddle.compat
as
cpt
from
paddle.fluid
import
core
from
paddle.fluid.framework
import
Program
def
print_arguments
(
args
):
...
...
@@ -61,3 +65,78 @@ def add_arguments(argname, type, default, help, argparser, **kwargs):
type
=
type
,
help
=
help
+
' Default: %(default)s.'
,
**
kwargs
)
def
save_persistable_nodes
(
executor
,
dirname
,
graph
):
"""
Save persistable nodes to the given directory by the executor.
Args:
executor(Executor): The executor to run for saving node values.
dirname(str): The directory path.
graph(IrGraph): All the required persistable nodes in the graph will be saved.
"""
persistable_node_names
=
set
()
persistable_nodes
=
[]
all_persistable_nodes
=
graph
.
all_persistable_nodes
()
for
node
in
all_persistable_nodes
:
name
=
cpt
.
to_text
(
node
.
name
())
if
name
not
in
persistable_node_names
:
persistable_node_names
.
add
(
name
)
persistable_nodes
.
append
(
node
)
program
=
Program
()
var_list
=
[]
for
node
in
persistable_nodes
:
var_desc
=
node
.
var
()
if
var_desc
.
type
()
==
core
.
VarDesc
.
VarType
.
RAW
or
\
var_desc
.
type
()
==
core
.
VarDesc
.
VarType
.
READER
:
continue
var
=
program
.
global_block
().
create_var
(
name
=
var_desc
.
name
(),
shape
=
var_desc
.
shape
(),
dtype
=
var_desc
.
dtype
(),
type
=
var_desc
.
type
(),
lod_level
=
var_desc
.
lod_level
(),
persistable
=
var_desc
.
persistable
())
var_list
.
append
(
var
)
fluid
.
io
.
save_vars
(
executor
=
executor
,
dirname
=
dirname
,
vars
=
var_list
)
def
load_persistable_nodes
(
executor
,
dirname
,
graph
):
"""
Load persistable node values from the given directory by the executor.
Args:
executor(Executor): The executor to run for loading node values.
dirname(str): The directory path.
graph(IrGraph): All the required persistable nodes in the graph will be loaded.
"""
persistable_node_names
=
set
()
persistable_nodes
=
[]
all_persistable_nodes
=
graph
.
all_persistable_nodes
()
for
node
in
all_persistable_nodes
:
name
=
cpt
.
to_text
(
node
.
name
())
if
name
not
in
persistable_node_names
:
persistable_node_names
.
add
(
name
)
persistable_nodes
.
append
(
node
)
program
=
Program
()
var_list
=
[]
def
_exist
(
var
):
return
os
.
path
.
exists
(
os
.
path
.
join
(
dirname
,
var
.
name
))
for
node
in
persistable_nodes
:
var_desc
=
node
.
var
()
if
var_desc
.
type
()
==
core
.
VarDesc
.
VarType
.
RAW
or
\
var_desc
.
type
()
==
core
.
VarDesc
.
VarType
.
READER
:
continue
var
=
program
.
global_block
().
create_var
(
name
=
var_desc
.
name
(),
shape
=
var_desc
.
shape
(),
dtype
=
var_desc
.
dtype
(),
type
=
var_desc
.
type
(),
lod_level
=
var_desc
.
lod_level
(),
persistable
=
var_desc
.
persistable
())
if
_exist
(
var
):
var_list
.
append
(
var
)
fluid
.
io
.
load_vars
(
executor
=
executor
,
dirname
=
dirname
,
vars
=
var_list
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录