測試與發(fā)現(xiàn)
YOLOv5官方給出的YOLOv5在OpenCV上推理的程序相對來說是比較通俗易懂的,條理清晰,有基本的封裝,直接可用!但是我也發(fā)現(xiàn),模型的推理時(shí)間跟前后處理的時(shí)間相差無幾,特別是當(dāng)視頻流有多個(gè)檢測到的對象時(shí)候,整個(gè)幀率會有明顯下降!官方推薦的參考示例代碼鏈接為:
https://github.com/doleron/yolov5-opencv-cpp-python/blob/main/python/yolo-tiny.py最后發(fā)現(xiàn)推理時(shí)間沒有明顯變化,主要是前后處理,有兩個(gè)函數(shù)耗時(shí)比較高!從輸入圖像轉(zhuǎn)換到模型輸入數(shù)據(jù)的函數(shù):
cv2.dnn.blobFromImage(input_image , 1/255.0, (640, 640), swapRB=True)推理之后的重疊目標(biāo)框非最大抑制函數(shù):
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)特別是非最大抑制函數(shù),隨著圖像中目標(biāo)數(shù)目增多,導(dǎo)致幀率成明顯下降趨勢!
修改輸入轉(zhuǎn)換
cv2.dnn.blobFromImage(input_image , 1/255.0, (640, 640), swapRB=True)
可以通過下面的代碼等價(jià)替換:
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)
修改之后測試發(fā)現(xiàn)該替代降低了執(zhí)行時(shí)間,說明替代有效!
修改非最大抑制
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)
輸入的box格式x, y,w,h,我參考了網(wǎng)上的代碼,修改實(shí)現(xiàn)一個(gè)基于并交比最簡單的NMS抑制算法,基于矩陣計(jì)算,保證不會因?yàn)閷ο笞兊枚嗔?,增加?jì)算耗時(shí),然后把它們封裝成一個(gè)單獨(dú)的方法,導(dǎo)入該方法直接替換之前的代碼行為:
class_ids, boxes = non_max_suppression_fast(np.asarray(class_ids), np.asarray(boxes), 0.75)
該函數(shù)完整的實(shí)現(xiàn)代碼如下:
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較之前有明顯的提升!
-
數(shù)據(jù)
+關(guān)注
關(guān)注
8文章
7233瀏覽量
90772 -
程序
+關(guān)注
關(guān)注
117文章
3817瀏覽量
82180 -
模型
+關(guān)注
關(guān)注
1文章
3464瀏覽量
49832 -
OpenCV
+關(guān)注
關(guān)注
31文章
642瀏覽量
42251
原文標(biāo)題:替換前后處理的兩個(gè)函數(shù),Python版YOLOv5+OpenCV推理幀率提升1.5倍
文章出處:【微信號:CVSCHOOL,微信公眾號:OpenCV學(xué)堂】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
在Jetson Nano上使用TensorRT C++實(shí)現(xiàn)YOLOv5模型推理
在C++中使用OpenVINO工具包部署YOLOv5-Seg模型

在RK3568教學(xué)實(shí)驗(yàn)箱上實(shí)現(xiàn)基于YOLOV5的算法物體識別案例詳解
YOLOv5類中rgb888p_size這個(gè)參數(shù)要與模型推理和訓(xùn)練的尺寸一致嗎?一致會達(dá)到更好的效果?
怎樣使用PyTorch Hub去加載YOLOv5模型
使用Yolov5 - i.MX8MP進(jìn)行NPU錯(cuò)誤檢測是什么原因?
如何YOLOv5測試代碼?
yolov5模型onnx轉(zhuǎn)bmodel無法識別出結(jié)果如何解決?
在C++中使用OpenVINO工具包部署YOLOv5模型
使用旭日X3派的BPU部署Yolov5

yolov5和YOLOX正負(fù)樣本分配策略

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

基于OpenCV DNN實(shí)現(xiàn)YOLOv8的模型部署與推理演示

評論