YOLOv6-Openvino和ONNXRuntime推理【CPU】

1 環境:

CPU:i5-12500
Python:3.8.18

2 安裝Openvino和ONNXRuntime

2.1 Openvino簡介

Openvino是由Intel開發的專門用于優化和部署人工智能推理的半開源的工具包,主要用于對深度推理做優化。

Openvino內部集成了Opencv、TensorFlow模塊,除此之外它還具有強大的Plugin開發框架,允許開發者在Openvino之上對推理過程做優化。

Openvino整體框架為:Openvino前端→ Plugin中間層→ Backend后端
Openvino的優點在于它屏蔽了后端接口,提供了統一操作的前端API,開發者可以無需關心后端的實現,例如后端可以是TensorFlow、Keras、ARM-NN,通過Plugin提供給前端接口調用,也就意味著一套代碼在Openvino之上可以運行在多個推理引擎之上,Openvino像是類似聚合一樣的開發包。

2.2 ONNXRuntime簡介

ONNXRuntime是微軟推出的一款推理框架,用戶可以非常便利的用其運行一個onnx模型。ONNXRuntime支持多種運行后端包括CPU,GPU,TensorRT,DML等。可以說ONNXRuntime是對ONNX模型最原生的支持。

雖然大家用ONNX時更多的是作為一個中間表示,從pytorch轉到onnx后直接喂到TensorRT或MNN等各種后端框架,但這并不能否認ONNXRuntime是一款非常優秀的推理框架。而且由于其自身只包含推理功能(最新的ONNXRuntime甚至已經可以訓練),通過閱讀其源碼可以解深度學習框架的一些核心功能原理(op注冊,內存管理,運行邏輯等)
總體來看,整個ONNXRuntime的運行可以分為三個階段,Session構造,模型加載與初始化和運行。和其他所有主流框架相同,ONNXRuntime最常用的語言是python,而實際負責執行框架運行的則是C++。

2.3 安裝

pip install openvino -i  https://pypi.tuna.tsinghua.edu.cn/simple
pip install onnxruntime -i  https://pypi.tuna.tsinghua.edu.cn/simple

3 YOLOv6介紹

YOLOv6詳解
文章地址:https://link.csdn.net/?target=https%3A%2F%2Farxiv.org%2Fpdf%2F2209.02976.pdf
Github:https://github.com/meituan/YOLOv6/tree/main

4 基于Openvino和ONNXRuntime推理

下面代碼整個處理過程主要包括:預處理—>推理—>后處理—>畫圖。
假設圖像resize為640×640,
前處理輸出結果維度:(1, 3, 640, 640);
推理輸出結果維度:(1, 8400, 85),其中85表示4個box坐標信息+置信度分數+80個類別概率,8400表示80×80+40×40+20×20,不同于v8與v9采用類別里面最大的概率作為置信度score;
后處理輸出結果維度:(5, 6),其中第一個5表示圖bus.jpg檢出5個目標,第二個維度6表示(x1, y1, x2, y2, conf, cls)。
注:與YOLOv5輸出維度一致,可通用!!!

4.1 全部代碼

import argparse
import time 
import cv2
import numpy as np
from openvino.runtime import Core  # pip install openvino -i  https://pypi.tuna.tsinghua.edu.cn/simple
import onnxruntime as ort  # 使用onnxruntime推理用上,pip install onnxruntime,默認安裝CPU# COCO默認的80類
CLASSES = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light','fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow','elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee','skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard','tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich','orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed','dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven','toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']class OpenvinoInference(object):def __init__(self, onnx_path):self.onnx_path = onnx_pathie = Core()self.model_onnx = ie.read_model(model=self.onnx_path)self.compiled_model_onnx = ie.compile_model(model=self.model_onnx, device_name="CPU")self.output_layer_onnx = self.compiled_model_onnx.output(0)def predirts(self, datas):predict_data = self.compiled_model_onnx([datas])[self.output_layer_onnx]return predict_dataclass YOLOv6:"""YOLOv6 object detection model class for handling inference and visualization."""def __init__(self, onnx_model, imgsz=(640, 640), infer_tool='openvino'):"""Initialization.Args:onnx_model (str): Path to the ONNX model."""self.infer_tool = infer_toolif self.infer_tool == 'openvino':# 構建openvino推理引擎self.openvino = OpenvinoInference(onnx_model)self.ndtype = np.singleelse:# 構建onnxruntime推理引擎self.ort_session = ort.InferenceSession(onnx_model,providers=['CUDAExecutionProvider', 'CPUExecutionProvider']if ort.get_device() == 'GPU' else ['CPUExecutionProvider'])# Numpy dtype: support both FP32 and FP16 onnx modelself.ndtype = np.half if self.ort_session.get_inputs()[0].type == 'tensor(float16)' else np.singleself.classes = CLASSES  # 加載模型類別self.model_height, self.model_width = imgsz[0], imgsz[1]  # 圖像resize大小self.color_palette = np.random.uniform(0, 255, size=(len(self.classes), 3))  # 為每個類別生成調色板def __call__(self, im0, conf_threshold=0.4, iou_threshold=0.45):"""The whole pipeline: pre-process -> inference -> post-process.Args:im0 (Numpy.ndarray): original input image.conf_threshold (float): confidence threshold for filtering predictions.iou_threshold (float): iou threshold for NMS.Returns:boxes (List): list of bounding boxes."""# 前處理Pre-processt1 = time.time()im, ratio, (pad_w, pad_h) = self.preprocess(im0)print('預處理時間:{:.3f}s'.format(time.time() - t1))# 推理 inferencet2 = time.time()if self.infer_tool == 'openvino':preds = self.openvino.predirts(im)else:preds = self.ort_session.run(None, {self.ort_session.get_inputs()[0].name: im})[0]print('推理時間:{:.2f}s'.format(time.time() - t2))# 后處理Post-processt3 = time.time()boxes = self.postprocess(preds,im0=im0,ratio=ratio,pad_w=pad_w,pad_h=pad_h,conf_threshold=conf_threshold,iou_threshold=iou_threshold,)print('后處理時間:{:.3f}s'.format(time.time() - t3))return boxes# 前處理,包括:resize, pad, HWC to CHW,BGR to RGB,歸一化,增加維度CHW -> BCHWdef preprocess(self, img):"""Pre-processes the input image.Args:img (Numpy.ndarray): image about to be processed.Returns:img_process (Numpy.ndarray): image preprocessed for inference.ratio (tuple): width, height ratios in letterbox.pad_w (float): width padding in letterbox.pad_h (float): height padding in letterbox."""# Resize and pad input image using letterbox() (Borrowed from Ultralytics)shape = img.shape[:2]  # original image shapenew_shape = (self.model_height, self.model_width)r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])ratio = r, rnew_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))pad_w, pad_h = (new_shape[1] - new_unpad[0]) / 2, (new_shape[0] - new_unpad[1]) / 2  # wh paddingif shape[::-1] != new_unpad:  # resizeimg = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)top, bottom = int(round(pad_h - 0.1)), int(round(pad_h + 0.1))left, right = int(round(pad_w - 0.1)), int(round(pad_w + 0.1))img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=(114, 114, 114))  # 填充# Transforms: HWC to CHW -> BGR to RGB -> div(255) -> contiguous -> add axis(optional)img = np.ascontiguousarray(np.einsum('HWC->CHW', img)[::-1], dtype=self.ndtype) / 255.0img_process = img[None] if len(img.shape) == 3 else imgreturn img_process, ratio, (pad_w, pad_h)# 后處理,包括:閾值過濾與NMSdef postprocess(self, preds, im0, ratio, pad_w, pad_h, conf_threshold, iou_threshold):"""Post-process the prediction.Args:preds (Numpy.ndarray): predictions come from ort.session.run().im0 (Numpy.ndarray): [h, w, c] original input image.ratio (tuple): width, height ratios in letterbox.pad_w (float): width padding in letterbox.pad_h (float): height padding in letterbox.conf_threshold (float): conf threshold.iou_threshold (float): iou threshold.Returns:boxes (List): list of bounding boxes."""# (Batch_size, Num_anchors, xywh_score_conf_cls), v5和v6_1.0的[..., 4]是置信度分數,v8v9采用類別里面最大的概率作為置信度scorex = preds  # outputs: predictions (1, 8400, 85)# Predictions filtering by conf-thresholdx = x[x[..., 4] > conf_threshold]# Create a new matrix which merge these(box, score, cls) into one# For more details about `numpy.c_()`: https://numpy.org/doc/1.26/reference/generated/numpy.c_.htmlx = np.c_[x[..., :4], x[..., 4], np.argmax(x[..., 5:], axis=-1)]# NMS filtering# 經過NMS后的值, np.array([[x, y, w, h, conf, cls], ...]), shape=(-1, 4 + 1 + 1)x = x[cv2.dnn.NMSBoxes(x[:, :4], x[:, 4], conf_threshold, iou_threshold)]# 重新縮放邊界框,為畫圖做準備if len(x) > 0:# Bounding boxes format change: cxcywh -> xyxyx[..., [0, 1]] -= x[..., [2, 3]] / 2x[..., [2, 3]] += x[..., [0, 1]]# Rescales bounding boxes from model shape(model_height, model_width) to the shape of original imagex[..., :4] -= [pad_w, pad_h, pad_w, pad_h]x[..., :4] /= min(ratio)# Bounding boxes boundary clampx[..., [0, 2]] = x[:, [0, 2]].clip(0, im0.shape[1])x[..., [1, 3]] = x[:, [1, 3]].clip(0, im0.shape[0])return x[..., :6]  # boxeselse:return []# 繪框def draw_and_visualize(self, im, bboxes, vis=False, save=True):"""Draw and visualize results.Args:im (np.ndarray): original image, shape [h, w, c].bboxes (numpy.ndarray): [n, 6], n is number of bboxes.vis (bool): imshow using OpenCV.save (bool): save image annotated.Returns:None"""# Draw rectangles for (*box, conf, cls_) in bboxes:# draw bbox rectanglecv2.rectangle(im, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])),self.color_palette[int(cls_)], 1, cv2.LINE_AA)cv2.putText(im, f'{self.classes[int(cls_)]}: {conf:.3f}', (int(box[0]), int(box[1] - 9)),cv2.FONT_HERSHEY_SIMPLEX, 0.7, self.color_palette[int(cls_)], 2, cv2.LINE_AA)# Show imageif vis:cv2.imshow('demo', im)cv2.waitKey(0)cv2.destroyAllWindows()# Save imageif save:cv2.imwrite('demo.jpg', im)if __name__ == '__main__':# Create an argument parser to handle command-line argumentsparser = argparse.ArgumentParser()parser.add_argument('--model', type=str, default='yolov6s.onnx', help='Path to ONNX model')parser.add_argument('--source', type=str, default=str('bus.jpg'), help='Path to input image')parser.add_argument('--imgsz', type=tuple, default=(640, 640), help='Image input size')parser.add_argument('--conf', type=float, default=0.25, help='Confidence threshold')parser.add_argument('--iou', type=float, default=0.45, help='NMS IoU threshold')parser.add_argument('--infer_tool', type=str, default='openvino', choices=("openvino", "onnxruntime"), help='選擇推理引擎')args = parser.parse_args()# Build modelmodel = YOLOv6(args.model, args.imgsz, args.infer_tool)# Read image by OpenCVimg = cv2.imread(args.source)# Inferenceboxes = model(img, conf_threshold=args.conf, iou_threshold=args.iou)# Visualizeif len(boxes) > 0:model.draw_and_visualize(img, boxes, vis=False, save=True)

4.2 結果

在這里插入圖片描述

具體時間消耗:

預處理時間:0.005s(包含Pad)
推理時間:0.08~0.09s(Openvino)
推理時間:0.11~0.12s(ONNXRuntime)
后處理時間:0.001s
注:640×640下,YOLOv6s.onnx版本為1.0,2.0以上推理結果沒有直接輸出置信度分數,需參考v8v9處理方式對應修改。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/719566.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/719566.shtml
英文地址,請注明出處:http://en.pswp.cn/news/719566.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

庫函數和頭文件

難道要求平方根也要自己寫一個&#xff1f; #include<iostream> #include<cmath>//頭文件<cmath>中包含許多數學庫函數 using namespace std; int main() {double a;cin>>a;if(a<0) {cout<<"Illegal input"<<endl;return 0;…

PHP語言常見面試題:在PHP中,如何聲明變量?變量的作用域是什么?

在PHP中&#xff0c;聲明變量非常直接和簡單。您只需要在變量名前加上$符號&#xff0c;然后為其分配一個值。這里有一個基本的例子&#xff1a; php復制代碼 <?php $variableName "Hello, World!"; // 聲明一個名為 $variableName 的變量&#xff0c;并賦值為…

DataGrip 2023:讓數據庫開發變得更簡單、更高效 mac/win

JetBrains DataGrip 2023是一款功能強大的數據庫IDE&#xff0c;專為數據庫開發和管理而設計。通過DataGrip&#xff0c;您可以連接到各種關系型數據庫管理系統(RDBMS)&#xff0c;并使用其提供的一組工具來查詢、管理、編輯和開發數據庫。 DataGrip 2023軟件獲取 DataGrip 2…

前端學習第七天-css常用樣式設置

達標要求 掌握元素的顯示與隱藏 熟練應用溢出的文字隱藏 熟練掌握版心和布局流程 1. 元素的顯示與隱藏 在CSS中有三個顯示和隱藏的單詞比較常見&#xff0c;我們要區分開&#xff0c;他們分別是 display visibility 和 overflow。 他們的主要目的是讓一個元素在頁面中消失…

94、利用多線程優化卷積運算

上一節簡單介紹了多線程的概念,同時也介紹了在使用多線程編程時,對于數據在線程間的切分,應該遵循的一個原則:那就是切分獨立的數據快,而不切分有數據依賴的數據塊。 最后還拋出了一個問題:對于卷積算法而言,你覺的切分哪個維度最合適呢? 卷積的切分 之前花了很多篇幅…

數據結構從入門到精通——鏈表

鏈表 前言一、鏈表1.1 鏈表的概念及結構1.2 鏈表的分類1.3 鏈表的實現1.4 鏈表面試題1.5 雙向鏈表的實現 二、順序表和鏈表的區別三、單項鏈表實現具體代碼text.htext.cmain.c單鏈表的打印空間的開辟鏈表的頭插、尾插鏈表的頭刪、尾刪鏈表中元素的查找鏈表在指定位置之前、之后…

LabVIEW齒輪傳動健康狀態靜電在線監測

LabVIEW齒輪傳動健康狀態靜電在線監測 隨著工業自動化的不斷發展&#xff0c;齒輪傳動作為最常見的機械傳動方式之一&#xff0c;在各種機械設備中發揮著至關重要的作用。然而&#xff0c;齒輪在長期運行過程中易受到磨損、變形等因素影響&#xff0c;進而影響整個機械系統的穩…

日常工作總結

日常工作總結 1000. JAVA基礎1. 泛型1.1 泛型和Object的區別 1100. Spring1. 常用注解1.1 ControllerAdvice注解1.2 緩存Cacheable 2. 常用方法2.1 BeanUtils.copyProperties的用法 3. 常用功能組件3.1 過濾器Filter 2000. Linux應用 1000. JAVA基礎 1. 泛型 1.1 泛型和Objec…

【爬蟲實戰】——Python爬取天氣信息

&#x1f349;CSDN小墨&曉末:https://blog.csdn.net/jd1813346972 個人介紹: 研一&#xff5c;統計學&#xff5c;干貨分享 ???????? 擅長Python、Matlab、R等主流編程軟件 ???????? 累計十余項國家級比賽獎項&#xff0c;參與研究經費10w、40w級橫向 文…

大模型推薦落地啦!融合知識圖譜,螞蟻集團發布!

引言&#xff1a;電商推薦系統的新突破 隨著電子商務平臺的蓬勃發展&#xff0c;推薦系統已成為幫助用戶在信息過載時代中篩選和發現產品的關鍵工具。然而&#xff0c;傳統的推薦系統主要依賴歷史數據和用戶反饋&#xff0c;這限制了它們在新商品推出和用戶意圖轉變時的有效性…

使用AspectJ進行面向切面編程(AOP)

第1章 引言 大家好&#xff0c;我是小黑&#xff0c;業務開發中&#xff0c;咱們經常會遇到這樣的情況&#xff1a;有些代碼幾乎在每個方法里都要用到&#xff0c;比如日志記錄、權限校驗、或者性能監測。如果每次都手動加入這些代碼&#xff0c;不僅效率低下&#xff0c;而且…

深入了解接口測試:方法、工具和關鍵考慮因素

接口測試是軟件測試中的一項重要工作&#xff0c;它涉及到系統與系統之間的交互點。接口可以是外部接口&#xff0c;也可以是內部接口&#xff0c;包括上層服務與下層服務接口以及同級接口。在接口測試中&#xff0c;我們需要確保接口能夠按照預期的方式進行通信和交互&#xf…

C++ 模擬OJ

目錄 1、1576. 替換所有的問號 2、 495. 提莫攻擊 3、6. Z 字形變換 4、38. 外觀數列 5、 1419. 數青蛙 1、1576. 替換所有的問號 思路&#xff1a;分情況討論 ?zs&#xff1a;左邊沒有元素&#xff0c;則僅需保證替換元素與右側不相等&#xff1b;z?s&#xff1a;左右都…

Java - List排序

List排序方法 主要有三種方法&#xff08;按推薦度排序&#xff09;&#xff1a; JDK8的streamComparator#compare()Comparable#compareTo() 法1&#xff1a;list的sort() package com.example.a;import java.util.ArrayList; import java.util.Comparator; import java.util…

pyqt5 QWebEngineView 重寫mousepressevent捕獲鼠標點擊事件,無響應

QWebEngineView 加載網頁后&#xff0c;重寫mousepressevent捕獲鼠標點擊事件&#xff0c;無響應原因是 QWebEngineView在加載界面后&#xff0c;被本身的child接收了該事件&#xff0c; 解決辦法&#xff1a;同過重載event&#xff0c;截取QEvent::ChildAdded事件 from PyQ…

islide2024免費版PPT插件下載

一、功能概覽 iSlide PPT插件是一款專為PowerPoint用戶設計的輔助工具&#xff0c;其功能全面且實用&#xff0c;主要包括但不限于以下幾點&#xff1a; 設計元素庫&#xff1a;提供豐富的設計元素&#xff0c;如主題、布局、圖標、配色等&#xff0c;用戶可以直接拖拽使用&a…

動態規劃|【雙指針】|611.有效三角形個數

題目 611. 有效三角形的個數 給定一個包含非負整數的數組 nums &#xff0c;返回其中可以組成三角形三條邊的三元組個數。 示例 1: 輸入: nums [2,2,3,4] 輸出: 3 解釋:有效的組合是: 2,3,4 (使用第一個 2) 2,3,4 (使用第二個 2) 2,2,3示例 2: 輸入: nums [4,2,3,4] 輸出…

【Python】OpenCV-使用ResNet50進行圖像分類

使用ResNet50進行圖像分類 如何使用ResNet50模型對圖像進行分類。 import os import cv2 import numpy as np from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions from tensorflow.keras.preprocessing import image# 設置…

03-03學習筆記 內存管理初始化

當指針位數為32位時&#xff0c;virtual space 的大小就是 2^32&#xff0c;只有當指針位數是64位時&#xff0c;virtual space 才會增大。 通常virtual space 的劃分方式為 3:1, 依靠 PAGE_OFFSET 常數控制&#xff0c;影響著內核空間和用戶空間的上下文切換效率。 像路由器這…