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'
  • 30
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值