Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleClas
提交
b574a47d
P
PaddleClas
项目概览
PaddlePaddle
/
PaddleClas
大约 1 年 前同步成功
通知
115
Star
4999
Fork
1114
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
19
列表
看板
标记
里程碑
合并请求
6
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleClas
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
19
Issue
19
列表
看板
标记
里程碑
合并请求
6
合并请求
6
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
b574a47d
编写于
8月 08, 2022
作者:
H
HydrogenSulfate
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update bash scripts and related python file to develop version
上级
d7670528
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
125 addition
and
187 deletion
+125
-187
deploy/paddleserving/recognition/config.yml
deploy/paddleserving/recognition/config.yml
+1
-1
deploy/paddleserving/recognition/run_cpp_serving.sh
deploy/paddleserving/recognition/run_cpp_serving.sh
+13
-6
deploy/paddleserving/recognition/test_cpp_serving_client.py
deploy/paddleserving/recognition/test_cpp_serving_client.py
+97
-178
deploy/paddleserving/run_cpp_serving.sh
deploy/paddleserving/run_cpp_serving.sh
+14
-2
未找到文件。
deploy/paddleserving/recognition/config.yml
浏览文件 @
b574a47d
...
...
@@ -31,7 +31,7 @@ op:
#Fetch结果列表,以client_config中fetch_var的alias_name为准
fetch_list
:
[
"
features"
]
det
:
concurrency
:
1
local_service_conf
:
...
...
deploy/paddleserving/recognition/run_cpp_serving.sh
浏览文件 @
b574a47d
nohup
python3
-m
paddle_serving_server.serve
\
--model
../../models/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_serving
\
--port
9293
>>
log_mainbody_detection.txt 1&>2 &
gpu_id
=
$1
nohup
python3
-m
paddle_serving_server.serve
\
--model
../../models/general_PPLCNet_x2_5_lite_v1.0_serving
\
--port
9294
>>
log_feature_extraction.txt 1&>2 &
# PP-ShiTu CPP serving script
if
[[
-n
"
${
gpu_id
}
"
]]
;
then
nohup
python3.7
-m
paddle_serving_server.serve
\
--model
../../models/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_serving ../../models/general_PPLCNet_x2_5_lite_v1.0_serving
\
--op
GeneralPicodetOp GeneralFeatureExtractOp
\
--port
9400
--gpu_id
=
"
${
gpu_id
}
"
>
log_PPShiTu.txt 2>&1 &
else
nohup
python3.7
-m
paddle_serving_server.serve
\
--model
../../models/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_serving ../../models/general_PPLCNet_x2_5_lite_v1.0_serving
\
--op
GeneralPicodetOp GeneralFeatureExtractOp
\
--port
9400
>
log_PPShiTu.txt 2>&1 &
fi
\ No newline at end of file
deploy/paddleserving/recognition/test_cpp_serving_client.py
浏览文件 @
b574a47d
...
...
@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import
sys
import
numpy
as
np
from
paddle_serving_client
import
Client
...
...
@@ -22,181 +21,101 @@ import faiss
import
os
import
pickle
class
MainbodyDetect
():
"""
pp-shitu mainbody detect.
include preprocess, process, postprocess
return detect results
Attention: Postprocess include num limit and box filter; no nms
"""
def
__init__
(
self
):
self
.
preprocess
=
DetectionSequential
([
DetectionFile2Image
(),
DetectionNormalize
(
[
0.485
,
0.456
,
0.406
],
[
0.229
,
0.224
,
0.225
],
True
),
DetectionResize
(
(
640
,
640
),
False
,
interpolation
=
2
),
DetectionTranspose
(
(
2
,
0
,
1
))
])
self
.
client
=
Client
()
self
.
client
.
load_client_config
(
"../../models/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_client/serving_client_conf.prototxt"
)
self
.
client
.
connect
([
'127.0.0.1:9293'
])
self
.
max_det_result
=
5
self
.
conf_threshold
=
0.2
def
predict
(
self
,
imgpath
):
im
,
im_info
=
self
.
preprocess
(
imgpath
)
im_shape
=
np
.
array
(
im
.
shape
[
1
:]).
reshape
(
-
1
)
scale_factor
=
np
.
array
(
list
(
im_info
[
'scale_factor'
])).
reshape
(
-
1
)
fetch_map
=
self
.
client
.
predict
(
feed
=
{
"image"
:
im
,
"im_shape"
:
im_shape
,
"scale_factor"
:
scale_factor
,
},
fetch
=
[
"save_infer_model/scale_0.tmp_1"
],
batch
=
False
)
return
self
.
postprocess
(
fetch_map
,
imgpath
)
def
postprocess
(
self
,
fetch_map
,
imgpath
):
#1. get top max_det_result
det_results
=
fetch_map
[
"save_infer_model/scale_0.tmp_1"
]
if
len
(
det_results
)
>
self
.
max_det_result
:
boxes_reserved
=
fetch_map
[
"save_infer_model/scale_0.tmp_1"
][:
self
.
max_det_result
]
else
:
boxes_reserved
=
det_results
#2. do conf threshold
boxes_list
=
[]
for
i
in
range
(
boxes_reserved
.
shape
[
0
]):
if
(
boxes_reserved
[
i
,
1
])
>
self
.
conf_threshold
:
boxes_list
.
append
(
boxes_reserved
[
i
,
:])
#3. add origin image box
origin_img
=
cv2
.
imread
(
imgpath
)
boxes_list
.
append
(
np
.
array
([
0
,
1.0
,
0
,
0
,
origin_img
.
shape
[
1
],
origin_img
.
shape
[
0
]]))
return
np
.
array
(
boxes_list
)
class
ObjectRecognition
():
"""
pp-shitu object recognion for all objects detected by MainbodyDetect.
include preprocess, process, postprocess
preprocess include preprocess for each image and batching.
Batch process
postprocess include retrieval and nms
"""
def
__init__
(
self
):
self
.
client
=
Client
()
self
.
client
.
load_client_config
(
"../../models/general_PPLCNet_x2_5_lite_v1.0_client/serving_client_conf.prototxt"
)
self
.
client
.
connect
([
"127.0.0.1:9294"
])
self
.
seq
=
Sequential
([
BGR2RGB
(),
Resize
((
224
,
224
)),
Div
(
255
),
Normalize
([
0.485
,
0.456
,
0.406
],
[
0.229
,
0.224
,
0.225
],
False
),
Transpose
((
2
,
0
,
1
))
])
self
.
searcher
,
self
.
id_map
=
self
.
init_index
()
self
.
rec_nms_thresold
=
0.05
self
.
rec_score_thres
=
0.5
self
.
feature_normalize
=
True
self
.
return_k
=
1
def
init_index
(
self
):
index_dir
=
"../../drink_dataset_v1.0/index"
assert
os
.
path
.
exists
(
os
.
path
.
join
(
index_dir
,
"vector.index"
)),
"vector.index not found ..."
assert
os
.
path
.
exists
(
os
.
path
.
join
(
index_dir
,
"id_map.pkl"
)),
"id_map.pkl not found ... "
searcher
=
faiss
.
read_index
(
os
.
path
.
join
(
index_dir
,
"vector.index"
))
with
open
(
os
.
path
.
join
(
index_dir
,
"id_map.pkl"
),
"rb"
)
as
fd
:
id_map
=
pickle
.
load
(
fd
)
return
searcher
,
id_map
def
predict
(
self
,
det_boxes
,
imgpath
):
#1. preprocess
batch_imgs
=
[]
origin_img
=
cv2
.
imread
(
imgpath
)
for
i
in
range
(
det_boxes
.
shape
[
0
]):
box
=
det_boxes
[
i
]
x1
,
y1
,
x2
,
y2
=
[
int
(
x
)
for
x
in
box
[
2
:]]
cropped_img
=
origin_img
[
y1
:
y2
,
x1
:
x2
,
:].
copy
()
tmp
=
self
.
seq
(
cropped_img
)
batch_imgs
.
append
(
tmp
)
batch_imgs
=
np
.
array
(
batch_imgs
)
#2. process
fetch_map
=
self
.
client
.
predict
(
feed
=
{
"x"
:
batch_imgs
},
fetch
=
[
"features"
],
batch
=
True
)
batch_features
=
fetch_map
[
"features"
]
#3. postprocess
if
self
.
feature_normalize
:
feas_norm
=
np
.
sqrt
(
np
.
sum
(
np
.
square
(
batch_features
),
axis
=
1
,
keepdims
=
True
))
batch_features
=
np
.
divide
(
batch_features
,
feas_norm
)
scores
,
docs
=
self
.
searcher
.
search
(
batch_features
,
self
.
return_k
)
results
=
[]
for
i
in
range
(
scores
.
shape
[
0
]):
pred
=
{}
if
scores
[
i
][
0
]
>=
self
.
rec_score_thres
:
pred
[
"bbox"
]
=
[
int
(
x
)
for
x
in
det_boxes
[
i
,
2
:]]
pred
[
"rec_docs"
]
=
self
.
id_map
[
docs
[
i
][
0
]].
split
()[
1
]
pred
[
"rec_scores"
]
=
scores
[
i
][
0
]
results
.
append
(
pred
)
return
self
.
nms_to_rec_results
(
results
)
def
nms_to_rec_results
(
self
,
results
):
filtered_results
=
[]
x1
=
np
.
array
([
r
[
"bbox"
][
0
]
for
r
in
results
]).
astype
(
"float32"
)
y1
=
np
.
array
([
r
[
"bbox"
][
1
]
for
r
in
results
]).
astype
(
"float32"
)
x2
=
np
.
array
([
r
[
"bbox"
][
2
]
for
r
in
results
]).
astype
(
"float32"
)
y2
=
np
.
array
([
r
[
"bbox"
][
3
]
for
r
in
results
]).
astype
(
"float32"
)
scores
=
np
.
array
([
r
[
"rec_scores"
]
for
r
in
results
])
areas
=
(
x2
-
x1
+
1
)
*
(
y2
-
y1
+
1
)
order
=
scores
.
argsort
()[::
-
1
]
while
order
.
size
>
0
:
i
=
order
[
0
]
xx1
=
np
.
maximum
(
x1
[
i
],
x1
[
order
[
1
:]])
yy1
=
np
.
maximum
(
y1
[
i
],
y1
[
order
[
1
:]])
xx2
=
np
.
minimum
(
x2
[
i
],
x2
[
order
[
1
:]])
yy2
=
np
.
minimum
(
y2
[
i
],
y2
[
order
[
1
:]])
w
=
np
.
maximum
(
0.0
,
xx2
-
xx1
+
1
)
h
=
np
.
maximum
(
0.0
,
yy2
-
yy1
+
1
)
inter
=
w
*
h
ovr
=
inter
/
(
areas
[
i
]
+
areas
[
order
[
1
:]]
-
inter
)
inds
=
np
.
where
(
ovr
<=
self
.
rec_nms_thresold
)[
0
]
order
=
order
[
inds
+
1
]
filtered_results
.
append
(
results
[
i
])
return
filtered_results
rec_nms_thresold
=
0.05
rec_score_thres
=
0.5
feature_normalize
=
True
return_k
=
1
index_dir
=
"../../drink_dataset_v1.0/index"
def
init_index
(
index_dir
):
assert
os
.
path
.
exists
(
os
.
path
.
join
(
index_dir
,
"vector.index"
)),
"vector.index not found ..."
assert
os
.
path
.
exists
(
os
.
path
.
join
(
index_dir
,
"id_map.pkl"
)),
"id_map.pkl not found ... "
searcher
=
faiss
.
read_index
(
os
.
path
.
join
(
index_dir
,
"vector.index"
))
with
open
(
os
.
path
.
join
(
index_dir
,
"id_map.pkl"
),
"rb"
)
as
fd
:
id_map
=
pickle
.
load
(
fd
)
return
searcher
,
id_map
#get box
def
nms_to_rec_results
(
results
,
thresh
=
0.1
):
filtered_results
=
[]
x1
=
np
.
array
([
r
[
"bbox"
][
0
]
for
r
in
results
]).
astype
(
"float32"
)
y1
=
np
.
array
([
r
[
"bbox"
][
1
]
for
r
in
results
]).
astype
(
"float32"
)
x2
=
np
.
array
([
r
[
"bbox"
][
2
]
for
r
in
results
]).
astype
(
"float32"
)
y2
=
np
.
array
([
r
[
"bbox"
][
3
]
for
r
in
results
]).
astype
(
"float32"
)
scores
=
np
.
array
([
r
[
"rec_scores"
]
for
r
in
results
])
areas
=
(
x2
-
x1
+
1
)
*
(
y2
-
y1
+
1
)
order
=
scores
.
argsort
()[::
-
1
]
while
order
.
size
>
0
:
i
=
order
[
0
]
xx1
=
np
.
maximum
(
x1
[
i
],
x1
[
order
[
1
:]])
yy1
=
np
.
maximum
(
y1
[
i
],
y1
[
order
[
1
:]])
xx2
=
np
.
minimum
(
x2
[
i
],
x2
[
order
[
1
:]])
yy2
=
np
.
minimum
(
y2
[
i
],
y2
[
order
[
1
:]])
w
=
np
.
maximum
(
0.0
,
xx2
-
xx1
+
1
)
h
=
np
.
maximum
(
0.0
,
yy2
-
yy1
+
1
)
inter
=
w
*
h
ovr
=
inter
/
(
areas
[
i
]
+
areas
[
order
[
1
:]]
-
inter
)
inds
=
np
.
where
(
ovr
<=
thresh
)[
0
]
order
=
order
[
inds
+
1
]
filtered_results
.
append
(
results
[
i
])
return
filtered_results
def
postprocess
(
fetch_dict
,
feature_normalize
,
det_boxes
,
searcher
,
id_map
,
return_k
,
rec_score_thres
,
rec_nms_thresold
):
batch_features
=
fetch_dict
[
"features"
]
#do feature norm
if
feature_normalize
:
feas_norm
=
np
.
sqrt
(
np
.
sum
(
np
.
square
(
batch_features
),
axis
=
1
,
keepdims
=
True
))
batch_features
=
np
.
divide
(
batch_features
,
feas_norm
)
scores
,
docs
=
searcher
.
search
(
batch_features
,
return_k
)
results
=
[]
for
i
in
range
(
scores
.
shape
[
0
]):
pred
=
{}
if
scores
[
i
][
0
]
>=
rec_score_thres
:
pred
[
"bbox"
]
=
[
int
(
x
)
for
x
in
det_boxes
[
i
,
2
:]]
pred
[
"rec_docs"
]
=
id_map
[
docs
[
i
][
0
]].
split
()[
1
]
pred
[
"rec_scores"
]
=
scores
[
i
][
0
]
results
.
append
(
pred
)
#do nms
results
=
nms_to_rec_results
(
results
,
rec_nms_thresold
)
return
results
#do client
if
__name__
==
"__main__"
:
det
=
MainbodyDetect
()
rec
=
ObjectRecognition
()
#1. get det_results
imgpath
=
"../../drink_dataset_v1.0/test_images/001.jpeg"
det_results
=
det
.
predict
(
imgpath
)
#2. get rec_results
rec_results
=
rec
.
predict
(
det_results
,
imgpath
)
print
(
rec_results
)
client
=
Client
()
client
.
load_client_config
([
"../../models/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_client"
,
"../../models/general_PPLCNet_x2_5_lite_v1.0_client"
])
client
.
connect
([
'127.0.0.1:9400'
])
im
=
cv2
.
imread
(
"../../drink_dataset_v1.0/test_images/001.jpeg"
)
im_shape
=
np
.
array
(
im
.
shape
[:
2
]).
reshape
(
-
1
)
fetch_map
=
client
.
predict
(
feed
=
{
"image"
:
im
,
"im_shape"
:
im_shape
},
fetch
=
[
"features"
,
"boxes"
],
batch
=
False
)
#add retrieval procedure
det_boxes
=
fetch_map
[
"boxes"
]
searcher
,
id_map
=
init_index
(
index_dir
)
results
=
postprocess
(
fetch_map
,
feature_normalize
,
det_boxes
,
searcher
,
id_map
,
return_k
,
rec_score_thres
,
rec_nms_thresold
)
print
(
results
)
deploy/paddleserving/run_cpp_serving.sh
浏览文件 @
b574a47d
#run cls server:
nohup
python3
-m
paddle_serving_server.serve
--model
ResNet50_vd_serving
--port
9292 &
gpu_id
=
$1
# ResNet50_vd CPP serving script
if
[[
-n
"
${
gpu_id
}
"
]]
;
then
nohup
python3.7
-m
paddle_serving_server.serve
\
--model
./ResNet50_vd_serving
\
--op
GeneralClasOp
\
--port
9292 &
else
nohup
python3.7
-m
paddle_serving_server.serve
\
--model
./ResNet50_vd_serving
\
--op
GeneralClasOp
\
--port
9292
--gpu_id
=
"
${
gpu_id
}
"
&
fi
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录