PyTorch转TensorRT

  • 安装TensorRT
    按照官网的安装教程即可,我装的是TensorRT-6.0.1.5。
  • 安装onnx
sudo apt-get install protobuf-compiler libprotoc-dev
pip install onnx==1.5.0
  • 安装 onnx-tensorrt
git clone --recursive https://github.com/onnx/onnx-tensorrt.git
mkdir build
cd build
cmake .. -DTENSORRT_ROOT=
OR
cmake .. -DTENSORRT_ROOT= -DGPU_ARCHS="61"
make -j8
sudo make install

如果遇到bug, `git checkout v5.0,然后删除build文件夹里的内容重新build.

  • 准备PyTorch model

简单起见,以torch.save(net,’./model_300.pkl’)形式保存

  • PyTorch model 转 ONNX
import torch
​
model = './model_300.pkl'
​
dummy_input = torch.randn(batch_size, 3, 300, 300, device='cuda')
model = torch.load(model)
torch.onnx.export(model, dummy_input,"mymodel.onnx" , verbose=False)
  • 测试ONNX model 是否与PyTorch model 输出一致
    你应该在onnx_tensorrt 目录下
import onnx_tensorrt.backend as backend
import cv2
import onnx
import numpy as np
​
​
model = onnx.load("model_300.onnx")
engine = backend.prepare(model, device='CUDA:0')
path = '../Net/test.jpg'
img = cv2.imread(path)
print(img.shape)
img = cv2.resize(img,(300,300))
img = img.transpose(2,0,1)
img = np.ascontiguousarray(img)
img = img[np.newaxis,:]
print(img.shape)
input_data= img.astype(np.float32)
data =np.random.random(size=(3, 300, 300))
#data = data.transpose(2,0,1)
data = data[np.newaxis,:]
#input_data=data.astype(np.float32)
#input_data = np.random.random(size=(1, 3, 300, 300)).astype(np.float32)
output_data = engine.run(input_data)
print(output_data)
  • ONNX 转 TensorRT engine
onnx2trt model_300.onnx -o my_engine.trt

ref:
https://zhuanlan.zhihu.com/p/74144263

你可能感兴趣的:(pytorch)