同步模式推理流程
OpenVINO2023版本的SDK支持同步與異步推理模式相比之前OpenVINO2021版本更加的簡潔,易用。同時支持創(chuàng)建多個Requst然后基于多個Requst實現(xiàn)流水線方式的推理從而提升CPU推理的吞吐率。同步模式下OpenVINO2023 SDK的推理方式如下:

推理的流程如下:
while(true) {
// capture frame
// populate CURRENT InferRequest
// Infer CURRENT InferRequest
//this call is synchronous
// display CURRENT result
}
以YOLOv5s的模型為例,在OpenVINO C++上同步推理的代碼實現(xiàn)如下:
// 創(chuàng)建IE插件, 查詢支持硬件設(shè)備 ov::Core core; std::string model_onnx = "D:/python/yolov5-7.0/yolov5s.onnx"; auto model = core.read_model(model_onnx); ov::CompiledModel cmodel = core.compile_model(model, "CPU"); // create infer request auto request = cmodel.create_infer_request(); cv::Mat frame; while (true) { bool ret = cap.read(frame); if (frame.empty()) { break; } image_detect(frame, request); char c = cv::waitKey(1); if (c == 27) { // ESC break; } }其中image_detect方法包含模型的圖像前處理、同步推理、后處理。其中同步推理:
// 前處理 // 開啟同步 request.infer(); // 后處理運行結(jié)果如下:
異步模式推理流程
當(dāng)使用OpenVINO2023提供的Request對象的回調(diào)功能以后,我們可以把模型的后處理直接放到回調(diào)中去,這樣異步推理方式就變成只有圖像前處理+模型推兩個步驟了,然后通過創(chuàng)建兩個Request基于流水線方式,實現(xiàn)異步流水線模式推理方式,這個時候推理流程如下:

推理的流程如下:
while(true) {
// capture frame
// populate NEXT InferRequest
// start NEXT InferRequest
// this call is async and returns immediately
// wait for the CURRENT InferRequest
// display CURRENT result
// swap CURRENT and NEXT InferRequests
}
首先需要創(chuàng)建兩個Request,然后分別設(shè)置它們的Callback部分代碼,主要是在Callback中完成后處理操作。這部分的代碼如下:
1//創(chuàng)建IE插件,查詢支持硬件設(shè)備
2ov::Corecore;
3std::stringmodel_onnx="D:/python/yolov5-7.0/yolov5s.onnx";
4automodel=core.read_model(model_onnx);
5ov::CompiledModelcmodel=core.compile_model(model,"AUTO");
6
7//createinferrequest
8autorequest=cmodel.create_infer_request();
9autonext_request=cmodel.create_infer_request();
10std::exception_ptrexception_var;
11request.set_callback([&](std::exception_ptrex){
12if(ex){
13exception_var=ex;
14return;
15}
16det_boxes.clear();
17det_ids.clear();
18ov::Tensoroutput=request.get_output_tensor();
19constfloat*prob=(float*)output.data();
20constov::ShapeoutputDims=output.get_shape();
21size_tnumRows=outputDims[1];
22size_tnumCols=outputDims[2];
23
24//后處理,1x25200x85
25std::vectorboxes;
26std::vectorclassIds;
27std::vectorconfidences;
28cv::Matdet_output(numRows,numCols,CV_32F,(float*)prob);
29for(inti=0;i(i,4);
31if(confidence0.45)?{
32????????????continue;
33????????}
34????????cv::Mat?classes_scores?=?det_output.row(i).colRange(5,?numCols);
35????????cv::Point?classIdPoint;
36????????double?score;
37????????minMaxLoc(classes_scores,?0,?&score,?0,?&classIdPoint);
38
39????????//?置信度?0~1之間
40????????if?(score?>0.25)
41{
42floatcx=det_output.at(i,0);
43floatcy=det_output.at(i,1);
44floatow=det_output.at(i,2);
45floatoh=det_output.at(i,3);
46intx=static_cast((cx-0.5*ow)*x_factor);
47inty=static_cast((cy-0.5*oh)*y_factor);
48intwidth=static_cast(ow*x_factor);
49intheight=static_cast(oh*y_factor);
50cv::Rectbox;
51box.x=x;
52box.y=y;
53box.width=width;
54box.height=height;
55
56boxes.push_back(box);
57classIds.push_back(classIdPoint.x);
58confidences.push_back(score);
59}
60}
61
62//NMS
63std::vectorindexes;
64cv::NMSBoxes(boxes,confidences,0.25,0.45,indexes);
65for(size_ti=0;i
依據(jù)上述的推理流程,最終調(diào)用執(zhí)行的代碼如下:
1cv::Matframe,next_frame;
2//dofirstframe
3cap.read(frame);
4async_image_detect(frame,request);
5std::millisecondstout{50};
6intcnt=0;
7while(true){
8boolret=cap.read(next_frame);
9if(next_frame.empty()){
10break;
11}
12
13int64start=cv::getTickCount();
14//繼續(xù)異步
15if(cnt%2==0){
16async_image_detect(next_frame,next_request);
17request.wait_for(tout);
18}
19if(cnt%2==1){
20async_image_detect(next_frame,request);
21next_request.wait_for(tout);
22}
23for(size_tt=0;t(cv::getTickFrequency());
31putText(frame,cv::format("FPS:%.2f",1.0/t),cv::Point(20,40),cv::FONT_HERSHEY_PLAIN,2.0,cv::Scalar(255,0,0),2,8);
32cv::imshow("OpenVINO2023-YOLOv57.0異步推理",frame);
33charc=cv::waitKey(1);
34if(c==27){//ESC
35break;
36}
37next_frame.copyTo(frame);
38cnt++;
39}
40cv::waitKey(0);
41cv::destroyAllWindows();
42return0;
其中async_image_detect方法中實現(xiàn)了YOLOv5模型推理的圖像前處理與啟動異步推理模式
preprocess(frame)
// 開啟異步
request.start_async();
審核編輯:湯梓紅
-
流水線
+關(guān)注
關(guān)注
0文章
127瀏覽量
27235 -
C++
+關(guān)注
關(guān)注
22文章
2124瀏覽量
77139 -
代碼
+關(guān)注
關(guān)注
30文章
4968瀏覽量
73999 -
SDK
+關(guān)注
關(guān)注
3文章
1101瀏覽量
51745 -
OpenVINO
+關(guān)注
關(guān)注
0文章
118瀏覽量
768
原文標(biāo)題:OpenVINO2023異步回調(diào)流水線提升推理吞吐率
文章出處:【微信號:CVSCHOOL,微信公眾號:OpenCV學(xué)堂】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
為什么無法在運行時C++推理中讀取OpenVINO?模型?
為什么深度學(xué)習(xí)中的Frame per Second高于OpenVINO?演示推理腳本?
使用OpenVINO?進(jìn)行推理時的內(nèi)存泄漏怎么解決?
使用Python API在OpenVINO?中創(chuàng)建了用于異步推理的自定義代碼,輸出張量的打印結(jié)果會重復(fù),為什么?
使用OpenVINO運行C++ API創(chuàng)建輸入tensor并執(zhí)行推理遇到的問題求解
在Raspberry Pi上從源代碼構(gòu)建OpenVINO 2021.3收到錯誤怎么解決?
在英特爾開發(fā)者套件上用OpenVINO? 2023.0加速YOLOv8-Pose姿態(tài)估計模型
OpenVINO場景文字檢測與文字識別教程
NNCF壓縮與量化YOLOv8模型與OpenVINO部署測試
如何快速下載OpenVINO Notebooks中的AI大模型
同步模式下OpenVINO2023 SDK的推理方式
評論