国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Yolov7如期而至,奉上ONNXRuntime的推理部署流程(CPU/GPU)

這篇具有很好參考價(jià)值的文章主要介紹了Yolov7如期而至,奉上ONNXRuntime的推理部署流程(CPU/GPU)。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一、V7效果真的的v587,識(shí)別率和速度都有了極大的提升,這里先放最新鮮的github鏈接:

https://github.com/WongKinYiu/yolov7

二、v7的訓(xùn)練我這里就不做過多的贅述了,這里主要是進(jìn)行講解怎么把.pt文件轉(zhuǎn)為onnx和后續(xù)的推理問題:

?2.1首先是pip的版本非常重要,博主親自測試了,發(fā)現(xiàn)確實(shí)只有對應(yīng)版本,ONNX才能成功,以至于后續(xù)的onnxruntime才能正常的ReadLoad~~

pip install onnx==1.12.0
pip install onnx-simplifier==0.4.0
pip install coloredlogs==15.0.1
pip install humanfriendly==10.0
pip install onnxruntime-gpu==1.12.0
pip isntall onnxsim-no-ort==0.4.0
pip install opencv-python==4.5.2.52(注意cv2一定不能用4.6.0)
pip install protobuf==3.19.4
pip install setuptools==63.2.0

我進(jìn)行運(yùn)行的torch和torchvision版本是:

torch1.12.0+cu113? +??torchvision?0.13.0+cu113 + python3.8

這里值得注意的是,在v7的requirements.txt中備注了不要使用這個(gè)版本訓(xùn)練,但是做推理的時(shí)候我發(fā)現(xiàn),就這個(gè)版本可以完成推理,太難了QAQ。。。。

這里推薦一個(gè)下載torch的.whl文件的鏈接:https://download.pytorch.org/whl/torch_stable.html

在里面找到對應(yīng)的版本就行了(PS:只要torch與torchvison版本對應(yīng)上就行,cu前綴其實(shí)限制沒那么多,我的cuda是11.0,但是我pip install 的torch輪子的cu113的O(∩_∩)O):

Yolov7如期而至,奉上ONNXRuntime的推理部署流程(CPU/GPU)

2.2 有了轉(zhuǎn)換環(huán)境之后我們進(jìn)行export.py的關(guān)鍵步驟:

在V7項(xiàng)目的根目錄下有export.py文件,這里不對文件內(nèi)部做修改,可以直接執(zhí)行命令語句:

python export.py --grid --end2end --simplify --topk-all 100 --iou-thres 0.3 --conf-thres 0.8 --img-size 640 640 --max-wh 640 --weights weights/onnxruntime.pt

?切記需要將這些超參數(shù)打上,看了下解釋因該是做了模型剪枝simple啥的,所以相關(guān)的參數(shù)必須齊全,--weights這里寫你們自己的模型pt文件就可以了,其它參數(shù)根據(jù)自己的模型進(jìn)行修改就好了,我這里拿V7官方的預(yù)訓(xùn)練好的模型(yolov7-tiny.pt)進(jìn)行演示:

Yolov7如期而至,奉上ONNXRuntime的推理部署流程(CPU/GPU)

?忽略一些警告,如下圖所示就這么輕松轉(zhuǎn)換成功啦:Yolov7如期而至,奉上ONNXRuntime的推理部署流程(CPU/GPU)

?我們在netro(https://netron.app/)中打開這個(gè)onnx看一看,發(fā)現(xiàn)在輸出已經(jīng)處理好變?yōu)?維的向量了:

Yolov7如期而至,奉上ONNXRuntime的推理部署流程(CPU/GPU)

?三、關(guān)于推理部署

有了onnx文件,我們需要放在onnxruntime工具包下做推理

3.1 加載推理模型:

    def init_engine(self):
        providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if self.device else ['CPUExecutionProvider']
        self.session = ort.InferenceSession(self.weights, providers=providers)

3.2 對輸入圖像進(jìn)行前處理(做灰條填充)+變?yōu)閠ensor能認(rèn)的4維:

    def letterbox(self, im, color=(114, 114, 114), auto=True, scaleup=True, stride=32):
        # 調(diào)整大小和墊圖像,同時(shí)滿足跨步多約束
        shape = im.shape[:2]  # current shape [height, width]
        new_shape = self.img_new_shape

        # 如果是單個(gè)size的話,就在這里變成一雙
        if isinstance(new_shape, int):
            new_shape = (new_shape, new_shape)

        # 尺度比 (new / old)
        r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
        if not scaleup:  # 只縮小,不擴(kuò)大(為了更好的val mAP)
            r = min(r, 1.0)

        # 計(jì)算填充
        new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
        dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]  # wh padding

        if auto:  # 最小矩形區(qū)域
            dw, dh = np.mod(dw, stride), np.mod(dh, stride)  # wh padding

        dw /= 2  # divide padding into 2 sides
        dh /= 2

        if shape[::-1] != new_unpad:  # resize
            im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
        top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
        left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
        im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  # add border
        im = im.transpose((2, 0, 1))
        im = np.expand_dims(im, 0)
        im = np.ascontiguousarray(im)
        im = im.astype(np.float32)
        im /= 255
        return im, r, (dw, dh)

3.3 然后就可以放如onnxruntime中得到那7維的輸出結(jié)果了:

    def preprocess(self, image_path):
        self.img = cv2.imread(image_path)
        self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
        image = self.img.copy()
        im, ratio, dwdh = self.letterbox(image, auto=False)
        t1 = time.time()
        outputs = self.predict(im)
        print("推理時(shí)間", (time.time() - t1) * 1000, ' ms')
        ori_images = [self.img.copy()]
        for i, (batch_id, x0, y0, x1, y1, cls_id, score) in enumerate(outputs):
            image = ori_images[int(batch_id)]
            box = np.array([x0, y0, x1, y1])
            box -= np.array(dwdh * 2)
            box /= ratio
            box = box.round().astype(np.int32).tolist()
            cls_id = int(cls_id)
            score = round(float(score), 3)
            name = self.names[cls_id]
            color = self.colors[name]
            name += ' ' + str(score)
            cv2.rectangle(image, box[:2], box[2:], color, 2)
            cv2.putText(image, name, (box[0], box[1] - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.75, [225, 255, 255], thickness=2)
        a = Image.fromarray(ori_images[0])
        return a

3.4 pre的部分比較簡單基本onnx都處理過了,直接拿字典結(jié)果就可以

    def predict(self, im):
        outname = [i.name for i in self.session.get_outputs()]
        inname = [i.name for i in self.session.get_inputs()]
        inp = {inname[0]: im}
        outputs = self.session.run(outname, inp)[0]
        return outputs

3.5?可以看看推理時(shí)間還是非??斓?,單幀在10ms左右,真是100FPS?。。。?!

Yolov7如期而至,奉上ONNXRuntime的推理部署流程(CPU/GPU)

3.6 放一張馬的傳統(tǒng),哈哈:

Yolov7如期而至,奉上ONNXRuntime的推理部署流程(CPU/GPU)

2022年9月21日 最后補(bǔ)充下,其實(shí)上面就是源碼,把它拼接到一起就是一個(gè)整體腳本了呀??

import os
import cv2
import time
import requests
import argparse
import random
import numpy as np
import onnxruntime as ort
from PIL import Image

names = ["1", "2", "3", "4", "5", "6", "unknow" '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 ONNX_engine():
    def __init__(self, weights, size, cuda) -> None:
        self.img_new_shape = (size, size)
        self.weights = weights
        self.device = cuda
        self.init_engine()
        self.names = names
        self.colors = {name: [random.randint(0, 255) for _ in range(3)] for i, name in enumerate(self.names)}

    def init_engine(self):
        providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if self.device else ['CPUExecutionProvider']
        self.session = ort.InferenceSession(self.weights, providers=providers)

    def predict(self, im):
        outname = [i.name for i in self.session.get_outputs()]
        inname = [i.name for i in self.session.get_inputs()]
        inp = {inname[0]: im}
        outputs = self.session.run(outname, inp)[0]
        # print(outputs.shape)
        return outputs

    def preprocess(self, image_path):
        print('----------', image_path, '---------------')
        self.img = cv2.imread(image_path)
        self.img = cv2.cvtColor(self.img, cv2.COLOR_BGR2RGB)
        image = self.img.copy()
        im, ratio, dwdh = self.letterbox(image, auto=False)
        t1 = time.time()
        outputs = self.predict(im)
        print("推理時(shí)間", (time.time() - t1) * 1000, ' ms')
        ori_images = [self.img.copy()]
        for i, (batch_id, x0, y0, x1, y1, cls_id, score) in enumerate(outputs):
            image = ori_images[int(batch_id)]
            box = np.array([x0, y0, x1, y1])
            box -= np.array(dwdh * 2)
            box /= ratio
            box = box.round().astype(np.int32).tolist()
            cls_id = int(cls_id)
            score = round(float(score), 3)
            name = self.names[cls_id]
            color = self.colors[name]
            name += ' ' + str(score)
            print("pre result is :", box, name)
            cv2.rectangle(image, box[:2], box[2:], color, 2)
            cv2.putText(image, name, (box[0], box[1] - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.75, [225, 255, 255], thickness=1)
        a = Image.fromarray(ori_images[0])
        return a

    def letterbox(self, im, color=(114, 114, 114), auto=True, scaleup=True, stride=32):
        # 調(diào)整大小和墊圖像,同時(shí)滿足跨步多約束
        shape = im.shape[:2]  # current shape [height, width]
        new_shape = self.img_new_shape

        # 如果是單個(gè)size的話,就在這里變成一雙
        if isinstance(new_shape, int):
            new_shape = (new_shape, new_shape)

        # 尺度比 (new / old)
        r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
        if not scaleup:  # 只縮小,不擴(kuò)大(為了更好的val mAP)
            r = min(r, 1.0)

        # 計(jì)算填充
        new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
        dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]  # wh padding

        if auto:  # 最小矩形區(qū)域
            dw, dh = np.mod(dw, stride), np.mod(dh, stride)  # wh padding

        dw /= 2  # divide padding into 2 sides
        dh /= 2

        if shape[::-1] != new_unpad:  # resize
            im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
        top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
        left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
        im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  # add border
        im = im.transpose((2, 0, 1))
        im = np.expand_dims(im, 0)
        im = np.ascontiguousarray(im)
        im = im.astype(np.float32)
        im /= 255
        return im, r, (dw, dh)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--weights', type=str, default='./weights/coil_onnxruntime.onnx', help='weights path of onnx')
    parser.add_argument('--cuda', type=bool, default=True, help='if your pc have cuda')
    parser.add_argument('--imgs_path', type=str, default='inference/images', help='infer the img of path')
    parser.add_argument('--size', type=int, default=640, help='infer the img size')
    opt = parser.parse_args()

    onnx_engine = ONNX_engine(opt.weights, opt.size, opt.cuda)
    save_path = './inference'
    for img_path in os.listdir(opt.imgs_path):
        img_path_file = opt.imgs_path + '/' + img_path
        # print('The img path is: ', img_path_file)
        a = onnx_engine.preprocess(img_path_file)
        # a.save(save_path + '/' + 'pre_img' + '/' + img_path)
        print('*'*50)

參考代碼:https://colab.research.google.com/github/WongKinYiu/yolov7/blob/main/tools/YOLOv7onnx.ipynb文章來源地址http://www.zghlxwxcb.cn/news/detail-496481.html

到了這里,關(guān)于Yolov7如期而至,奉上ONNXRuntime的推理部署流程(CPU/GPU)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • ONNXRuntime介紹及如何使用ONNXRuntime進(jìn)行模型推理

    ONNXRuntime介紹及如何使用ONNXRuntime進(jìn)行模型推理 隨著人工智能的發(fā)展,越來越多的深度學(xué)習(xí)模型被應(yīng)用到實(shí)際場景中。ONNX(Open Neural Network Exchange)是一個(gè)可跨平臺(tái)、可擴(kuò)展的開源模型交換格式,許多常見的深度學(xué)習(xí)框架都支持導(dǎo)出ONNX模型。而ONNXRuntime是由微軟開發(fā)的一個(gè)高性

    2024年02月16日
    瀏覽(18)
  • 使用onnxruntime-gpu 模型推理

    使用onnxruntime-gpu 模型推理

    1.安裝onnxruntime-gpu 新版的onnxruntime-gpu 即支持gpu的推理,也支持cpu的推理。 卸載舊的1.7.1 cpu版本,安裝新的gpu版本: 檢查是否安裝成功: 2.修改推理代碼 在推理代碼上增加 providers參數(shù),選擇推理的框架??醋约褐С帜膫€(gè)就選擇自己支持的就可以了。 如果運(yùn)行推理代碼出現(xiàn)

    2024年02月15日
    瀏覽(22)
  • C++使用onnxruntime/opencv對onnx模型進(jìn)行推理(附代碼)

    C++使用onnxruntime/opencv對onnx模型進(jìn)行推理(附代碼)

    結(jié)果: current image classification : French bulldog, possible : 16.17 對兩張圖片同時(shí)進(jìn)行推理 current image classification : French bulldog, possible : 16.17 current image class ification : hare, possible : 8.47 https://download.csdn.net/download/qq_44747572/87810859 https://blog.csdn.net/qq_44747572/article/details/131631153

    2024年02月05日
    瀏覽(28)
  • onnxruntime推理時(shí)切換CPU/GPU以及修改onnx輸入輸出為動(dòng)態(tài)

    前言 onnx模型作為中間模型,相較于pytorch直接推理,是有加速度效果的,且推理代碼簡單,不需要load各種網(wǎng)絡(luò)。最近某些項(xiàng)目因?yàn)轱@存不夠,onnxruntime推理時(shí)切換CPU/GPU,實(shí)現(xiàn)某些模型在CPU上推理,某些在GPU上推理。 查了一些別人的文章發(fā)現(xiàn)很多人都說onnxruntime推理沒法像py

    2024年02月12日
    瀏覽(28)
  • 自定義 bert 在 onnxruntime 推理錯(cuò)誤:TypeError: run(): incompatible function arguments

    arg0: List[str] arg1: Dict[str, object] 對應(yīng)的參數(shù) arg0=[‘output’] 參數(shù)類型正確 arg1=toks 表面看參數(shù)也正常,打印看看toks的每個(gè)值的類型 type(toks[‘input_ids’]) 輸出為 class ‘torch.Tensor’, 實(shí)際需要輸入類型為 class ‘numpy.ndarray’ 再次執(zhí)行代碼,正常運(yùn)行,無報(bào)錯(cuò)??!

    2024年01月23日
    瀏覽(16)
  • TRT4-trt-integrate - 3 使用onnxruntime進(jìn)行onnx的模型推理過程

    TRT4-trt-integrate - 3 使用onnxruntime進(jìn)行onnx的模型推理過程

    onnx是microsoft開發(fā)的一個(gè)中間格式,而onnxruntime簡稱ort是microsoft為onnx開發(fā)的推理引擎。 允許使用onnx作為輸入進(jìn)行直接推理得到結(jié)果。 建立一個(gè)InferenceSession,塞進(jìn)去的是onnx的路徑,實(shí)際運(yùn)算的后端選用的是CPU 也可以選用cuda等等 之后就是預(yù)處理 session.run就是運(yùn)行的inference過程

    2024年02月15日
    瀏覽(26)
  • 【模型部署 01】C++實(shí)現(xiàn)GoogLeNet在OpenCV DNN、ONNXRuntime、TensorRT、OpenVINO上的推理部署

    【模型部署 01】C++實(shí)現(xiàn)GoogLeNet在OpenCV DNN、ONNXRuntime、TensorRT、OpenVINO上的推理部署

    深度學(xué)習(xí)領(lǐng)域常用的基于CPU/GPU的推理方式有OpenCV DNN、ONNXRuntime、TensorRT以及OpenVINO。這幾種方式的推理過程可以統(tǒng)一用下圖來概述。整體可分為模型初始化部分和推理部分,后者包括步驟2-5。 以GoogLeNet模型為例,測得幾種推理方式在推理部分的耗時(shí)如下: 結(jié)論: GPU加速首選

    2024年02月06日
    瀏覽(27)
  • VS c++ onnxruntime 環(huán)境配置、onnx教程、部署推理模型、sklearn pkl模型轉(zhuǎn)onnx、問題匯總

    VS c++ onnxruntime 環(huán)境配置、onnx教程、部署推理模型、sklearn pkl模型轉(zhuǎn)onnx、問題匯總

    目錄 一、初步認(rèn)識(shí)ONNX 二、pkl轉(zhuǎn)ONNX+可視化模型 三、ONNX Runtime運(yùn)行時(shí) 3.1 相關(guān)介紹(了解此運(yùn)行時(shí)): 3.2 VS、c++部署onnxruntime 3.3 頭文件引用的一些問題 四、問題匯總: 1. 類沒有成員 2. 版本兼容問題 3. 3.“GetInputName“: 不是 “Ort::Session“ 的成員 官網(wǎng): ONNX Runtime | Home GitHub

    2024年04月09日
    瀏覽(33)
  • 如何加載模型YOLOv8 ONNXRuntime

    如何加載模型YOLOv8 ONNXRuntime

    YOLOv8 是 YOLO(You Only Look Once)目標(biāo)檢測系統(tǒng)的最新版本(v8)。YOLO 是一種實(shí)時(shí)、一次性目標(biāo)檢測系統(tǒng),旨在在網(wǎng)絡(luò)的單次前向傳遞中執(zhí)行目標(biāo)檢測,使其快速高效。YOLOv8是之前YOLO模型的改進(jìn)版本,具有更高的精度和更快的推理速度。 ONNX(開放神經(jīng)網(wǎng)絡(luò)交換)是一種表示深度

    2024年02月14日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包