Serving icon indicating copy to clipboard operation
Serving copied to clipboard

如何获取op的多个输出?

Open XinyuDu opened this issue 2 years ago • 2 comments

改造了Serving/core/general-server/op/general_detection_op.cpp增加了输出boxes的代码。但是client端获取不到增加的输出。请问应该怎么改prototxt或者client端实现获取多个输出? 增加部分如下:

    int box_size_out = box_num*4*2*sizeof(int);
    void *box_data_out = MempoolWrapper::instance().malloc(box_size_out);
    if (!box_data_out) {
      LOG(ERROR) << "Malloc failed, size: " << box_size_out;
      return -1;
    }
    
    int* databuf_int = reinterpret_cast<int*>(box_data_out);
    for (int i = 0; i < box_num; ++i) {
      for (int row = 0; row < 4; row++){
        *databuf_int = boxes[i][row][0];
        databuf_int++;
        *databuf_int = boxes[i][row][1];
        databuf_int++;
      }
    }

    // memcpy(databuf_data_out, &boxes, databuf_size_out);
    char *box_char_out = reinterpret_cast<char*>(box_data_out);
    paddle::PaddleBuf paddleBuf_out_2(box_char_out, box_size_out);
    paddle::PaddleTensor tensor_out_2;

    tensor_out_2.name = "boxes";
    tensor_out_2.dtype = paddle::PaddleDType::INT32;
    tensor_out_2.shape = {box_num, 4, 2};
    tensor_out_2.data = paddleBuf_out_2;
    out->push_back(tensor_out_2);

获取serving输出的det_cpp_client.py如下:

import sys
import numpy as np
import base64
import os
import cv2
from paddle_serving_app.reader import Sequential, URL2Image, ResizeByFactor
from paddle_serving_app.reader import Div, Normalize, Transpose
from paddle_serving_app.reader import OCRReader
from paddle_serving_app.reader import DBPostProcess, FilterBoxes, GetRotateCropImage, SortedBoxes


client = Client()
client.load_client_config(sys.argv[1:])
client.connect(["127.0.0.1:9293"])
fetch_list = client.get_fetch_names()
print(fetch_list)

test_img_dir = "imgs/"
img_file = '1.jpg'

def cv2_to_base64(image):
    return base64.b64encode(image)  #data.tostring()).decode('utf8')

with open(os.path.join(test_img_dir, img_file), 'rb') as file:
    image_data = file.read()
    image = cv2_to_base64(image_data)

det_out = client.predict(
        feed={"x": image},
        fetch=["x","boxes"],
        batch=True)

print(det_out)

serving启动代码如下:

python3 -m paddle_serving_server.serve --model ocr_det_model --op GeneralDetectionOp --port 9293

client启动代码如下:

python3 det_cpp_client.py ocr_det_client

XinyuDu avatar Sep 27 '22 06:09 XinyuDu

Message that will be displayed on users' first issue

github-actions[bot] avatar Sep 27 '22 06:09 github-actions[bot]

请问您的问题解决了吗?我也有同样的需求

ooxoxx avatar Jul 05 '23 22:07 ooxoxx