NVIDIA TensorRT是一个高性能深度学习推理平台。它包括深度学习推理优化器和运行时,可为深度学习推理应用程序提供低延迟和高吞吐量。推理时,基于TensorRT的应用程序比仅CPU平台的执行速度快40倍。使用TensorRT,您可以优化所有主流框架中训练出的神经网络模型。
简单说,TensorRT是用来优化推理过程,而不是训练过程,这也符合大多数应用场景,因为最终部署到产品中,所做的工作就是推理。别看TensorRT包含了Tensor关键词,实际上它适用于TensorFlow、caffe、PyTorch等所有主流深度学习框架。
在硬件上的需求就是Nvidia的显卡,准确说是支持CUDA的显卡。在前面一篇文章《谷歌GPU云计算平台,免费又好用》中提到过Google Colab的硬件为NVIDIA Tesla T4,支持TensorRT。下面就说说如何在Google Colab上安装TensorRT。
from google.colab import drive
drive.mount('/content/gdrive')
import os
project_path = '/content/gdrive/My Drive/nvidia' #change dir to your project folder
os.chdir(project_path) #change dir
!nvcc --version
!cat /etc/issue
import tensorflow as tf
print(tf.__version__)
输出如下:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:01_CDT_2018
Cuda compilation tools, release 10.0, V10.0.130
Ubuntu 18.04.2 LTS \n \l
1.13.1
可以看到CUDA的版本为10.0,Ubuntu系统版本为18.04,Tensorflow的版本为1.13.1,这些信息有用,在下载TensorRT时,需要下载对应的软件包。
由于nvidia官网下载需要注册、登录等诸多步骤,而且Google云端硬盘的上传速度比较慢,所以我放了一份拷贝到dropbox中,从dropbox下载要快得多:
!wget -O nv-tensorrt-repo-ubuntu1804-cuda10.0-trt5.1.2.2-rc-20190227_1-1_amd64.deb https://www.dropbox.com/s/45pz13r4e8ip4bl/nv-tensorrt-repo-ubuntu1804-cuda10.0-trt5.1.2.2-rc-20190227_1-1_amd64.deb?dl=0
如果直接安装tensorrt,会出现如下错误:
The following packages have unmet dependencies:
tensorrt : Depends: libnvinfer5 (= 5.1.2-1+cuda10.0) but 5.1.2-1+cuda10.1 is to be installed
Depends: libnvinfer-dev (= 5.1.2-1+cuda10.0) but 5.1.2-1+cuda10.1 is to be installed
E: Unable to correct problems, you have held broken packages.
按照 https://devtalk.nvidia.com/default/topic/1049920/trouble-installing-tensorrt-5-1-rc-for-tensorflow-1-13-1/ 上的方法,先安装libnvinfer5=5.1.2-1+cuda10.0和libnvinfer-dev=5.1.2-1+cuda10.0,然后再安装tensorrt:
!dpkg -i nv-tensorrt-repo-ubuntu1804-cuda10.0-trt5.1.2.2-rc-20190227_1-1_amd64.deb
!apt-key add /var/nv-tensorrt-repo-cuda10.0-trt5.1.2.2-rc-20190227/7fa2af80.pub
!apt-get update
!apt-get install -y --no-install-recommends libnvinfer5=5.1.2-1+cuda10.0
!apt-get install -y --no-install-recommends libnvinfer-dev=5.1.2-1+cuda10.0
!apt-get install tensorrt
!apt-get install python3-libnvinfer-dev
!apt-get install uff-converter-tf
!dpkg -l | grep TensorRT
输出如下:
ii graphsurgeon-tf 5.1.2-1+cuda10.0 amd64 GraphSurgeon for TensorRT package
ii libnvinfer-dev 5.1.2-1+cuda10.0 amd64 TensorRT development libraries and headers
ii libnvinfer-samples 5.1.2-1+cuda10.0 all TensorRT samples and documentation
ii libnvinfer5 5.1.2-1+cuda10.0 amd64 TensorRT runtime libraries
ii python3-libnvinfer 5.1.2-1+cuda10.0 amd64 Python 3 bindings for TensorRT
ii python3-libnvinfer-dev 5.1.2-1+cuda10.0 amd64 Python 3 development package for TensorRT
ii tensorrt 5.1.2.2-1+cuda10.0 amd64 Meta package of TensorRT
ii uff-converter-tf 5.1.2-1+cuda10.0 amd64 UFF converter for TensorRT package
至此,TensorRT在Google Colab上安装完毕。
为什么我会写下这篇文章呢?前段时间购入了Jetson Nano,虽然有GPU加持,但毕竟属于边缘计算设备,性能有限。如果能够使用TensorRT加速,自然可以扩大深度学习的应用范围。然而在Jetson Nano上折腾,挺麻烦,还是先在成熟的平台上把TensorRT研究熟,然后再应用到Jetson Nano上面。希望文章对你也有帮助。
你还可以看: