tensorrt安装onnx安装

前期准备

在安装tensorrt之前,cuda和cudnn 以及显卡驱动都需要提前安装好,可参考如下:

安装驱动:服务器安装系统ubuntu16.04_森林盲点的博客-CSDN博客

cuda和cudnn安装:cuda、cudnnn安装、卸载、查询_森林盲点的博客-CSDN博客

tensorrt下载与安装

TensorRT-7.0.0.11.Ubuntu-16.04.x86_64-gnu.cuda-10.0.cudnn7.6.tar.gz

下载地址:tensorrt下载地址

tar -xzvf TensorRT-XXX.tar.gz

$ vim ~/.bashrc # 打开环境变量文件
# 将下面三个环境变量写入环境变量文件并保存
export LD_LIBRARY_PATH=TensorRT解压路径/lib:$LD_LIBRARY_PATH
# 使刚刚修改的环境变量文件生效
$ source ~/.bashrc

进入tensorRT目录下的Python目录

# 对于python2
$ sudo pip2 install tensorrt-XXX-cp27-cp27mu-linux_x86_64.whl
# 对于python3
$ sudo pip3 install tensorrt-XXX-cp37-cp37m-linux_x86_64.whl

遇到的问题:

1、ImportError: libnvinfer.so.7: cannot open shared object file: No such file or directory

检查bashrc中添加的tensorrt的lib路径是否填写正确;

2、在执行命令:python -m onnxsim retinate_hat_hair_beard.onnx retinate_hat_hair_beard_sim.onnx报错ModuleNotFoundError: No module named ‘onnx.optimizer‘

在使用 onnx-simplifier 简化onnx模型中一些多余的操作算子时,是因为onnx 的版本太新,onnx.optimizer 已经从onnx 中分离了出来,成了一个单独的项目。

目前的版本如下,可正常运行:

  • onnx 1.7.0
  • onnx-simplifer 0.2.16

安装onnx

参考文献

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
TensorRT是NVIDIA深度学习推理库,可以用于加速神经网络模型的推理,包括 ONNX 模型。下面是使用 TensorRT 加速 ONNX 模型的一些步骤: 1. 安装 TensorRT:可以从 NVIDIA 官网下载安装 TensorRT 的相应版本。 2. 将 ONNX 模型转换为 TensorRT 引擎:使用 TensorRTONNX Parser 将 ONNX 模型转换为 TensorRT 引擎。这可以通过以下代码实现: ```python import tensorrt as trt import onnx # Load the ONNX model as a graph and prepare the TensorRT inference engine onnx_model = onnx.load('model.onnx') onnx.checker.check_model(onnx_model) trt_engine = trt.utils.\ onnx_to_trt_engine(onnx_model, max_batch_size=1, max_workspace_size=1 << 28, precision_mode="FP16", minimum_segment_size=2) ``` 3. 创建 TensorRT 推理引擎:创建 TensorRT 推理引擎实例,并为其分配输入和输出张量的内存。这可以通过以下代码实现: ```python # Create a TensorRT inference engine trt_logger = trt.Logger(trt.Logger.WARNING) trt_runtime = trt.Runtime(trt_logger) trt_context = trt_engine.create_execution_context() # Allocate memory for inputs and outputs input_shape = trt_engine.get_binding_shape(0) output_shape = trt_engine.get_binding_shape(1) input_size = trt.volume(input_shape) * trt_engine.max_batch_size * np.dtype(np.float32).itemsize output_size = trt.volume(output_shape) * trt_engine.max_batch_size * np.dtype(np.float32).itemsize # Allocate device memory d_input = cuda.mem_alloc(input_size) d_output = cuda.mem_alloc(output_size) ``` 4. 执行推理:将输入数据复制到设备内存,执行推理,然后将输出数据从设备内存复制回主机内存。这可以通过以下代码实现: ```python # Copy input data to device memory cuda.memcpy_htod(d_input, input_data) # Execute the inference trt_context.execute_v2(bindings=[int(d_input), int(d_output)]) # Copy output data from device memory output_data = np.empty(output_shape, dtype=np.float32) cuda.memcpy_dtoh(output_data, d_output) ``` 这些步骤可以帮助你使用 TensorRT 加速 ONNX 模型的推理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值