Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
29e7b33c
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看板
提交
29e7b33c
编写于
12月 05, 2019
作者:
B
baiyfbupt
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add brief introduction about distillation demo
上级
81506640
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
15 addition
and
10 deletion
+15
-10
demo/distillation/distillation_demo.py
demo/distillation/distillation_demo.py
+9
-10
doc/demo_guide.md
doc/demo_guide.md
+6
-0
未找到文件。
demo/distillation/distillation_demo.py
浏览文件 @
29e7b33c
...
@@ -13,9 +13,8 @@ import numpy as np
...
@@ -13,9 +13,8 @@ import numpy as np
import
paddle.fluid
as
fluid
import
paddle.fluid
as
fluid
sys
.
path
.
append
(
sys
.
path
[
0
]
+
"/../"
)
sys
.
path
.
append
(
sys
.
path
[
0
]
+
"/../"
)
import
models
import
models
import
imagenet_reader
as
reader
from
utility
import
add_arguments
,
print_arguments
,
_download
,
_decompress
from
utility
import
add_arguments
,
print_arguments
,
_download
,
_decompress
from
single_distiller
import
merge
,
l2_loss
,
soft_label_loss
,
fsp_loss
from
paddleslim.dist
import
merge
,
l2_loss
,
soft_label_loss
,
fsp_loss
logging
.
basicConfig
(
format
=
'%(asctime)s-%(levelname)s: %(message)s'
)
logging
.
basicConfig
(
format
=
'%(asctime)s-%(levelname)s: %(message)s'
)
_logger
=
logging
.
getLogger
(
__name__
)
_logger
=
logging
.
getLogger
(
__name__
)
...
@@ -33,7 +32,7 @@ add_arg('lr_strategy', str, "piecewise_decay", "The learning rate decay
...
@@ -33,7 +32,7 @@ add_arg('lr_strategy', str, "piecewise_decay", "The learning rate decay
add_arg
(
'l2_decay'
,
float
,
3e-5
,
"The l2_decay parameter."
)
add_arg
(
'l2_decay'
,
float
,
3e-5
,
"The l2_decay parameter."
)
add_arg
(
'momentum_rate'
,
float
,
0.9
,
"The value of momentum_rate."
)
add_arg
(
'momentum_rate'
,
float
,
0.9
,
"The value of momentum_rate."
)
add_arg
(
'num_epochs'
,
int
,
120
,
"The number of total epochs."
)
add_arg
(
'num_epochs'
,
int
,
120
,
"The number of total epochs."
)
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
,
20
,
"Log period in batches."
)
add_arg
(
'log_period'
,
int
,
20
,
"Log period in batches."
)
add_arg
(
'model'
,
str
,
"MobileNet"
,
"Set the network to use."
)
add_arg
(
'model'
,
str
,
"MobileNet"
,
"Set the network to use."
)
add_arg
(
'pretrained_model'
,
str
,
None
,
"Whether to use pretrained model."
)
add_arg
(
'pretrained_model'
,
str
,
None
,
"Whether to use pretrained model."
)
...
@@ -76,7 +75,7 @@ def create_optimizer(args):
...
@@ -76,7 +75,7 @@ def create_optimizer(args):
def
compress
(
args
):
def
compress
(
args
):
if
args
.
data
==
"
mnist
"
:
if
args
.
data
==
"
cifar10
"
:
import
paddle.dataset.cifar
as
reader
import
paddle.dataset.cifar
as
reader
train_reader
=
reader
.
train10
()
train_reader
=
reader
.
train10
()
val_reader
=
reader
.
test10
()
val_reader
=
reader
.
test10
()
...
@@ -146,9 +145,9 @@ def compress(args):
...
@@ -146,9 +145,9 @@ def compress(args):
name
=
'image'
,
shape
=
image_shape
,
dtype
=
'float32'
)
name
=
'image'
,
shape
=
image_shape
,
dtype
=
'float32'
)
predict
=
teacher_model
.
net
(
image
,
class_dim
=
class_dim
)
predict
=
teacher_model
.
net
(
image
,
class_dim
=
class_dim
)
#print("="*50+"teacher_model_params"+"="*50)
#print("="*50+"teacher_model_params"+"="*50)
#for v in teacher_program.list_vars():
#for v in teacher_program.list_vars():
# print(v.name, v.shape)
# print(v.name, v.shape)
exe
.
run
(
t_startup
)
exe
.
run
(
t_startup
)
_download
(
'http://paddle-imagenet-models-name.bj.bcebos.com/ResNet50_pretrained.tar'
,
'.'
)
_download
(
'http://paddle-imagenet-models-name.bj.bcebos.com/ResNet50_pretrained.tar'
,
'.'
)
...
@@ -176,8 +175,8 @@ def compress(args):
...
@@ -176,8 +175,8 @@ def compress(args):
place
)
place
)
with
fluid
.
program_guard
(
main
,
s_startup
):
with
fluid
.
program_guard
(
main
,
s_startup
):
l2_loss
_v
=
l2_loss
(
"teacher_fc_0.tmp_0"
,
"fc_0.tmp_0"
,
main
)
l2_loss
=
l2_loss
(
"teacher_fc_0.tmp_0"
,
"fc_0.tmp_0"
,
main
)
loss
=
avg_cost
+
l2_loss
_v
loss
=
avg_cost
+
l2_loss
opt
=
create_optimizer
(
args
)
opt
=
create_optimizer
(
args
)
opt
.
minimize
(
loss
)
opt
.
minimize
(
loss
)
exe
.
run
(
s_startup
)
exe
.
run
(
s_startup
)
...
@@ -192,7 +191,7 @@ def compress(args):
...
@@ -192,7 +191,7 @@ def compress(args):
parallel_main
,
parallel_main
,
feed
=
data
,
feed
=
data
,
fetch_list
=
[
fetch_list
=
[
loss
.
name
,
avg_cost
.
name
,
l2_loss
_v
.
name
loss
.
name
,
avg_cost
.
name
,
l2_loss
.
name
])
])
if
step_id
%
args
.
log_period
==
0
:
if
step_id
%
args
.
log_period
==
0
:
_logger
.
info
(
_logger
.
info
(
...
...
doc/demo_guide.md
浏览文件 @
29e7b33c
## [蒸馏](../demo/distillation/distillation_demo.py)
## [蒸馏](../demo/distillation/distillation_demo.py)
蒸馏demo默认使用ResNet50作为teacher网络,MobileNet作为student网络,此外还支持将teacher和student换成
[
models目录
](
../demo/models
)
支持的任意模型
demo中对teahcer模型和student模型的一层特征图添加了l2_loss的蒸馏损失函数,使用时也可根据需要选择fsp_loss, soft_label_loss以及自定义的loss函数
训练默认使用的是cifar10数据集,piecewise_decay学习率衰减策略,momentum优化器进行120轮蒸馏训练。使用者也可以简单地用args参数切换为使用ImageNet数据集,cosine_decay学习率衰减策略等其他训练配置
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录