注意一定要版本匹配,安装环境windows10+cuda11.1+cudnn11+torch1.7
关于windows10安装cuda11.1+cudnn11+torch1.7请参考我的博客https://blog.csdn.net/Douzaikongcheng/article/details/109945656
TensorRT 官网下载: https://developer.nvidia.com/nvidia-tensorrt-7x-download
下载的文件为:TensorRT-7.1.3.4.Windows10.x86_64.cuda-11.0.cudnn8.0.zip
在下载的tensorrt文件夹里面找到以下两个whl文件并在cmd安装
安装命令:
pip install D:\tensorrt\TensorRT-7.1.3.4\graphsurgeon\graphsurgeon-0.4.5-py2.py3-none-any.whl
pip install D:\tensorrt\TensorRT-7.1.3.4\uff\uff-0.6.9-py2.py3-none-any.whl
例如cuda的安装目录为:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\bin,就将以上几个文件复制到这个路径下
用vs2015双击打开TensorRT-7.1.3.4\samples\sampleMNISTsample_mnist.sln, 然后设置编译环境
配置属性->常规
添加库:D:\tensorrt\TensorRT-7.1.3.4\lib
添加目录:D:\tensorrt\TensorRT-7.1.3.4\bin
编译出错
修改目标平台版本
再次编译出现报错:
Severity Code Description Project File Line Source Suppression State Error C1083 Cannot open include file: 'cuda_runtime_api.h': No such file or directory sample_mnist d:\tensorrt\tensorrt-7.1.3.4\samples\common\common.h 38 Build
找到cuda_runtime_api.h然后添加路径C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\include
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\include\cuda_runtime_api.h
再次编译出现报错:
Severity Code Description Project File Line Source Suppression State Error LNK1181 cannot open input file 'cudnn.lib' sample_mnist D:\tensorrt\TensorRT-7.1.3.4\samples\sampleMNIST\LINK 1 Build
添加路径
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\lib\x64\cudnn.lib C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\lib\x64\*.lib
记得点ok和确定
编译时出现三个错误
Severity Code Description Project File Line Source Suppression State Error LNK1181 cannot open input file 'cudnn.lib' sample_mnist D:\tensorrt\TensorRT-7.1.3.4\samples\sampleMNIST\LINK 1 Build Severity Code Description Project File Line Source Suppression State Error LNK1181 cannot open input file 'cublas.lib' sample_mnist D:\tensorrt\TensorRT-7.1.3.4\samples\sampleMNIST\LINK 1 Build Severity Code Description Project File Line Source Suppression State Error LNK1181 cannot open input file 'cudart.lib' sample_mnist D:\tensorrt\TensorRT-7.1.3.4\samples\sampleMNIST\LINK 1 Build
按照下面方法解决
将nvinfer.lib、nvinfer_plugin.lib、nvonnxparser.lib和nvparsers.lib加入链接器–>输入–>附加依赖项
再次编译
编译成功且看到可执行文件的输出路径
sample_mnist.vcxproj -> D:\tensorrt\TensorRT-7.1.3.4\samples\sampleMNIST\\..\..\bin\sample_mnist.exe ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
./sample_mnist [-h] [--datadir=/path/to/data/dir/] [--useDLA=N] [--fp16 or --int8]
查找到nvrtc64_110_0.dll然后将nvrtc64_110_0.dll放在文件夹下C:\Windows\System32(当运行程序时提示找不到某个文件,首先查看这个文件有没有,如果有说明路径不对,复制文件到正确的路径或者添加环境变量。)
nvrtc: error: invalid value for --gpu-architecture (-arch)
尝试重新编译之后还是这个报错
C:\Users\Y>D:\tensorrt\TensorRT-7.1.3.4\bin\sample_mnist.exe &&&& RUNNING TensorRT.sample_mnist # D:\tensorrt\TensorRT-7.1.3.4\bin\sample_mnist.exe Could not find mnist.prototxt in data directories: data/mnist/ data/samples/mnist/ &&&& FAILED
加上选项参数,在cmd里运行
C:\Users\Y>D:\tensorrt\TensorRT-7.1.3.4\bin\sample_mnist.exe --datadir D:\tensorrt\TensorRT-7.1.3.4\data\mnist &&&& RUNNING TensorRT.sample_mnist # D:\tensorrt\TensorRT-7.1.3.4\data\mnist\sample_mnist.exe --datadir D:\tensorrt\TensorRT-7.1.3.4\data\mnist [04/15/2021-22:54:18] [I] Building and running a GPU inference engine for MNIST
nvrtc: error: invalid value for --gpu-architecture (-arch)
CUDA 11.0 支持 sm_80
. 但是 CUDA 11.0中的nvrtc 不支持compute_80
.
解决方案
参考链接https://zhuanlan.zhihu.com/p/267757590
pip install torch==1.7.1+cu110 torchvision==0.8.2+cu110 torchaudio===0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
cuda版本为11.1,可能是cuda版本不匹配
C:\Users\Y>nvcc -V nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2020 NVIDIA Corporation Built on Mon_Oct_12_20:54:10_Pacific_Daylight_Time_2020 Cuda compilation tools, release 11.1, V11.1.105 Build cuda_11.1.relgpu_drvr455TC455_06.29190527_0
重新下载tensorrt
重新走一下刚才的安装步骤
再次编译验证
D:\tensorrt\TensorRT-7.2.3.4\bin\sample_mnist.exe --datadir D:\tensorrt\TensorRT-7.1.3.4\data\mnist
结果:Could not find 0.pgm in data directories:
C:\Users\USER>D:\tensorrt\TensorRT-7.2.3.4\bin\sample_mnist.exe --datadir D:\tensorrt\TensorRT-7.1.3.4\data\mnist &&&& RUNNING TensorRT.sample_mnist # D:\tensorrt\TensorRT-7.2.3.4\bin\sample_mnist.exe --datadir D:\tensorrt\TensorRT-7.1.3.4\data\mnist [04/15/2021-23:24:13] [I] Building and running a GPU inference engine for MNIST [04/15/2021-23:24:20] [W] [TRT] TensorRT was linked against cuDNN 8.1.0 but loaded cuDNN 8.0.4 [04/15/2021-23:24:35] [I] [TRT] Some tactics do not have sufficient workspace memory to run. Increasing workspace size may increase performance, please check verbose output. [04/15/2021-23:24:35] [I] [TRT] Detected 1 inputs and 1 output network tensors. [04/15/2021-23:24:35] [W] [TRT] TensorRT was linked against cuDNN 8.1.0 but loaded cuDNN 8.0.4 [04/15/2021-23:24:35] [W] [TRT] TensorRT was linked against cuDNN 8.1.0 but loaded cuDNN 8.0.4 Could not find 0.pgm in data directories: D:\tensorrt\TensorRT-7.1.3.4\data\mnist &&&& FAILED
python D:\tensorrt\TensorRT-7.1.3.4\data\mnist\download_pgms.py
尝试修改download_pgms.py文件失败
with urllib.request.urlopen("http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz") as res: data = load_mnist_data(gzip.decompress(res.read())) with urllib.request.urlopen("http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz") as res: labels = load_mnist_labels(gzip.decompress(res.read()))
D:/tensorrt/TensorRT-7.1.3.4/data/mnist/MNIST/raw/
输入Y显示中止,输入回车成功
MNIST数据一直下载不成功,而且从tensorrt7之后不提供数据,但是之前的版本有数据,可以下载tensorrt5
复制TensorRT-5.1.5.0中的mnist文件夹到桌面,名为mnist
复制\TensorRT-7.2.3.4\data\mnist中的文件到mnist(上一步的mnist),注意替换掉原来的文件
用桌面的mnist文件夹替换\TensorRT-7.2.3.4\data\mnist的mnist
D:\tensorrt\TensorRT-7.2.3.4\bin\sample_mnist.exe --datadir D:\tensorrt\TensorRT-7.2.3.4\data\mnist
C:\Users\USER>D:\tensorrt\TensorRT-7.2.3.4\bin\sample_mnist.exe --datadir D:\tensorrt\TensorRT-7.2.3.4\data\mnist &&&& RUNNING TensorRT.sample_mnist # D:\tensorrt\TensorRT-7.2.3.4\bin\sample_mnist.exe --datadir D:\tensorrt\TensorRT-7.2.3.4\data\mnist [04/16/2021-10:44:54] [I] Building and running a GPU inference engine for MNIST [04/16/2021-10:45:01] [W] [TRT] TensorRT was linked against cuDNN 8.1.0 but loaded cuDNN 8.0.4 [04/16/2021-10:45:16] [I] [TRT] Some tactics do not have sufficient workspace memory to run. Increasing workspace size may increase performance, please check verbose output. [04/16/2021-10:45:17] [I] [TRT] Detected 1 inputs and 1 output network tensors. [04/16/2021-10:45:17] [W] [TRT] TensorRT was linked against cuDNN 8.1.0 but loaded cuDNN 8.0.4 [04/16/2021-10:45:17] [W] [TRT] TensorRT was linked against cuDNN 8.1.0 but loaded cuDNN 8.0.4 [04/16/2021-10:45:17] [I] Input: @@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@+ :@@@@@@@@ @@@@@@@@@@@@@@%= :. --%@@@@@ @@@@@@@@@@@@@%. -@= - :@@@@@ @@@@@@@@@@@@@: -@@#%@@ #@@@@ @@@@@@@@@@@@: #@@@@@@@-#@@@@ @@@@@@@@@@@= #@@@@@@@@=%@@@@ @@@@@@@@@@= #@@@@@@@@@:@@@@@ @@@@@@@@@+ -@@@@@@@@@%.@@@@@ @@@@@@@@@::@@@@@@@@@@+-@@@@@ @@@@@@@@-.%@@@@@@@@@@.*@@@@@ @@@@@@@@ *@@@@@@@@@@@ *@@@@@ @@@@@@@% %@@@@@@@@@%.-@@@@@@ @@@@@@@:*@@@@@@@@@+. %@@@@@@ @@@@@@# @@@@@@@@@# .*@@@@@@@ @@@@@@# @@@@@@@@= +@@@@@@@@ @@@@@@# @@@@@@%. .+@@@@@@@@@ @@@@@@# @@@@@*. -%@@@@@@@@@@ @@@@@@# --- =@@@@@@@@@@@@ @@@@@@# *%@@@@@@@@@@@@@ @@@@@@@%: -=%@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@ [04/16/2021-10:45:17] [I] Output: 0: ********** 1: 2: 3: 4: 5: 6: 7: 8: 9: &&&& PASSED TensorRT.sample_mnist # D:\tensorrt\TensorRT-7.2.3.4\bin\sample_mnist.exe --datadir D:\tensorrt\TensorRT-7.2.3.4\data\mnist
或者双击sample_mnist.exe都可以
https://blog.csdn.net/yangzzguang/article/details/85570663
https://blog.csdn.net/shanglianlm/article/details/92847719
https://www.pythonf.cn/read/64495
清华大学开源软件镜像站
https://mirrors.tuna.tsinghua.edu.cn/