提交 d1e5bea2 编写于 作者: H HydrogenSulfate

debug

上级 cb93e20d
...@@ -22,181 +22,102 @@ import faiss ...@@ -22,181 +22,102 @@ import faiss
import os import os
import pickle import pickle
rec_nms_thresold = 0.05
class MainbodyDetect(): rec_score_thres = 0.5
""" feature_normalize = True
pp-shitu mainbody detect. return_k = 1
include preprocess, process, postprocess index_dir = "../../drink_dataset_v1.0/index"
return detect results
Attention: Postprocess include num limit and box filter; no nms
""" def init_index(index_dir):
assert os.path.exists(os.path.join(
def __init__(self): index_dir, "vector.index")), "vector.index not found ..."
self.preprocess = DetectionSequential([ assert os.path.exists(os.path.join(
DetectionFile2Image(), DetectionNormalize( index_dir, "id_map.pkl")), "id_map.pkl not found ... "
[0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True),
DetectionResize( searcher = faiss.read_index(os.path.join(index_dir, "vector.index"))
(640, 640), False, interpolation=2), DetectionTranspose(
(2, 0, 1)) with open(os.path.join(index_dir, "id_map.pkl"), "rb") as fd:
]) id_map = pickle.load(fd)
return searcher, id_map
self.client = Client()
self.client.load_client_config(
"../../models/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_client/serving_client_conf.prototxt" #get box
) def nms_to_rec_results(results, thresh=0.1):
self.client.connect(['127.0.0.1:9293']) filtered_results = []
self.max_det_result = 5 x1 = np.array([r["bbox"][0] for r in results]).astype("float32")
self.conf_threshold = 0.2 y1 = np.array([r["bbox"][1] for r in results]).astype("float32")
x2 = np.array([r["bbox"][2] for r in results]).astype("float32")
def predict(self, imgpath): y2 = np.array([r["bbox"][3] for r in results]).astype("float32")
im, im_info = self.preprocess(imgpath) scores = np.array([r["rec_scores"] for r in results])
im_shape = np.array(im.shape[1:]).reshape(-1)
scale_factor = np.array(list(im_info['scale_factor'])).reshape(-1) areas = (x2 - x1 + 1) * (y2 - y1 + 1)
order = scores.argsort()[::-1]
fetch_map = self.client.predict( while order.size > 0:
feed={ i = order[0]
"image": im, xx1 = np.maximum(x1[i], x1[order[1:]])
"im_shape": im_shape, yy1 = np.maximum(y1[i], y1[order[1:]])
"scale_factor": scale_factor, xx2 = np.minimum(x2[i], x2[order[1:]])
}, yy2 = np.minimum(y2[i], y2[order[1:]])
fetch=["save_infer_model/scale_0.tmp_1"],
batch=False) w = np.maximum(0.0, xx2 - xx1 + 1)
return self.postprocess(fetch_map, imgpath) h = np.maximum(0.0, yy2 - yy1 + 1)
inter = w * h
def postprocess(self, fetch_map, imgpath): ovr = inter / (areas[i] + areas[order[1:]] - inter)
#1. get top max_det_result inds = np.where(ovr <= thresh)[0]
det_results = fetch_map["save_infer_model/scale_0.tmp_1"] order = order[inds + 1]
if len(det_results) > self.max_det_result: filtered_results.append(results[i])
boxes_reserved = fetch_map[ return filtered_results
"save_infer_model/scale_0.tmp_1"][:self.max_det_result]
else:
boxes_reserved = det_results def postprocess(fetch_dict, feature_normalize, det_boxes, searcher, id_map,
return_k, rec_score_thres, rec_nms_thresold):
#2. do conf threshold batch_features = fetch_dict["features"]
boxes_list = []
for i in range(boxes_reserved.shape[0]): #do feature norm
if (boxes_reserved[i, 1]) > self.conf_threshold: if feature_normalize:
boxes_list.append(boxes_reserved[i, :]) feas_norm = np.sqrt(
np.sum(np.square(batch_features), axis=1, keepdims=True))
#3. add origin image box batch_features = np.divide(batch_features, feas_norm)
origin_img = cv2.imread(imgpath)
boxes_list.append( scores, docs = searcher.search(batch_features, return_k)
np.array([0, 1.0, 0, 0, origin_img.shape[1], origin_img.shape[0]]))
return np.array(boxes_list) results = []
for i in range(scores.shape[0]):
pred = {}
class ObjectRecognition(): if scores[i][0] >= rec_score_thres:
""" pred["bbox"] = [int(x) for x in det_boxes[i, 2:]]
pp-shitu object recognion for all objects detected by MainbodyDetect. pred["rec_docs"] = id_map[docs[i][0]].split()[1]
include preprocess, process, postprocess pred["rec_scores"] = scores[i][0]
preprocess include preprocess for each image and batching. results.append(pred)
Batch process
postprocess include retrieval and nms #do nms
""" results = nms_to_rec_results(results, rec_nms_thresold)
return results
def __init__(self):
self.client = Client()
self.client.load_client_config( #do client
"../../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
if __name__ == "__main__": if __name__ == "__main__":
det = MainbodyDetect() client = Client()
rec = ObjectRecognition() client.load_client_config([
"../../models/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_client",
#1. get det_results "../../models/general_PPLCNet_x2_5_lite_v1.0_client"
imgpath = "../../drink_dataset_v1.0/test_images/001.jpeg" ])
det_results = det.predict(imgpath) client.connect(['127.0.0.1:9400'])
#2. get rec_results im = cv2.imread("../../drink_dataset_v1.0/test_images/001.jpeg")
rec_results = rec.predict(det_results, imgpath) im_shape = np.array(im.shape[:2]).reshape(-1)
print(rec_results) fetch_map = client.predict(
feed={"image": im,
"im_shape": im_shape},
fetch=["features", "boxes"],
batch=False)
print(fetch_map.keys())
#add retrieval procedure
det_boxes = fetch_map["boxes"]
print(det_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)
...@@ -52,7 +52,7 @@ Linux GPU/CPU PYTHON 服务化部署测试的主程序为`test_serving_infer.sh ...@@ -52,7 +52,7 @@ Linux GPU/CPU PYTHON 服务化部署测试的主程序为`test_serving_infer.sh
``` ```
- 安装 PaddleServing 相关组件,包括serving_client、serving-app,自动编译带自定义OP的serving_server包(测试PP-ShiTu时),以及自动下载并解压推理模型 - 安装 PaddleServing 相关组件,包括serving_client、serving-app,自动编译带自定义OP的serving_server包(测试PP-ShiTu时),以及自动下载并解压推理模型
```bash ```bash
bash test_tipc/prepare.sh test_tipc/configs/ResNet50/ResNet50_linux_gpu_normal_normal_serving_cpp_linux_gpu_cpu.txt serving_infer bash test_tipc/prepare.sh test_tipc/configs/PPLCNet/PPLCNet_x1_0_linux_gpu_normal_normal_serving_cpp_linux_gpu_cpu.txt serving_infer
``` ```
### 2.3 功能测试 ### 2.3 功能测试
...@@ -63,24 +63,28 @@ Linux GPU/CPU PYTHON 服务化部署测试的主程序为`test_serving_infer.sh ...@@ -63,24 +63,28 @@ Linux GPU/CPU PYTHON 服务化部署测试的主程序为`test_serving_infer.sh
bash test_tipc/test_serving_infer.sh ${your_params_file} bash test_tipc/test_serving_infer.sh ${your_params_file}
``` ```
`ResNet50``Linux GPU/CPU PYTHON 服务化部署测试`为例,命令如下所示。 `PPLCNet_x1_0``Linux GPU/CPU C++ 服务化部署测试`为例,命令如下所示。
```bash ```bash
bash test_tipc/test_serving_infer.sh test_tipc/configs/ResNet50/ResNet50_linux_gpu_normal_normal_serving_python_linux_gpu_cpu.txt bash test_tipc/test_serving_infer.sh test_tipc/configs/PPLCNet/PPLCNet_x1_0_linux_gpu_normal_normal_serving_cpp_linux_gpu_cpu.txt
``` ```
输出结果如下,表示命令运行成功。 输出结果如下,表示命令运行成功。
``` ```
Run successfully with command - python3.7 pipeline_http_client.py > ../../test_tipc/output/ResNet50/server_infer_gpu_pipeline_http_batchsize_1.log 2>&1! Run successfully with command - PPLCNet_x1_0 - python3.7 test_cpp_serving_client.py > ../../test_tipc/output/PPLCNet_x1_0/server_infer_cpp_gpu_pipeline_batchsize_1.log 2>&1 !
Run successfully with command - python3.7 pipeline_http_client.py > ../../test_tipc/output/ResNet50/server_infer_cpu_pipeline_http_batchsize_1.log 2>&1 ! Run successfully with command - PPLCNet_x1_0 - python3.7 test_cpp_serving_client.py > ../../test_tipc/output/PPLCNet_x1_0/server_infer_cpp_cpu_pipeline_batchsize_1.log 2>&1 !
``` ```
预测结果会自动保存在 `./test_tipc/output/ResNet50/server_infer_gpu_pipeline_http_batchsize_1.log` ,可以看到 PaddleServing 的运行结果: 预测结果会自动保存在 `./test_tipc/output/PPLCNet_x1_0/server_infer_gpu_pipeline_http_batchsize_1.log` ,可以看到 PaddleServing 的运行结果:
``` ```
{'err_no': 0, 'err_msg': '', 'key': ['label', 'prob'], 'value': ["['daisy']", '[0.998314619064331]']} WARNING: Logging before InitGoogleLogging() is written to STDERR
I0612 09:55:16.109890 38303 naming_service_thread.cpp:202] brpc::policy::ListNamingService("127.0.0.1:9292"): added 1
I0612 09:55:16.172924 38303 general_model.cpp:490] [client]logid=0,client_cost=60.772ms,server_cost=57.6ms.
prediction: daisy, probability: 0.9099399447441101
0.06275796890258789
``` ```
......
...@@ -204,7 +204,9 @@ if [[ ${MODE} = "serving_infer" ]]; then ...@@ -204,7 +204,9 @@ if [[ ${MODE} = "serving_infer" ]]; then
${python_name} -m pip install paddle-serving-app==0.9.0 -i https://pypi.tuna.tsinghua.edu.cn/simple ${python_name} -m pip install paddle-serving-app==0.9.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
python_name=$(func_parser_value "${lines[2]}") python_name=$(func_parser_value "${lines[2]}")
if [[ ${FILENAME} =~ "cpp" ] && [ ${model_name} =~ "ShiTu" ]]; then if [[ ${FILENAME} =~ "cpp" ] && [ ${model_name} =~ "ShiTu" ]]; then
pushd ./deploy/paddleserving
bash build_server.sh ${python_name} bash build_server.sh ${python_name}
popd
else else
${python_name} -m pip install install paddle-serving-server-gpu==0.9.0.post101 -i https://pypi.tuna.tsinghua.edu.cn/simple ${python_name} -m pip install install paddle-serving-server-gpu==0.9.0.post101 -i https://pypi.tuna.tsinghua.edu.cn/simple
fi fi
......
...@@ -263,13 +263,13 @@ function func_serving_rec(){ ...@@ -263,13 +263,13 @@ function func_serving_rec(){
det_trans_model_cmd="${python_interp} ${trans_model_py} ${set_dirname} ${set_model_filename} ${set_params_filename} ${set_serving_server} ${set_serving_client}" det_trans_model_cmd="${python_interp} ${trans_model_py} ${set_dirname} ${set_model_filename} ${set_params_filename} ${set_serving_server} ${set_serving_client}"
eval $det_trans_model_cmd eval $det_trans_model_cmd
cp_prototxt_cmd="cp ./paddleserving/preprocess/general_PPLCNet_x2_5_lite_v1.0_serving/*.prototxt ${cls_serving_server_value}" cp_prototxt_cmd="cp ./paddleserving/recognition/preprocess/general_PPLCNet_x2_5_lite_v1.0_serving/*.prototxt ${cls_serving_server_value}"
eval ${cp_prototxt_cmd} eval ${cp_prototxt_cmd}
cp_prototxt_cmd="cp ./paddleserving/preprocess/general_PPLCNet_x2_5_lite_v1.0_client/*.prototxt ${cls_serving_client_value}" cp_prototxt_cmd="cp ./paddleserving/recognition/preprocess/general_PPLCNet_x2_5_lite_v1.0_client/*.prototxt ${cls_serving_client_value}"
eval ${cp_prototxt_cmd} eval ${cp_prototxt_cmd}
cp_prototxt_cmd="cp ./paddleserving/preprocess/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_client/*.prototxt ${det_serving_client_value}" cp_prototxt_cmd="cp ./paddleserving/recognition/preprocess/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_client/*.prototxt ${det_serving_client_value}"
eval ${cp_prototxt_cmd} eval ${cp_prototxt_cmd}
cp_prototxt_cmd="cp ./paddleserving/preprocess/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_serving/*.prototxt ${det_serving_server_value}" cp_prototxt_cmd="cp ./paddleserving/recognition/preprocess/picodet_PPLCNet_x2_5_mainbody_lite_v1.0_serving/*.prototxt ${det_serving_server_value}"
eval ${cp_prototxt_cmd} eval ${cp_prototxt_cmd}
prototxt_dataline=$(awk 'NR==1, NR==3{print}' ${cls_serving_server_value}/serving_server_conf.prototxt) prototxt_dataline=$(awk 'NR==1, NR==3{print}' ${cls_serving_server_value}/serving_server_conf.prototxt)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册