trt | torch2trt的使用方式

一、安装

1. 安装 tensorrt python 接口
  • 下载 trt 包 .tar.gz
https://developer.nvidia.com/nvidia-tensorrt-5x-download
  • 解压
tar xvf TensorRT-6.0.1.5.Ubuntu-18.04.x86_64-gnu.cuda-10.1.cudnn7.6.tar.gz
  • 安装 trt python 接口
cd python

pip install tensorrt-6.0.1.5-cp37-none-linux_x86_64.whl
  • 安装 uff
cd uff

pip install uff-0.6.5-py2.py3-none-any.whl
  • 验证trt是否安装成功
python

import tensorrt
2. 安装 torch2trt
sudo apt-get install libprotobuf* protobuf-compiler ninja-build

git clone https://github.com/NVIDIA-AI-IOT/torch2trt

cd torch2trt

sudo python setup.py install --plugins

二、代码演示

model = BNNproAtt()
model.load_state_dict(torch.load('/src/2_toeng/pytorch_2_eng/reid2trt/BNNproAtt0525.pt', map_location = 'cpu')
model.eval().float().cuda()

input_data = torch.rand((4, 3, 384, 128), dtype = torch.float).cuda()

t0 = time.time()
out = model(input_data)
t1 = time.time()
print("pytorch costed time: ", t1 - t0)

# convert to TensorRT model
model_trt = torch2trt(model, [input_data], max_batch_size = 4, int8_mode = True)

t2 = time.time()
out_trt = model_trt(input_data)
t3 = time.time()
print("trt costed time: ", t3 - t2)

# check the output against pytorch
print(torch.max(torch.abs(out - out_trt)

torch.save(model_trt.state_dict(), '/src/2_toeng/pytroch_2_eng/reid2trt/bnn_trt_int8.pt'

相关推荐

  1. trt | torch2trt使用方式

    2024-03-24 18:46:01       19 阅读
  2. Torch2TRT编译和使用踩坑

    2024-03-24 18:46:01       47 阅读
  3. 0053__Linux----tr命令详细使用方法

    2024-03-24 18:46:01       8 阅读
  4. ros2/ros1中cmakelists.txt文件解释

    2024-03-24 18:46:01       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-24 18:46:01       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-24 18:46:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-24 18:46:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-24 18:46:01       18 阅读

热门阅读

  1. P7480 Reboot from Blue 线段树优化建图跑最短路

    2024-03-24 18:46:01       17 阅读
  2. 和neo4j相似的python工具

    2024-03-24 18:46:01       18 阅读
  3. 【2024-03-20】华为春招笔试三道编程题解

    2024-03-24 18:46:01       16 阅读
  4. C# System.Console.WriteLine的格式化输出

    2024-03-24 18:46:01       16 阅读
  5. C#面:简述可空类型

    2024-03-24 18:46:01       17 阅读
  6. 【2024-03-18】顺丰春招笔试两道编程题解

    2024-03-24 18:46:01       19 阅读
  7. 【串口开发】android 智能设备开发 知识笔记

    2024-03-24 18:46:01       20 阅读
  8. 学习笔记 | 微信小程序项目day06

    2024-03-24 18:46:01       22 阅读
  9. mysql基础02

    2024-03-24 18:46:01       15 阅读