当前位置:网站首页>【环境搭建】onnxruntime
【环境搭建】onnxruntime
2022-08-09 09:03:00 【.云哲.】
1,介绍
onnxruntime是一个用于onnx模型推理的引擎。
2,安装
2.1 cuda,cudnn
2.2 cmake,版本>=3.13.0
sudo apt-get install libssl-dev
sudo apt-get autoremove cmake # 卸载
wget https://cmake.org/files/v3.17/cmake-3.17.3.tar.gz
tar -xf cmake-3.17.3
cd cmake-3.17.3
./bootstrap
make -j 8
sudo make install
cmake -version
cmake version 3.17.3
CMake suite maintained and supported by Kitware (kitware.com/cmake).
2.3 tensorrt
2.4 onnxruntime
conda activate py36 # 切换虚拟环境
git clone https://github.com/microsoft/onnxruntime.git
cd onnxruntime
git submodule sync
git submodule update --init --recursive
./build.sh \
--use_cuda \
--cuda_version=11.0 \
--cuda_home=/usr/local/cuda \
--cudnn_home=/usr/local/cuda \
--use_tensorrt --tensorrt_home=$TENSORRT_ROOT \
--build_shared_lib --enable_pybind \
--build_wheel --update --build
pip build/Linux/Debug/dist/onnxruntime_gpu_tensorrt-1.6.0-cp36-cp36m-linux_x86_64.whl
# 获取指定版本
git clone -b v1.6.0 https://github.com/microsoft/onnxruntime.git
cd onnxruntime
git checkout -b v1.6.0
git submodule sync
git submodule update --init --recursive
3,pip 安装
pip install onnxruntime-gpu==1.6.0 onnx==1.9.0 onnxconverter_common==1.6.0 # cuda 10.2
pip install onnxruntime-gpu==1.8.1 onnx==1.9.0 onnxconverter_common==1.8.1 # cuda 11.0
import onnxruntime
onnxruntime.get_available_providers()
sess = onnxruntime.InferenceSession(onnx_path)
sess.set_providers(['CUDAExecutionProvider'], [ {'device_id': 0}])
result = sess.run([output_name], {input_name:x}) # x is input
3,onnxruntime and pytorch inference
# -*- coding: utf-8 -*-
import torch
from torchvision import models
import onnxruntime
import numpy as np
model = models.resnet18(pretrained=True)
model.eval().cuda()
## pytorch
x = torch.rand(2,3,224,224).cuda()
out_pt = model(x)
print(out_pt.size())
# onnx
onnx_path = "resnet18.onnx"
dynamic_axes = {'input': {0: 'batch_size'}}
torch.onnx.export(model,
x,
onnx_path,
export_params=True,
opset_version=11,
do_constant_folding=True,
input_names=['input'],
dynamic_axes=dynamic_axes)
sess = onnxruntime.InferenceSession(onnx_path)
sess.set_providers(['CUDAExecutionProvider'], [ {'device_id': 0}])
input_name = sess.get_inputs()[0].name
output_name = sess.get_outputs()[0].name
# onnxruntime inference
xx = x.cpu().numpy().astype(np.float32)
result = sess.run([output_name], {input_name:xx}) # x is input
print(result[0].shape)
# MSE
mse = np.mean((out_pt.data.cpu().numpy() - result[0]) ** 2)
print(mse)
输出结果
torch.Size([2, 1000])
(2, 1000)
9.275762e-13
边栏推荐
猜你喜欢
随机推荐
电子产品整机结构设计的一般性思路
H5页面px不对,单位不对等问题
关于指针、地址的大小的问题(以及malloc的用法)
ASP.net中的数据库应用
epoll LT和ET 问题总结
PoPW token distribution mechanism may ignite the next bull market
大学四年不努力,出社会后浑浑噩噩深感无力,辞去工作,从头开始
define 可变参数定义
100句话,是否会触动你?
convert转换时间详解
微信小程序获取用户收货地址列表wx.chooseAddress
shell 定时监控并处理脚本
mysql优化——show processlist命令详解
makefile - 学习小结
Anaconda4.8.3介绍、安装及使用教程安装(win10)并修改Jupyter默认工作目录
SQL server中的数据类型
Where does detection go forward?
【CNN】2022 ECCV Oral 自反馈学习的mixup训练框架AutoMix
C#获取网卡地址
使用C语言实现双向链表(带头结点)