Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
1979f939
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看板
提交
1979f939
编写于
2月 09, 2018
作者:
X
xzl
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix comments
上级
e9e084be
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
29 addition
and
13 deletion
+29
-13
fluid/image_classification/mobilenet.py
fluid/image_classification/mobilenet.py
+29
-13
未找到文件。
fluid/image_classification/mobilenet.py
浏览文件 @
1979f939
import
os
import
paddle.v2
as
paddle
import
paddle.v2.fluid
as
fluid
import
time
from
paddle.v2.fluid.initializer
import
MSRA
from
paddle.v2.fluid.param_attr
import
ParamAttr
parameter_attr
=
ParamAttr
(
initializer
=
MSRA
())
def
conv_bn_layer
(
input
,
...
...
@@ -22,6 +26,7 @@ def conv_bn_layer(input,
groups
=
num_groups
,
act
=
None
,
use_cudnn
=
use_cudnn
,
param_attr
=
parameter_attr
,
bias_attr
=
False
)
return
fluid
.
layers
.
batch_norm
(
input
=
conv
,
act
=
act
)
...
...
@@ -30,7 +35,7 @@ def depthwise_separable(input, num_filters1, num_filters2, num_groups, stride,
scale
):
"""
"""
tmp
=
conv_bn_layer
(
depthwise_conv
=
conv_bn_layer
(
input
=
input
,
filter_size
=
3
,
num_filters
=
int
(
num_filters1
*
scale
),
...
...
@@ -39,13 +44,13 @@ def depthwise_separable(input, num_filters1, num_filters2, num_groups, stride,
num_groups
=
int
(
num_groups
*
scale
),
use_cudnn
=
False
)
tmp
=
conv_bn_layer
(
input
=
tmp
,
pointwise_conv
=
conv_bn_layer
(
input
=
depthwise_conv
,
filter_size
=
1
,
num_filters
=
int
(
num_filters2
*
scale
),
stride
=
1
,
padding
=
0
)
return
tmp
return
pointwise_conv
def
mobile_net
(
img
,
class_dim
,
scale
=
1.0
):
...
...
@@ -67,6 +72,7 @@ def mobile_net(img, class_dim, scale=1.0):
num_groups
=
32
,
stride
=
1
,
scale
=
scale
)
tmp
=
depthwise_separable
(
tmp
,
num_filters1
=
64
,
...
...
@@ -74,6 +80,7 @@ def mobile_net(img, class_dim, scale=1.0):
num_groups
=
64
,
stride
=
2
,
scale
=
scale
)
# 28x28
tmp
=
depthwise_separable
(
tmp
,
...
...
@@ -82,6 +89,7 @@ def mobile_net(img, class_dim, scale=1.0):
num_groups
=
128
,
stride
=
1
,
scale
=
scale
)
tmp
=
depthwise_separable
(
tmp
,
num_filters1
=
128
,
...
...
@@ -89,6 +97,7 @@ def mobile_net(img, class_dim, scale=1.0):
num_groups
=
128
,
stride
=
2
,
scale
=
scale
)
# 14x14
tmp
=
depthwise_separable
(
tmp
,
...
...
@@ -97,6 +106,7 @@ def mobile_net(img, class_dim, scale=1.0):
num_groups
=
256
,
stride
=
1
,
scale
=
scale
)
tmp
=
depthwise_separable
(
tmp
,
num_filters1
=
256
,
...
...
@@ -104,6 +114,7 @@ def mobile_net(img, class_dim, scale=1.0):
num_groups
=
256
,
stride
=
2
,
scale
=
scale
)
# 14x14
for
i
in
range
(
5
):
tmp
=
depthwise_separable
(
...
...
@@ -121,6 +132,7 @@ def mobile_net(img, class_dim, scale=1.0):
num_groups
=
512
,
stride
=
2
,
scale
=
scale
)
tmp
=
depthwise_separable
(
tmp
,
num_filters1
=
1024
,
...
...
@@ -130,9 +142,16 @@ def mobile_net(img, class_dim, scale=1.0):
scale
=
scale
)
tmp
=
fluid
.
layers
.
pool2d
(
input
=
tmp
,
pool_size
=
7
,
pool_stride
=
1
,
pool_type
=
'avg'
)
tmp
=
fluid
.
layers
.
fc
(
input
=
tmp
,
size
=
class_dim
,
act
=
'softmax'
)
input
=
tmp
,
pool_size
=
0
,
pool_stride
=
1
,
pool_type
=
'avg'
,
global_pooling
=
True
)
tmp
=
fluid
.
layers
.
fc
(
input
=
tmp
,
size
=
class_dim
,
act
=
'softmax'
,
param_attr
=
parameter_attr
)
return
tmp
...
...
@@ -174,14 +193,11 @@ def train(learning_rate, batch_size, num_passes, model_save_dir='model'):
for
pass_id
in
range
(
num_passes
):
accuracy
.
reset
(
exe
)
for
batch_id
,
data
in
enumerate
(
train_reader
()):
start_time
=
time
.
time
()
loss
,
acc
=
exe
.
run
(
fluid
.
default_main_program
(),
feed
=
feeder
.
feed
(
data
),
fetch_list
=
[
avg_cost
]
+
accuracy
.
metrics
)
pass_elapsed
=
time
.
time
()
-
start_time
print
(
"Pass {0}, batch {1}, loss {2}, acc {3}"
.
format
(
pass_id
,
batch_id
,
loss
[
0
],
acc
[
0
]))
print
'cost : %f s'
%
(
pass_elapsed
)
pass_acc
=
accuracy
.
eval
(
exe
)
test_accuracy
.
reset
(
exe
)
...
...
@@ -193,10 +209,10 @@ def train(learning_rate, batch_size, num_passes, model_save_dir='model'):
print
(
"End pass {0}, train_acc {1}, test_acc {2}"
.
format
(
pass_id
,
pass_acc
,
test_pass_acc
))
if
pass_id
%
10
==
0
:
print
'save models'
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
=
80
,
num_passes
=
4
00
)
train
(
learning_rate
=
0.005
,
batch_size
=
40
,
num_passes
=
3
00
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录