安装tensorflow(gpu版)

环境

系统:windows 7
显卡:GeForce 940M
显卡驱动:417.71(25.21.14.1771)
anaconda:2018.12
conda:4.6.7
python:3.6.2
tensorflow-gpu:1.1.0
CUDA:CUDA Toolkit 8.0 GA1 (Sept 2016)
cuDNN:cuDNN v5.1 (Jan 20, 2017), for CUDA 8.0

安装tensorflow-gpu

  1. 打开Anaconda Prompt,可以配置清华仓库镜:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
  1. 创建运行环境,并激活环境:
conda create -n tf-gpu python=3.6
activate tf-gpu
  1. 安装tensorflow-gpu:(不能使用pip,否则安装后不能使用?)
conda install tensorflow-gpu

安装CUDA和CUDNN

CUDA

CUDA(Compute Unified Device Architecture,统一计算设备架构),是显卡厂商NVIDIA推出的运算平台。 CUDA™是一种由NVIDIA推出的通用并行计算架构,该架构使GPU能够解决复杂的计算问题。 它包含了CUDA指令集架构(ISA)以及GPU内部的并行计算引擎。
网站: https://developer.nvidia.com/cuda-toolkit
文档:
https://docs.nvidia.com/cuda/
https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html
安装教程:
https://docs.nvidia.com/cuda/cuda-installation-guide-microsoft-windows/index.html
首先应该检查CUDA是否支持你的显卡,CUDA支持的显卡有:
https://developer.nvidia.com/cuda-gpus
下载:
所有版本:https://developer.nvidia.com/cuda-toolkit-archive
最新版本:https://developer.nvidia.com/cuda-downloads
下载时版本选择CUDA Toolkit 8.0 GA1 (Sept 2016):
https://developer.nvidia.com/cuda-80-download-archive
(注意:tensorflow很可能不支持最新版的CUDA,见 https://tensorflow.google.cn/install/gpu)
(下载也有坑,nvidia下载很慢,而且到一半老是中途断开,无法重连服务器,然后就只有重下,浪费了我好几个G流量…建议使用IDM下载器,可以支持8线程同时下载,速度很快还不会断开,我已将IDM安装包和破解上传到百度云:https://pan.baidu.com/s/1qfXUbByuDtTlR9HJLV044A 提取码:8j41)
CUDA 8.0版本的文档:
https://docs.nvidia.com/cuda/archive/8.0/
https://docs.nvidia.com/cuda/archive/8.0/cuda-installation-guide-microsoft-windows/index.html
安装方法: 一直下一步,默认安装即可。
验证: 打开cmd,运行nvcc -V显示版本:

C:\Users\Administrator>nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Sat_Sep__3_19:05:48_CDT_2016
Cuda compilation tools, release 8.0, V8.0.44

显示显卡信息C:\Program Files\NVIDIA Corporation\NVSMI\nvidia-smi.exe

C:\Program Files\NVIDIA Corporation\NVSMI>nvidia-smi.exe
Tue Mar 12 16:32:43 2019
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 391.24                 Driver Version: 391.24                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name            TCC/WDDM | Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce 940M       WDDM  | 00000000:01:00.0 Off |                  N/A |
| N/A   35C    P0    N/A /  N/A |     61MiB /  2048MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

显示可用设备C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\extras\demo_suite\deviceQuery.exe

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\extras\demo_suite>deviceQuery.exe
deviceQuery.exe Starting...

 CUDA Device Query (Runtime API) version (CUDART static linking)

Detected 1 CUDA Capable device(s)

Device 0: "GeForce 940M"
  CUDA Driver Version / Runtime Version          9.1 / 8.0
  CUDA Capability Major/Minor version number:    5.0
  Total amount of global memory:                 2048 MBytes (2147483648 bytes)
  ( 3) Multiprocessors, (128) CUDA Cores/MP:     384 CUDA Cores
  GPU Max Clock rate:                            1176 MHz (1.18 GHz)
  Memory Clock rate:                             1001 Mhz
  Memory Bus Width:                              64-bit
  L2 Cache Size:                                 1048576 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096
)
  Maximum Layered 1D Texture Size, (num) layers  1D=(16384), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(16384, 16384), 2048 layers
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  2048
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 1 copy engine(s)
  Run time limit on kernels:                     Yes
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Disabled
  CUDA Device Driver Mode (TCC or WDDM):         WDDM (Windows Display Driver Model)
  Device supports Unified Addressing (UVA):      Yes
  Device PCI Domain ID / Bus ID / location ID:   0 / 1 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 9.1, CUDA Runtime Version = 8.0, NumDevs =
1, Device0 = GeForce 940M
Result = PASS

如果未显示出gpu,显示的信息为cudaGetDeviceCount returned 38 -> no CUDA-capable device is detected Result,应该是显卡驱动问题,更新显卡的驱动到最新版本。
通过CUDA-Z软件也可以检测,如果未检测到显卡,说明显卡驱动需要更新:http://cuda-z.sourceforge.net/

CUDNN

The NVIDIA CUDA® Deep Neural Network library (cuDNN) is a GPU-accelerated library of primitives for deep neural networks. cuDNN provides highly tuned implementations for standard routines such as forward and backward convolution, pooling, normalization, and activation layers.
网站: https://developer.nvidia.com/cudnn
安装教程:
https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html
下载:
所有版本:https://developer.nvidia.com/rdp/cudnn-archive
最新版本:https://developer.nvidia.com/rdp/cudnn-download
** 对于CUDA Toolkit 8.0 GA1 (Sept 2016),下载版本选择cuDNN v5.1 (Jan 20, 2017), for CUDA 8.0,下载链接:https://developer.nvidia.com/compute/machine-learning/cudnn/secure/v5.1/prod_20161129/8.0/cudnn-8.0-windows7-x64-v5.1-zip
安装方法:

  1. 将解压后的三个文件夹bin、include、lib拷贝到CUDA安装根目录下(默认安装路径为 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0)
  2. 在环境变量PATH中添加:
    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin;
    C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64;

查看电脑GPU和CPU:

import os
from tensorflow.python.client import device_lib
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "99"
 
if __name__ == "__main__":
    print(device_lib.list_local_devices())

结果:

(tf-gpu) C:\Users\Administrator>python
Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 12:30:02) [MSC v
.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> from tensorflow.python.client import device_lib
>>> os.environ["TF_CPP_MIN_LOG_LEVEL"] = "99"
>>>
... if __name__ == "__main__":
...     print(device_lib.list_local_devices())
...
[name: "/cpu:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 16604710572955220293
, name: "/gpu:0"
device_type: "GPU"
memory_limit: 1801740288
locality {
  bus_id: 1
}
incarnation: 7449078333642404109
physical_device_desc: "device: 0, name: GeForce 940M, pci bus id: 0000:01:00.0"
]

更新显卡驱动的坑

阶段一

  如果deviceQuery.exe显示的信息为cudaGetDeviceCount returned 38 -> no CUDA-capable device is detected Result,应该是显卡驱动问题,更新显卡的驱动到最新版本。
  本人显卡型号为NVIDIA GeForce 940M,原驱动版本号为21.21.13.6930。更新显卡驱动遇到了大坑。本来是通过右键计算机的设备管理器,找到显卡,右键属性来更新驱动程序,不料更新过程中直接黑屏,电脑卡死,怎么按都没有反应,等了几分钟后无果,然后强制电源键关机。再次开机之后…完蛋了,进不去系统了,开机电脑黑屏显示:

Windows 未能启动,原因可能是最近更改了硬件或软件。解决此问题的步骤:
1.插入windows安装光盘并重新启动计算机。
2.选择语言设置,然后单击“下一步”。
3.单击修复计算机。
如果没有此光盘请与您的系统管理员或计算机制造商联系,以获得帮助。

文件:\Windows\System32\drivers\nvpciflt.sys
状态:0x0000098
信息:由于关键系统驱动程序丢失或损坏,因此 Windows 无法加载。

  系统崩了之后,借别人的电脑使用通用peU盘制作工具把自己的U盘做成了U盘启动盘,然后开机进入BIOS设置U盘优先启动,通过U盘内的系统找到了上面出问题的文件(C:\Windows\System32\drivers\nvpciflt.sys)并将其删除。将BIOS改回去后再次启动,系统能进去了。但是,我的显卡却在设备管理器中检测不到了,因为我删的文件是显卡的驱动文件之一,而且因为显卡出错系统打不开软件,连开始菜单关机都没反应。只好强制电源键关机重启,通过尝试发现显卡在开机后的一小会儿是可以检测的到的,于是乎,趁着这一小会儿,赶紧打开设备管理器器-显卡-右键属性-回滚驱动程序,成功了,驱动程序回滚到了原先的版本,电脑算是恢复了正常。
  但是,回滚了之后,显卡驱动并没有得到更新。这次通过360驱动大师来更新驱动,选择了驱动安装-稳定模式,更新的驱动版本号为:21.21.13.7585。这次更新成功了(更新过程中的闪屏是正常的),但是更新完之后…deviceQuery.exe显示的信息还是cudaGetDeviceCount returned 38 -> no CUDA-capable device is detected Result。简直绝望。
  继续尝试,发现360驱动大师还有驱动安装-最新模式,于是选择最新模式的版本号:21.21.13.9124,更新驱动。结果…再次黑屏,又是怎么按都没反应…开始怀疑人生。这次没有直接强制关机重启,而是上床睡了两三个小时,起床之后仍然在黑屏,然后就只好强制关机重启了。感觉这次重启又要系统崩溃了,但是!并没有!,神奇地进入了系统,一看驱动版本号,更新成功了!!!运行deviceQuery.exe,终于出现了前述的Detected 1 CUDA Capable device(s) Device 0: "GeForce 940M",终于终于搞定了,俺滴个亲娘耶。(有可能第一次更新驱动等的时间比较短,虽然黑屏了但是更新可能仍在进行,所以第一次强制关机重启就把系统搞崩了。黑屏肯定是不正常的,可能是因为版本差太多或者兼容性问题什么的,不过更新显卡驱动闪屏是正常的。)

阶段二

  本来以为大功告成,但是大约一周之后又出现了莫名其妙的问题…每次跑程序训练完之后总会出现GPU is lost,并且导致 python.exe 停止工作,而且 python.exe 进程怎么也关不掉,或者会出现电脑死机或蓝屏。之前训练没问题的驱动现在又变得不好使了,吐血。
  试了很多方法,最后还是继续换驱动,使用360驱动大师换驱动,黑屏,系统崩,开不了机了,修。继续将nvpciflt.sys删掉(实际上是转移到其他位置),这次开机那一会儿显卡都检测不到了,驱动是彻底崩了,有点慌。尝试后发现,再将nvpciflt.sys恢复到原来位置就又可以开机了,并且驱动也显示正常了,玄学。
  使用了3次360驱动大师更新显卡驱动,两次都直接黑屏,对其失去了信任,太坑了。所以打算去找NVIDIA的官方驱动
  但是搜索到的驱动版本号都是一些419.67、419.35、419.17、398.11之类,和设备管理显示的驱动版本号很不相同。但我慢慢发现了NVIDIA驱动的命名规则,官网上搜到的5位驱动号其实和设备管理器驱动号的后5位是对应的,比如设备管理器显示21.21.13.9124,那么官网上搜到的版本号就是391.24。深入了解后,391.24是驱动版本号(同时也是上文nvidia-smi中显示的版本号),而21.21.13.9124是驱动DLL版本号。
  使用官网驱动(*.exe)安装果然很稳,从来不会黑屏,更新驱动很顺利就能完成(下载官方驱动仍然建议前面提到的IDM下载器,否则可能中途失败)。我的电脑是神舟战神K640e-i5 D1,显卡是GeForce 940M,前面已经试过的驱动版本有:369.30、375.85、391.24,全都GG。使用官网驱动试过的版本有398.82、417.71、419.35,发现398.82跑完程序会经常蓝屏报错IRQL_NOT_LESS_OR_EQUAL,419.35不跑程序都会无故各种蓝屏、非常不稳定,最终最终,这款驱动可以稳定工作:417.71 !!!(Driver DLL版本: 25.21.14.1771) 终于寻到你,心很累,但用着还没发现什么问题。
  这些就是我更新显卡驱动踩过的坑,一把辛酸泪,有遇到相同问题的可以参考。

NVIDIA 的一些有用链接:
(cn-国内域名,com-国际域名)
官网:
https://www.nvidia.cn/
https://www.nvidia.com/en-us/
开发者中心:
https://developer.nvidia.com/
所有显卡驱动:
https://www.nvidia.cn/Download/index.aspx?lang=cn
https://www.nvidia.com/Download/index.aspx
所有显卡驱动历史版本:
https://www.nvidia.cn/Download/Find.aspx?lang=cn
https://www.nvidia.com/Download/Find.aspx?lang=en-us
GeForce显卡驱动:
https://www.geforce.cn/drivers
https://www.geforce.com/drivers
驱动备份与还原:
https://www.nvidia.com/object/notebook_driver_instructions.html

参考:
TensorFlow - GPU 支持
TensorFlow - 使用 GPU
CSDN - Win10下Tensorflow(GPU版)安装趟坑实录
CSDN - 出现“no CUDA-capable device is detected”报错解决
CSDN - tensorflow查看电脑的CPU和GPU
stackoverflow - On Windows, running “import tensorflow” generates No module named “_pywrap_tensorflow” error
stackoverflow - “no CUDA-capable device is detected” with CUDA-capable GPU installed Win7

Helpful Links:
stackoverflow - win10 tensorflow gpu dont work:Device mapping: no known devices
stackoverflow - tensorflow on GPU: no known devices, despite cuda’s deviceQuery returning a “PASS” result
github - mrry/tensorflow_self_check.py: TensorFlow on Windows self-check
CSDN - 【TensorFlow】Windows10 64 位下安装 TensorFlow - 官方原生支持

猜你喜欢

转载自blog.csdn.net/ccgcccccc/article/details/89044358
今日推荐