Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Serving
提交
93e3d126
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看板
提交
93e3d126
编写于
6月 09, 2020
作者:
B
barrierye
浏览文件
操作
浏览文件
下载
差异文件
merge
上级
1a811d4c
de55ec88
变更
20
隐藏空白更改
内联
并排
Showing
20 changed file
with
983 addition
and
20 deletion
+983
-20
core/configure/CMakeLists.txt
core/configure/CMakeLists.txt
+22
-0
core/configure/proto/multi_lang_general_model_service.proto
core/configure/proto/multi_lang_general_model_service.proto
+50
-0
python/examples/bert/benchmark.py
python/examples/bert/benchmark.py
+36
-4
python/examples/bert/bert_client.py
python/examples/bert/bert_client.py
+0
-8
python/examples/fit_a_line/test_multilang_client.py
python/examples/fit_a_line/test_multilang_client.py
+32
-0
python/examples/fit_a_line/test_multilang_server.py
python/examples/fit_a_line/test_multilang_server.py
+36
-0
python/examples/imagenet/benchmark.py
python/examples/imagenet/benchmark.py
+1
-1
python/examples/ocr_detection/7.jpg
python/examples/ocr_detection/7.jpg
+0
-0
python/examples/ocr_detection/text_det_client.py
python/examples/ocr_detection/text_det_client.py
+47
-0
python/paddle_serving_app/models/model_list.py
python/paddle_serving_app/models/model_list.py
+3
-0
python/paddle_serving_app/reader/__init__.py
python/paddle_serving_app/reader/__init__.py
+2
-1
python/paddle_serving_app/reader/image_reader.py
python/paddle_serving_app/reader/image_reader.py
+246
-0
python/paddle_serving_client/__init__.py
python/paddle_serving_client/__init__.py
+176
-2
python/paddle_serving_server/__init__.py
python/paddle_serving_server/__init__.py
+162
-0
python/paddle_serving_server_gpu/__init__.py
python/paddle_serving_server_gpu/__init__.py
+162
-0
python/requirements.txt
python/requirements.txt
+2
-0
python/setup.py.app.in
python/setup.py.app.in
+2
-1
python/setup.py.client.in
python/setup.py.client.in
+2
-1
python/setup.py.server.in
python/setup.py.server.in
+1
-1
python/setup.py.server_gpu.in
python/setup.py.server_gpu.in
+1
-1
未找到文件。
core/configure/CMakeLists.txt
浏览文件 @
93e3d126
...
...
@@ -35,6 +35,10 @@ py_proto_compile(general_model_config_py_proto SRCS proto/general_model_config.p
add_custom_target
(
general_model_config_py_proto_init ALL COMMAND
${
CMAKE_COMMAND
}
-E touch __init__.py
)
add_dependencies
(
general_model_config_py_proto general_model_config_py_proto_init
)
py_grpc_proto_compile
(
multi_lang_general_model_service_py_proto SRCS proto/multi_lang_general_model_service.proto
)
add_custom_target
(
multi_lang_general_model_service_py_proto_init ALL COMMAND
${
CMAKE_COMMAND
}
-E touch __init__.py
)
add_dependencies
(
multi_lang_general_model_service_py_proto multi_lang_general_model_service_py_proto_init
)
if
(
CLIENT
)
py_proto_compile
(
sdk_configure_py_proto SRCS proto/sdk_configure.proto
)
add_custom_target
(
sdk_configure_py_proto_init ALL COMMAND
${
CMAKE_COMMAND
}
-E touch __init__.py
)
...
...
@@ -50,6 +54,12 @@ add_custom_command(TARGET general_model_config_py_proto POST_BUILD
COMMAND cp *.py
${
PADDLE_SERVING_BINARY_DIR
}
/python/paddle_serving_client/proto
COMMENT
"Copy generated general_model_config proto file into directory paddle_serving_client/proto."
WORKING_DIRECTORY
${
CMAKE_CURRENT_BINARY_DIR
}
)
add_custom_command
(
TARGET multi_lang_general_model_service_py_proto POST_BUILD
COMMAND
${
CMAKE_COMMAND
}
-E make_directory
${
PADDLE_SERVING_BINARY_DIR
}
/python/paddle_serving_client/proto
COMMAND cp *.py
${
PADDLE_SERVING_BINARY_DIR
}
/python/paddle_serving_client/proto
COMMENT
"Copy generated multi_lang_general_model_service proto file into directory paddle_serving_client/proto."
WORKING_DIRECTORY
${
CMAKE_CURRENT_BINARY_DIR
}
)
endif
()
if
(
APP
)
...
...
@@ -96,6 +106,12 @@ add_custom_command(TARGET pyserving_channel_py_proto POST_BUILD
COMMAND cp *.py
${
PADDLE_SERVING_BINARY_DIR
}
/python/paddle_serving_server/proto
COMMENT
"Copy generated pyserving_channel proto file into directory paddle_serving_server/proto."
WORKING_DIRECTORY
${
CMAKE_CURRENT_BINARY_DIR
}
)
add_custom_command
(
TARGET multi_lang_general_model_service_py_proto POST_BUILD
COMMAND
${
CMAKE_COMMAND
}
-E make_directory
${
PADDLE_SERVING_BINARY_DIR
}
/python/paddle_serving_server/proto
COMMAND cp *.py
${
PADDLE_SERVING_BINARY_DIR
}
/python/paddle_serving_server/proto
COMMENT
"Copy generated multi_lang_general_model_service proto file into directory paddle_serving_server/proto."
WORKING_DIRECTORY
${
CMAKE_CURRENT_BINARY_DIR
}
)
else
()
add_custom_command
(
TARGET server_config_py_proto POST_BUILD
COMMAND
${
CMAKE_COMMAND
}
-E make_directory
...
...
@@ -126,5 +142,11 @@ add_custom_command(TARGET pyserving_channel_py_proto POST_BUILD
COMMAND cp *.py
${
PADDLE_SERVING_BINARY_DIR
}
/python/paddle_serving_server_gpu/proto
COMMENT
"Copy generated pyserving_channel proto file into directory paddle_serving_server_gpu/proto."
WORKING_DIRECTORY
${
CMAKE_CURRENT_BINARY_DIR
}
)
add_custom_command
(
TARGET multi_lang_general_model_service_py_proto POST_BUILD
COMMAND
${
CMAKE_COMMAND
}
-E make_directory
${
PADDLE_SERVING_BINARY_DIR
}
/python/paddle_serving_server_gpu/proto
COMMAND cp *.py
${
PADDLE_SERVING_BINARY_DIR
}
/python/paddle_serving_server_gpu/proto
COMMENT
"Copy generated multi_lang_general_model_service proto file into directory paddle_serving_server_gpu/proto."
WORKING_DIRECTORY
${
CMAKE_CURRENT_BINARY_DIR
}
)
endif
()
endif
()
core/configure/proto/multi_lang_general_model_service.proto
0 → 100644
浏览文件 @
93e3d126
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax
=
"proto2"
;
message
Tensor
{
optional
bytes
data
=
1
;
repeated
int32
int_data
=
2
;
repeated
int64
int64_data
=
3
;
repeated
float
float_data
=
4
;
optional
int32
elem_type
=
5
;
repeated
int32
shape
=
6
;
repeated
int32
lod
=
7
;
// only for fetch tensor currently
};
message
FeedInst
{
repeated
Tensor
tensor_array
=
1
;
};
message
FetchInst
{
repeated
Tensor
tensor_array
=
1
;
};
message
Request
{
repeated
FeedInst
insts
=
1
;
repeated
string
feed_var_names
=
2
;
repeated
string
fetch_var_names
=
3
;
required
bool
is_python
=
4
[
default
=
false
];
};
message
Response
{
repeated
ModelOutput
outputs
=
1
;
optional
string
tag
=
2
;
};
message
ModelOutput
{
repeated
FetchInst
insts
=
1
;
optional
string
engine_name
=
2
;
}
service
MultiLangGeneralModelService
{
rpc
inference
(
Request
)
returns
(
Response
)
{}
};
python/examples/bert/benchmark.py
浏览文件 @
93e3d126
...
...
@@ -19,6 +19,8 @@ from __future__ import unicode_literals, absolute_import
import
os
import
sys
import
time
import
json
import
requests
from
paddle_serving_client
import
Client
from
paddle_serving_client.utils
import
MultiThreadRunner
from
paddle_serving_client.utils
import
benchmark_args
,
show_latency
...
...
@@ -72,7 +74,39 @@ def single_func(idx, resource):
print
(
"unsupport batch size {}"
.
format
(
args
.
batch_size
))
elif
args
.
request
==
"http"
:
raise
(
"not implemented"
)
reader
=
ChineseBertReader
({
"max_seq_len"
:
128
})
fetch
=
[
"pooled_output"
]
server
=
"http://"
+
resource
[
"endpoint"
][
idx
%
len
(
resource
[
"endpoint"
])]
+
"/bert/prediction"
start
=
time
.
time
()
for
i
in
range
(
turns
):
if
args
.
batch_size
>=
1
:
l_start
=
time
.
time
()
feed_batch
=
[]
b_start
=
time
.
time
()
for
bi
in
range
(
args
.
batch_size
):
feed_batch
.
append
({
"words"
:
dataset
[
bi
]})
req
=
json
.
dumps
({
"feed"
:
feed_batch
,
"fetch"
:
fetch
})
b_end
=
time
.
time
()
if
profile_flags
:
sys
.
stderr
.
write
(
"PROFILE
\t
pid:{}
\t
bert_pre_0:{} bert_pre_1:{}
\n
"
.
format
(
os
.
getpid
(),
int
(
round
(
b_start
*
1000000
)),
int
(
round
(
b_end
*
1000000
))))
result
=
requests
.
post
(
server
,
data
=
req
,
headers
=
{
"Content-Type"
:
"application/json"
})
l_end
=
time
.
time
()
if
latency_flags
:
latency_list
.
append
(
l_end
*
1000
-
l_start
*
1000
)
else
:
print
(
"unsupport batch size {}"
.
format
(
args
.
batch_size
))
else
:
raise
ValueError
(
"not implemented {} request"
.
format
(
args
.
request
))
end
=
time
.
time
()
if
latency_flags
:
return
[[
end
-
start
],
latency_list
]
...
...
@@ -82,9 +116,7 @@ def single_func(idx, resource):
if
__name__
==
'__main__'
:
multi_thread_runner
=
MultiThreadRunner
()
endpoint_list
=
[
"127.0.0.1:9292"
,
"127.0.0.1:9293"
,
"127.0.0.1:9294"
,
"127.0.0.1:9295"
]
endpoint_list
=
[
"127.0.0.1:9292"
]
turns
=
10
start
=
time
.
time
()
result
=
multi_thread_runner
.
run
(
...
...
python/examples/bert/bert_client.py
浏览文件 @
93e3d126
...
...
@@ -14,15 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import
os
import
sys
import
numpy
as
np
import
paddlehub
as
hub
import
ujson
import
random
import
time
from
paddlehub.common.logger
import
logger
import
socket
from
paddle_serving_client
import
Client
from
paddle_serving_client.utils
import
benchmark_args
from
paddle_serving_app.reader
import
ChineseBertReader
...
...
python/examples/fit_a_line/test_multilang_client.py
0 → 100644
浏览文件 @
93e3d126
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=doc-string-missing
from
paddle_serving_client
import
MultiLangClient
import
sys
client
=
MultiLangClient
()
client
.
load_client_config
(
sys
.
argv
[
1
])
client
.
connect
([
"127.0.0.1:9393"
])
import
paddle
test_reader
=
paddle
.
batch
(
paddle
.
reader
.
shuffle
(
paddle
.
dataset
.
uci_housing
.
test
(),
buf_size
=
500
),
batch_size
=
1
)
for
data
in
test_reader
():
future
=
client
.
predict
(
feed
=
{
"x"
:
data
[
0
][
0
]},
fetch
=
[
"price"
],
asyn
=
True
)
fetch_map
=
future
.
result
()
print
(
"{} {}"
.
format
(
fetch_map
[
"price"
][
0
],
data
[
0
][
1
][
0
]))
python/examples/fit_a_line/test_multilang_server.py
0 → 100644
浏览文件 @
93e3d126
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=doc-string-missing
import
os
import
sys
from
paddle_serving_server
import
OpMaker
from
paddle_serving_server
import
OpSeqMaker
from
paddle_serving_server
import
MultiLangServer
op_maker
=
OpMaker
()
read_op
=
op_maker
.
create
(
'general_reader'
)
general_infer_op
=
op_maker
.
create
(
'general_infer'
)
response_op
=
op_maker
.
create
(
'general_response'
)
op_seq_maker
=
OpSeqMaker
()
op_seq_maker
.
add_op
(
read_op
)
op_seq_maker
.
add_op
(
general_infer_op
)
op_seq_maker
.
add_op
(
response_op
)
server
=
MultiLangServer
()
server
.
set_op_sequence
(
op_seq_maker
.
get_op_sequence
())
server
.
load_model_config
(
sys
.
argv
[
1
])
server
.
prepare_server
(
workdir
=
"work_dir1"
,
port
=
9393
,
device
=
"cpu"
)
server
.
run_server
()
python/examples/imagenet/benchmark.py
浏览文件 @
93e3d126
...
...
@@ -73,7 +73,7 @@ def single_func(idx, resource):
print
(
"unsupport batch size {}"
.
format
(
args
.
batch_size
))
elif
args
.
request
==
"http"
:
py_version
=
2
py_version
=
sys
.
version_info
[
0
]
server
=
"http://"
+
resource
[
"endpoint"
][
idx
%
len
(
resource
[
"endpoint"
])]
+
"/image/prediction"
start
=
time
.
time
()
...
...
python/examples/ocr_detection/7.jpg
0 → 100644
浏览文件 @
93e3d126
90.5 KB
python/examples/ocr_detection/text_det_client.py
0 → 100644
浏览文件 @
93e3d126
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
os
from
paddle_serving_client
import
Client
from
paddle_serving_app.reader
import
Sequential
,
File2Image
,
ResizeByFactor
from
paddle_serving_app.reader
import
Div
,
Normalize
,
Transpose
from
paddle_serving_app.reader
import
DBPostProcess
,
FilterBoxes
client
=
Client
()
client
.
load_client_config
(
"ocr_det_client/serving_client_conf.prototxt"
)
client
.
connect
([
"127.0.0.1:9494"
])
read_image_file
=
File2Image
()
preprocess
=
Sequential
([
ResizeByFactor
(
32
,
960
),
Div
(
255
),
Normalize
([
0.485
,
0.456
,
0.406
],
[
0.229
,
0.224
,
0.225
]),
Transpose
(
(
2
,
0
,
1
))
])
post_func
=
DBPostProcess
({
"thresh"
:
0.3
,
"box_thresh"
:
0.5
,
"max_candidates"
:
1000
,
"unclip_ratio"
:
1.5
,
"min_size"
:
3
})
filter_func
=
FilterBoxes
(
10
,
10
)
img
=
read_image_file
(
name
)
ori_h
,
ori_w
,
_
=
img
.
shape
img
=
preprocess
(
img
)
new_h
,
new_w
,
_
=
img
.
shape
ratio_list
=
[
float
(
new_h
)
/
ori_h
,
float
(
new_w
)
/
ori_w
]
outputs
=
client
.
predict
(
feed
=
{
"image"
:
img
},
fetch
=
[
"concat_1.tmp_0"
])
dt_boxes_list
=
post_func
(
outputs
[
"concat_1.tmp_0"
],
[
ratio_list
])
dt_boxes
=
filter_func
(
dt_boxes_list
[
0
],
[
ori_h
,
ori_w
])
python/paddle_serving_app/models/model_list.py
浏览文件 @
93e3d126
...
...
@@ -31,6 +31,7 @@ class ServingModels(object):
self
.
model_dict
[
"ImageClassification"
]
=
[
"resnet_v2_50_imagenet"
,
"mobilenet_v2_imagenet"
]
self
.
model_dict
[
"TextDetection"
]
=
[
"ocr_detection"
]
self
.
model_dict
[
"OCR"
]
=
[
"ocr_rec"
]
image_class_url
=
"https://paddle-serving.bj.bcebos.com/paddle_hub_models/image/ImageClassification/"
...
...
@@ -40,6 +41,7 @@ class ServingModels(object):
senta_url
=
"https://paddle-serving.bj.bcebos.com/paddle_hub_models/text/SentimentAnalysis/"
semantic_url
=
"https://paddle-serving.bj.bcebos.com/paddle_hub_models/text/SemanticModel/"
wordseg_url
=
"https://paddle-serving.bj.bcebos.com/paddle_hub_models/text/LexicalAnalysis/"
ocr_det_url
=
"https://paddle-serving.bj.bcebos.com/ocr/"
self
.
url_dict
=
{}
...
...
@@ -55,6 +57,7 @@ class ServingModels(object):
pack_url
(
self
.
model_dict
,
"ImageSegmentation"
,
image_seg_url
)
pack_url
(
self
.
model_dict
,
"ImageClassification"
,
image_class_url
)
pack_url
(
self
.
model_dict
,
"OCR"
,
ocr_url
)
pack_url
(
self
.
model_dict
,
"TextDetection"
,
ocr_det_url
)
def
get_model_list
(
self
):
return
self
.
model_dict
...
...
python/paddle_serving_app/reader/__init__.py
浏览文件 @
93e3d126
...
...
@@ -13,8 +13,9 @@
# limitations under the License.
from
.chinese_bert_reader
import
ChineseBertReader
from
.image_reader
import
ImageReader
,
File2Image
,
URL2Image
,
Sequential
,
Normalize
from
.image_reader
import
CenterCrop
,
Resize
,
Transpose
,
Div
,
RGB2BGR
,
BGR2RGB
from
.image_reader
import
CenterCrop
,
Resize
,
Transpose
,
Div
,
RGB2BGR
,
BGR2RGB
,
ResizeByFactor
from
.image_reader
import
RCNNPostprocess
,
SegPostprocess
,
PadStride
from
.image_reader
import
DBPostProcess
,
FilterBoxes
from
.lac_reader
import
LACReader
from
.senta_reader
import
SentaReader
from
.imdb_reader
import
IMDBDataset
...
...
python/paddle_serving_app/reader/image_reader.py
浏览文件 @
93e3d126
...
...
@@ -11,6 +11,9 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
cv2
import
os
import
numpy
as
np
...
...
@@ -18,6 +21,8 @@ import base64
import
sys
from
.
import
functional
as
F
from
PIL
import
Image
,
ImageDraw
from
shapely.geometry
import
Polygon
import
pyclipper
import
json
_cv2_interpolation_to_str
=
{
cv2
.
INTER_LINEAR
:
"cv2.INTER_LINEAR"
,
None
:
"None"
}
...
...
@@ -43,6 +48,196 @@ def generate_colormap(num_classes):
return
color_map
class
DBPostProcess
(
object
):
"""
The post process for Differentiable Binarization (DB).
"""
def
__init__
(
self
,
params
):
self
.
thresh
=
params
[
'thresh'
]
self
.
box_thresh
=
params
[
'box_thresh'
]
self
.
max_candidates
=
params
[
'max_candidates'
]
self
.
unclip_ratio
=
params
[
'unclip_ratio'
]
self
.
min_size
=
3
def
boxes_from_bitmap
(
self
,
pred
,
_bitmap
,
dest_width
,
dest_height
):
'''
_bitmap: single map with shape (1, H, W),
whose values are binarized as {0, 1}
'''
bitmap
=
_bitmap
height
,
width
=
bitmap
.
shape
outs
=
cv2
.
findContours
((
bitmap
*
255
).
astype
(
np
.
uint8
),
cv2
.
RETR_LIST
,
cv2
.
CHAIN_APPROX_SIMPLE
)
if
len
(
outs
)
==
3
:
img
,
contours
,
_
=
outs
[
0
],
outs
[
1
],
outs
[
2
]
elif
len
(
outs
)
==
2
:
contours
,
_
=
outs
[
0
],
outs
[
1
]
num_contours
=
min
(
len
(
contours
),
self
.
max_candidates
)
boxes
=
np
.
zeros
((
num_contours
,
4
,
2
),
dtype
=
np
.
int16
)
scores
=
np
.
zeros
((
num_contours
,
),
dtype
=
np
.
float32
)
for
index
in
range
(
num_contours
):
contour
=
contours
[
index
]
points
,
sside
=
self
.
get_mini_boxes
(
contour
)
if
sside
<
self
.
min_size
:
continue
points
=
np
.
array
(
points
)
score
=
self
.
box_score_fast
(
pred
,
points
.
reshape
(
-
1
,
2
))
if
self
.
box_thresh
>
score
:
continue
box
=
self
.
unclip
(
points
).
reshape
(
-
1
,
1
,
2
)
box
,
sside
=
self
.
get_mini_boxes
(
box
)
if
sside
<
self
.
min_size
+
2
:
continue
box
=
np
.
array
(
box
)
if
not
isinstance
(
dest_width
,
int
):
dest_width
=
dest_width
.
item
()
dest_height
=
dest_height
.
item
()
box
[:,
0
]
=
np
.
clip
(
np
.
round
(
box
[:,
0
]
/
width
*
dest_width
),
0
,
dest_width
)
box
[:,
1
]
=
np
.
clip
(
np
.
round
(
box
[:,
1
]
/
height
*
dest_height
),
0
,
dest_height
)
boxes
[
index
,
:,
:]
=
box
.
astype
(
np
.
int16
)
scores
[
index
]
=
score
return
boxes
,
scores
def
unclip
(
self
,
box
):
unclip_ratio
=
self
.
unclip_ratio
poly
=
Polygon
(
box
)
distance
=
poly
.
area
*
unclip_ratio
/
poly
.
length
offset
=
pyclipper
.
PyclipperOffset
()
offset
.
AddPath
(
box
,
pyclipper
.
JT_ROUND
,
pyclipper
.
ET_CLOSEDPOLYGON
)
expanded
=
np
.
array
(
offset
.
Execute
(
distance
))
return
expanded
def
get_mini_boxes
(
self
,
contour
):
bounding_box
=
cv2
.
minAreaRect
(
contour
)
points
=
sorted
(
list
(
cv2
.
boxPoints
(
bounding_box
)),
key
=
lambda
x
:
x
[
0
])
index_1
,
index_2
,
index_3
,
index_4
=
0
,
1
,
2
,
3
if
points
[
1
][
1
]
>
points
[
0
][
1
]:
index_1
=
0
index_4
=
1
else
:
index_1
=
1
index_4
=
0
if
points
[
3
][
1
]
>
points
[
2
][
1
]:
index_2
=
2
index_3
=
3
else
:
index_2
=
3
index_3
=
2
box
=
[
points
[
index_1
],
points
[
index_2
],
points
[
index_3
],
points
[
index_4
]
]
return
box
,
min
(
bounding_box
[
1
])
def
box_score_fast
(
self
,
bitmap
,
_box
):
h
,
w
=
bitmap
.
shape
[:
2
]
box
=
_box
.
copy
()
xmin
=
np
.
clip
(
np
.
floor
(
box
[:,
0
].
min
()).
astype
(
np
.
int
),
0
,
w
-
1
)
xmax
=
np
.
clip
(
np
.
ceil
(
box
[:,
0
].
max
()).
astype
(
np
.
int
),
0
,
w
-
1
)
ymin
=
np
.
clip
(
np
.
floor
(
box
[:,
1
].
min
()).
astype
(
np
.
int
),
0
,
h
-
1
)
ymax
=
np
.
clip
(
np
.
ceil
(
box
[:,
1
].
max
()).
astype
(
np
.
int
),
0
,
h
-
1
)
mask
=
np
.
zeros
((
ymax
-
ymin
+
1
,
xmax
-
xmin
+
1
),
dtype
=
np
.
uint8
)
box
[:,
0
]
=
box
[:,
0
]
-
xmin
box
[:,
1
]
=
box
[:,
1
]
-
ymin
cv2
.
fillPoly
(
mask
,
box
.
reshape
(
1
,
-
1
,
2
).
astype
(
np
.
int32
),
1
)
return
cv2
.
mean
(
bitmap
[
ymin
:
ymax
+
1
,
xmin
:
xmax
+
1
],
mask
)[
0
]
def
__call__
(
self
,
pred
,
ratio_list
):
pred
=
pred
[:,
0
,
:,
:]
segmentation
=
pred
>
self
.
thresh
boxes_batch
=
[]
for
batch_index
in
range
(
pred
.
shape
[
0
]):
height
,
width
=
pred
.
shape
[
-
2
:]
tmp_boxes
,
tmp_scores
=
self
.
boxes_from_bitmap
(
pred
[
batch_index
],
segmentation
[
batch_index
],
width
,
height
)
boxes
=
[]
for
k
in
range
(
len
(
tmp_boxes
)):
if
tmp_scores
[
k
]
>
self
.
box_thresh
:
boxes
.
append
(
tmp_boxes
[
k
])
if
len
(
boxes
)
>
0
:
boxes
=
np
.
array
(
boxes
)
ratio_h
,
ratio_w
=
ratio_list
[
batch_index
]
boxes
[:,
:,
0
]
=
boxes
[:,
:,
0
]
/
ratio_w
boxes
[:,
:,
1
]
=
boxes
[:,
:,
1
]
/
ratio_h
boxes_batch
.
append
(
boxes
)
return
boxes_batch
def
__repr__
(
self
):
return
self
.
__class__
.
__name__
+
\
" thresh: {1}, box_thresh: {2}, max_candidates: {3}, unclip_ratio: {4}, min_size: {5}"
.
format
(
self
.
thresh
,
self
.
box_thresh
,
self
.
max_candidates
,
self
.
unclip_ratio
,
self
.
min_size
)
class
FilterBoxes
(
object
):
def
__init__
(
self
,
width
,
height
):
self
.
filter_width
=
width
self
.
filter_height
=
height
def
order_points_clockwise
(
self
,
pts
):
"""
reference from: https://github.com/jrosebr1/imutils/blob/master/imutils/perspective.py
# sort the points based on their x-coordinates
"""
xSorted
=
pts
[
np
.
argsort
(
pts
[:,
0
]),
:]
# grab the left-most and right-most points from the sorted
# x-roodinate points
leftMost
=
xSorted
[:
2
,
:]
rightMost
=
xSorted
[
2
:,
:]
# now, sort the left-most coordinates according to their
# y-coordinates so we can grab the top-left and bottom-left
# points, respectively
leftMost
=
leftMost
[
np
.
argsort
(
leftMost
[:,
1
]),
:]
(
tl
,
bl
)
=
leftMost
rightMost
=
rightMost
[
np
.
argsort
(
rightMost
[:,
1
]),
:]
(
tr
,
br
)
=
rightMost
rect
=
np
.
array
([
tl
,
tr
,
br
,
bl
],
dtype
=
"float32"
)
return
rect
def
clip_det_res
(
self
,
points
,
img_height
,
img_width
):
for
pno
in
range
(
4
):
points
[
pno
,
0
]
=
int
(
min
(
max
(
points
[
pno
,
0
],
0
),
img_width
-
1
))
points
[
pno
,
1
]
=
int
(
min
(
max
(
points
[
pno
,
1
],
0
),
img_height
-
1
))
return
points
def
__call__
(
self
,
dt_boxes
,
image_shape
):
img_height
,
img_width
=
image_shape
[
0
:
2
]
dt_boxes_new
=
[]
for
box
in
dt_boxes
:
box
=
self
.
order_points_clockwise
(
box
)
box
=
self
.
clip_det_res
(
box
,
img_height
,
img_width
)
rect_width
=
int
(
np
.
linalg
.
norm
(
box
[
0
]
-
box
[
1
]))
rect_height
=
int
(
np
.
linalg
.
norm
(
box
[
0
]
-
box
[
3
]))
if
rect_width
<=
self
.
filter_width
or
\
rect_height
<=
self
.
filter_height
:
continue
dt_boxes_new
.
append
(
box
)
dt_boxes
=
np
.
array
(
dt_boxes_new
)
return
dt_boxes
def
__repr__
(
self
):
return
self
.
__class__
.
__name__
+
" filter_width: {1}, filter_height: {2}"
.
format
(
self
.
filter_width
,
self
.
filter_height
)
class
SegPostprocess
(
object
):
def
__init__
(
self
,
class_num
):
self
.
class_num
=
class_num
...
...
@@ -473,6 +668,57 @@ class Resize(object):
_cv2_interpolation_to_str
[
self
.
interpolation
])
class
ResizeByFactor
(
object
):
"""Resize the input numpy array Image to a size multiple of factor which is usually required by a network
Args:
factor (int): Resize factor. make width and height multiple factor of the value of factor. Default is 32
max_side_len (int): max size of width and height. if width or height is larger than max_side_len, just resize the width or the height. Default is 2400
"""
def
__init__
(
self
,
factor
=
32
,
max_side_len
=
2400
):
self
.
factor
=
factor
self
.
max_side_len
=
max_side_len
def
__call__
(
self
,
img
):
h
,
w
,
_
=
img
.
shape
resize_w
=
w
resize_h
=
h
if
max
(
resize_h
,
resize_w
)
>
self
.
max_side_len
:
if
resize_h
>
resize_w
:
ratio
=
float
(
self
.
max_side_len
)
/
resize_h
else
:
ratio
=
float
(
self
.
max_side_len
)
/
resize_w
else
:
ratio
=
1.
resize_h
=
int
(
resize_h
*
ratio
)
resize_w
=
int
(
resize_w
*
ratio
)
if
resize_h
%
self
.
factor
==
0
:
resize_h
=
resize_h
elif
resize_h
//
self
.
factor
<=
1
:
resize_h
=
self
.
factor
else
:
resize_h
=
(
resize_h
//
32
-
1
)
*
32
if
resize_w
%
self
.
factor
==
0
:
resize_w
=
resize_w
elif
resize_w
//
self
.
factor
<=
1
:
resize_w
=
self
.
factor
else
:
resize_w
=
(
resize_w
//
self
.
factor
-
1
)
*
self
.
factor
try
:
if
int
(
resize_w
)
<=
0
or
int
(
resize_h
)
<=
0
:
return
None
,
(
None
,
None
)
im
=
cv2
.
resize
(
img
,
(
int
(
resize_w
),
int
(
resize_h
)))
except
:
print
(
resize_w
,
resize_h
)
sys
.
exit
(
0
)
return
im
def
__repr__
(
self
):
return
self
.
__class__
.
__name__
+
'(factor={0}, max_side_len={1})'
.
format
(
self
.
factor
,
self
.
max_side_len
)
class
PadStride
(
object
):
def
__init__
(
self
,
stride
):
self
.
coarsest_stride
=
stride
...
...
python/paddle_serving_client/__init__.py
浏览文件 @
93e3d126
...
...
@@ -21,7 +21,10 @@ import google.protobuf.text_format
import
numpy
as
np
import
time
import
sys
from
.serving_client
import
PredictorRes
import
grpc
from
.proto
import
multi_lang_general_model_service_pb2
from
.proto
import
multi_lang_general_model_service_pb2_grpc
int_type
=
0
float_type
=
1
...
...
@@ -125,6 +128,8 @@ class Client(object):
self
.
all_numpy_input
=
True
self
.
has_numpy_input
=
False
self
.
rpc_timeout_ms
=
20000
from
.serving_client
import
PredictorRes
self
.
predictorres_constructor
=
PredictorRes
def
load_client_config
(
self
,
path
):
from
.serving_client
import
PredictorClient
...
...
@@ -304,7 +309,7 @@ class Client(object):
self
.
profile_
.
record
(
'py_prepro_1'
)
self
.
profile_
.
record
(
'py_client_infer_0'
)
result_batch_handle
=
PredictorRes
()
result_batch_handle
=
self
.
predictorres_constructor
()
if
self
.
all_numpy_input
:
res
=
self
.
client_handle_
.
numpy_predict
(
float_slot_batch
,
float_feed_names
,
float_shape
,
int_slot_batch
,
...
...
@@ -372,3 +377,172 @@ class Client(object):
def
release
(
self
):
self
.
client_handle_
.
destroy_predictor
()
self
.
client_handle_
=
None
class
MultiLangClient
(
object
):
def
__init__
(
self
):
self
.
channel_
=
None
def
load_client_config
(
self
,
path
):
if
not
isinstance
(
path
,
str
):
raise
Exception
(
"GClient only supports multi-model temporarily"
)
self
.
_parse_model_config
(
path
)
def
connect
(
self
,
endpoint
):
self
.
channel_
=
grpc
.
insecure_channel
(
endpoint
[
0
])
#TODO
self
.
stub_
=
multi_lang_general_model_service_pb2_grpc
.
MultiLangGeneralModelServiceStub
(
self
.
channel_
)
def
_flatten_list
(
self
,
nested_list
):
for
item
in
nested_list
:
if
isinstance
(
item
,
(
list
,
tuple
)):
for
sub_item
in
self
.
_flatten_list
(
item
):
yield
sub_item
else
:
yield
item
def
_parse_model_config
(
self
,
model_config_path
):
model_conf
=
m_config
.
GeneralModelConfig
()
f
=
open
(
model_config_path
,
'r'
)
model_conf
=
google
.
protobuf
.
text_format
.
Merge
(
str
(
f
.
read
()),
model_conf
)
self
.
feed_names_
=
[
var
.
alias_name
for
var
in
model_conf
.
feed_var
]
self
.
feed_types_
=
{}
self
.
feed_shapes_
=
{}
self
.
fetch_names_
=
[
var
.
alias_name
for
var
in
model_conf
.
fetch_var
]
self
.
fetch_types_
=
{}
self
.
lod_tensor_set_
=
set
()
for
i
,
var
in
enumerate
(
model_conf
.
feed_var
):
self
.
feed_types_
[
var
.
alias_name
]
=
var
.
feed_type
self
.
feed_shapes_
[
var
.
alias_name
]
=
var
.
shape
if
var
.
is_lod_tensor
:
self
.
lod_tensor_set_
.
add
(
var
.
alias_name
)
else
:
counter
=
1
for
dim
in
self
.
feed_shapes_
[
var
.
alias_name
]:
counter
*=
dim
for
i
,
var
in
enumerate
(
model_conf
.
fetch_var
):
self
.
fetch_types_
[
var
.
alias_name
]
=
var
.
fetch_type
if
var
.
is_lod_tensor
:
self
.
lod_tensor_set_
.
add
(
var
.
alias_name
)
def
_pack_feed_data
(
self
,
feed
,
fetch
,
is_python
):
req
=
multi_lang_general_model_service_pb2
.
Request
()
req
.
fetch_var_names
.
extend
(
fetch
)
req
.
feed_var_names
.
extend
(
feed
.
keys
())
req
.
is_python
=
is_python
feed_batch
=
None
if
isinstance
(
feed
,
dict
):
feed_batch
=
[
feed
]
elif
isinstance
(
feed
,
list
):
feed_batch
=
feed
else
:
raise
Exception
(
"{} not support"
.
format
(
type
(
feed
)))
init_feed_names
=
False
for
feed_data
in
feed_batch
:
inst
=
multi_lang_general_model_service_pb2
.
FeedInst
()
for
name
in
req
.
feed_var_names
:
tensor
=
multi_lang_general_model_service_pb2
.
Tensor
()
var
=
feed_data
[
name
]
v_type
=
self
.
feed_types_
[
name
]
if
is_python
:
data
=
None
if
isinstance
(
var
,
list
):
if
v_type
==
0
:
# int64
data
=
np
.
array
(
var
,
dtype
=
"int64"
)
elif
v_type
==
1
:
# float32
data
=
np
.
array
(
var
,
dtype
=
"float32"
)
else
:
raise
Exception
(
"error type."
)
else
:
data
=
var
if
var
.
dtype
==
"float64"
:
data
=
data
.
astype
(
"float32"
)
tensor
.
data
=
data
.
tobytes
()
else
:
if
v_type
==
0
:
# int64
if
isinstance
(
var
,
np
.
ndarray
):
tensor
.
int64_data
.
extend
(
var
.
reshape
(
-
1
).
tolist
())
else
:
tensor
.
int64_data
.
extend
(
self
.
_flatten_list
(
var
))
elif
v_type
==
1
:
# float32
if
isinstance
(
var
,
np
.
ndarray
):
tensor
.
float_data
.
extend
(
var
.
reshape
(
-
1
).
tolist
())
else
:
tensor
.
float_data
.
extend
(
self
.
_flatten_list
(
var
))
else
:
raise
Exception
(
"error type."
)
if
isinstance
(
var
,
np
.
ndarray
):
tensor
.
shape
.
extend
(
list
(
var
.
shape
))
else
:
tensor
.
shape
.
extend
(
self
.
feed_shapes_
[
name
])
inst
.
tensor_array
.
append
(
tensor
)
req
.
insts
.
append
(
inst
)
return
req
def
_unpack_resp
(
self
,
resp
,
fetch
,
is_python
,
need_variant_tag
):
result_map
=
{}
inst
=
resp
.
outputs
[
0
].
insts
[
0
]
tag
=
resp
.
tag
for
i
,
name
in
enumerate
(
fetch
):
var
=
inst
.
tensor_array
[
i
]
v_type
=
self
.
fetch_types_
[
name
]
if
is_python
:
if
v_type
==
0
:
# int64
result_map
[
name
]
=
np
.
frombuffer
(
var
.
data
,
dtype
=
"int64"
)
elif
v_type
==
1
:
# float32
result_map
[
name
]
=
np
.
frombuffer
(
var
.
data
,
dtype
=
"float32"
)
else
:
raise
Exception
(
"error type."
)
else
:
if
v_type
==
0
:
# int64
result_map
[
name
]
=
np
.
array
(
list
(
var
.
int64_data
),
dtype
=
"int64"
)
elif
v_type
==
1
:
# float32
result_map
[
name
]
=
np
.
array
(
list
(
var
.
float_data
),
dtype
=
"float32"
)
else
:
raise
Exception
(
"error type."
)
result_map
[
name
].
shape
=
list
(
var
.
shape
)
if
name
in
self
.
lod_tensor_set_
:
result_map
[
"{}.lod"
.
format
(
name
)]
=
np
.
array
(
list
(
var
.
lod
))
return
result_map
if
not
need_variant_tag
else
[
result_map
,
tag
]
def
_done_callback_func
(
self
,
fetch
,
is_python
,
need_variant_tag
):
def
unpack_resp
(
resp
):
return
self
.
_unpack_resp
(
resp
,
fetch
,
is_python
,
need_variant_tag
)
return
unpack_resp
def
predict
(
self
,
feed
,
fetch
,
need_variant_tag
=
False
,
asyn
=
False
,
is_python
=
True
):
req
=
self
.
_pack_feed_data
(
feed
,
fetch
,
is_python
=
is_python
)
if
not
asyn
:
resp
=
self
.
stub_
.
inference
(
req
)
return
self
.
_unpack_resp
(
resp
,
fetch
,
is_python
=
is_python
,
need_variant_tag
=
need_variant_tag
)
else
:
call_future
=
self
.
stub_
.
inference
.
future
(
req
)
return
MultiLangPredictFuture
(
call_future
,
self
.
_done_callback_func
(
fetch
,
is_python
=
is_python
,
need_variant_tag
=
need_variant_tag
))
class
MultiLangPredictFuture
(
object
):
def
__init__
(
self
,
call_future
,
callback_func
):
self
.
call_future_
=
call_future
self
.
callback_func_
=
callback_func
def
result
(
self
):
resp
=
self
.
call_future_
.
result
()
return
self
.
callback_func_
(
resp
)
python/paddle_serving_server/__init__.py
浏览文件 @
93e3d126
...
...
@@ -25,6 +25,13 @@ from contextlib import closing
import
collections
import
fcntl
import
numpy
as
np
import
grpc
from
.proto
import
multi_lang_general_model_service_pb2
from
.proto
import
multi_lang_general_model_service_pb2_grpc
from
multiprocessing
import
Pool
,
Process
from
concurrent
import
futures
class
OpMaker
(
object
):
def
__init__
(
self
):
...
...
@@ -428,3 +435,158 @@ class Server(object):
print
(
"Going to Run Command"
)
print
(
command
)
os
.
system
(
command
)
class
MultiLangServerService
(
multi_lang_general_model_service_pb2_grpc
.
MultiLangGeneralModelService
):
def
__init__
(
self
,
model_config_path
,
endpoints
):
from
paddle_serving_client
import
Client
self
.
_parse_model_config
(
model_config_path
)
self
.
bclient_
=
Client
()
self
.
bclient_
.
load_client_config
(
"{}/serving_server_conf.prototxt"
.
format
(
model_config_path
))
self
.
bclient_
.
connect
(
endpoints
)
def
_parse_model_config
(
self
,
model_config_path
):
model_conf
=
m_config
.
GeneralModelConfig
()
f
=
open
(
"{}/serving_server_conf.prototxt"
.
format
(
model_config_path
),
'r'
)
model_conf
=
google
.
protobuf
.
text_format
.
Merge
(
str
(
f
.
read
()),
model_conf
)
self
.
feed_names_
=
[
var
.
alias_name
for
var
in
model_conf
.
feed_var
]
self
.
feed_types_
=
{}
self
.
feed_shapes_
=
{}
self
.
fetch_names_
=
[
var
.
alias_name
for
var
in
model_conf
.
fetch_var
]
self
.
fetch_types_
=
{}
self
.
lod_tensor_set_
=
set
()
for
i
,
var
in
enumerate
(
model_conf
.
feed_var
):
self
.
feed_types_
[
var
.
alias_name
]
=
var
.
feed_type
self
.
feed_shapes_
[
var
.
alias_name
]
=
var
.
shape
if
var
.
is_lod_tensor
:
self
.
lod_tensor_set_
.
add
(
var
.
alias_name
)
for
i
,
var
in
enumerate
(
model_conf
.
fetch_var
):
self
.
fetch_types_
[
var
.
alias_name
]
=
var
.
fetch_type
if
var
.
is_lod_tensor
:
self
.
lod_tensor_set_
.
add
(
var
.
alias_name
)
def
_flatten_list
(
self
,
nested_list
):
for
item
in
nested_list
:
if
isinstance
(
item
,
(
list
,
tuple
)):
for
sub_item
in
self
.
_flatten_list
(
item
):
yield
sub_item
else
:
yield
item
def
_unpack_request
(
self
,
request
):
feed_names
=
list
(
request
.
feed_var_names
)
fetch_names
=
list
(
request
.
fetch_var_names
)
is_python
=
request
.
is_python
feed_batch
=
[]
for
feed_inst
in
request
.
insts
:
feed_dict
=
{}
for
idx
,
name
in
enumerate
(
feed_names
):
var
=
feed_inst
.
tensor_array
[
idx
]
v_type
=
self
.
feed_types_
[
name
]
data
=
None
if
is_python
:
if
v_type
==
0
:
data
=
np
.
frombuffer
(
var
.
data
,
dtype
=
"int64"
)
elif
v_type
==
1
:
data
=
np
.
frombuffer
(
var
.
data
,
dtype
=
"float32"
)
else
:
raise
Exception
(
"error type."
)
else
:
if
v_type
==
0
:
# int64
data
=
np
.
array
(
list
(
var
.
int64_data
),
dtype
=
"int64"
)
elif
v_type
==
1
:
# float32
data
=
np
.
array
(
list
(
var
.
float_data
),
dtype
=
"float32"
)
else
:
raise
Exception
(
"error type."
)
data
.
shape
=
list
(
feed_inst
.
tensor_array
[
idx
].
shape
)
feed_dict
[
name
]
=
data
feed_batch
.
append
(
feed_dict
)
return
feed_batch
,
fetch_names
,
is_python
def
_pack_resp_package
(
self
,
result
,
fetch_names
,
is_python
,
tag
):
resp
=
multi_lang_general_model_service_pb2
.
Response
()
# Only one model is supported temporarily
model_output
=
multi_lang_general_model_service_pb2
.
ModelOutput
()
inst
=
multi_lang_general_model_service_pb2
.
FetchInst
()
for
idx
,
name
in
enumerate
(
fetch_names
):
tensor
=
multi_lang_general_model_service_pb2
.
Tensor
()
v_type
=
self
.
fetch_types_
[
name
]
if
is_python
:
tensor
.
data
=
result
[
name
].
tobytes
()
else
:
if
v_type
==
0
:
# int64
tensor
.
int64_data
.
extend
(
result
[
name
].
reshape
(
-
1
).
tolist
())
elif
v_type
==
1
:
# float32
tensor
.
float_data
.
extend
(
result
[
name
].
reshape
(
-
1
).
tolist
())
else
:
raise
Exception
(
"error type."
)
tensor
.
shape
.
extend
(
list
(
result
[
name
].
shape
))
if
name
in
self
.
lod_tensor_set_
:
tensor
.
lod
.
extend
(
result
[
"{}.lod"
.
format
(
name
)].
tolist
())
inst
.
tensor_array
.
append
(
tensor
)
model_output
.
insts
.
append
(
inst
)
resp
.
outputs
.
append
(
model_output
)
resp
.
tag
=
tag
return
resp
def
inference
(
self
,
request
,
context
):
feed_dict
,
fetch_names
,
is_python
=
self
.
_unpack_request
(
request
)
data
,
tag
=
self
.
bclient_
.
predict
(
feed
=
feed_dict
,
fetch
=
fetch_names
,
need_variant_tag
=
True
)
return
self
.
_pack_resp_package
(
data
,
fetch_names
,
is_python
,
tag
)
class
MultiLangServer
(
object
):
def
__init__
(
self
,
worker_num
=
2
):
self
.
bserver_
=
Server
()
self
.
worker_num_
=
worker_num
def
set_op_sequence
(
self
,
op_seq
):
self
.
bserver_
.
set_op_sequence
(
op_seq
)
def
load_model_config
(
self
,
model_config_path
):
if
not
isinstance
(
model_config_path
,
str
):
raise
Exception
(
"MultiLangServer only supports multi-model temporarily"
)
self
.
bserver_
.
load_model_config
(
model_config_path
)
self
.
model_config_path_
=
model_config_path
def
prepare_server
(
self
,
workdir
=
None
,
port
=
9292
,
device
=
"cpu"
):
default_port
=
12000
self
.
port_list_
=
[]
for
i
in
range
(
1000
):
if
default_port
+
i
!=
port
and
self
.
_port_is_available
(
default_port
+
i
):
self
.
port_list_
.
append
(
default_port
+
i
)
break
self
.
bserver_
.
prepare_server
(
workdir
=
workdir
,
port
=
self
.
port_list_
[
0
],
device
=
device
)
self
.
gport_
=
port
def
_launch_brpc_service
(
self
,
bserver
):
bserver
.
run_server
()
def
_port_is_available
(
self
,
port
):
with
closing
(
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
))
as
sock
:
sock
.
settimeout
(
2
)
result
=
sock
.
connect_ex
((
'0.0.0.0'
,
port
))
return
result
!=
0
def
run_server
(
self
):
p_bserver
=
Process
(
target
=
self
.
_launch_brpc_service
,
args
=
(
self
.
bserver_
,
))
p_bserver
.
start
()
server
=
grpc
.
server
(
futures
.
ThreadPoolExecutor
(
max_workers
=
self
.
worker_num_
))
multi_lang_general_model_service_pb2_grpc
.
add_MultiLangGeneralModelServiceServicer_to_server
(
MultiLangServerService
(
self
.
model_config_path_
,
[
"0.0.0.0:{}"
.
format
(
self
.
port_list_
[
0
])]),
server
)
server
.
add_insecure_port
(
'[::]:{}'
.
format
(
self
.
gport_
))
server
.
start
()
p_bserver
.
join
()
server
.
wait_for_termination
()
python/paddle_serving_server_gpu/__init__.py
浏览文件 @
93e3d126
...
...
@@ -27,6 +27,13 @@ import argparse
import
collections
import
fcntl
import
numpy
as
np
import
grpc
from
.proto
import
multi_lang_general_model_service_pb2
from
.proto
import
multi_lang_general_model_service_pb2_grpc
from
multiprocessing
import
Pool
,
Process
from
concurrent
import
futures
def
serve_args
():
parser
=
argparse
.
ArgumentParser
(
"serve"
)
...
...
@@ -469,3 +476,158 @@ class Server(object):
print
(
command
)
os
.
system
(
command
)
class
MultiLangServerService
(
multi_lang_general_model_service_pb2_grpc
.
MultiLangGeneralModelService
):
def
__init__
(
self
,
model_config_path
,
endpoints
):
from
paddle_serving_client
import
Client
self
.
_parse_model_config
(
model_config_path
)
self
.
bclient_
=
Client
()
self
.
bclient_
.
load_client_config
(
"{}/serving_server_conf.prototxt"
.
format
(
model_config_path
))
self
.
bclient_
.
connect
(
endpoints
)
def
_parse_model_config
(
self
,
model_config_path
):
model_conf
=
m_config
.
GeneralModelConfig
()
f
=
open
(
"{}/serving_server_conf.prototxt"
.
format
(
model_config_path
),
'r'
)
model_conf
=
google
.
protobuf
.
text_format
.
Merge
(
str
(
f
.
read
()),
model_conf
)
self
.
feed_names_
=
[
var
.
alias_name
for
var
in
model_conf
.
feed_var
]
self
.
feed_types_
=
{}
self
.
feed_shapes_
=
{}
self
.
fetch_names_
=
[
var
.
alias_name
for
var
in
model_conf
.
fetch_var
]
self
.
fetch_types_
=
{}
self
.
lod_tensor_set_
=
set
()
for
i
,
var
in
enumerate
(
model_conf
.
feed_var
):
self
.
feed_types_
[
var
.
alias_name
]
=
var
.
feed_type
self
.
feed_shapes_
[
var
.
alias_name
]
=
var
.
shape
if
var
.
is_lod_tensor
:
self
.
lod_tensor_set_
.
add
(
var
.
alias_name
)
for
i
,
var
in
enumerate
(
model_conf
.
fetch_var
):
self
.
fetch_types_
[
var
.
alias_name
]
=
var
.
fetch_type
if
var
.
is_lod_tensor
:
self
.
lod_tensor_set_
.
add
(
var
.
alias_name
)
def
_flatten_list
(
self
,
nested_list
):
for
item
in
nested_list
:
if
isinstance
(
item
,
(
list
,
tuple
)):
for
sub_item
in
self
.
_flatten_list
(
item
):
yield
sub_item
else
:
yield
item
def
_unpack_request
(
self
,
request
):
feed_names
=
list
(
request
.
feed_var_names
)
fetch_names
=
list
(
request
.
fetch_var_names
)
is_python
=
request
.
is_python
feed_batch
=
[]
for
feed_inst
in
request
.
insts
:
feed_dict
=
{}
for
idx
,
name
in
enumerate
(
feed_names
):
var
=
feed_inst
.
tensor_array
[
idx
]
v_type
=
self
.
feed_types_
[
name
]
data
=
None
if
is_python
:
if
v_type
==
0
:
data
=
np
.
frombuffer
(
var
.
data
,
dtype
=
"int64"
)
elif
v_type
==
1
:
data
=
np
.
frombuffer
(
var
.
data
,
dtype
=
"float32"
)
else
:
raise
Exception
(
"error type."
)
else
:
if
v_type
==
0
:
# int64
data
=
np
.
array
(
list
(
var
.
int64_data
),
dtype
=
"int64"
)
elif
v_type
==
1
:
# float32
data
=
np
.
array
(
list
(
var
.
float_data
),
dtype
=
"float32"
)
else
:
raise
Exception
(
"error type."
)
data
.
shape
=
list
(
feed_inst
.
tensor_array
[
idx
].
shape
)
feed_dict
[
name
]
=
data
feed_batch
.
append
(
feed_dict
)
return
feed_batch
,
fetch_names
,
is_python
def
_pack_resp_package
(
self
,
result
,
fetch_names
,
is_python
,
tag
):
resp
=
multi_lang_general_model_service_pb2
.
Response
()
# Only one model is supported temporarily
model_output
=
multi_lang_general_model_service_pb2
.
ModelOutput
()
inst
=
multi_lang_general_model_service_pb2
.
FetchInst
()
for
idx
,
name
in
enumerate
(
fetch_names
):
tensor
=
multi_lang_general_model_service_pb2
.
Tensor
()
v_type
=
self
.
fetch_types_
[
name
]
if
is_python
:
tensor
.
data
=
result
[
name
].
tobytes
()
else
:
if
v_type
==
0
:
# int64
tensor
.
int64_data
.
extend
(
result
[
name
].
reshape
(
-
1
).
tolist
())
elif
v_type
==
1
:
# float32
tensor
.
float_data
.
extend
(
result
[
name
].
reshape
(
-
1
).
tolist
())
else
:
raise
Exception
(
"error type."
)
tensor
.
shape
.
extend
(
list
(
result
[
name
].
shape
))
if
name
in
self
.
lod_tensor_set_
:
tensor
.
lod
.
extend
(
result
[
"{}.lod"
.
format
(
name
)].
tolist
())
inst
.
tensor_array
.
append
(
tensor
)
model_output
.
insts
.
append
(
inst
)
resp
.
outputs
.
append
(
model_output
)
resp
.
tag
=
tag
return
resp
def
inference
(
self
,
request
,
context
):
feed_dict
,
fetch_names
,
is_python
=
self
.
_unpack_request
(
request
)
data
,
tag
=
self
.
bclient_
.
predict
(
feed
=
feed_dict
,
fetch
=
fetch_names
,
need_variant_tag
=
True
)
return
self
.
_pack_resp_package
(
data
,
fetch_names
,
is_python
,
tag
)
class
MultiLangServer
(
object
):
def
__init__
(
self
,
worker_num
=
2
):
self
.
bserver_
=
Server
()
self
.
worker_num_
=
worker_num
def
set_op_sequence
(
self
,
op_seq
):
self
.
bserver_
.
set_op_sequence
(
op_seq
)
def
load_model_config
(
self
,
model_config_path
):
if
not
isinstance
(
model_config_path
,
str
):
raise
Exception
(
"MultiLangServer only supports multi-model temporarily"
)
self
.
bserver_
.
load_model_config
(
model_config_path
)
self
.
model_config_path_
=
model_config_path
def
prepare_server
(
self
,
workdir
=
None
,
port
=
9292
,
device
=
"cpu"
):
default_port
=
12000
self
.
port_list_
=
[]
for
i
in
range
(
1000
):
if
default_port
+
i
!=
port
and
self
.
_port_is_available
(
default_port
+
i
):
self
.
port_list_
.
append
(
default_port
+
i
)
break
self
.
bserver_
.
prepare_server
(
workdir
=
workdir
,
port
=
self
.
port_list_
[
0
],
device
=
device
)
self
.
gport_
=
port
def
_launch_brpc_service
(
self
,
bserver
):
bserver
.
run_server
()
def
_port_is_available
(
self
,
port
):
with
closing
(
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
))
as
sock
:
sock
.
settimeout
(
2
)
result
=
sock
.
connect_ex
((
'0.0.0.0'
,
port
))
return
result
!=
0
def
run_server
(
self
):
p_bserver
=
Process
(
target
=
self
.
_launch_brpc_service
,
args
=
(
self
.
bserver_
,
))
p_bserver
.
start
()
server
=
grpc
.
server
(
futures
.
ThreadPoolExecutor
(
max_workers
=
self
.
worker_num_
))
multi_lang_general_model_service_pb2_grpc
.
add_MultiLangGeneralModelServiceServicer_to_server
(
MultiLangServerService
(
self
.
model_config_path_
,
[
"0.0.0.0:{}"
.
format
(
self
.
port_list_
[
0
])]),
server
)
server
.
add_insecure_port
(
'[::]:{}'
.
format
(
self
.
gport_
))
server
.
start
()
p_bserver
.
join
()
server
.
wait_for_termination
()
python/requirements.txt
浏览文件 @
93e3d126
numpy>=1.12, <=1.16.4 ; python_version<"3.5"
grpcio-tools>=1.28.1
grpcio>=1.28.1
python/setup.py.app.in
浏览文件 @
93e3d126
...
...
@@ -42,7 +42,8 @@ if '${PACK}' == 'ON':
REQUIRED_PACKAGES = [
'six >= 1.10.0', 'sentencepiece', 'opencv-python', 'pillow'
'six >= 1.10.0', 'sentencepiece', 'opencv-python', 'pillow',
'shapely', 'pyclipper'
]
packages=['paddle_serving_app',
...
...
python/setup.py.client.in
浏览文件 @
93e3d126
...
...
@@ -58,7 +58,8 @@ if '${PACK}' == 'ON':
REQUIRED_PACKAGES = [
'six >= 1.10.0', 'protobuf >= 3.1.0', 'numpy >= 1.12'
'six >= 1.10.0', 'protobuf >= 3.1.0', 'numpy >= 1.12', 'grpcio >= 1.28.1',
'grpcio-tools >= 1.28.1'
]
if not find_package("paddlepaddle") and not find_package("paddlepaddle-gpu"):
...
...
python/setup.py.server.in
浏览文件 @
93e3d126
...
...
@@ -37,7 +37,7 @@ def python_version():
max_version, mid_version, min_version = python_version()
REQUIRED_PACKAGES = [
'six >= 1.10.0', 'protobuf >= 3.1.0',
'six >= 1.10.0', 'protobuf >= 3.1.0',
'grpcio >= 1.28.1', 'grpcio-tools >= 1.28.1',
'paddle_serving_client', 'flask >= 1.1.1', 'paddle_serving_app'
]
...
...
python/setup.py.server_gpu.in
浏览文件 @
93e3d126
...
...
@@ -37,7 +37,7 @@ def python_version():
max_version, mid_version, min_version = python_version()
REQUIRED_PACKAGES = [
'six >= 1.10.0', 'protobuf >= 3.1.0',
'six >= 1.10.0', 'protobuf >= 3.1.0',
'grpcio >= 1.28.1', 'grpcio-tools >= 1.28.1',
'paddle_serving_client', 'flask >= 1.1.1', 'paddle_serving_app'
]
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录