Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
models
提交
30a7f175
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看板
提交
30a7f175
编写于
6月 12, 2020
作者:
S
sunyingbin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
hello-world alexnet dygraph commit-20200612
上级
0f38ae13
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
129 addition
and
0 deletion
+129
-0
dygraph/alexnet/network.py
dygraph/alexnet/network.py
+129
-0
未找到文件。
dygraph/alexnet/network.py
0 → 100644
浏览文件 @
30a7f175
"""
动态图构建 AlexNet
"""
import
paddle.fluid
as
fluid
import
numpy
as
np
class
Conv2D
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
,
name_scope
,
num_channels
,
num_filters
,
filter_size
,
stride
=
1
,
padding
=
0
,
dilation
=
1
,
groups
=
1
,
act
=
None
,
use_cudnn
=
False
,
param_attr
=
None
,
bias_attr
=
None
):
super
(
Conv2D
,
self
).
__init__
(
name_scope
)
self
.
_conv2d
=
fluid
.
dygraph
.
Conv2D
(
num_channels
=
num_channels
,
num_filters
=
num_filters
,
filter_size
=
filter_size
,
stride
=
stride
,
padding
=
padding
,
dilation
=
dilation
,
groups
=
groups
,
param_attr
=
param_attr
,
bias_attr
=
bias_attr
,
act
=
act
,
use_cudnn
=
use_cudnn
)
def
forward
(
self
,
inputs
):
x
=
self
.
_conv2d
(
inputs
)
return
x
class
Conv2DPool
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
,
name_scope
,
num_channels
,
num_filters
,
filter_size
,
pool_size
,
pool_stride
,
pool_padding
=
0
,
pool_type
=
'max'
,
global_pooling
=
False
,
conv_stride
=
1
,
conv_padding
=
0
,
conv_dilation
=
1
,
conv_groups
=
1
,
act
=
None
,
use_cudnn
=
False
,
param_attr
=
None
,
bias_attr
=
None
):
super
(
Conv2DPool
,
self
).
__init__
(
name_scope
)
self
.
_conv2d
=
fluid
.
dygraph
.
Conv2D
(
num_channels
=
num_channels
,
num_filters
=
num_filters
,
filter_size
=
filter_size
,
stride
=
conv_stride
,
padding
=
conv_padding
,
dilation
=
conv_dilation
,
groups
=
conv_groups
,
param_attr
=
param_attr
,
bias_attr
=
bias_attr
,
act
=
act
,
use_cudnn
=
use_cudnn
)
self
.
_pool2d
=
fluid
.
dygraph
.
Pool2D
(
pool_size
=
pool_size
,
pool_type
=
pool_type
,
pool_stride
=
pool_stride
,
pool_padding
=
pool_padding
,
global_pooling
=
global_pooling
,
use_cudnn
=
use_cudnn
)
def
forward
(
self
,
inputs
):
x
=
self
.
_conv2d
(
inputs
)
x
=
self
.
_pool2d
(
x
)
return
x
class
AlexNet
(
fluid
.
dygraph
.
Layer
):
def
__init__
(
self
,
name_scope
,
class_dim
):
super
(
AlexNet
,
self
).
__init__
(
name_scope
)
self
.
conv_pool_1
=
Conv2DPool
(
self
.
full_name
(),
3
,
64
,
11
,
3
,
2
,
conv_stride
=
4
,
conv_padding
=
2
,
act
=
'relu'
)
self
.
conv_pool_2
=
Conv2DPool
(
self
.
full_name
(),
64
,
192
,
5
,
3
,
2
,
conv_stride
=
1
,
conv_padding
=
2
,
act
=
'relu'
)
self
.
conv_3
=
Conv2D
(
self
.
full_name
(),
192
,
384
,
3
,
1
,
1
,
act
=
'relu'
)
self
.
conv_4
=
Conv2D
(
self
.
full_name
(),
384
,
256
,
3
,
1
,
1
,
act
=
'relu'
)
self
.
conv_pool_5
=
Conv2DPool
(
self
.
full_name
(),
256
,
256
,
3
,
3
,
2
,
conv_stride
=
1
,
conv_padding
=
1
,
act
=
'relu'
)
self
.
fc6
=
fluid
.
dygraph
.
FC
(
self
.
full_name
(),
9216
,
4096
,
act
=
'relu'
)
self
.
fc7
=
fluid
.
dygraph
.
FC
(
self
.
full_name
(),
4096
,
4096
,
act
=
'relu'
)
self
.
fc8
=
fluid
.
dygraph
.
FC
(
self
.
full_name
(),
4096
,
class_dim
,
act
=
'softmax'
)
def
forward
(
self
,
inputs
,
label
=
None
):
out
=
self
.
conv_pool_1
(
inputs
)
out
=
self
.
conv_pool_2
(
out
)
out
=
self
.
conv_3
(
out
)
out
=
self
.
conv_4
(
out
)
out
=
self
.
conv_pool_5
(
out
)
out
=
self
.
fc6
(
out
)
out
=
fluid
.
layers
.
dropout
(
out
,
0.5
)
out
=
self
.
fc7
(
out
)
out
=
fluid
.
layers
.
dropout
(
out
,
0.5
)
out
=
self
.
fc8
(
out
)
if
label
is
not
None
:
acc
=
fluid
.
layers
.
accuracy
(
input
=
out
,
label
=
label
)
return
out
,
acc
else
:
return
out
if
__name__
==
'__main__'
:
with
fluid
.
dygraph
.
guard
():
alexnet
=
AlexNet
(
'alex-net'
,
3
)
img
=
np
.
zeros
([
2
,
3
,
224
,
224
]).
astype
(
'float32'
)
img
=
fluid
.
dygraph
.
to_variable
(
img
)
outs
=
alexnet
(
img
).
numpy
()
print
(
outs
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录