01配置OpenVINO C++開發(fā)環(huán)境
配置OpenVINO C++開發(fā)環(huán)境的詳細(xì)步驟,請(qǐng)參考《在Windows中基于Visual Studio配置OpenVINO C++開發(fā)環(huán)境》。
02下載并轉(zhuǎn)換YOLOv5預(yù)訓(xùn)練模型
下載并轉(zhuǎn)換YOLOv5預(yù)訓(xùn)練模型的詳細(xì)步驟,請(qǐng)參考:《基于OpenVINO2022.2和蝰蛇峽谷優(yōu)化并部署YOLOv5模型》,本文所使用的OpenVINO是2022.3 LTS版。
完成上述步驟后,可以獲得YOLOv5的IR模型文件:yolov5s.xml 和 yolov5s.bin,如下圖所示:
圖 1-1YOLOv5 IR模型文件
03使用OpenVINO Runtime C++ API編寫推理程序
一個(gè)端到端的AI推理程序,主要包含五個(gè)典型的處理流程:
1.采集圖像&圖像解碼
2.圖像數(shù)據(jù)預(yù)處理
3.AI推理計(jì)算
4.對(duì)推理結(jié)果進(jìn)行后處理
5.將處理后的結(jié)果集成到業(yè)務(wù)流程
圖 1-2端到端的AI推理程序處理流程
采集圖像&圖像解碼
OpenCV提供imread()函數(shù)將圖像文件載入內(nèi)存
Matcv::imread(constString&filename, intflags=IMREAD_COLOR)
若是從視頻流(例如,視頻文件、網(wǎng)絡(luò)攝像頭、3D攝像頭(Realsense)等)中,一幀一幀讀取圖像數(shù)據(jù)到內(nèi)存,則使用cv::VideoCapture類,對(duì)應(yīng)范例代碼請(qǐng)參考OpenCV官方范例代碼:
https://github.com/opencv/opencv/tree/4.x/samples/cpp。
圖 1-3從視頻流讀取圖像幀范例
YOLOv5的圖像預(yù)處理
圖像數(shù)據(jù)輸入YOLOv5模型前需要做預(yù)處理,其主要工作有:使用Letterbox算法對(duì)圖像進(jìn)行非變形放縮,然后完成轉(zhuǎn)換顏色通道、歸一化數(shù)據(jù)、更改數(shù)據(jù)布局和數(shù)值精度。
直接調(diào)用OpenCV的cv::resize()函數(shù)將原始圖像按照模型輸入要求的尺寸進(jìn)行放縮,雖然實(shí)現(xiàn)起來簡(jiǎn)單,但會(huì)導(dǎo)致圖像中的被檢測(cè)對(duì)象變形。Letterbox算法一種不會(huì)導(dǎo)致被檢測(cè)對(duì)象變形的縮放,主要步驟為:
1.計(jì)算寬高縮放比例,選擇較小那個(gè)縮放系數(shù)
2.計(jì)算縮放后的尺寸,原始圖片的長(zhǎng)寬都乘以較小的縮放系數(shù)
3.計(jì)算短邊需要填充的灰邊數(shù),將短邊的兩邊各自填充一半的灰行 參考YOLOv5的Letterbox算法實(shí)現(xiàn)方式,本文的Letterbox函數(shù)實(shí)現(xiàn)如下所示:
cv::Matletterbox(cv::Mat&img, std::vectornew_shape={640, 640}){
// Get current image shape [height, width]
// Refer to https://github.com/ultralytics/yolov5/blob/master/utils/augmentations.py#L111
intimg_h =img.rows;
intimg_w =img.cols;
// Compute scale ratio(new / old) and target resized shape
floatscale =std::min(new_shape[1] *1.0/img_h, new_shape[0] *1.0/img_w);
intresize_h =int(round(img_h *scale));
intresize_w =int(round(img_w *scale));
// Compute padding
intpad_h =new_shape[1] -resize_h;
intpad_w =new_shape[0] -resize_w;
// Resize and pad image while meeting stride-multiple constraints
cv::Mat resized_img;
cv::resize(img, resized_img, cv::Size(resize_w, resize_h));
// divide padding into 2 sides
floathalf_h =pad_h *1.0/2;
floathalf_w =pad_w *1.0/2;
// Compute padding boarder
inttop =int(round(half_h -0.1));
intbottom =int(round(half_h +0.1));
intleft =int(round(half_w -0.1));
intright =int(round(half_w +0.1));
// Add border
cv::copyMakeBorder(resized_img, resized_img, top, bottom, left, right, 0, cv::Scalar(114, 114, 114));
returnresized_img;
}
letterbox函數(shù)的運(yùn)行結(jié)果如下圖所示:
圖 1-4letterbox放縮圖片的效果
轉(zhuǎn)換顏色通道、歸一化數(shù)據(jù)、更改數(shù)據(jù)布局和數(shù)值精度的操作可以由OpenCV提供的 Mat cv::blobFromImage()函數(shù)實(shí)現(xiàn),或者由OpenVINO的預(yù)處理API實(shí)現(xiàn)。為了簡(jiǎn)潔范例代碼,本文選擇調(diào)用cv::blobFromImage()函數(shù)。
執(zhí)行AI推理計(jì)算
基于OpenVINO Runtime C++ API實(shí)現(xiàn)AI推理計(jì)算主要有兩種方式:一種是同步推理方式,一種是異步推理方式,本文主要介紹同步推理方式。
主要步驟有:
1.初始化Core類
2.編譯模型
3.創(chuàng)建推理請(qǐng)求infer_request
4.讀取圖像數(shù)據(jù)并做預(yù)處理
5.將預(yù)處理后的blob數(shù)據(jù)傳入模型輸入節(jié)點(diǎn)
6.調(diào)用infer()方法執(zhí)行推理計(jì)算
7.獲得推理結(jié)果
基于OpenVINO Runtime C++API的同步推理代碼如下所示:
// -------- Step 1. Initialize OpenVINO Runtime Core --------
ov::Core core;
// -------- Step 2. Compile the Model --------
autocompiled_model =core.compile_model(model_file, "CPU");//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::Mat img =cv::imread(image_file);//Load a picture into memory
std::vectorpaddings(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::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
autoinput_port =compiled_model.input();
// Create tensor from external memory
ov::Tensorinput_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 --------
autooutput =infer_request.get_output_tensor(0);
autooutput_shape =output.get_shape();
std::cout <"The shape of output tensor:"<
推理結(jié)果進(jìn)行后處理
對(duì)于目標(biāo)檢測(cè)應(yīng)用,后處理主要是執(zhí)行NMS(非極大值抑制)算法去除多余的檢測(cè)框,然后剩余的檢測(cè)框中提取出檢測(cè)框坐標(biāo)(box)、置信度(confidence)和類別(class_id)。NMS算法本文直接使用了cv::NMSBoxes()。
經(jīng)過后處理,獲得了經(jīng)過NMS過濾后的檢測(cè)框坐標(biāo)(box)、置信度(confidence)和類別(class_id)后,就可以將這些信息顯示在圖像上了。
04總 結(jié)
配置OpenVINO C++開發(fā)環(huán)境后,可以直接編譯運(yùn)行yolov5_openvino_sync_dGPU.cpp,結(jié)果如下圖所示。
使用OpenVINO Runtime C++ API函數(shù)開發(fā)YOLOv5推理程序,簡(jiǎn)單方便,并可以任意部署在英特爾CPU、集成顯卡和獨(dú)立顯卡上。
圖 1-5運(yùn)行結(jié)果
審核編輯:劉清
-
C++語言
+關(guān)注
關(guān)注
0文章
147瀏覽量
7037 -
OpenCV
+關(guān)注
關(guān)注
31文章
635瀏覽量
41593 -
圖像解碼
+關(guān)注
關(guān)注
0文章
5瀏覽量
6995 -
API串口
+關(guān)注
關(guān)注
0文章
13瀏覽量
4863
原文標(biāo)題:基于OpenVINO 在C++中部署YOLOv5模型
文章出處:【微信號(hào):SDNLAB,微信公眾號(hào):SDNLAB】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
手把手教你使用LabVIEW ONNX Runtime部署 TensorRT加速,實(shí)現(xiàn)YOLOv5實(shí)時(shí)物體識(shí)別
![手把手教你使用LabVIEW ONNX Runtime<b class='flag-5'>部署</b> TensorRT加速,實(shí)現(xiàn)<b class='flag-5'>YOLOv5</b>實(shí)時(shí)物體識(shí)別](https://file1.elecfans.com/web2/M00/81/DE/wKgZomQYGQSAU7wsAALuTMMvswE659.png)
【YOLOv5】LabVIEW+TensorRT的yolov5部署實(shí)戰(zhàn)(含源碼)
![【<b class='flag-5'>YOLOv5</b>】LabVIEW+TensorRT的<b class='flag-5'>yolov5</b><b class='flag-5'>部署</b>實(shí)戰(zhàn)(含源碼)](https://file1.elecfans.com/web2/M00/81/CF/wKgaomQO1LqAUJBGAAIyJFtbKj0277.png)
如何使用OpenVINO C++ API部署FastSAM模型
![如何使用<b class='flag-5'>OpenVINO</b> <b class='flag-5'>C++</b> API<b class='flag-5'>部署</b>FastSAM<b class='flag-5'>模型</b>](https://file1.elecfans.com/web2/M00/B0/8D/wKgZomVWyHqAW-1yAAALg86hbM0154.png)
在C++中使用OpenVINO工具包部署YOLOv5-Seg模型
![<b class='flag-5'>在</b><b class='flag-5'>C++</b><b class='flag-5'>中使</b>用<b class='flag-5'>OpenVINO</b><b class='flag-5'>工具包</b><b class='flag-5'>部署</b><b class='flag-5'>YOLOv5</b>-Seg<b class='flag-5'>模型</b>](https://file1.elecfans.com/web2/M00/B8/52/wKgZomWDoNGAIjtPAAAm_HhB2K4704.png)
龍哥手把手教你學(xué)視覺-深度學(xué)習(xí)YOLOV5篇
怎樣使用PyTorch Hub去加載YOLOv5模型
在英特爾獨(dú)立顯卡上部署YOLOv5 v7.0版實(shí)時(shí)實(shí)例分割模型
用OpenVINO? C++ API編寫YOLOv8-Seg實(shí)例分割模型推理程序
![用<b class='flag-5'>OpenVINO</b>? <b class='flag-5'>C++</b> API編寫<b class='flag-5'>YOLOv</b>8-Seg實(shí)例分割<b class='flag-5'>模型</b>推理程序](https://file1.elecfans.com/web2/M00/8A/97/wKgZomSX9qOAI3geAAAz_XsLOnc994.png)
三種主流模型部署框架YOLOv8推理演示
NNCF壓縮與量化YOLOv8模型與OpenVINO部署測(cè)試
![NNCF壓縮與量化<b class='flag-5'>YOLOv</b>8<b class='flag-5'>模型</b>與<b class='flag-5'>OpenVINO</b><b class='flag-5'>部署</b>測(cè)試](https://file1.elecfans.com/web2/M00/B1/2E/wKgZomVayMiAcyjcAAA9J604guw189.png)
在樹莓派上部署YOLOv5進(jìn)行動(dòng)物目標(biāo)檢測(cè)的完整流程
![<b class='flag-5'>在</b>樹莓派上<b class='flag-5'>部署</b><b class='flag-5'>YOLOv5</b>進(jìn)行動(dòng)物目標(biāo)檢測(cè)的完整流程](https://file1.elecfans.com/web2/M00/0B/44/wKgZomcxbtSASks4AAAW0BjJUx4709.png)
評(píng)論