Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
af523df4
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
af523df4
编写于
9月 11, 2017
作者:
Y
Yu Yang
提交者:
GitHub
9月 11, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #4023 from reyoung/feature/serialize_protobuf
Make paddle.v2.inference can direct load protobuf
上级
d71190f0
2b352212
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
26 addition
and
5 deletion
+26
-5
python/paddle/v2/inference.py
python/paddle/v2/inference.py
+17
-5
python/paddle/v2/topology.py
python/paddle/v2/topology.py
+9
-0
未找到文件。
python/paddle/v2/inference.py
浏览文件 @
af523df4
...
@@ -2,6 +2,7 @@ import numpy
...
@@ -2,6 +2,7 @@ import numpy
import
collections
import
collections
import
topology
import
topology
import
minibatch
import
minibatch
import
cPickle
__all__
=
[
'infer'
,
'Inference'
]
__all__
=
[
'infer'
,
'Inference'
]
...
@@ -25,11 +26,23 @@ class Inference(object):
...
@@ -25,11 +26,23 @@ class Inference(object):
:type parameters: paddle.v2.parameters.Parameters
:type parameters: paddle.v2.parameters.Parameters
"""
"""
def
__init__
(
self
,
output_layer
,
parameters
):
def
__init__
(
self
,
parameters
,
output_layer
=
None
,
fileobj
=
None
):
import
py_paddle.swig_paddle
as
api
import
py_paddle.swig_paddle
as
api
if
output_layer
is
not
None
:
topo
=
topology
.
Topology
(
output_layer
)
topo
=
topology
.
Topology
(
output_layer
)
gm
=
api
.
GradientMachine
.
createFromConfigProto
(
gm
=
api
.
GradientMachine
.
createFromConfigProto
(
topo
.
proto
(),
api
.
CREATE_MODE_TESTING
,
[
api
.
PARAMETER_VALUE
])
topo
.
proto
(),
api
.
CREATE_MODE_TESTING
,
[
api
.
PARAMETER_VALUE
])
self
.
__data_types__
=
topo
.
data_type
()
elif
fileobj
is
not
None
:
tmp
=
cPickle
.
load
(
fileobj
)
gm
=
api
.
GradientMachine
.
createByConfigProtoStr
(
tmp
[
'protobin'
],
api
.
CREATE_MODE_TESTING
,
[
api
.
PARAMETER_VALUE
])
self
.
__data_types__
=
tmp
[
'data_type'
]
else
:
raise
ValueError
(
"Either output_layer or fileobj must be set"
)
for
param
in
gm
.
getParameters
():
for
param
in
gm
.
getParameters
():
val
=
param
.
getBuf
(
api
.
PARAMETER_VALUE
)
val
=
param
.
getBuf
(
api
.
PARAMETER_VALUE
)
name
=
param
.
getName
()
name
=
param
.
getName
()
...
@@ -43,7 +56,6 @@ class Inference(object):
...
@@ -43,7 +56,6 @@ class Inference(object):
# called here, but it's better to call this function in one place.
# called here, but it's better to call this function in one place.
param
.
setValueUpdated
()
param
.
setValueUpdated
()
self
.
__gradient_machine__
=
gm
self
.
__gradient_machine__
=
gm
self
.
__data_types__
=
topo
.
data_type
()
def
iter_infer
(
self
,
input
,
feeding
=
None
):
def
iter_infer
(
self
,
input
,
feeding
=
None
):
from
data_feeder
import
DataFeeder
from
data_feeder
import
DataFeeder
...
...
python/paddle/v2/topology.py
浏览文件 @
af523df4
...
@@ -18,6 +18,7 @@ from paddle.proto.ModelConfig_pb2 import ModelConfig
...
@@ -18,6 +18,7 @@ from paddle.proto.ModelConfig_pb2 import ModelConfig
import
paddle.trainer_config_helpers
as
conf_helps
import
paddle.trainer_config_helpers
as
conf_helps
import
layer
as
v2_layer
import
layer
as
v2_layer
import
config_base
import
config_base
import
cPickle
__all__
=
[
'Topology'
]
__all__
=
[
'Topology'
]
...
@@ -100,6 +101,14 @@ class Topology(object):
...
@@ -100,6 +101,14 @@ class Topology(object):
return
layer
return
layer
return
None
return
None
def
serialize_for_inference
(
self
,
stream
):
protobin
=
self
.
proto
().
SerializeToString
()
data_type
=
self
.
data_type
()
cPickle
.
dump
({
'protobin'
:
protobin
,
'data_type'
:
data_type
},
stream
,
cPickle
.
HIGHEST_PROTOCOL
)
def
__check_layer_type__
(
layer
):
def
__check_layer_type__
(
layer
):
if
not
isinstance
(
layer
,
config_base
.
Layer
):
if
not
isinstance
(
layer
,
config_base
.
Layer
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录