Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
e8d6f633
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看板
未验证
提交
e8d6f633
编写于
2月 02, 2023
作者:
K
kangguangli
提交者:
GitHub
2月 02, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
replace with_data_parallel with fleet (#1626)
上级
8bf2df5b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
47 addition
and
40 deletion
+47
-40
demo/distillation/README.md
demo/distillation/README.md
+3
-3
demo/distillation/distill.py
demo/distillation/distill.py
+44
-37
未找到文件。
demo/distillation/README.md
浏览文件 @
e8d6f633
...
...
@@ -13,7 +13,7 @@
默认配置:
```
yaml
batch_size
:
256
batch_size
:
64
init_lr
:
0.1
lr_strategy
:
piecewise_decay
l2_decay
:
3e-5
...
...
@@ -21,14 +21,14 @@ momentum_rate: 0.9
num_epochs
:
120
data
:
imagenet
```
训练使用默认配置启动即可
训练使用默认配置启动即可
。这里的batch_size指每张卡上的batch_size。
### 2. 启动训练
在配置好ImageNet数据集后,用以下命令启动训练即可:
```
shell
CUDA_VISIBLE_DEVICES
=
0,1,2,3 python distill.py
CUDA_VISIBLE_DEVICES
=
0,1,2,3 python
-m
paddle.distributed.launch
distill.py
```
### 3. 训练结果
...
...
demo/distillation/distill.py
浏览文件 @
e8d6f633
...
...
@@ -15,6 +15,9 @@ import models
from
utility
import
add_arguments
,
print_arguments
,
_download
,
_decompress
from
paddleslim.dist
import
merge
,
l2
,
soft_label
from
paddle.distributed
import
fleet
from
paddle.distributed.fleet
import
DistributedStrategy
logging
.
basicConfig
(
format
=
'%(asctime)s-%(levelname)s: %(message)s'
)
_logger
=
logging
.
getLogger
(
__name__
)
_logger
.
setLevel
(
logging
.
INFO
)
...
...
@@ -76,6 +79,9 @@ def create_optimizer(args):
def
compress
(
args
):
fleet
.
init
(
is_collective
=
True
)
if
args
.
data
==
"cifar10"
:
train_dataset
=
paddle
.
vision
.
datasets
.
Cifar10
(
mode
=
'train'
)
val_dataset
=
paddle
.
vision
.
datasets
.
Cifar10
(
mode
=
'test'
)
...
...
@@ -103,38 +109,38 @@ def compress(args):
else
:
devices_num
=
int
(
os
.
environ
.
get
(
'CPU_NUM'
,
1
))
with
paddle
.
static
.
program_guard
(
student_program
,
s_startup
):
with
paddle
.
utils
.
unique_name
.
guard
():
image
=
paddle
.
static
.
data
(
name
=
'image'
,
shape
=
[
None
]
+
image_shape
,
dtype
=
'float32
'
)
label
=
paddle
.
static
.
data
(
name
=
'label'
,
shape
=
[
None
,
1
],
dtype
=
'int64'
)
train_loader
=
paddle
.
io
.
DataLoader
(
train_dataset
,
places
=
places
,
feed_list
=
[
image
,
label
],
drop_last
=
True
,
batch_size
=
int
(
args
.
batch_size
/
devices_num
)
,
return_list
=
False
,
shuffle
=
True
,
use_shared_memory
=
Tru
e
,
num_workers
=
4
)
valid_loader
=
paddle
.
io
.
DataLoader
(
val_dataset
,
places
=
place
,
feed_list
=
[
image
,
label
]
,
drop_last
=
False
,
return_li
st
=
False
,
use_shared_memory
=
Tru
e
,
batch_size
=
args
.
batch_siz
e
,
shuffle
=
False
)
# model definition
model
=
models
.
__dict__
[
args
.
model
]()
out
=
model
.
net
(
input
=
image
,
class_dim
=
class_dim
)
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
)
acc_top5
=
paddle
.
metric
.
accuracy
(
input
=
out
,
label
=
label
,
k
=
5
)
image
=
paddle
.
static
.
data
(
name
=
'image'
,
shape
=
[
None
]
+
image_shape
,
dtype
=
'float32'
)
label
=
paddle
.
static
.
data
(
name
=
'label'
,
shape
=
[
None
,
1
],
dtype
=
'int64
'
)
sampler
=
paddle
.
io
.
DistributedBatchSampler
(
train_dataset
,
shuffle
=
False
,
drop_last
=
True
,
batch_size
=
args
.
batch_size
)
train_loader
=
paddle
.
io
.
DataLoader
(
train_dataset
,
places
=
places
,
feed_list
=
[
image
,
label
]
,
batch_sampler
=
sampler
,
return_list
=
Fals
e
,
use_shared_memory
=
False
,
num_workers
=
4
)
valid_loader
=
paddle
.
io
.
DataLoader
(
val_dataset
,
places
=
place
,
feed_list
=
[
image
,
label
]
,
drop_la
st
=
False
,
return_list
=
Fals
e
,
use_shared_memory
=
Fals
e
,
batch_size
=
args
.
batch_size
,
shuffle
=
False
)
# model definition
model
=
models
.
__dict__
[
args
.
model
](
)
out
=
model
.
net
(
input
=
image
,
class_dim
=
class_dim
)
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
)
acc_top5
=
paddle
.
metric
.
accuracy
(
input
=
out
,
label
=
label
,
k
=
5
)
val_program
=
student_program
.
clone
(
for_test
=
True
)
exe
=
paddle
.
static
.
Executor
(
place
)
...
...
@@ -172,18 +178,19 @@ def compress(args):
data_name_map
=
{
'image'
:
'image'
}
merge
(
teacher_program
,
student_program
,
data_name_map
,
place
)
build_strategy
=
paddle
.
static
.
BuildStrategy
()
dist_strategy
=
DistributedStrategy
()
dist_strategy
.
build_strategy
=
build_strategy
with
paddle
.
static
.
program_guard
(
student_program
,
s_startup
):
distill_loss
=
soft_label
(
"teacher_fc_0.tmp_0"
,
"fc_0.tmp_0"
,
student_program
)
loss
=
avg_cost
+
distill_loss
lr
,
opt
=
create_optimizer
(
args
)
opt
=
fleet
.
distributed_optimizer
(
opt
,
strategy
=
dist_strategy
)
opt
.
minimize
(
loss
)
exe
.
run
(
s_startup
)
build_strategy
=
paddle
.
static
.
BuildStrategy
()
build_strategy
.
fuse_all_reduce_ops
=
False
parallel_main
=
paddle
.
static
.
CompiledProgram
(
student_program
).
with_data_parallel
(
loss_name
=
loss
.
name
,
build_strategy
=
build_strategy
)
parallel_main
=
student_program
for
epoch_id
in
range
(
args
.
num_epochs
):
for
step_id
,
data
in
enumerate
(
train_loader
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录