Win10 下安装 Tensorflow 1.11 GPU版

10月初,微软发布了新的 Win10 LTSC 2019[1],相比之前安装使用的 Win10 2016 LTSB 来说,吸引我升级的主要动力是看重其任务管理器中增加了 GPU 监测功能,在进行 GPU 计算任务的时候就可以原生监控显卡的使用情况了,很方便。

使用 LTSC 版本的原因是系统够简约,但是不便之处在于想要使用高版本只能重装而无法通过升级进行(目前我没发现升级方法)。

这次重装系统,顺便记录一下全新安装 TensorFlow 的过程步骤。

当前最新的 TensorFlow 为 1.11 版,为了省事,采用官网提供的已经编译好的版本。从官网查看,其 GPU 版的硬件、软件依赖条件:

TensorFlowGPURequirements

下面开始按步骤安装。

1. 安装显卡驱动

默认 Windows 10 安装好之后会自动安装好显卡驱动,其版本已经满足 9.0 的要求,不过为了保持最新避免潜在问题还是更新一下。这一步没什么好说的。去 NVIDIA® GPU drivers 网站对应自己机器下载安装就好。

2. 安装 CUDA 9.0 开发套件

  1. 下载安装
    目前的 TensorFlow 1.11 GPU 版本需要 9.0 版的 Toolkit,而 NVIDIA 官网给出的下载已经指向了 10.0 版本。除非从源码编译,否则从官网下载的 TensorFlow 1.11 GPU 版本在 10.0 的 Toolkit 下是无法正常工作的。从这里 找到所需 CUDA Toolkit 9.0 版本下载。按照默认组件和配置安装即可,默认安装到 C 盘。
    CUDA_Install
  2. 配置环境变量
    右键[此电脑]->[属性]->[高级系统设置]->[环境变量]->[系统变量] 中选择 ‘Path’,编辑,保证加入以下 4 条内容:
    CUDA_Path

本人测试,需要注销重新登陆以使环境变量设置生效。 安装成功后,在终端输入 nvcc -V 可以显示如下结果:

1
2
3
4
5
C:\Users\xxx>nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2017 NVIDIA Corporation
Built on Fri_Sep__1_21:08:32_Central_Daylight_Time_2017
Cuda compilation tools, release 9.0, V9.0.176

3. 安装 cuDNN

什么是 cuDNN,可以参考这篇文章[2]:

NVIDIA cuDNN是用于深度神经网络的GPU加速库。它强调性能、易用性和低内存开销。NVIDIA cuDNN可以集成到更高级别的机器学习框架中,如加州大学伯克利分校的流行CAFFE软件。简单的,插入式设计可以让开发人员专注于设计和实现神经网络模型,而不是调整性能,同时还可以在GPU上实现高性能现代并行计算。

cuDNN 需要注册才能下载,不想注册也可以网上搜索别人已经下载好的。安装比较简单,下载下来之后解压缩,将其中的所有内容拷贝到 CUDA 9.0 安装路径下(我这里是 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0)即可。这样放在一起的好处是不用再单独配置系统环境(上一步已经配置好了)。
cuDNN

把文件 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\extras\CUPTI\libx64\cupti64_80.dll 拷贝到 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin 中。

4. 安装 Python 3.6.x 版本

这里我是通过 Miniconda3 来安装的。关于 Miniconda3 的安装介绍可以参考这篇文章[3]

安装过程中路径设到了 D 盘,同时设置了加入 Miniconda3 到系统环境变量,这样可以直接在终端运行 conda 命令。Miniconda3 安装完成之后的默认环境是基于 Python 3.7 的。通过执行 conda update conda 命令,Python 版本会回落到 3.6.7。虽然不知道为什么,但是省去了再手动安装 3.6.x 版本 Python 的步骤。 通过执行 conda install python=3.6 命令,将默认 Python 版本回落到 3.6.x。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
C:\Users\xxx>conda update conda
Solving environment: done

## Package Plan ##

environment location: D:\Programs\Miniconda3

added / updated specs:
- conda


The following packages will be downloaded:

package | build
---------------------------|-----------------
menuinst-1.4.14 | py36hfa6e2cd_0 92 KB
conda-4.5.11 | py36_0 1.0 MB
requests-2.19.1 | py36_0 96 KB
pywin32-223 | py36hfa6e2cd_1 9.3 MB
wheel-0.32.2 | py36_0 52 KB
wincertstore-0.2 | py36h7fe50ca_0 13 KB
setuptools-40.4.3 | py36_0 576 KB
win_inet_pton-1.0.1 | py36_1 6 KB
six-1.11.0 | py36_1 21 KB
chardet-3.0.4 | py36_1 210 KB
urllib3-1.23 | py36_0 152 KB
pycparser-2.19 | py36_0 174 KB
cryptography-2.3.1 | py36h7a1dbc1_2 522 KB
ruamel_yaml-0.15.46 | py36hfa6e2cd_0 262 KB
cffi-1.11.5 | py36h74b6da3_1 213 KB
pysocks-1.6.8 | py36_0 23 KB
idna-2.7 | py36_0 132 KB
python-3.6.7 | h33f27b4_1 20.9 MB
asn1crypto-0.24.0 | py36_0 155 KB
certifi-2018.10.15 | py36_0 138 KB
pyopenssl-18.0.0 | py36_0 83 KB
vs2015_runtime-14.15.26706 | h3a45250_0 2.2 MB
pycosat-0.6.3 | py36hfa6e2cd_0 98 KB
pip-10.0.1 | py36_0 1.8 MB
vc-14.1 | h0510ff6_4 6 KB
openssl-1.1.1 | he774522_0 5.7 MB
------------------------------------------------------------
Total: 43.9 MB

The following packages will be UPDATED:

asn1crypto: 0.24.0-py37_0 --> 0.24.0-py36_0
certifi: 2018.8.24-py37_1 --> 2018.10.15-py36_0
cffi: 1.11.5-py37h74b6da3_1 --> 1.11.5-py36h74b6da3_1
chardet: 3.0.4-py37_1 --> 3.0.4-py36_1
conda: 4.5.11-py37_0 --> 4.5.11-py36_0
cryptography: 2.3.1-py37h74b6da3_0 --> 2.3.1-py36h7a1dbc1_2
idna: 2.7-py37_0 --> 2.7-py36_0
menuinst: 1.4.14-py37hfa6e2cd_0 --> 1.4.14-py36hfa6e2cd_0
openssl: 1.0.2p-hfa6e2cd_0 --> 1.1.1-he774522_0
pip: 10.0.1-py37_0 --> 10.0.1-py36_0
pycosat: 0.6.3-py37hfa6e2cd_0 --> 0.6.3-py36hfa6e2cd_0
pycparser: 2.18-py37_1 --> 2.19-py36_0
pyopenssl: 18.0.0-py37_0 --> 18.0.0-py36_0
pysocks: 1.6.8-py37_0 --> 1.6.8-py36_0
pywin32: 223-py37hfa6e2cd_1 --> 223-py36hfa6e2cd_1
requests: 2.19.1-py37_0 --> 2.19.1-py36_0
ruamel_yaml: 0.15.46-py37hfa6e2cd_0 --> 0.15.46-py36hfa6e2cd_0
setuptools: 40.2.0-py37_0 --> 40.4.3-py36_0
six: 1.11.0-py37_1 --> 1.11.0-py36_1
urllib3: 1.23-py37_0 --> 1.23-py36_0
vc: 14-h0510ff6_3 --> 14.1-h0510ff6_4
vs2015_runtime: 14.0.25123-3 --> 14.15.26706-h3a45250_0
wheel: 0.31.1-py37_0 --> 0.32.2-py36_0
win_inet_pton: 1.0.1-py37_1 --> 1.0.1-py36_1
wincertstore: 0.2-py37_0 --> 0.2-py36h7fe50ca_0

The following packages will be DOWNGRADED:

python: 3.7.0-hea74fb7_0 --> 3.6.7-h33f27b4_1

Proceed ([y]/n)? y


Downloading and Extracting Packages
menuinst-1.4.14 | 92 KB | ############################################################################ | 100%
conda-4.5.11 | 1.0 MB | ############################################################################ | 100%
requests-2.19.1 | 96 KB | ############################################################################ | 100%
pywin32-223 | 9.3 MB | ############################################################################ | 100%
wheel-0.32.2 | 52 KB | ############################################################################ | 100%
wincertstore-0.2 | 13 KB | ############################################################################ | 100%
setuptools-40.4.3 | 576 KB | ############################################################################ | 100%
win_inet_pton-1.0.1 | 6 KB | ############################################################################ | 100%
six-1.11.0 | 21 KB | ############################################################################ | 100%
chardet-3.0.4 | 210 KB | ############################################################################ | 100%
urllib3-1.23 | 152 KB | ############################################################################ | 100%
pycparser-2.19 | 174 KB | ############################################################################ | 100%
cryptography-2.3.1 | 522 KB | ############################################################################ | 100%
ruamel_yaml-0.15.46 | 262 KB | ############################################################################ | 100%
cffi-1.11.5 | 213 KB | ############################################################################ | 100%
pysocks-1.6.8 | 23 KB | ############################################################################ | 100%
idna-2.7 | 132 KB | ############################################################################ | 100%
python-3.6.7 | 20.9 MB | ############################################################################ | 100%
asn1crypto-0.24.0 | 155 KB | ############################################################################ | 100%
certifi-2018.10.15 | 138 KB | ############################################################################ | 100%
pyopenssl-18.0.0 | 83 KB | ############################################################################ | 100%
vs2015_runtime-14.15 | 2.2 MB | ############################################################################ | 100%
pycosat-0.6.3 | 98 KB | ############################################################################ | 100%
pip-10.0.1 | 1.8 MB | ############################################################################ | 100%
vc-14.1 | 6 KB | ############################################################################ | 100%
openssl-1.1.1 | 5.7 MB | ############################################################################ | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

C:\Users\xxx>

安装完成之后,需要注销重新登陆以使环境变量设置生效。

在终端中可以输入 conda info 来查看默认环境下的配置信息了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
C:\Users\xxx>conda info

active environment : None
user config file : C:\Users\xxx\.condarc
populated config files :
conda version : 4.5.11
conda-build version : not installed
python version : 3.6.7.final.0
base environment : D:\Programs\Miniconda3 (writable)
channel URLs : https://repo.anaconda.com/pkgs/main/win-64
https://repo.anaconda.com/pkgs/main/noarch
https://repo.anaconda.com/pkgs/free/win-64
https://repo.anaconda.com/pkgs/free/noarch
https://repo.anaconda.com/pkgs/r/win-64
https://repo.anaconda.com/pkgs/r/noarch
https://repo.anaconda.com/pkgs/pro/win-64
https://repo.anaconda.com/pkgs/pro/noarch
https://repo.anaconda.com/pkgs/msys2/win-64
https://repo.anaconda.com/pkgs/msys2/noarch
package cache : D:\Programs\Miniconda3\pkgs
C:\Users\xxx\AppData\Local\conda\conda\pkgs
envs directories : D:\Programs\Miniconda3\envs
C:\Users\xxx\AppData\Local\conda\conda\envs
C:\Users\xxx\.conda\envs
platform : win-64
user-agent : conda/4.5.11 requests/2.19.1 CPython/3.6.7 Windows/10 Windows/10.0.17763
administrator : False
netrc file : None
offline mode : False


C:\Users\xxx>

5. 安装 tensorflow-gpu 1.11.0

采用 pip install 命令安装。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
C:\Users\xxx>pip install tensorflow-gpu
Collecting tensorflow-gpu
Downloading https://files.pythonhosted.org/packages/3d/a0/60f72b76915a7c83e336e7f9ccf3a08305c30c7262cd15fedde44e026c3f/tensorflow_gpu-1.11.0-cp36-cp36m-win_amd64.whl (74.9MB)
100% |████████████████████████████████| 74.9MB 125kB/s
Collecting termcolor>=1.1.0 (from tensorflow-gpu)
Downloading https://files.pythonhosted.org/packages/8a/48/a76be51647d0eb9f10e2a4511bf3ffb8cc1e6b14e9e4fab46173aa79f981/termcolor-1.1.0.tar.gz
Collecting absl-py>=0.1.6 (from tensorflow-gpu)
Downloading https://files.pythonhosted.org/packages/0c/63/f505d2d4c21db849cf80bad517f0065a30be6b006b0a5637f1b95584a305/absl-py-0.6.1.tar.gz (94kB)
100% |████████████████████████████████| 102kB 1.1MB/s
Collecting tensorboard<1.12.0,>=1.11.0 (from tensorflow-gpu)
Downloading https://files.pythonhosted.org/packages/9b/2f/4d788919b1feef04624d63ed6ea45a49d1d1c834199ec50716edb5d310f4/tensorboard-1.11.0-py3-none-any.whl (3.0MB)
100% |████████████████████████████████| 3.0MB 499kB/s
Collecting numpy>=1.13.3 (from tensorflow-gpu)
Downloading https://files.pythonhosted.org/packages/10/b6/feaabbe393afe1ad4c803cdd7c2ada688613448e0987b016a3980b2f08c6/numpy-1.15.3-cp36-none-win_amd64.whl (13.5MB)
100% |████████████████████████████████| 13.5MB 758kB/s
Requirement already satisfied: six>=1.10.0 in d:\programs\miniconda3\lib\site-packages (from tensorflow-gpu) (1.11.0)
Collecting astor>=0.6.0 (from tensorflow-gpu)
Downloading https://files.pythonhosted.org/packages/35/6b/11530768cac581a12952a2aad00e1526b89d242d0b9f59534ef6e6a1752f/astor-0.7.1-py2.py3-none-any.whl
Collecting keras-preprocessing>=1.0.3 (from tensorflow-gpu)
Downloading https://files.pythonhosted.org/packages/fc/94/74e0fa783d3fc07e41715973435dd051ca89c550881b3454233c39c73e69/Keras_Preprocessing-1.0.5-py2.py3-none-any.whl
Collecting keras-applications>=1.0.5 (from tensorflow-gpu)
Downloading https://files.pythonhosted.org/packages/3f/c4/2ff40221029f7098d58f8d7fb99b97e8100f3293f9856f0fb5834bef100b/Keras_Applications-1.0.6-py2.py3-none-any.whl (44kB)
100% |████████████████████████████████| 51kB 2.0MB/s
Collecting protobuf>=3.6.0 (from tensorflow-gpu)
Downloading https://files.pythonhosted.org/packages/e8/df/d606d07cff0fc8d22abcc54006c0247002d11a7f2d218eb008d48e76851d/protobuf-3.6.1-cp36-cp36m-win_amd64.whl (1.1MB)
100% |████████████████████████████████| 1.1MB 473kB/s
Collecting setuptools<=39.1.0 (from tensorflow-gpu)
Downloading https://files.pythonhosted.org/packages/8c/10/79282747f9169f21c053c562a0baa21815a8c7879be97abd930dbcf862e8/setuptools-39.1.0-py2.py3-none-any.whl (566kB)
100% |████████████████████████████████| 573kB 487kB/s
Collecting grpcio>=1.8.6 (from tensorflow-gpu)
Downloading https://files.pythonhosted.org/packages/f0/90/520757ccafb14f03e8e46a54bacd45f5f9cca6b96b58b83b66a272f059df/grpcio-1.16.0-cp36-cp36m-win_amd64.whl (1.5MB)
100% |████████████████████████████████| 1.5MB 627kB/s
Requirement already satisfied: wheel>=0.26 in d:\programs\miniconda3\lib\site-packages (from tensorflow-gpu) (0.32.2)
Collecting gast>=0.2.0 (from tensorflow-gpu)
Downloading https://files.pythonhosted.org/packages/5c/78/ff794fcae2ce8aa6323e789d1f8b3b7765f601e7702726f430e814822b96/gast-0.2.0.tar.gz
Collecting werkzeug>=0.11.10 (from tensorboard<1.12.0,>=1.11.0->tensorflow-gpu)
Downloading https://files.pythonhosted.org/packages/20/c4/12e3e56473e52375aa29c4764e70d1b8f3efa6682bef8d0aae04fe335243/Werkzeug-0.14.1-py2.py3-none-any.whl (322kB)
100% |████████████████████████████████| 327kB 251kB/s
Collecting markdown>=2.6.8 (from tensorboard<1.12.0,>=1.11.0->tensorflow-gpu)
Downloading https://files.pythonhosted.org/packages/7a/6b/5600647404ba15545ec37d2f7f58844d690baf2f81f3a60b862e48f29287/Markdown-3.0.1-py2.py3-none-any.whl (89kB)
100% |████████████████████████████████| 92kB 808kB/s
Collecting h5py (from keras-applications>=1.0.5->tensorflow-gpu)
Downloading https://files.pythonhosted.org/packages/12/6c/00c38c5ce9322f1cc421d93217c44739646a106c61859622eccc297a5c05/h5py-2.8.0-cp36-cp36m-win_amd64.whl (2.3MB)
100% |████████████████████████████████| 2.3MB 519kB/s
Building wheels for collected packages: termcolor, absl-py, gast
Running setup.py bdist_wheel for termcolor ... done
Stored in directory: C:\Users\xxx\AppData\Local\pip\Cache\wheels\7c\06\54\bc84598ba1daf8f970247f550b175aaaee85f68b4b0c5ab2c6
Running setup.py bdist_wheel for absl-py ... done
Stored in directory: C:\Users\xxx\AppData\Local\pip\Cache\wheels\18\ea\5e\e36e1b8739e78cd2eba0a08fdc602c2b16a4b263912af8cb64
Running setup.py bdist_wheel for gast ... done
Stored in directory: C:\Users\xxx\AppData\Local\pip\Cache\wheels\9a\1f\0e\3cde98113222b853e98fc0a8e9924480a3e25f1b4008cedb4f
Successfully built termcolor absl-py gast
Installing collected packages: termcolor, absl-py, grpcio, werkzeug, markdown, setuptools, protobuf, numpy, tensorboard, astor, keras-preprocessing, h5py, keras-applications, gast, tensorflow-gpu
Found existing installation: setuptools 40.4.3
Uninstalling setuptools-40.4.3:
Successfully uninstalled setuptools-40.4.3
Successfully installed absl-py-0.6.1 astor-0.7.1 gast-0.2.0 grpcio-1.16.0 h5py-2.8.0 keras-applications-1.0.6 keras-preprocessing-1.0.5 markdown-3.0.1 numpy-1.15.3 protobuf-3.6.1 setuptools-39.1.0 tensorboard-1.11.0 tensorflow-gpu-1.11.0 termcolor-1.1.0 werkzeug-0.14.1

C:\Users\xxx>

6. 测试 tensorflow-gpu 是否安装正确

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
C:\Users\xxx>python
Python 3.6.7 |Anaconda, Inc.| (default, Oct 28 2018, 19:44:12) [MSC v.1915 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello,TensorFlow!')
>>> sess = tf.Session()
2018-11-02 14:28:29.039266: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1411] Found device 0 with properties:
name: GeForce GTX 750 Ti major: 5 minor: 0 memoryClockRate(GHz): 1.137
pciBusID: 0000:01:00.0
totalMemory: 2.00GiB freeMemory: 1.64GiB
2018-11-02 14:28:29.047124: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1490] Adding visible gpu devices: 0
2018-11-02 14:28:30.089044: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-11-02 14:28:30.093224: I tensorflow/core/common_runtime/gpu/gpu_device.cc:977] 0
2018-11-02 14:28:30.095429: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 0: N
2018-11-02 14:28:30.097770: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1103] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1392 MB memory) -> physical GPU (device: 0, name: GeForce GTX 750 Ti, pci bus id: 0000:01:00.0, compute capability: 5.0)
>>> print(sess.run(hello))
b'Hello,TensorFlow!'
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a+b))
42
>>> quit()

C:\Users\xxx>

在安装完成之后,第一次运行 sess = tf.Session() 时,输出会卡在 tensorflow/core/common_runtime/gpu/gpu_device.cc:1490] Adding visible gpu devices: 0 这一步很久,之后每次调用就很快了。

另外,在新版 Windows 10 中的任务管理器 [性能] 栏中可以很方便看到 GPU 的使用情况。箭头所指的变化位置及为运行命令 sess = tf.Session() 的时刻。
GPU_Usage_Show

参考

1: 分享一个热腾腾的Win10企业版LTSC官方原版及精简版;
2: CPU、GPU、CUDA,CuDNN 简介;
3: Windows 10下安装Miniconda3;
4: win10+cuda9.0+cuDNN 7.0+Tensorflow1.5(GPU)安装.


本文作者:Northan
本文链接: https://www.anmou.me/20181103-Install_TensorFlow_1.11_for_GPU_In_Windows_10/
版权声明:作者拥有版权,转载请注明出处。