Windows配置tensorflow-gpu的过程与注意事项

之前在tensorflow-gpu的配置上花了一个多礼拜的时间,windows配环境的坑非常多,我基本上该踩的坑都踩了,不该踩的也踩了,而且网上的教程也十分杂乱,因此写一篇windows配置tensorflow-gpu当前版本的教程来整理一下。

本机环境:

Windows 7

tensorflow-gpu 1.3.0

python 3.6

cuda 8.0 + cudnn 6.0

tensorflow官方在1.2.0开始的版本后能够支持在windows环境下的运行,官网上 (https://www.tensorflow.org/versions/r0.12/get_started/os_setup.html#download-and-setup)有一些关于windows环境的安装教程,当然第一这个网站被墙,第二它讲的也不是很详细,不过有一些重点需要把握:

· 当前最新版本的tensorflow支持python 3.5及以上(python3.6需要tf1.2.0版本以后才支持)

· 当前最新版本的tensorflow理论上支持cuda 7.0及以上和cudnn 3.0及以上,不过官方推荐的版本是cuda 8.0和cudnn 5.0(此处官网写的有问题,最新的tensorflow版本需要的是cudnn 6.0,后面再具体说明一下),如果说使用其它的版本就需要你手动编译安装,用官方推荐的版本就可以直接使用pip安装。

· windows上运行tensorflow需要MSVCP140.DLL文件在环境变量的目录下,如果没有则需安装 Visual C++ 2015 redistributable (x64 version)。

所以在tensorflow-gpu安装前,你需要事先安装cuda和cudnn,可以前往nvidia官网上下载,由于当前cuda和cudnn的版本分别为9.0和7.0,但是tensorflow在最新环境上需要手动编译安装,所以要选择旧版本进行下载(CUDA Toolkit ArchiveNVIDIA cuDNN),此处cuda8.0有两个版本,分别为8.0.44和8.0.61,据网上的一些教程称,这里的8.0特指8.0.44,不过我实测两个版本都装过,都是可以正常运行的。这里再多说一句,如果要手动编译安装的话,可以前往tensorflow官方的cmake项目Github (tensorflow/tensorflow),下面有具体说明,不过我手动编译安装过很多次,全部在等待很长时间后编译失败,我也不知道是连接外网下包时网络问题还是什么其它的原因,反正手动编译安装并不简单。

在安装cuda前,需要了解的是cuda的运行需要Visual Studio 2015及以下的版本,最低到VS2010,也就是说你不仅要事先安装VS,还不能下载最新的VS2017。。。VS的安装也是一个对时间和硬盘空间的极大摧残的过程,在安装完之后你就可以安装cuda了,值得一提的是cuda附带的驱动也要进行安装,因为cuda需要运行在某个版本的驱动以上,否则会出现运行的问题,比如蓝屏……照着cuda的安装文件进行一番操作之后,你要安装cudnn。cudnn的安装比较简单,你只需要把解压缩后的文件放到cuda根目录下覆盖就行了,当然cudnn的bin文件夹中的cudnn64_X.dll(X为cudnn版本号)所在的文件夹需要加入环境变量,tensorflow-gpu执行的时候需要运行该文件。

一般来说,如果一切顺利的话,应该就可以正常运行了,可以将github官网上的样例代码在本地进行一下测试,如果能够跑出结果,则大功告成。下面来分析一下我在配置环境中踩到的各种坑:

1、没有安装官方的推荐的cuda和cudnn版本

如果说没有安装官方推荐的版本,并且是pip安装的话,你在python文件中import tensorflow的时候会报错(DLL load failed: 找不到指定的模块,以及,No module named ‘_pywrap_tensorflow’等),需要做的是换官方推荐的版本,或者手动编译安装。

2、手动编译安装失败的问题

经我测试多次,MSbuild编译到最后都是报大量的错误。我个人认为大概率的可能是编译过程中需要下载很多其他的包,但这些包可能存放在外网,或许是网速慢或许是被墙总之有些包可能并没有成功获取,导致最后编译失败。当然也有可能是其它的原因,因为当时我使用的是VS2017来进行编译的,有可能是VS版本或者别的什么问题。

3、VS安装需要大量的C盘空间

VS的安装需要大量的空间,尤其是C盘,这不是你在安装的时候选择安装到别的盘就能解决的,原因是VS在安装过程中会获取很多包以及安装文件,而这些文件会全部默认放到C盘Program Files相应的目录下,最后会占至少6G的空间(VS2017由于是模块化安装,相对会好一点),所以如果说你的C盘空间不够,那你只能去使用一下分区助手,把别的盘划一些给C盘,我曾尝试过不装VS或者安装VS2017来节省C盘空间(因为当时C盘只剩6G不到了),但是最后全部失败了,你还是多分点空间给C盘吧。。

4、有关cudnn版本的问题

这个问题是非常坑爹的,不管是官网教程还是网上的大部分教程,都说是安装cuda8.0+cudnn5.0,否则会在import tensorflow的时候报错(DLL load failed: 找不到指定的模块,以及,No module named ‘_pywrap_tensorflow’等)。然而我在之前无数失败之后按照这个版本安装的情况下,还是报了这个错,真的当时都快要崩溃了,最后我辗转网络,终于找到了一个用于测试windows下tensorflow安装情况的python测试脚本,用它可以来检测你报错的原因(原脚本地址:https://gist.github.com/mrry/ee5dbcfdd045fa48a27d56664411d41c),由于该网站可能被墙,因此我发一下脚本代码:

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""A script for testing that TensorFlow is installed correctly on Windows. The script will attempt to verify your TensorFlow installation, and print suggestions for how to fix your installation. """

import ctypes
import imp
import sys

def main():
  try:
    import tensorflow as tf
    print("TensorFlow successfully installed.")
    if tf.test.is_built_with_cuda():
      print("The installed version of TensorFlow includes GPU support.")
    else:
      print("The installed version of TensorFlow does not include GPU support.")
    sys.exit(0)
  except ImportError:
    print("ERROR: Failed to import the TensorFlow module.")

  candidate_explanation = False

  python_version = sys.version_info.major, sys.version_info.minor
  print("\n- Python version is %d.%d." % python_version)
  if not (python_version == (3, 5) or python_version == (3, 6)):
    candidate_explanation = True
    print("- The official distribution of TensorFlow for Windows requires "
          "Python version 3.5 or 3.6.")
  
  try:
    _, pathname, _ = imp.find_module("tensorflow")
    print("\n- TensorFlow is installed at: %s" % pathname)
  except ImportError:
    candidate_explanation = False
    print(""" - No module named TensorFlow is installed in this Python environment. You may  install it using the command `pip install tensorflow`.""")

  try:
    msvcp140 = ctypes.WinDLL("msvcp140.dll")
  except OSError:
    candidate_explanation = True
    print(""" - Could not load 'msvcp140.dll'. TensorFlow requires that this DLL be  installed in a directory that is named in your %PATH% environment  variable. You may install this DLL by downloading Microsoft Visual  C++ 2015 Redistributable Update 3 from this URL:  https://www.microsoft.com/en-us/download/details.aspx?id=53587""")

  try:
    cudart64_80 = ctypes.WinDLL("cudart64_80.dll")
  except OSError:
    candidate_explanation = True
    print(""" - Could not load 'cudart64_80.dll'. The GPU version of TensorFlow  requires that this DLL be installed in a directory that is named in  your %PATH% environment variable. Download and install CUDA 8.0 from  this URL: https://developer.nvidia.com/cuda-toolkit""")

  try:
    nvcuda = ctypes.WinDLL("nvcuda.dll")
  except OSError:
    candidate_explanation = True
    print(""" - Could not load 'nvcuda.dll'. The GPU version of TensorFlow requires that  this DLL be installed in a directory that is named in your %PATH%  environment variable. Typically it is installed in 'C:\Windows\System32'.  If it is not present, ensure that you have a CUDA-capable GPU with the  correct driver installed.""")

  cudnn5_found = False
  try:
    cudnn5 = ctypes.WinDLL("cudnn64_5.dll")
    cudnn5_found = True
  except OSError:
    candidate_explanation = True
    print(""" - Could not load 'cudnn64_5.dll'. The GPU version of TensorFlow  requires that this DLL be installed in a directory that is named in  your %PATH% environment variable. Note that installing cuDNN is a  separate step from installing CUDA, and it is often found in a  different directory from the CUDA DLLs. You may install the  necessary DLL by downloading cuDNN 5.1 from this URL:  https://developer.nvidia.com/cudnn""")

  cudnn6_found = False
  try:
    cudnn = ctypes.WinDLL("cudnn64_6.dll")
    cudnn6_found = True
  except OSError:
    candidate_explanation = True

  if not cudnn5_found or not cudnn6_found:
    print()
    if not cudnn5_found and not cudnn6_found:
      print("- Could not find cuDNN.")
    elif not cudnn5_found:
      print("- Could not find cuDNN 5.1.")
    else:
      print("- Could not find cuDNN 6.")
      print("""  The GPU version of TensorFlow requires that the correct cuDNN DLL be installed  in a directory that is named in your %PATH% environment variable. Note that  installing cuDNN is a separate step from installing CUDA, and it is often  found in a different directory from the CUDA DLLs. The correct version of  cuDNN depends on your version of TensorFlow:   * TensorFlow 1.2.1 or earlier requires cuDNN 5.1. ('cudnn64_5.dll')  * TensorFlow 1.3 or later requires cuDNN 6. ('cudnn64_6.dll')   You may install the necessary DLL by downloading cuDNN from this URL:  https://developer.nvidia.com/cudnn""")
    
  if not candidate_explanation:
    print(""" - All required DLLs appear to be present. Please open an issue on the  TensorFlow GitHub page: https://github.com/tensorflow/tensorflow/issues""")

  sys.exit(-1)

if __name__ == "__main__":
  main()

从这里我才发现问题的所在:tensorflow在1.2.1版本前需要的是cudnn5.0,但在之后就需要cudnn6.0了。。但并没有任何地方说明这一点,只有在stackflow的哪个问题的回复下面我好像有看到过类似的内容,但也不全。所以cudnn版本和tensorflow版本一定要注意匹配,实在无法正常导入的话可以用以上脚本测试一下,大部分问题都可以解决。

5、nvidia驱动版本的问题

在第一次成功安装的过程中,我并没有选择更新驱动版本,因为之前更新过驱动版本,然后很多游戏就开不了了,会出现什么显卡未弹出的奇怪错误。然而在我使用Pycharm跑样例代码的时候,电脑数次在运行过程中出现蓝屏现象。后来我在cmd里面跑样例代码,cmd里面给出了错误原因,就是驱动版本不足导致cuda运行有问题。所以后来我安装了cuda安装文件中自带的驱动,除了偶尔还会弹出显卡未弹出的错误外,没有什么别的问题。当然显卡未弹出我也不知道是什么原因,不过好像没有太大影响。

以上就是Windows下配置tensorflow gpu版本的过程和各种注意事项,希望大家可以少踩一点坑。

—————-2018.3.1 编辑—————-

之前有提到会出现显卡未弹出的错误,经我反复测试各种安装方法,应该是nvidia的显卡驱动有问题,具体是我电脑显卡的问题还是说nvidia940M显卡都有这样问题我个人不太清楚。

我的解决方法是使用GPU-Z软件,如果说能正确打开GPU-Z且不跳出显卡未弹出的错误的话,就可以正常使用tensorflow-gpu了,当然如果跳出这个错误,需要重启一下电脑重新尝试。我的电脑打开GPU-Z时跳出错误的概率大概在50%,如果碰到错误就重启再开就行了。

    原文作者:胡国盛
    原文地址: https://zhuanlan.zhihu.com/p/30068718
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞