pytorch yolov7 模型转tensorRT

TensorRT安装 

下载

下载地址 https://developer.nvidia.com/nvidia-tensorrt-download 

选择需要的版本,点击进入:

 勾选 同意。。。选择具体的版本

选择下载8.4.2.4 版本的tar.gz安装包:

查看下载的安装包:

 安装

解压安装包:

tar -xzvf  TensorRT-8.4.2.4.Linux.x86_64-gnu.cuda-11.6.cudnn8.4.tar.gz

 设置环境变量:vi ~/.bashrc,写入下面设置

export LD_LIBRARY_PATH=/path/TensorRT-8.4.2.4/lib:$LD_LIBRARY_PATH
export LIBRARY_PATH=/path/TensorRT-8.4.2.4/lib::$LIBRARY_PATH

source ~/.bashrc

验证TensorRT是否安装正确,cd进入samples文件夹:

cd samples

make -j8

如果环境什么都没出错的话,这里编译是会很顺利的。然后cd进入bin文件夹,

cd ..
cd bin
./sample_onnx_mnist

安装python包(可选)

cd /path/TensorRT-8.4.2.4/python

 根据自己需要安装对应python的安装包, 例如:

pip install tensorrt-8.4.2.4-cp37-none-linux_x86_64.whl

还需要安装pycuda

pip install pycuda==2019.1.1

检查python中tensorrt是否安装成功

>>> import tensorrt as tr
>>> tr.__version__
'8.4.2.4'

模型转换

pt -> onnx

安装依赖包:

pip install onnx==1.14.0

pip install nvidia-pyindex

pip install onnx_graphsurgeon

pip install coremltools

转换

git clone https://github.com/WongKinYiu/yolov7.git

cd yolov7

# 640
python export.py --weights yolov7-tiny.pt  --dynamic  --grid
python export.py --weights yolov7.pt  --dynamic  --grid
python export.py --weights yolov7x.pt  --dynamic  --grid
# 1280
python export.py --weights yolov7-w6.pt  --dynamic  --grid --img-size 1280

onnx - > trt

git clone https://github.com/FeiYull/TensorRT-Alpha.git

cd TensorRT-Alpha/data/yolov7

# 640
/path/TensorRT-8.4.2.4/bin/trtexec    --onnx=yolov7-tiny.onnx  --saveEngine=yolov7-tiny.trt  --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640
/path/TensorRT-8.4.2.4/bin/trtexec    --onnx=yolov7.onnx   	--saveEngine=yolov7.trt       --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640
/path/TensorRT-8.4.2.4/bin/trtexec    --onnx=yolov7x.onnx   	--saveEngine=yolov7x.trt      --buildOnly --minShapes=images:1x3x640x640 --optShapes=images:4x3x640x640 --maxShapes=images:8x3x640x640
# 1280
/path/TensorRT-8.4.2.4/bin/trtexec    --onnx=yolov7-w6.onnx    --saveEngine=yolov7-w6.trt    --buildOnly --minShapes=images:1x3x1280x1280 --optShapes=images:4x3x1280x1280 --maxShapes=images:8x3x1280x1280

# note:if report an error(Error Code 1: Cuda Runtime (an illegal memory access was encountered "bool context = m_context->executeV2((void**)bindings)" returns false) 
when running the model(yolov7-w6), just lower the batch_size.

注意:若是报错so包找不到:

cd /path/TensorRT-8.4.2.4/bin/

ldd trtexec

查看so是否没有找不到,若so包正常,说明export环境变量没生效,请 source ~/.bashrc,或者手动执行

export LD_LIBRARY_PATH=/path/TensorRT-8.4.2.4/lib:$LD_LIBRARY_PATH
export LIBRARY_PATH=/path/TensorRT-8.4.2.4/lib::$LIBRARY_PATH

转换成功

 deepstream安装

网页资源  

  1. NVIDIA DeepStream SDK Developer Guide
  2. DEEPSTREAM API REFERENCE
  3. NVIDIA DeepStream SDK API Reference

docker下载

 docker pull nvcr.io/nvidia/deepstream:6.1-devel

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YOLOv5 PyTorch模型换为TensorRT模型需要以下步骤: 1. 安装TensorRTPyTorch。 2. 下载并安装yolov5。 3. 使用PyTorchyolov5模型换为ONNX格式。 ``` python models/export.py --weights yolov5s.pt --img 640 --batch 1 --include onnx # yolov5s ``` 4. 安装ONNX-TensorRT。 ``` git clone https://github.com/onnx/onnx-tensorrt.git cd onnx-tensorrt git submodule update --init --recursive mkdir build && cd build cmake .. -DTENSORRT_ROOT=/path/to/tensorrt -DCMAKE_CXX_COMPILER=g++-7 make -j sudo make install ``` 5. 使用ONNX-TensorRT将ONNX模型换为TensorRT模型。 ``` import onnx import onnx_tensorrt.backend as backend model = onnx.load("yolov5s.onnx") # Load the ONNX model engine = backend.prepare(model, device="CUDA:0") # Prepare the TensorRT model with open("yolov5s.engine", "wb") as f: # Serialize the TensorRT engine f.write(engine.serialize()) ``` 6. 测试TensorRT模型的性能和准确性。 ``` import pycuda.driver as cuda import pycuda.autoinit import numpy as np import time # Load the TensorRT engine with open("yolov5s.engine", "rb") as f: engine = cuda.Context().deserialize_cuda_engine(f.read()) # Create the TensorRT inference context context = engine.create_execution_context() # Allocate the input and output buffers input_shape = engine.get_binding_shape(0) output_shape = engine.get_binding_shape(1) input_buffer = cuda.mem_alloc(np.prod(input_shape) * 4) output_buffer = cuda.mem_alloc(np.prod(output_shape) * 4) # Prepare the input data input_data = np.random.rand(*input_shape).astype(np.float32) # Copy the input data to the input buffer cuda.memcpy_htod(input_buffer, input_data) # Run inference start_time = time.time() context.execute_v2(bindings=[int(input_buffer), int(output_buffer)]) end_time = time.time() # Copy the output data to the output buffer output_data = np.empty(output_shape, dtype=np.float32) cuda.memcpy_dtoh(output_data, output_buffer) # Print the inference time and output data print("Inference time: {} ms".format((end_time - start_time) * 1000)) print("Output shape: {}".format(output_shape)) print("Output data: {}".format(output_data)) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值