Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
5542db3c
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看板
提交
5542db3c
编写于
4月 25, 2018
作者:
Y
Yibing Liu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Enable the parallel training of mobilenet
上级
98a47232
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
35 addition
and
74 deletion
+35
-74
fluid/image_classification/mobilenet.py
fluid/image_classification/mobilenet.py
+0
-69
fluid/image_classification/train.py
fluid/image_classification/train.py
+35
-5
未找到文件。
fluid/image_classification/mobilenet.py
浏览文件 @
5542db3c
...
...
@@ -153,72 +153,3 @@ def mobile_net(img, class_dim, scale=1.0):
act
=
'softmax'
,
param_attr
=
parameter_attr
)
return
tmp
def
train
(
learning_rate
,
batch_size
,
num_passes
,
model_save_dir
=
'model'
):
class_dim
=
102
image_shape
=
[
3
,
224
,
224
]
image
=
fluid
.
layers
.
data
(
name
=
'image'
,
shape
=
image_shape
,
dtype
=
'float32'
)
label
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'int64'
)
out
=
mobile_net
(
image
,
class_dim
=
class_dim
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
out
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
optimizer
=
fluid
.
optimizer
.
Momentum
(
learning_rate
=
learning_rate
,
momentum
=
0.9
,
regularization
=
fluid
.
regularizer
.
L2Decay
(
5
*
1e-5
))
opts
=
optimizer
.
minimize
(
avg_cost
)
b_size_var
=
fluid
.
layers
.
create_tensor
(
dtype
=
'int64'
)
b_acc_var
=
fluid
.
layers
.
accuracy
(
input
=
out
,
label
=
label
,
total
=
b_size_var
)
inference_program
=
fluid
.
default_main_program
().
clone
()
with
fluid
.
program_guard
(
inference_program
):
inference_program
=
fluid
.
io
.
get_inference_program
(
target_vars
=
[
b_acc_var
,
b_size_var
])
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
exe
.
run
(
fluid
.
default_startup_program
())
train_reader
=
paddle
.
batch
(
paddle
.
dataset
.
flowers
.
train
(),
batch_size
=
batch_size
)
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
flowers
.
test
(),
batch_size
=
batch_size
)
feeder
=
fluid
.
DataFeeder
(
place
=
place
,
feed_list
=
[
image
,
label
])
train_pass_acc_evaluator
=
fluid
.
average
.
WeightedAverage
()
test_pass_acc_evaluator
=
fluid
.
average
.
WeightedAverage
()
for
pass_id
in
range
(
num_passes
):
train_pass_acc_evaluator
.
reset
()
for
batch_id
,
data
in
enumerate
(
train_reader
()):
loss
,
acc
,
size
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
feeder
.
feed
(
data
),
fetch_list
=
[
avg_cost
,
b_acc_var
,
b_size_var
])
train_pass_acc_evaluator
.
add
(
value
=
acc
,
weight
=
size
)
print
(
"Pass {0}, batch {1}, loss {2}, acc {3}"
.
format
(
pass_id
,
batch_id
,
loss
[
0
],
acc
[
0
]))
test_pass_acc_evaluator
.
reset
()
for
data
in
test_reader
():
loss
,
acc
,
size
=
exe
.
run
(
inference_program
,
feed
=
feeder
.
feed
(
data
),
fetch_list
=
[
avg_cost
,
b_acc_var
,
b_size_var
])
test_pass_acc_evaluator
.
add
(
value
=
acc
,
weight
=
size
)
print
(
"End pass {0}, train_acc {1}, test_acc {2}"
.
format
(
pass_id
,
train_pass_acc_evaluator
.
eval
(),
test_pass_acc_evaluator
.
eval
()))
if
pass_id
%
10
==
0
:
model_path
=
os
.
path
.
join
(
model_save_dir
,
str
(
pass_id
))
print
'save models to %s'
%
(
model_path
)
fluid
.
io
.
save_inference_model
(
model_path
,
[
'image'
],
[
out
],
exe
)
if
__name__
==
'__main__'
:
train
(
learning_rate
=
0.005
,
batch_size
=
40
,
num_passes
=
300
)
fluid/image_classification/train.py
浏览文件 @
5542db3c
...
...
@@ -5,6 +5,7 @@ import sys
import
paddle.v2
as
paddle
import
paddle.fluid
as
fluid
from
se_resnext
import
SE_ResNeXt
from
mobilenet
import
mobile_net
import
reader
import
argparse
...
...
@@ -18,6 +19,11 @@ add_arg('batch_size', int, 256, "Minibatch size.")
add_arg
(
'num_layers'
,
int
,
50
,
"How many layers for SE-ResNeXt model."
)
add_arg
(
'with_mem_opt'
,
bool
,
True
,
"Whether to use memory optimization or not."
)
add_arg
(
'parallel_exe'
,
bool
,
True
,
"Whether to use ParallelExecutor to train or not."
)
add_arg
(
'model'
,
type
=
str
,
choices
=
[
'mobilenet'
,
'se_resnext'
],
default
=
'se_resnext'
,
help
=
"Which model to use."
)
# yapf: enable
...
...
@@ -44,7 +50,12 @@ def train_parallel_do(args,
with
pd
.
do
():
image_
=
pd
.
read_input
(
image
)
label_
=
pd
.
read_input
(
label
)
out
=
SE_ResNeXt
(
input
=
image_
,
class_dim
=
class_dim
,
layers
=
layers
)
if
args
.
model
is
'se_resnext'
:
out
=
SE_ResNeXt
(
input
=
image_
,
class_dim
=
class_dim
,
layers
=
layers
)
else
:
out
=
mobile_net
(
img
=
image_
,
class_dim
=
class_dim
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
out
,
label
=
label_
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
acc_top1
=
fluid
.
layers
.
accuracy
(
input
=
out
,
label
=
label_
,
k
=
1
)
...
...
@@ -58,7 +69,11 @@ def train_parallel_do(args,
acc_top1
=
fluid
.
layers
.
mean
(
x
=
acc_top1
)
acc_top5
=
fluid
.
layers
.
mean
(
x
=
acc_top5
)
else
:
out
=
SE_ResNeXt
(
input
=
image
,
class_dim
=
class_dim
,
layers
=
layers
)
if
args
.
model
is
'se_resnext'
:
out
=
SE_ResNeXt
(
input
=
image
,
class_dim
=
class_dim
,
layers
=
layers
)
else
:
out
=
mobile_net
(
img
=
image
,
class_dim
=
class_dim
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
out
,
label
=
label
)
avg_cost
=
fluid
.
layers
.
mean
(
x
=
cost
)
acc_top1
=
fluid
.
layers
.
accuracy
(
input
=
out
,
label
=
label
,
k
=
1
)
...
...
@@ -150,7 +165,8 @@ def train_parallel_do(args,
test_acc5
))
sys
.
stdout
.
flush
()
model_path
=
os
.
path
.
join
(
model_save_dir
,
str
(
pass_id
))
model_path
=
os
.
path
.
join
(
model_save_dir
+
'/'
+
args
.
model
,
str
(
pass_id
))
if
not
os
.
path
.
isdir
(
model_path
):
os
.
makedirs
(
model_path
)
fluid
.
io
.
save_persistables
(
exe
,
model_path
)
...
...
@@ -171,7 +187,11 @@ def train_parallel_exe(args,
image
=
fluid
.
layers
.
data
(
name
=
'image'
,
shape
=
image_shape
,
dtype
=
'float32'
)
label
=
fluid
.
layers
.
data
(
name
=
'label'
,
shape
=
[
1
],
dtype
=
'int64'
)
out
=
SE_ResNeXt
(
input
=
image
,
class_dim
=
class_dim
,
layers
=
layers
)
if
args
.
model
is
'se_resnext'
:
out
=
SE_ResNeXt
(
input
=
image
,
class_dim
=
class_dim
,
layers
=
layers
)
else
:
out
=
mobile_net
(
img
=
image
,
class_dim
=
class_dim
)
cost
=
fluid
.
layers
.
cross_entropy
(
input
=
out
,
label
=
label
)
acc_top1
=
fluid
.
layers
.
accuracy
(
input
=
out
,
label
=
label
,
k
=
1
)
acc_top5
=
fluid
.
layers
.
accuracy
(
input
=
out
,
label
=
label
,
k
=
5
)
...
...
@@ -207,6 +227,15 @@ def train_parallel_exe(args,
train_reader
=
paddle
.
batch
(
reader
.
train
(),
batch_size
=
batch_size
)
test_reader
=
paddle
.
batch
(
reader
.
test
(),
batch_size
=
batch_size
)
# data reader
train_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
flowers
.
train
(),
buf_size
=
5120
),
batch_size
=
batch_size
)
test_reader
=
paddle
.
batch
(
paddle
.
dataset
.
flowers
.
test
(),
batch_size
=
batch_size
)
feeder
=
fluid
.
DataFeeder
(
place
=
place
,
feed_list
=
[
image
,
label
])
train_exe
=
fluid
.
ParallelExecutor
(
use_cuda
=
True
,
loss_name
=
avg_cost
.
name
)
...
...
@@ -270,7 +299,8 @@ def train_parallel_exe(args,
test_acc5
))
sys
.
stdout
.
flush
()
model_path
=
os
.
path
.
join
(
model_save_dir
,
str
(
pass_id
))
model_path
=
os
.
path
.
join
(
model_save_dir
+
'/'
+
args
.
model
,
str
(
pass_id
))
if
not
os
.
path
.
isdir
(
model_path
):
os
.
makedirs
(
model_path
)
fluid
.
io
.
save_persistables
(
exe
,
model_path
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录