.
其实安装 tensorflow-gpu, CUDA, cnDNN 这些东西没有什么难度的,但是就是会遇到版本匹配问题,很浪费时间。
所以把安装方法详细介绍一遍,希望能帮到大家。
各个软件版本如下:
首先,检测一下显卡是否支持CUDA。
CUDA 是 Nvidia 显卡特有的,AMD 显卡的类似功能叫做VEGA。
下载 GPU-Z 软件,就可以看到显卡是否支持CUDA。
看到 CUDA 选择前面打了勾就表示是支持 CUDA 的。
由于后续我们要安装 CUDA,为了支持最新版本的 CUDA,需要把显卡驱动升级到最新。
前面用 GPU-Z 看到了显卡的型号,我们就根据这个型号去 Nvidia 官网下载对应的驱动即可。
Nvidia 驱动官网:https://www.nvidia.com/Download/index.aspx
在上面的页面中,根据提示选择自己的显卡型号,我的是 750 Ti。然后点击“Search”。
接着出现显示驱动的详细信息,点击“DOWNLOAD"即可。
驱动文件下载后如下:
右键“以管理员身份运行”,解压后,进入安装程序,如下:
点击“同意并继续”,
点击“下一步”,开始安装,期间会黑屏一下。
安装完显卡驱动之后,建议重新计算机。
该工具包让你可以创建高性能的GPU加速应用程序。
有两种安装方式:在线安装和离线安装,建议选择离线安装,因为安装文件有 2G+,使用P2P软件下载比较快。
CUDA 下载地址:https://developer.nvidia.com/cuda-downloads (可能需要VPN,下面给出P2P链接)
如上图,根据自己的配置进行下载即可,
Windows - 64位 - CUDA 10 的地址是(直接拷贝链接然后打开P2P软件下载):
http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda_10.2.89_441.22_win10.exe
离线安装文件如下:
右击安装文件,选择“以管理员身份运行”,
指定解压路径,然后点击“OK",
解压完成后,自动打开安装界面,如下:
点击“下一步”,开始安装
安装完成~
右击桌面“此电脑” - “属性” - “高级系统设置” - “环境变量”,
可以看到,在安装完 CUDA 之后,自动添加了几个变量,不需要我们动手:
查看 Path 变量:
可以看到 CUDA 的路径也添加到系统环境变量当中了:
打开命令行,输入以下命令:
C:\WINDOWS\system32> nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2019 NVIDIA Corporation
Built on Wed_Oct_23_19:32:27_Pacific_Daylight_Time_2019
Cuda compilation tools, release 10.2, V10.2.89
看到上面输出内容即表示 CUDA 安装成功。
Nvidia cuDNN是用于深度神经网络的GPU加速库。
cvDNN 与 CUDA 的版本是有关联的,具体见下文。
cuDNN下载地址:https://developer.nvidia.com/rdp/cudnn-archive
根据自己安装的 CUDA 的版本,下载对应的 cuDNN的版本即可。
例如:Windows 10 - 64位 - cuDNN v7.6.4 下载地址(直接拷贝链接然后打开P2P软件下载):
https://developer.nvidia.com/compute/machine-learning/cudnn/secure/7.6.4.38/Production/10.1_20190923/cudnn-10.1-windows10-x64-v7.6.4.38.zip
安装文件如下:
无须安装,解压文件即可:
解压完成之后,把cuDNN 的内容复制到 CUDA 目录下:
把 CUDA\lib\x64 添加到环境变量中,如下图所示:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\lib\x64
TensorFlow 是一个开源软件库,用于使用数据流图进行数值计算,它让 机器学习变得更快更简单。tensorflow-gpu 是 GPU 版本的 TensorFlow。
安装好 Python 3.7 + pip 之后,直接使用 pip 命令安装即可:
pip install tensorflow-gpu == 2.1
看到以下输出则表示安装成功:
Successfully installed astor-0.8.1 gast-0.2.2 keras-applications-1.0.8 tensorboard-2.0.2 tensorflow-estimator-2.1.1 tensorflow-gpu-2.1.0
进入 python 交互命令窗口:
>>> import tensorflow as tf
2020-06-03 00:24:17.960848: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
>>> print(tf.config.list_physical_devices('GPU'))
2020-06-03 00:24:33.659128: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-06-03 00:24:33.687434: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1555] Found device 0 with properties:
pciBusID: 0000:05:00.0 name: GeForce GTX 750 Ti computeCapability: 5.0
coreClock: 1.0845GHz coreCount: 5 deviceMemorySize: 2.00GiB deviceMemoryBandwidth: 80.47GiB/s
2020-06-03 00:24:33.693089: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
2020-06-03 00:24:33.702776: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_10.dll
2020-06-03 00:24:33.711254: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_10.dll
2020-06-03 00:24:33.716824: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_10.dll
2020-06-03 00:24:33.727397: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2020-06-03 00:24:33.735731: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2020-06-03 00:24:33.751438: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-06-03 00:24:33.756007: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1697] Adding visible gpu devices: 0
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
安装了 tensorflow 2.0 + CUDA 10.2, 在 " import tensorflow as tf " 的时候出现错误:
Could not load dynamic library 'cudart64_100.dll'
错误原因,tensorflow 2.0 依赖的是 CUDA 10.0,所以它会去尝试加载 " cudart64_100.dll ",这个动态库的命名形式为:
cudart64_vesion.dll
* cudart64_100.dll
* cudart64_101.dll
* cudart64_102.dll
对应关系:
所以,我们只需要下载 " cudart64_100.dll " 文件,然后把它放到 CUDA 安装目录的 " bin " 目录下即可。这样 tensorflow 就可以找到这个动态库来加载了。
文件下载地址:https://download.zip.dll-files.com/f0ab3cac7e90959a38e97b262ebdf3f2/cudart64_100.zip?token=5i1TJlOi2yZ1FtWLhoghkw&expires=1591158929
下载后放到以下目录下:
安装 tensorflow==2.1 或 2.2 ,CUDA = 10.2 ,此时可能会报以下错误:
>>> import tensorflow as tf
Traceback (most recent call last):
File "D:\Program Files\Python37\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in
from tensorflow.python.pywrap_tensorflow_internal import *
File "D:\Program Files\Python37\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in
_pywrap_tensorflow_internal = swig_import_helper()
File "D:\Program Files\Python37\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "D:\Program Files\Python37\lib\imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
File "D:\Program Files\Python37\lib\imp.py", line 342, in load_dynamic
return _load(spec)
ImportError: DLL load failed: 找不到指定的模块。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "", line 1, in
File "D:\Program Files\Python37\lib\site-packages\tensorflow\__init__.py", line 101, in
from tensorflow_core import *
File "D:\Program Files\Python37\lib\site-packages\tensorflow_core\__init__.py", line 40, in
from tensorflow.python.tools import module_util as _module_util
File "D:\Program Files\Python37\lib\site-packages\tensorflow\__init__.py", line 50, in __getattr__
module = self._load()
File "D:\Program Files\Python37\lib\site-packages\tensorflow\__init__.py", line 44, in _load
module = _importlib.import_module(self.__name__)
File "D:\Program Files\Python37\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "D:\Program Files\Python37\lib\site-packages\tensorflow_core\python\__init__.py", line 49, in
from tensorflow.python import pywrap_tensorflow
File "D:\Program Files\Python37\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 74, in
raise ImportError(msg)
ImportError: Traceback (most recent call last):
File "D:\Program Files\Python37\lib\site-packages\tensorflow_core\python\pywrap_tensorflow.py", line 58, in
from tensorflow.python.pywrap_tensorflow_internal import *
File "D:\Program Files\Python37\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 28, in
_pywrap_tensorflow_internal = swig_import_helper()
File "D:\Program Files\Python37\lib\site-packages\tensorflow_core\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
File "D:\Program Files\Python37\lib\imp.py", line 242, in load_module
return load_dynamic(name, filename, file)
File "D:\Program Files\Python37\lib\imp.py", line 342, in load_dynamic
return _load(spec)
ImportError: DLL load failed: 找不到指定的模块。
解决方法是:
方法二详解:
下载 Visual Studio 最新的 Redistributable。
下载地址 :https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads
下载 x64,下载完成后进行安装。
安装完后需要重启:
.