Google Colab 上安装 TensorRT

栏目: 数据库 · 发布时间: 6年前

内容简介:题图摄于上海NVIDIA TensorRT是一个高性能深度学习推理平台。它包括深度学习推理优化器和运行时,可为深度学习推理应用程序提供低延迟和高吞吐量。推理时,基于TensorRT的应用程序比仅CPU平台的执行速度快40倍。使用TensorRT,您可以优化所有主流框架中训练出的神经网络模型。

Google Colab 上安装 TensorRT

题图摄于上海

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')
  • 切换当前目录为Google云端硬盘的项目文件夹:

import os

project_path = '/content/gdrive/My Drive/nvidia'  #change dir to your project folder
os.chdir(project_path)  #change dir
  • 确定CUDA,Ubuntu,TensorFlow的版本:

!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时,需要下载对应的软件包。

  • 下载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.

按照网上提供的方法,先安装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上面。

希望文章对你也有帮助。

你还可以看:


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

An Introduction to the Analysis of Algorithms

An Introduction to the Analysis of Algorithms

Robert Sedgewick、Philippe Flajolet / Addison-Wesley Professional / 1995-12-10 / CAD 67.99

This book is a thorough overview of the primary techniques and models used in the mathematical analysis of algorithms. The first half of the book draws upon classical mathematical material from discre......一起来看看 《An Introduction to the Analysis of Algorithms》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具