Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
2ed20815
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看板
未验证
提交
2ed20815
编写于
12月 02, 2021
作者:
W
whs
提交者:
GitHub
12月 02, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add option for CE of pruning (#942)
上级
25f89072
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
42 addition
and
11 deletion
+42
-11
demo/dygraph/pruning/train.py
demo/dygraph/pruning/train.py
+14
-1
demo/prune/train.py
demo/prune/train.py
+28
-10
未找到文件。
demo/dygraph/pruning/train.py
浏览文件 @
2ed20815
...
...
@@ -7,6 +7,7 @@ import paddle
import
argparse
import
functools
import
math
import
random
import
time
import
numpy
as
np
sys
.
path
.
append
(
...
...
@@ -43,6 +44,7 @@ add_arg('pruned_ratio', float, None, "The ratios to be pruned.")
add_arg
(
'criterion'
,
str
,
"l1_norm"
,
"The prune criterion to be used, support l1_norm and batch_norm_scale."
)
add_arg
(
'use_gpu'
,
bool
,
True
,
"Whether to GPUs."
)
add_arg
(
'checkpoint'
,
str
,
None
,
"The path of checkpoint which is used for resume training."
)
add_arg
(
'ce_test'
,
bool
,
False
,
"Whether to CE test."
)
# yapf: enable
model_list
=
models
.
__all__
...
...
@@ -109,6 +111,16 @@ def create_optimizer(args, parameters, steps_per_epoch):
def
compress
(
args
):
num_workers
=
4
shuffle
=
True
if
args
.
ce_test
:
# set seed
seed
=
111
paddle
.
seed
(
seed
)
np
.
random
.
seed
(
seed
)
random
.
seed
(
seed
)
num_workers
=
0
shuffle
=
False
paddle
.
set_device
(
'gpu'
if
args
.
use_gpu
else
'cpu'
)
train_reader
=
None
...
...
@@ -187,7 +199,8 @@ def compress(args):
batch_size
=
args
.
batch_size
//
ParallelEnv
().
nranks
,
verbose
=
1
,
save_dir
=
args
.
model_path
,
num_workers
=
8
)
num_workers
=
num_workers
,
shuffle
=
shuffle
)
def
main
():
...
...
demo/prune/train.py
浏览文件 @
2ed20815
...
...
@@ -5,6 +5,7 @@ import paddle
import
argparse
import
functools
import
math
import
random
import
time
import
numpy
as
np
sys
.
path
[
0
]
=
os
.
path
.
join
(
os
.
path
.
dirname
(
"__file__"
),
os
.
path
.
pardir
)
...
...
@@ -31,13 +32,14 @@ add_arg('momentum_rate', float, 0.9, "The value of momentum_ra
add_arg
(
'num_epochs'
,
int
,
120
,
"The number of total epochs."
)
parser
.
add_argument
(
'--step_epochs'
,
nargs
=
'+'
,
type
=
int
,
default
=
[
30
,
60
,
90
],
help
=
"piecewise decay step"
)
add_arg
(
'config_file'
,
str
,
None
,
"The config file for compression with yaml format."
)
add_arg
(
'data'
,
str
,
"
mnist"
,
"Which data to use. 'mnist
' or 'imagenet'"
)
add_arg
(
'data'
,
str
,
"
cifar10"
,
"Which data to use. 'cifar10
' or 'imagenet'"
)
add_arg
(
'log_period'
,
int
,
10
,
"Log period in batches."
)
add_arg
(
'test_period'
,
int
,
10
,
"Test period in epoches."
)
add_arg
(
'model_path'
,
str
,
"./models"
,
"The path to save model."
)
add_arg
(
'pruned_ratio'
,
float
,
None
,
"The ratios to be pruned."
)
add_arg
(
'criterion'
,
str
,
"l1_norm"
,
"The prune criterion to be used, support l1_norm and batch_norm_scale."
)
add_arg
(
'save_inference'
,
bool
,
False
,
"Whether to save inference model."
)
add_arg
(
'ce_test'
,
bool
,
False
,
"Whether to CE test."
)
# yapf: enable
model_list
=
models
.
__all__
...
...
@@ -94,16 +96,31 @@ def create_optimizer(args, step_per_epoch):
def
compress
(
args
):
num_workers
=
4
shuffle
=
True
if
args
.
ce_test
:
# set seed
seed
=
111
paddle
.
seed
(
seed
)
np
.
random
.
seed
(
seed
)
random
.
seed
(
seed
)
num_workers
=
0
shuffle
=
False
train_reader
=
None
test_reader
=
None
if
args
.
data
==
"mnist"
:
need_pretrain
=
True
if
args
.
data
==
"cifar10"
:
transform
=
T
.
Compose
([
T
.
Transpose
(),
T
.
Normalize
([
127.5
],
[
127.5
])])
train_dataset
=
paddle
.
vision
.
datasets
.
MNIST
(
mode
=
'train'
,
backend
=
"cv2"
,
transform
=
transform
)
val_dataset
=
paddle
.
vision
.
datasets
.
MNIST
(
mode
=
'test'
,
backend
=
"cv2"
,
transform
=
transform
)
train_dataset
=
paddle
.
vision
.
datasets
.
Cifar10
(
mode
=
"train"
,
backend
=
"cv2"
,
transform
=
transform
)
val_dataset
=
paddle
.
vision
.
datasets
.
Cifar10
(
mode
=
"test"
,
backend
=
"cv2"
,
transform
=
transform
)
class_dim
=
10
image_shape
=
"1,28,28"
image_shape
=
"3, 32, 32"
need_pretrain
=
False
elif
args
.
data
==
"imagenet"
:
import
imagenet_reader
as
reader
train_dataset
=
reader
.
ImageNetDataset
(
mode
=
'train'
)
...
...
@@ -129,10 +146,10 @@ def compress(args):
feed_list
=
[
image
,
label
],
drop_last
=
True
,
batch_size
=
batch_size_per_card
,
shuffle
=
Tru
e
,
shuffle
=
shuffl
e
,
return_list
=
False
,
use_shared_memory
=
True
,
num_workers
=
16
)
num_workers
=
num_workers
)
valid_loader
=
paddle
.
io
.
DataLoader
(
val_dataset
,
places
=
place
,
...
...
@@ -147,6 +164,7 @@ def compress(args):
# model definition
model
=
models
.
__dict__
[
args
.
model
]()
out
=
model
.
net
(
input
=
image
,
class_dim
=
class_dim
)
label
=
paddle
.
reshape
(
label
,
[
-
1
,
1
])
cost
=
paddle
.
nn
.
functional
.
loss
.
cross_entropy
(
input
=
out
,
label
=
label
)
avg_cost
=
paddle
.
mean
(
x
=
cost
)
acc_top1
=
paddle
.
metric
.
accuracy
(
input
=
out
,
label
=
label
,
k
=
1
)
...
...
@@ -157,7 +175,7 @@ def compress(args):
exe
.
run
(
paddle
.
static
.
default_startup_program
())
if
args
.
pretrained_model
:
if
need_pretrain
and
args
.
pretrained_model
:
def
if_exist
(
var
):
return
os
.
path
.
exists
(
os
.
path
.
join
(
args
.
pretrained_model
,
var
.
name
))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录