2021-03-10 安装tensorRT

首先查看当前虚拟环境

conda env list
当前环境

搜索python版本

conda search python
显示结果

创建新的虚拟环境,并定义python版本为3.8

conda create -n tensorflow-gpu_py38 python=3.8

切换进入 tensorflow-gpu_py38 环境

conda activate tensorflow-gpu_py38
查看当前环境包

安装 tensorflow-gpu,不指定具体版本由环境自动决定最高兼容版本

conda install tensorflow-gpu
自动安装版本 tensorflow-gpu = 2.4.1

自动安装cudnn=7.6.5.32 cudatoolkit=10.1.243

卸载旧版本cudatoolkit (cudnn、tensorflow-gpu等被自动卸载)

conda remove cudatoolkit

安装指定新版本(搜索得出)

conda install cudatoolkit==11.1.1
conda install cudnn==8.1.0.77

再次安装 tensorflow-gpu,但是不兼容,无法正确安装

conda install tensorflow-gpu

TensorRT官网下载链接(https://developer.nvidia.com/nvidia-tensorrt-7x-download
TensorRT-7.2.3.4.Ubuntu-18.04.x86_64-gnu.cuda-11.1.cudnn8.1.tar.gz
解压之后得到TensorRT-7.2.3.4目录

pip install /home/sichange-ai/AI_TOOLS/CUDA/tensorRT/TensorRT-7.2.3.4/python/tensorrt-7.2.3.4-cp38-none-linux_x86_64.whl
安装成功

在终端执行添加环境变量,解决找不到"libcudnn.so.7"等共享库问题

export LD_LIBRARY_PATH=/home/sichange-ai/AI_TOOLS/CUDA/tensorRT/TensorRT-7.2.3.4/lib:/home/sichange-ai/aitools/anaconda3/envs/tensorflow-gpu_py38/lib

进入python,执行import tensorrt,无报错信息


执行成功
>>> import tensorrt
>>> print(tensorrt.__version__)
7.2.3.4
>>> assert tensorrt.Builder(tensorrt.Logger())

总结

1、打开终端进入环境

conda activate tensorflow-gpu_py38

2、更改环境变量

export LD_LIBRARY_PATH=/home/sichange-ai/AI_TOOLS/CUDA/tensorRT/TensorRT-7.2.3.4/lib:/home/sichange-ai/aitools/anaconda3/envs/tensorflow-gpu_py38/lib

3、进入python

python

4、执行

import tensorrt
无错误信息
>>> import tensorrt
>>> print(tensorrt.__version__)
7.2.3.4
>>> assert tensorrt.Builder(tensorrt.Logger())

参考资料(https://docs.nvidia.com/deeplearning/tensorrt/archives/tensorrt-723/quick-start-guide/index.html

推荐阅读更多精彩内容