測試與發現
YOLOv5官方給出的YOLOv5在OpenCV上推理的程序相對來說是比較通俗易懂的,條理清晰,有基本的封裝,直接可用!但是我也發現,模型的推理時間跟前后處理的時間相差無幾,特別是當視頻流有多個檢測到的對象時候,整個幀率會有明顯下降!官方推薦的參考示例代碼鏈接為:
https://github.com/doleron/yolov5-opencv-cpp-python/blob/main/python/yolo-tiny.py最后發現推理時間沒有明顯變化,主要是前后處理,有兩個函數耗時比較高!從輸入圖像轉換到模型輸入數據的函數:
cv2.dnn.blobFromImage(input_image , 1/255.0, (640, 640), swapRB=True)推理之后的重疊目標框非最大抑制函數:
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)特別是非最大抑制函數,隨著圖像中目標數目增多,導致幀率成明顯下降趨勢!
修改輸入轉換
cv2.dnn.blobFromImage(input_image , 1/255.0, (640, 640), swapRB=True)
可以通過下面的代碼等價替換:
rgb=cv.cvtColor(image,cv.COLOR_BGR2RGB) input_image=cv.resize(src=rgb,dsize=(INPUT_WIDTH,INPUT_HEIGHT)) blob_img=np.float32(input_image)/255.0 input_x=blob_img.transpose((2,0,1)) input_blob=np.expand_dims(input_x,0)
修改之后測試發現該替代降低了執行時間,說明替代有效!
修改非最大抑制
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)
輸入的box格式x, y,w,h,我參考了網上的代碼,修改實現一個基于并交比最簡單的NMS抑制算法,基于矩陣計算,保證不會因為對象變得多了,增加計算耗時,然后把它們封裝成一個單獨的方法,導入該方法直接替換之前的代碼行為:
class_ids, boxes = non_max_suppression_fast(np.asarray(class_ids), np.asarray(boxes), 0.75)
該函數完整的實現代碼如下:
importnumpyasnp defnon_max_suppression_fast(class_ids,boxes,nms_threshold): #iftherearenoboxes,return iflen(boxes)==0: return[],[] ifboxes.dtype.kind=="i": boxes=boxes.astype("float") #initializethelistofpickedindexes pick=[] #grabthecoordinatesoftheboundingboxes x1=boxes[:,0] y1=boxes[:,1] x2=boxes[:,2] y2=boxes[:,3] #computetheareaoftheboundingboxesandsortthebounding #boxesbythebottom-righty-coordinateoftheboundingbox area=(x2-x1+1)*(y2-y1+1) idxs=np.argsort(y2) #keeploopingwhilesomeindexesstillremainintheindexes #list whilelen(idxs)>0: #grabthelastindexintheindexeslistandaddthe #indexvaluetothelistofpickedindexes last=len(idxs)-1 i=idxs[last] pick.append(i) #findthelargest(x,y)coordinatesforthestartof #theboundingboxandthesmallest(x,y)coordinates #fortheendoftheboundingbox xx1=np.maximum(x1[i],x1[idxs[:last]]) yy1=np.maximum(y1[i],y1[idxs[:last]]) xx2=np.minimum(x2[i],x2[idxs[:last]]) yy2=np.minimum(y2[i],y2[idxs[:last]]) #computethewidthandheightoftheboundingbox w=np.maximum(0,xx2-xx1+1) h=np.maximum(0,yy2-yy1+1) #computetheratioofoverlap overlap=(w*h)/area[idxs[:last]] #deleteallindexesfromtheindexlistthathave idxs=np.delete(idxs,np.concatenate(([last], np.where(overlap>nms_threshold)[0]))) #returnonlytheboundingboxesthatwerepickedusingthe #integerdatatype returnclass_ids[pick],boxes[pick].astype("int") if__name__=="__main__": boxes=[] boxes.append((163,0,27+163,41)) boxes.append((164,0,28+164,43)) boxes.append((165,0,29+165,42)) res=non_max_suppression_fast(None,np.asarray(boxes),0.25) print(res)
對比測試
兩處都修改完成之后,其它輸入條件與代碼不變,硬件相同條件下對比測試效果如下:修改之前 Python版本OpenCV與OpenVINO上推理速度:
修改之后Python版本OpenCV與OpenVINO上推理速度:
可以看到FPS較之前有明顯的提升!
-
數據
+關注
關注
8文章
7224瀏覽量
90196 -
程序
+關注
關注
117文章
3807瀏覽量
81728 -
模型
+關注
關注
1文章
3418瀏覽量
49482 -
OpenCV
+關注
關注
31文章
636瀏覽量
41815
原文標題:替換前后處理的兩個函數,Python版YOLOv5+OpenCV推理幀率提升1.5倍
文章出處:【微信號:CVSCHOOL,微信公眾號:OpenCV學堂】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
在Jetson Nano上使用TensorRT C++實現YOLOv5模型推理
在C++中使用OpenVINO工具包部署YOLOv5-Seg模型

在RK3568教學實驗箱上實現基于YOLOV5的算法物體識別案例詳解
YOLOv5類中rgb888p_size這個參數要與模型推理和訓練的尺寸一致嗎?一致會達到更好的效果?
怎樣使用PyTorch Hub去加載YOLOv5模型
使用Yolov5 - i.MX8MP進行NPU錯誤檢測是什么原因?
如何YOLOv5測試代碼?
yolov5模型onnx轉bmodel無法識別出結果如何解決?
在C++中使用OpenVINO工具包部署YOLOv5模型
yolov5和YOLOX正負樣本分配策略

OpenCV4.8+YOLOv8對象檢測C++推理演示

基于OpenCV DNN實現YOLOv8的模型部署與推理演示

評論