Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Serving
提交
8781ca33
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看板
提交
8781ca33
编写于
3月 19, 2020
作者:
M
MRXLT
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add batch predict for HTTP service
上级
b0bda428
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
74 addition
and
29 deletion
+74
-29
python/examples/imagenet/image_classification_service.py
python/examples/imagenet/image_classification_service.py
+15
-5
python/examples/imagenet/image_classification_service_gpu.py
python/examples/imagenet/image_classification_service_gpu.py
+17
-6
python/examples/imagenet/image_http_client.py
python/examples/imagenet/image_http_client.py
+13
-4
python/paddle_serving_server/web_service.py
python/paddle_serving_server/web_service.py
+14
-7
python/paddle_serving_server_gpu/web_service.py
python/paddle_serving_server_gpu/web_service.py
+15
-7
未找到文件。
python/examples/imagenet/image_classification_service.py
浏览文件 @
8781ca33
...
...
@@ -25,11 +25,21 @@ class ImageService(WebService):
reader
=
ImageReader
()
if
"image"
not
in
feed
:
raise
(
"feed data error!"
)
sample
=
base64
.
b64decode
(
feed
[
"image"
])
img
=
reader
.
process_image
(
sample
)
res_feed
=
{}
res_feed
[
"image"
]
=
img
.
reshape
(
-
1
)
return
res_feed
,
fetch
if
isinstance
(
feed
[
"image"
],
list
):
feed_batch
=
[]
for
image
in
feed
[
"image"
]:
sample
=
base64
.
b64decode
(
image
)
img
=
reader
.
process_image
(
sample
)
res_feed
=
{}
res_feed
[
"image"
]
=
img
.
reshape
(
-
1
)
feed_batch
.
append
(
res_feed
)
return
feed_batch
,
fetch
else
:
sample
=
base64
.
b64decode
(
feed
[
"image"
])
img
=
reader
.
process_image
(
sample
)
res_feed
=
{}
res_feed
[
"image"
]
=
img
.
reshape
(
-
1
)
return
res_feed
,
fetch
image_service
=
ImageService
(
name
=
"image"
)
...
...
python/examples/imagenet/image_classification_service_gpu.py
浏览文件 @
8781ca33
...
...
@@ -25,16 +25,27 @@ class ImageService(WebService):
reader
=
ImageReader
()
if
"image"
not
in
feed
:
raise
(
"feed data error!"
)
sample
=
base64
.
b64decode
(
feed
[
"image"
])
img
=
reader
.
process_image
(
sample
)
res_feed
=
{}
res_feed
[
"image"
]
=
img
.
reshape
(
-
1
)
return
res_feed
,
fetch
print
(
type
(
feed
[
"image"
]),
isinstance
(
feed
[
"image"
],
list
))
if
isinstance
(
feed
[
"image"
],
list
):
feed_batch
=
[]
for
image
in
feed
[
"image"
]:
sample
=
base64
.
b64decode
(
image
)
img
=
reader
.
process_image
(
sample
)
res_feed
=
{}
res_feed
[
"image"
]
=
img
.
reshape
(
-
1
)
feed_batch
.
append
(
res_feed
)
return
feed_batch
,
fetch
else
:
sample
=
base64
.
b64decode
(
feed
[
"image"
])
img
=
reader
.
process_image
(
sample
)
res_feed
=
{}
res_feed
[
"image"
]
=
img
.
reshape
(
-
1
)
return
res_feed
,
fetch
image_service
=
ImageService
(
name
=
"image"
)
image_service
.
load_model_config
(
sys
.
argv
[
1
])
image_service
.
set_gpus
(
"0,1
,2,3
"
)
image_service
.
set_gpus
(
"0,1"
)
image_service
.
prepare_server
(
workdir
=
sys
.
argv
[
2
],
port
=
int
(
sys
.
argv
[
3
]),
device
=
"gpu"
)
image_service
.
run_server
()
python/examples/imagenet/image_http_client.py
浏览文件 @
8781ca33
...
...
@@ -24,17 +24,26 @@ def predict(image_path, server):
req
=
json
.
dumps
({
"image"
:
image
,
"fetch"
:
[
"score"
]})
r
=
requests
.
post
(
server
,
data
=
req
,
headers
=
{
"Content-Type"
:
"application/json"
})
print
(
r
.
json
()[
"score"
][
0
])
return
r
def
batch_predict
(
image_path
,
server
):
image
=
base64
.
b64encode
(
open
(
image_path
).
read
())
req
=
json
.
dumps
({
"image"
:
[
image
,
image
],
"fetch"
:
[
"score"
]})
r
=
requests
.
post
(
server
,
data
=
req
,
headers
=
{
"Content-Type"
:
"application/json"
})
print
(
r
.
json
()[
"result"
][
1
][
"score"
][
0
])
return
r
if
__name__
==
"__main__"
:
server
=
"http://127.0.0.1:9
295
/image/prediction"
server
=
"http://127.0.0.1:9
393
/image/prediction"
#image_path = "./data/n01440764_10026.JPEG"
image_list
=
os
.
listdir
(
"./
data/
image_data/n01440764/"
)
image_list
=
os
.
listdir
(
"./image_data/n01440764/"
)
start
=
time
.
time
()
for
img
in
image_list
:
image_file
=
"./
data/
image_data/n01440764/"
+
img
image_file
=
"./image_data/n01440764/"
+
img
res
=
predict
(
image_file
,
server
)
print
(
res
.
json
()[
"score"
][
0
])
end
=
time
.
time
()
print
(
end
-
start
)
python/paddle_serving_server/web_service.py
浏览文件 @
8781ca33
...
...
@@ -64,12 +64,19 @@ class WebService(object):
if
"fetch"
not
in
request
.
json
:
abort
(
400
)
feed
,
fetch
=
self
.
preprocess
(
request
.
json
,
request
.
json
[
"fetch"
])
if
"fetch"
in
feed
:
del
feed
[
"fetch"
]
fetch_map
=
client_service
.
predict
(
feed
=
feed
,
fetch
=
fetch
)
fetch_map
=
self
.
postprocess
(
feed
=
request
.
json
,
fetch
=
fetch
,
fetch_map
=
fetch_map
)
return
fetch_map
if
isinstance
(
feed
,
list
):
fetch_map_batch
=
client_service
.
batch_predict
(
feed_batch
=
feed
,
fetch
=
fetch
)
fetch_map_batch
=
self
.
postprocess
(
feed
=
request
.
json
,
fetch
=
fetch
,
fetch_map
=
fetch_map_batch
)
result
=
{
"result"
:
fetch_map_batch
}
elif
isinstance
(
feed
,
dict
):
if
"fetch"
in
feed
:
del
feed
[
"fetch"
]
fetch_map
=
client_service
.
predict
(
feed
=
feed
,
fetch
=
fetch
)
result
=
self
.
postprocess
(
feed
=
request
.
json
,
fetch
=
fetch
,
fetch_map
=
fetch_map
)
return
result
app_instance
.
run
(
host
=
"0.0.0.0"
,
port
=
self
.
port
,
...
...
@@ -92,5 +99,5 @@ class WebService(object):
def
preprocess
(
self
,
feed
=
{},
fetch
=
[]):
return
feed
,
fetch
def
postprocess
(
self
,
feed
=
{},
fetch
=
[],
fetch_map
=
{}
):
def
postprocess
(
self
,
feed
=
{},
fetch
=
[],
fetch_map
=
None
):
return
fetch_map
python/paddle_serving_server_gpu/web_service.py
浏览文件 @
8781ca33
...
...
@@ -95,12 +95,20 @@ class WebService(object):
while
True
:
request_json
=
inputqueue
.
get
()
feed
,
fetch
=
self
.
preprocess
(
request_json
,
request_json
[
"fetch"
])
if
"fetch"
in
feed
:
del
feed
[
"fetch"
]
fetch_map
=
client
.
predict
(
feed
=
feed
,
fetch
=
fetch
)
fetch_map
=
self
.
postprocess
(
feed
=
request_json
,
fetch
=
fetch
,
fetch_map
=
fetch_map
)
self
.
output_queue
.
put
(
fetch_map
)
if
isinstance
(
feed
,
list
):
fetch_map_batch
=
client
.
batch_predict
(
feed_batch
=
feed
,
fetch
=
fetch
)
fetch_map_batch
=
self
.
postprocess
(
feed
=
request_json
,
fetch
=
fetch
,
fetch_map
=
fetch_map_batch
)
result
=
{
"result"
:
fetch_map_batch
}
elif
isinstance
(
feed
,
dict
):
if
"fetch"
in
feed
:
del
feed
[
"fetch"
]
fetch_map
=
client
.
predict
(
feed
=
feed
,
fetch
=
fetch
)
result
=
self
.
postprocess
(
feed
=
request_json
,
fetch
=
fetch
,
fetch_map
=
fetch_map
)
self
.
output_queue
.
put
(
result
)
def
_launch_web_service
(
self
,
gpu_num
):
app_instance
=
Flask
(
__name__
)
...
...
@@ -186,5 +194,5 @@ class WebService(object):
def
preprocess
(
self
,
feed
=
{},
fetch
=
[]):
return
feed
,
fetch
def
postprocess
(
self
,
feed
=
{},
fetch
=
[],
fetch_map
=
{}
):
def
postprocess
(
self
,
feed
=
{},
fetch
=
[],
fetch_map
=
None
):
return
fetch_map
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录