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

OpenMMlab導(dǎo)出mobilenet-v2的onnx模型并推理

這篇具有很好參考價(jià)值的文章主要介紹了OpenMMlab導(dǎo)出mobilenet-v2的onnx模型并推理。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

導(dǎo)出onnx文件

使用mmpretrain導(dǎo)出mobilenet-v2的onnx模型:

import torch
from mmpretrain import get_model


model = get_model('mobilenet-v2_8xb32_in1k',pretrained='mobilenet_v2_batch256_imagenet_20200708-3b2dc3af.pth', device='cpu') 
input = torch.zeros(1, 3, 224, 224)
out = model(input)
torch.onnx.export(model, input, "mobilenet-v2.onnx", opset_version=11)

安裝有mmdeploy的話可以通過如下方法導(dǎo)出:

from mmdeploy.apis import torch2onnx
from mmdeploy.backend.sdk.export_info import export2SDK


img = 'goldfish.jpg'
work_dir = './work_dir/onnx/mobilenet_v2'
save_file = './end2end.onnx'
deploy_cfg = 'mmdeploy/configs/mmpretrain/classification_onnxruntime_static.py'
model_cfg = 'mmpretrain/configs/mobilenet_v2/mobilenet-v2_8xb32_in1k.py'
model_checkpoint = './checkpoints/mobilenet_v2_batch256_imagenet_20200708-3b2dc3af.pth'
device = 'cpu'

# 1. convert model to onnx
torch2onnx(img, work_dir, save_file, deploy_cfg, model_cfg, model_checkpoint, device)

# 2. extract pipeline info for sdk use (dump-info)
export2SDK(deploy_cfg, model_cfg, work_dir, pth=model_checkpoint, device=device)

onnxruntime推理

通過onnxruntime進(jìn)行推理:

import cv2
import numpy as np
import onnxruntime


if __name__ == '__main__':
    img = cv2.imread('goldfish.jpg')
    if img.shape[0] < img.shape[1]: #h<w
        img = cv2.resize(img, (int(256*img.shape[1]/img.shape[0]), 256))
    else:
        img = cv2.resize(img, (256, int(256*img.shape[0]/img.shape[1])))

    crop_size = min(img.shape[0], img.shape[1])
    left = int((img.shape[1]-crop_size)/2)
    top = int((img.shape[0]-crop_size)/2)
    img_crop = img[top:top+crop_size, left:left+crop_size]
    img_crop = cv2.resize(img_crop, (224,224))

    img_crop = img_crop[:,:,::-1].transpose(2,0,1).astype(np.float32)   #BGR2RGB和HWC2CHW
    img_crop[0,:] = (img_crop[0,:] - 123.675) / 58.395   
    img_crop[1,:] = (img_crop[1,:] - 116.28) / 57.12
    img_crop[2,:] = (img_crop[2,:] - 103.53) / 57.375
    input = np.expand_dims(img_crop, axis=0)  

    onnx_session = onnxruntime.InferenceSession("mobilenet_v2.onnx", providers=['CPUExecutionProvider'])

    input_name=[]
    for node in onnx_session.get_inputs():
        input_name.append(node.name)

    output_name=[]
    for node in onnx_session.get_outputs():
        output_name.append(node.name)

    input_feed={}
    for name in input_name:
        input_feed[name] = input

    pred = onnx_session.run(None, input_feed)
    print(np.argmax(pred))

使用mmdeploy推理:

from mmdeploy.apis import inference_model

model
_cfg = 'mmpretrain/configs/mobilenet_v2/mobilenet-v2_8xb32_in1k.py'    
deploy_cfg = 'mmdeploy/configs/mmpretrain/classification_onnxruntime_static.py'
img = 'goldfish.jpg'
backend_files = ['work_dir/onnx/mobilenet_v2/end2end.onnx']
device = 'cpu'

result = inference_model(model_cfg, deploy_cfg, backend_files, img, device)
print(result)

或者

import cv2
from mmdeploy_runtime import Classifier


img = cv2.imread('goldfish.jpg')
classifier = Classifier(model_path='work_dir/onnx/mobilenet_v2', device_name='cpu')
result = classifier(img)
for label_id, score in result:
    print(label_id, score)

導(dǎo)出engine文件

這里通過trtexec轉(zhuǎn)換onnx文件,LZ的版本是TensorRT-8.2.1.8。

./trtexec.exe --onnx=mobilenet_v2.onnx --saveEngine=mobilenet_v2.engine

tensorrt推理

import cv2
import numpy as np
import tensorrt as trt
import pycuda.autoinit  #負(fù)責(zé)數(shù)據(jù)初始化,內(nèi)存管理,銷毀等
import pycuda.driver as cuda  #GPU CPU之間的數(shù)據(jù)傳輸


if __name__ == '__main__':
    # 創(chuàng)建logger:日志記錄器
    logger = trt.Logger(trt.Logger.WARNING)
    # 創(chuàng)建runtime并反序列化生成engine
    with open("mobilenet_v2.engine", "rb") as f, trt.Runtime(logger) as runtime:
        engine = runtime.deserialize_cuda_engine(f.read())
    context = engine.create_execution_context()
    # 分配CPU鎖頁內(nèi)存和GPU顯存
    h_input = cuda.pagelocked_empty(trt.volume(context.get_binding_shape(0)), dtype=np.float32)
    h_output = cuda.pagelocked_empty(trt.volume(context.get_binding_shape(1)), dtype=np.float32)
    d_input = cuda.mem_alloc(h_input.nbytes)
    d_output = cuda.mem_alloc(h_output.nbytes)
    # 創(chuàng)建cuda流
    stream = cuda.Stream()

    img = cv2.imread('goldfish.jpg')
    if img.shape[0] < img.shape[1]: #h<w
        img = cv2.resize(img, (int(256*img.shape[1]/img.shape[0]), 256))
    else:
        img = cv2.resize(img, (256, int(256*img.shape[0]/img.shape[1])))
    
    crop_size = min(img.shape[0], img.shape[1])
    left = int((img.shape[1]-crop_size)/2)
    top = int((img.shape[0]-crop_size)/2)
    img_crop = img[top:top+crop_size, left:left+crop_size]
    img_crop = cv2.resize(img_crop, (224,224))
    
    img_crop = img_crop[:,:,::-1].transpose(2,0,1).astype(np.float32)  #BGR2RGB和HWC2CHW
    img_crop[0,:] = (img_crop[0,:] - 123.675) / 58.395   
    img_crop[1,:] = (img_crop[1,:] - 116.28) / 57.12
    img_crop[2,:] = (img_crop[2,:] - 103.53) / 57.375
    input = np.expand_dims(img_crop, axis=0)   
    
    np.copyto(h_input, input.ravel())

    # 創(chuàng)建context并進(jìn)行推理
    with engine.create_execution_context() as context:
        # Transfer input data to the GPU.
        cuda.memcpy_htod_async(d_input, h_input, stream)
        # Run inference.
        context.execute_async_v2(bindings=[int(d_input), int(d_output)], stream_handle=stream.handle)
        # Transfer predictions back from the GPU.
        cuda.memcpy_dtoh_async(h_output, d_output, stream)
        # Synchronize the stream
        stream.synchronize()
        # Return the host output. 該數(shù)據(jù)等同于原始模型的輸出數(shù)據(jù)
        pred = np.argmax(h_output)
        print(pred)

使用mmdeploy推理:

from mmdeploy.apis import inference_model


model_cfg = 'mmpretrain/configs/mobilenet_v2/mobilenet-v2_8xb32_in1k.py'
deploy_cfg = 'mmdeploy/configs/mmpretrain/classification_tensorrt_static-224x224.py'
backend_files = ['work_dir/trt/mobilenet_v2/end2end.engine']
img = 'goldfish.jpg'
device = 'cuda'

result = inference_model(model_cfg, deploy_cfg, backend_files, img, device)
print(result)

或者文章來源地址http://www.zghlxwxcb.cn/news/detail-744067.html

import cv2
from mmdeploy_runtime import Classifier


img = cv2.imread('goldfish.jpg')
classifier = Classifier(model_path='work_dir/onnx/mobilenet_v2', device_name='cpu')

result = classifier(img)
for label_id, score in result:
    print(label_id, score)

到了這里,關(guān)于OpenMMlab導(dǎo)出mobilenet-v2的onnx模型并推理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

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

相關(guān)文章

  • 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日
    瀏覽(41)
  • 使用Tools for AI封裝onnx模型并推理

    使用Tools for AI封裝onnx模型并推理

    進(jìn)行這一步之前,請(qǐng)確保已正確安裝配置了Visual Studio 2017 和 Microsoft Visual Studio Tools for AI環(huán)境。 項(xiàng)目的代碼也可以在這里找到,下面的步驟是帶著大家從頭到尾做一遍。 創(chuàng)建Windows窗體應(yīng)用(.NET Framework)項(xiàng)目,這里給項(xiàng)目起名ClassifyBear。 注意,項(xiàng)目路徑不要包含中文。 在解決

    2024年02月20日
    瀏覽(32)
  • onnx模型轉(zhuǎn)engine并進(jìn)行推理全過程解析

    onnx模型轉(zhuǎn)engine并進(jìn)行推理全過程解析

    深度學(xué)習(xí)模型在訓(xùn)練好以后,下一步就是部署到不同的設(shè)備進(jìn)行測(cè)試,不同設(shè)備之間的轉(zhuǎn)換一般可以通過中間件ONNX進(jìn)行轉(zhuǎn)換,以達(dá)到不同平臺(tái)的通用。本文以模型轉(zhuǎn)為ONNX為起點(diǎn),分析介紹ONNX轉(zhuǎn)為TensorRT Engine并進(jìn)行推理的整個(gè)流程鏈路。 ONNX序列化為TRT模型的整個(gè)流程可以用

    2024年02月06日
    瀏覽(103)
  • OpenCV DNN模塊推理YOLOv5 ONNX模型方法

    本文檔主要描述 python 平臺(tái),使用 opencv-python 深度神經(jīng)網(wǎng)絡(luò)模塊 dnn ,推理 YOLOv5 模型的方法。 文檔主要包含以下內(nèi)容: opencv-python 模塊的安裝 YOLOv5 模型格式的說明 ONNX 格式模型的加載 圖片數(shù)據(jù)的預(yù)處理 模型推理 推理結(jié)果后處理,包括 NMS , cxcywh 坐標(biāo)轉(zhuǎn)換為 xyxy 坐標(biāo)等 關(guān)鍵方

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

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

    結(jié)果: current image classification : French bulldog, possible : 16.17 對(duì)兩張圖片同時(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)
  • Mxnet導(dǎo)出onnx模型

    requirements mxnet==1.9.1 python3.8+ onnxsim 導(dǎo)出模型

    2024年01月22日
    瀏覽(18)
  • YOLOX目標(biāo)檢測(cè)實(shí)戰(zhàn):LabVIEW+YOLOX ONNX模型實(shí)現(xiàn)推理檢測(cè)(含源碼)

    YOLOX目標(biāo)檢測(cè)實(shí)戰(zhàn):LabVIEW+YOLOX ONNX模型實(shí)現(xiàn)推理檢測(cè)(含源碼)

    目錄 前言 一、什么是YOLOX 二、環(huán)境搭建 1、部署本項(xiàng)目時(shí)所用環(huán)境: 2、LabVIEW工具包下載及安裝: 三、模型的獲取與轉(zhuǎn)化【推薦方式一】 1、方式一:直接在官網(wǎng)下載yolox的onnx模型 2、方式二:將標(biāo)準(zhǔn)模型pth轉(zhuǎn)化為onnx(較為復(fù)雜) 3、獲取onnx模型總結(jié) 四、LabVIEW實(shí)現(xiàn)YOLOX ONN

    2024年02月15日
    瀏覽(18)
  • Torch 模型 onnx 文件的導(dǎo)出和調(diào)用

    Torch 模型 onnx 文件的導(dǎo)出和調(diào)用

    Open Neural Network Exchange (ONNX,開放神經(jīng)網(wǎng)絡(luò)交換) 格式,是一個(gè)用于表示深度學(xué)習(xí)模型的標(biāo)準(zhǔn),可使模型在不同框架之間進(jìn)行轉(zhuǎn)移 Torch 所定義的模型為動(dòng)態(tài)圖,其前向傳播是由類方法定義和實(shí)現(xiàn)的 但是 Python 代碼的效率是比較底下的,試想把動(dòng)態(tài)圖轉(zhuǎn)化為靜態(tài)圖,模型的推理速

    2024年02月02日
    瀏覽(21)
  • yolov5 pt 模型 導(dǎo)出 onnx

    yolov5 pt 模型 導(dǎo)出 onnx

    在訓(xùn)練好的yolov5 pt 模型 可以 通過 export.py 進(jìn)行導(dǎo)出 onnx 導(dǎo)出流程 在 export.py 設(shè)置模型和數(shù)據(jù)源的yaml 在官方的文檔中 說明了可以導(dǎo)出的具體的類型。 在 --include 添加導(dǎo)出的類型, 不同的 類型的 環(huán)境要求不一樣,建議虛擬環(huán)境,比如onnx 和 openvino 的numpy 版本要求不一只,一

    2024年02月11日
    瀏覽(23)
  • 導(dǎo)出LLaMA等LLM模型為onnx

    導(dǎo)出LLaMA等LLM模型為onnx

    通過onnx模型可以在支持onnx推理的推理引擎上進(jìn)行推理,從而可以將LLM部署在更加廣泛的平臺(tái)上面。此外還可以具有避免pytorch依賴,獲得更好的性能等優(yōu)勢(shì)。 這篇博客(大模型LLaMa及周邊項(xiàng)目(二) - 知乎)進(jìn)行了llama導(dǎo)出onnx的開創(chuàng)性的工作,但是依賴于侵入式修改transform

    2024年02月14日
    瀏覽(35)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包