Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Serving
提交
7a2a3efd
S
Serving
项目概览
PaddlePaddle
/
Serving
大约 1 年 前同步成功
通知
186
Star
833
Fork
253
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
105
列表
看板
标记
里程碑
合并请求
10
Wiki
2
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Serving
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
105
Issue
105
列表
看板
标记
里程碑
合并请求
10
合并请求
10
Pages
分析
分析
仓库分析
DevOps
Wiki
2
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
7a2a3efd
编写于
4月 02, 2021
作者:
T
TeslaZhao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update paddle predictor API verson to 2.0
上级
753800da
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
53 addition
and
50 deletion
+53
-50
python/examples/pipeline/ocr/web_service.py
python/examples/pipeline/ocr/web_service.py
+3
-3
python/examples/pipeline/simple_web_service/web_service.py
python/examples/pipeline/simple_web_service/web_service.py
+7
-4
python/paddle_serving_app/local_predict.py
python/paddle_serving_app/local_predict.py
+43
-43
未找到文件。
python/examples/pipeline/ocr/web_service.py
浏览文件 @
7a2a3efd
...
...
@@ -48,7 +48,7 @@ class DetOp(Op):
imgs
=
[]
for
key
in
input_dict
.
keys
():
data
=
base64
.
b64decode
(
input_dict
[
key
].
encode
(
'utf8'
))
data
=
np
.
from
string
(
data
,
np
.
uint8
)
data
=
np
.
from
buffer
(
data
,
np
.
uint8
)
self
.
im
=
cv2
.
imdecode
(
data
,
cv2
.
IMREAD_COLOR
)
self
.
ori_h
,
self
.
ori_w
,
_
=
self
.
im
.
shape
det_img
=
self
.
det_preprocess
(
self
.
im
)
...
...
@@ -57,7 +57,7 @@ class DetOp(Op):
return
{
"image"
:
np
.
concatenate
(
imgs
,
axis
=
0
)},
False
,
None
,
""
def
postprocess
(
self
,
input_dicts
,
fetch_dict
,
log_id
):
# print(fetch_dict)
# print(fetch_dict)
det_out
=
fetch_dict
[
"concat_1.tmp_0"
]
ratio_list
=
[
float
(
self
.
new_h
)
/
self
.
ori_h
,
float
(
self
.
new_w
)
/
self
.
ori_w
...
...
@@ -114,5 +114,5 @@ class OcrService(WebService):
uci_service
=
OcrService
(
name
=
"ocr"
)
uci_service
.
prepare_pipeline_config
(
"config
2
.yml"
)
uci_service
.
prepare_pipeline_config
(
"config.yml"
)
uci_service
.
run_service
()
python/examples/pipeline/simple_web_service/web_service.py
浏览文件 @
7a2a3efd
...
...
@@ -14,7 +14,7 @@
try
:
from
paddle_serving_server.web_service
import
WebService
,
Op
except
ImportError
:
from
paddle_serving_server.web_service
import
WebService
,
Op
from
paddle_serving_server
_gpu
.web_service
import
WebService
,
Op
import
logging
import
numpy
as
np
import
sys
...
...
@@ -34,8 +34,11 @@ class UciOp(Op):
x_value
=
input_dict
[
"x"
].
split
(
self
.
batch_separator
)
x_lst
=
[]
for
x_val
in
x_value
:
x_lst
.
append
(
np
.
array
([
float
(
x
.
strip
())
for
x
in
x_val
.
split
(
self
.
separator
)]).
reshape
(
1
,
13
))
input_dict
[
"x"
]
=
np
.
concatenate
(
x_lst
,
axis
=
0
)
x_lst
.
append
(
np
.
array
([
float
(
x
.
strip
())
for
x
in
x_val
.
split
(
self
.
separator
)
]).
reshape
(
1
,
13
))
input_dict
[
"x"
]
=
np
.
concatenate
(
x_lst
,
axis
=
0
)
proc_dict
=
{}
return
input_dict
,
False
,
None
,
""
...
...
@@ -53,5 +56,5 @@ class UciService(WebService):
uci_service
=
UciService
(
name
=
"uci"
)
uci_service
.
prepare_pipeline_config
(
"config
2
.yml"
)
uci_service
.
prepare_pipeline_config
(
"config.yml"
)
uci_service
.
run_service
()
python/paddle_serving_app/local_predict.py
浏览文件 @
7a2a3efd
...
...
@@ -19,16 +19,12 @@ import os
import
google.protobuf.text_format
import
numpy
as
np
import
argparse
import
paddle.fluid
as
fluid
import
paddle.inference
as
inference
from
.proto
import
general_model_config_pb2
as
m_config
from
paddle.fluid.core
import
PaddleTensor
from
paddle.fluid.core
import
AnalysisConfig
from
paddle.fluid.core
import
create_paddle_predictor
import
paddle.inference
as
paddle_infer
import
logging
logging
.
basicConfig
(
format
=
"%(asctime)s - %(levelname)s - %(message)s"
)
logger
=
logging
.
getLogger
(
"
fluid
"
)
logger
=
logging
.
getLogger
(
"
LocalPredictor
"
)
logger
.
setLevel
(
logging
.
INFO
)
...
...
@@ -62,7 +58,7 @@ class LocalPredictor(object):
use_xpu
=
False
,
use_feed_fetch_ops
=
False
):
"""
Load model config
and set the engine config for the paddle predictor
Load model config
s and create the paddle predictor by Paddle Inference API.
Args:
model_path: model config path.
...
...
@@ -83,14 +79,18 @@ class LocalPredictor(object):
model_conf
=
google
.
protobuf
.
text_format
.
Merge
(
str
(
f
.
read
()),
model_conf
)
if
os
.
path
.
exists
(
os
.
path
.
join
(
model_path
,
"__params__"
)):
config
=
AnalysisConfig
(
os
.
path
.
join
(
model_path
,
"__model__"
),
os
.
path
.
join
(
model_path
,
"__params__"
))
config
=
paddle_infer
.
Config
(
os
.
path
.
join
(
model_path
,
"__model__"
),
os
.
path
.
join
(
model_path
,
"__params__"
))
else
:
config
=
AnalysisConfig
(
model_path
)
logger
.
info
(
"load_model_config params: model_path:{}, use_gpu:{},
\
config
=
paddle_infer
.
Config
(
model_path
)
logger
.
info
(
"LocalPredictor load_model_config params: model_path:{}, use_gpu:{},
\
gpu_id:{}, use_profile:{}, thread_num:{}, mem_optim:{}, ir_optim:{},
\
use_trt:{}, use_lite:{}, use_xpu: {}, use_feed_fetch_ops:{}"
.
format
(
model_path
,
use_gpu
,
gpu_id
,
use_profile
,
thread_num
,
mem_optim
,
ir_optim
,
use_trt
,
use_lite
,
use_xpu
,
use_feed_fetch_ops
))
model_path
,
use_gpu
,
gpu_id
,
use_profile
,
thread_num
,
mem_optim
,
ir_optim
,
use_trt
,
use_lite
,
use_xpu
,
use_feed_fetch_ops
))
self
.
feed_names_
=
[
var
.
alias_name
for
var
in
model_conf
.
feed_var
]
self
.
fetch_names_
=
[
var
.
alias_name
for
var
in
model_conf
.
fetch_var
]
...
...
@@ -129,7 +129,7 @@ class LocalPredictor(object):
if
use_lite
:
config
.
enable_lite_engine
(
precision_mode
=
inference
.
PrecisionType
.
Float32
,
precision_mode
=
paddle_infer
.
PrecisionType
.
Float32
,
zero_copy
=
True
,
passes_filter
=
[],
ops_filter
=
[])
...
...
@@ -138,11 +138,11 @@ class LocalPredictor(object):
# 2MB l3 cache
config
.
enable_xpu
(
8
*
1024
*
1024
)
self
.
predictor
=
create_paddl
e_predictor
(
config
)
self
.
predictor
=
paddle_infer
.
creat
e_predictor
(
config
)
def
predict
(
self
,
feed
=
None
,
fetch
=
None
,
batch
=
False
,
log_id
=
0
):
"""
Predict locally
Run model inference by Paddle Inference API.
Args:
feed: feed var
...
...
@@ -155,14 +155,16 @@ class LocalPredictor(object):
fetch_map: dict
"""
if
feed
is
None
or
fetch
is
None
:
raise
ValueError
(
"You should specify feed and fetch for prediction"
)
raise
ValueError
(
"You should specify feed and fetch for prediction.
\
log_id:{}"
.
format
(
log_id
))
fetch_list
=
[]
if
isinstance
(
fetch
,
str
):
fetch_list
=
[
fetch
]
elif
isinstance
(
fetch
,
list
):
fetch_list
=
fetch
else
:
raise
ValueError
(
"Fetch only accepts string and list of string"
)
raise
ValueError
(
"Fetch only accepts string and list of string.
\
log_id:{}"
.
format
(
log_id
))
feed_batch
=
[]
if
isinstance
(
feed
,
dict
):
...
...
@@ -170,27 +172,21 @@ class LocalPredictor(object):
elif
isinstance
(
feed
,
list
):
feed_batch
=
feed
else
:
raise
ValueError
(
"Feed only accepts dict and list of dict"
)
int_slot_batch
=
[]
float_slot_batch
=
[]
int_feed_names
=
[]
float_feed_names
=
[]
int_shape
=
[]
float_shape
=
[]
fetch_names
=
[]
counter
=
0
batch_size
=
len
(
feed_batch
)
raise
ValueError
(
"Feed only accepts dict and list of dict.
\
log_id:{}"
.
format
(
log_id
))
fetch_names
=
[]
# Filter invalid fetch names
for
key
in
fetch_list
:
if
key
in
self
.
fetch_names_
:
fetch_names
.
append
(
key
)
if
len
(
fetch_names
)
==
0
:
raise
ValueError
(
"Fetch names should not be empty or out of saved fetch list.
"
)
return
{}
"Fetch names should not be empty or out of saved fetch list.
\
log_id:{}"
.
format
(
log_id
))
# Assemble the input data of paddle predictor
input_names
=
self
.
predictor
.
get_input_names
()
for
name
in
input_names
:
if
isinstance
(
feed
[
name
],
list
):
...
...
@@ -204,27 +200,31 @@ class LocalPredictor(object):
feed
[
name
]
=
feed
[
name
].
astype
(
"int32"
)
else
:
raise
ValueError
(
"local predictor receives wrong data type"
)
input_tensor
=
self
.
predictor
.
get_input_tensor
(
name
)
input_tensor
_handle
=
self
.
predictor
.
get_input_handle
(
name
)
if
"{}.lod"
.
format
(
name
)
in
feed
:
input_tensor
.
set_lod
([
feed
[
"{}.lod"
.
format
(
name
)]])
input_tensor
_handle
.
set_lod
([
feed
[
"{}.lod"
.
format
(
name
)]])
if
batch
==
False
:
input_tensor
.
copy_from_cpu
(
feed
[
name
][
np
.
newaxis
,
:])
input_tensor
_handle
.
copy_from_cpu
(
feed
[
name
][
np
.
newaxis
,
:])
else
:
input_tensor
.
copy_from_cpu
(
feed
[
name
])
output_tensors
=
[]
input_tensor
_handle
.
copy_from_cpu
(
feed
[
name
])
output_tensor
_handle
s
=
[]
output_names
=
self
.
predictor
.
get_output_names
()
for
output_name
in
output_names
:
output_tensor
=
self
.
predictor
.
get_output_tensor
(
output_name
)
output_tensors
.
append
(
output_tensor
)
output_tensor_handle
=
self
.
predictor
.
get_output_handle
(
output_name
)
output_tensor_handles
.
append
(
output_tensor_handle
)
# Run inference
self
.
predictor
.
run
()
# Assemble output data of predict results
outputs
=
[]
self
.
predictor
.
zero_copy_run
()
for
output_tensor
in
output_tensors
:
output
=
output_tensor
.
copy_to_cpu
()
for
output_tensor_handle
in
output_tensor_handles
:
output
=
output_tensor_handle
.
copy_to_cpu
()
outputs
.
append
(
output
)
fetch_map
=
{}
for
i
,
name
in
enumerate
(
fetch
):
fetch_map
[
name
]
=
outputs
[
i
]
if
len
(
output_tensors
[
i
].
lod
())
>
0
:
fetch_map
[
name
+
".lod"
]
=
np
.
array
(
output_tensor
s
[
i
].
lod
()[
0
]).
astype
(
'int32'
)
if
len
(
output_tensor
_handle
s
[
i
].
lod
())
>
0
:
fetch_map
[
name
+
".lod"
]
=
np
.
array
(
output_tensor
_handles
[
i
]
.
lod
()[
0
]).
astype
(
'int32'
)
return
fetch_map
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录