基于OpenVINO C++ API部署YOLOv5-Seg實例分割模型

上一篇文章《基于OpenVNO部署YOLOv5-seg實時實例分割模型》 介紹了基于OpenVINO Python API部署YOLOv5-Seg實例分割模型,本文介紹基于OpenVINO C++ API部署YOLOv5-Seg實例分割模型,主要步驟有:

  1. 配置OpenVINO C++開發(fā)環(huán)境
  2. 下載并轉(zhuǎn)換YOLOv5-Seg預(yù)訓(xùn)練模型
  3. 使用OpenVINO Runtime C++ API編寫推理程序

下面,本文將依次詳述。

第一步,配置OpenVINO C++開發(fā)環(huán)境,請參考《在Windows中基于Visual Studio配置OpenVINO C++開發(fā)環(huán)境》

第二步,參考《基于OpenVNO部署YOLOv5-seg實時實例分割模型》 克隆YOLOv5 Github 代碼倉到本地,然后運行命令獲得 yolov5s-seg ONNX 格式模型:yolov5s-seg.onnx:

python export.py --weights yolov5s-seg.pt --include onnx

接著運行命令獲得yolov5s-seg IR格式模型:yolov5s-seg.xml和yolov5s-seg.bin,如下圖所示

mo -m yolov5s-seg.onnx --compress_to_fp16

yolov5-seg ONNX格式和IR格式模型

第三步:使用OpenVINO Runtime C++ API編寫推理程序。一個端到端的AI推理程序,主要包含五個典型的處理流程:

  1. 采集圖像&圖像解碼
  2. 圖像數(shù)據(jù)預(yù)處理
  3. AI推理計算
  4. 對推理結(jié)果進行后處理
  5. 將處理后的結(jié)果集成到業(yè)務(wù)流程
image.png

基于OpenVINO Runtime C++API的同步推理代碼的關(guān)鍵片段如下所示:

int main(int argc, char* argv[]) {
    // -------- Get OpenVINO runtime version --------
    std::cout << ov::get_openvino_version().description << ':' << ov::get_openvino_version().buildNumber << std::endl;

    // -------- Step 1. Initialize OpenVINO Runtime Core --------
    ov::Core core;

    // -------- Step 2. Compile the Model --------
    auto compiled_model = core.compile_model(model_file, "GPU.1"); //GPU.1 is dGPU A770

    // -------- Step 3. Create an Inference Request --------
    ov::InferRequest infer_request = compiled_model.create_infer_request();

    // -------- Step 4. Read a picture file and do the preprocess --------
    cv::RNG rng;
    cv::Mat img = cv::imread(image_file); //Load a picture into memory
    cv::Mat masked_img;
    std::vector<float> paddings(3);       //scale, half_h, half_w
    cv::Mat resized_img = letterbox(img, paddings); //resize to (640,640) by letterbox
    // BGR->RGB, u8(0-255)->f32(0.0-1.0), HWC->NCHW
    cv::Mat blob = cv::dnn::blobFromImage(resized_img, 1 / 255.0, cv::Size(640, 640), cv::Scalar(0, 0, 0), true);

    // -------- Step 5. Feed the blob into the input node of YOLOv5 -------
    // Get input port for model with one input
    auto input_port = compiled_model.input();
    // Create tensor from external memory
    ov::Tensor input_tensor(input_port.get_element_type(), input_port.get_shape(), blob.ptr(0));
    // Set input tensor for model with one input
    infer_request.set_input_tensor(input_tensor);

    // -------- Step 6. Start inference --------
    infer_request.infer();

    // -------- Step 7. Get the inference result --------
    auto detect = infer_request.get_output_tensor(0);
    auto detect_shape = detect.get_shape();
    std::cout << "The shape of Detection tensor:"<< detect_shape << std::endl;
    auto proto = infer_request.get_output_tensor(1);
    auto proto_shape = proto.get_shape();
    std::cout << "The shape of Proto tensor:" << proto_shape << std::endl;

    // --------- Do the Post Process

    // Detect Matrix: 25200 x 117  
    cv::Mat detect_buffer(detect_shape[1], detect_shape[2], CV_32F, detect.data());
    // Proto Matrix:  1x32x160x160 => 32 x 25600
    cv::Mat proto_buffer(proto_shape[1], proto_shape[2] * proto_shape[3], CV_32F, proto.data());

    // -------- Step 8. Post-process the inference result -----------
   ...
}

完整范例代碼:https://gitee.com/ppov-nuc/yolov5_infer/blob/main/yolov5seg_openvino_dGPU.cpp

運行結(jié)果如下:
yolov5seg_openvino_dGPU.cpp運行結(jié)果
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容