Win10下部署TensorFlow以及一些避坑小指南

2023-05-16

第一步,下载Anaconda3

Anaconda官网目前最新的版本是Python3.6的,想要历史版本的,去下面的网站下载:

https://repo.continuum.io/archive/

我们下载了支持Python3.5.2的Anaconda3-4.2.0版本

Anaconda3-4.2.0-Windows-x86_64.exe

这里写图片描述

安装后查看是否安装成功

C:\Users\yuhul>python
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

第二步,需要升级一下pip

C:\Users\yuhul>python -m pip install --upgrade pip
Collecting pip
  Using cached pip-9.0.1-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 8.1.2
    Uninstalling pip-8.1.2:
      Successfully uninstalled pip-8.1.2
Successfully installed pip-9.0.1

第三步,安装CUDA

1.检查兼容性

首先要确保自己的显卡支持CUDA,这个需要是NVIDIA的显卡,可以去这个网址查查
https://developer.nvidia.com/cuda-gpus

例如我的笔记本显卡是GeForce GTX 850M就可以,如下图

这里写图片描述

2.CUDA8.0下载安装(官方建议版本)

下载地址如下:
https://developer.nvidia.com/cuda-toolkit-archive
选好自己的操作系统,我的是Win10,于是Version选择10
这里写图片描述
官网下载速度非常快,强烈推荐,下载完毕后,直接安装
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
中间准备安装的时候会删除旧的驱动,安装一些新的驱动和依赖,比如Visual Studio

这里写图片描述
这里写图片描述

3.检查是否安装成功

安装完毕,检查一下,OK

C:\Users\yuhul>nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Mon_Jan__9_17:32:33_CST_2017
Cuda compilation tools, release 8.0, V8.0.60

第四步,准备cuDNN

下载cuDNN5.1(官方建议版本),需要简单注册一下,下载地址如下:
https://developer.nvidia.com/cudnn

注册、认证、改密码三个步骤,大约需要3-5分钟左右,如果觉得麻烦,也可以去百度网盘下载,文件不是很大,地址如下:
链接:http://pan.baidu.com/s/1mhXR7GO 密码:ivo0
这里写图片描述

将压缩文件解压到D盘下,解压包就在D:\cuda目录下,注意需要将D:\cuda\bin加入Path环境变量,不然会报错。下面的避坑指南里面专门说明这个。

第五步,pip安装TensorFlow

安装命令如下

pip install --upgrade tensorflow-gpu

安装成功后,会有类似下面的信息提示。

Successfully installed protobuf-3.3.0 tensorflow-gpu-1.1.0 werkzeug-0.12.2

测试一下

>>> import tensorflow as tf
>>>
>>> a = tf.random_normal((100, 100))
>>> b = tf.random_normal((100, 500))
>>> c = tf.matmul(a, b)
>>> sess = tf.InteractiveSession()
2017-05-26 15:23:07.907576: I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 850M, pci bus id: 0000:01:00.0)
>>> sess.run(c)
array([[-17.43533134, -12.35972118,  -3.60145664, ...,  -1.92315793,
         -3.40039849,   1.06092656],
       [  3.80604815,   9.810215  ,   4.58090258, ...,  -4.17350674,
        -10.96353817,   1.36589038],
       [ 12.71141529,  16.51175308,  -5.85236883, ...,  -2.61258698,
          6.45679283, -13.10684395],
       ...,
       [  0.80980408,   4.90229654,  -3.36032319, ...,   6.21134901,
         -4.51875353,   7.74244833],
       [ -1.87480545,  21.59571075, -14.30918312, ...,  -9.37330914,
          6.85860014,  -1.14205527],
       [  7.93599844,  12.27898979,  16.6888485 , ...,   4.40559864,
        -15.18470287,  -7.69400549]], dtype=float32)

到此,基本成功。

下面是一些小坑

1. 升级setuptools

第一次安装的时候

C:\Users\yuhul>pip install --upgrade tensorflow-gpu
Collecting tensorflow-gpu
  Downloading tensorflow_gpu-1.1.0-cp35-cp35m-win_amd64.whl (48.5MB)
    100% |████████████████████████████████| 48.6MB 25kB/s
Collecting protobuf>=3.2.0 (from tensorflow-gpu)
  Downloading protobuf-3.3.0.tar.gz (271kB)
    100% |████████████████████████████████| 276kB 1.5MB/s
Collecting werkzeug>=0.11.10 (from tensorflow-gpu)
  Downloading Werkzeug-0.12.2-py2.py3-none-any.whl (312kB)
    100% |████████████████████████████████| 317kB 1.6MB/s
Collecting numpy>=1.11.0 (from tensorflow-gpu)
  Downloading numpy-1.12.1-cp35-none-win_amd64.whl (7.7MB)
    100% |████████████████████████████████| 7.7MB 159kB/s
Requirement already up-to-date: six>=1.10.0 in d:\anaconda3\lib\site-packages (from tensorflow-gpu)
Requirement already up-to-date: wheel>=0.26 in d:\anaconda3\lib\site-packages (from tensorflow-gpu)
Collecting setuptools (from protobuf>=3.2.0->tensorflow-gpu)
  Downloading setuptools-35.0.2-py2.py3-none-any.whl (390kB)
    100% |████████████████████████████████| 399kB 1.3MB/s
Collecting appdirs>=1.4.0 (from setuptools->protobuf>=3.2.0->tensorflow-gpu)
  Downloading appdirs-1.4.3-py2.py3-none-any.whl
Collecting packaging>=16.8 (from setuptools->protobuf>=3.2.0->tensorflow-gpu)
  Downloading packaging-16.8-py2.py3-none-any.whl
Collecting pyparsing (from packaging>=16.8->setuptools->protobuf>=3.2.0->tensorflow-gpu)
  Downloading pyparsing-2.2.0-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 3.7MB/s
Building wheels for collected packages: protobuf
  Running setup.py bdist_wheel for protobuf ... done
  Stored in directory: C:\Users\yuhul\AppData\Local\pip\Cache\wheels\1b\42\a0\4c7343df5b629ec9c75655468dce7652b28026896b0209ba55
Successfully built protobuf
Installing collected packages: appdirs, pyparsing, packaging, setuptools, protobuf, werkzeug, numpy, tensorflow-gpu
  Found existing installation: pyparsing 2.1.4
    Uninstalling pyparsing-2.1.4:
      Successfully uninstalled pyparsing-2.1.4
  Found existing installation: setuptools 27.2.0
Cannot remove entries from nonexistent file d:\anaconda3\lib\site-packages\easy-install.pth
You are using pip version 8.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.

升级了pip

C:\Users\yuhul>python -m pip install --upgrade pip
Collecting pip
  Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 562kB/s
Installing collected packages: pip
  Found existing installation: pip 8.1.1
    Uninstalling pip-8.1.1:
      Successfully uninstalled pip-8.1.1
Successfully installed pip-9.0.1

继续安装,仍然报错

Cannot remove entries from nonexistent file d:\anaconda3\lib\site-packages\easy-install.pth

找到一篇文章,专门研究这个问题的,文章链接是
http://blog.csdn.net/jasonzzj/article/details/53490674

更新一下setuptools

C:\Users\yuhul>pip install setuptools --ignore-installed
Collecting setuptools
  Using cached setuptools-35.0.2-py2.py3-none-any.whl
Collecting appdirs>=1.4.0 (from setuptools)
  Using cached appdirs-1.4.3-py2.py3-none-any.whl
Collecting packaging>=16.8 (from setuptools)
  Using cached packaging-16.8-py2.py3-none-any.whl
Collecting six>=1.6.0 (from setuptools)
  Downloading six-1.10.0-py2.py3-none-any.whl
Collecting pyparsing (from packaging>=16.8->setuptools)
  Using cached pyparsing-2.2.0-py2.py3-none-any.whl
Installing collected packages: appdirs, six, pyparsing, packaging, setuptools
Successfully installed appdirs-1.4.3 packaging-16.8 pyparsing-2.2.0 setuptools-35.0.2 six-1.10.0

继续尝试,安装成功

C:\Users\yuhul>pip install --upgrade tensorflow-gpu
Collecting tensorflow-gpu
  Using cached tensorflow_gpu-1.1.0-cp35-cp35m-win_amd64.whl
Collecting werkzeug>=0.11.10 (from tensorflow-gpu)
  Using cached Werkzeug-0.12.2-py2.py3-none-any.whl
Requirement already up-to-date: wheel>=0.26 in d:\anaconda3\lib\site-packages (from tensorflow-gpu)
Requirement already up-to-date: six>=1.10.0 in d:\anaconda3\lib\site-packages (from tensorflow-gpu)
Requirement already up-to-date: numpy>=1.11.0 in d:\anaconda3\lib\site-packages (from tensorflow-gpu)
Collecting protobuf>=3.2.0 (from tensorflow-gpu)
Requirement already up-to-date: setuptools in d:\anaconda3\lib\site-packages (from protobuf>=3.2.0->tensorflow-gpu)
Requirement already up-to-date: appdirs>=1.4.0 in d:\anaconda3\lib\site-packages (from setuptools->protobuf>=3.2.0->tensorflow-gpu)
Requirement already up-to-date: packaging>=16.8 in d:\anaconda3\lib\site-packages (from setuptools->protobuf>=3.2.0->tensorflow-gpu)
Requirement already up-to-date: pyparsing in d:\anaconda3\lib\site-packages (from packaging>=16.8->setuptools->protobuf>=3.2.0->tensorflow-gpu)
Installing collected packages: werkzeug, protobuf, tensorflow-gpu
  Found existing installation: Werkzeug 0.11.11
    DEPRECATION: Uninstalling a distutils installed project (werkzeug) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project.
    Uninstalling Werkzeug-0.11.11:
      Successfully uninstalled Werkzeug-0.11.11
Successfully installed protobuf-3.3.0 tensorflow-gpu-1.1.0 werkzeug-0.12.2

2.cuda环境变量配置

引入TensorFlow包,报错

>>> import tensorflow as tf
Traceback (most recent call last):
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
    return importlib.import_module(mname)
  File "D:\Anaconda3\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 577, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 906, in create_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
ImportError: DLL load failed: 找不到指定的模块。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 41, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 21, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
    return importlib.import_module('_pywrap_tensorflow_internal')
  File "D:\Anaconda3\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\__init__.py", line 51, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 52, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
    return importlib.import_module(mname)
  File "D:\Anaconda3\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 577, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 906, in create_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
ImportError: DLL load failed: 找不到指定的模块。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 41, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 21, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "D:\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
    return importlib.import_module('_pywrap_tensorflow_internal')
  File "D:\Anaconda3\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named '_pywrap_tensorflow_internal'


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/install_sources#common_installation_problems

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.
>>>

后面将cuda加入Path环境变量,例如我的目录是D:\cuda\bin,问题解决。

第一次运行的时候,会有一些提示信息,

The TensorFlow library wasn’t compiled to use (SSE, SSE2, SSE3, SSE4.1, SSE4.2, AVX, AVX2, FMA)instructions, but these are available on your machine and could speed up CPU computations。

第二次再运行的时候就没有了。

具体如下:

>>> sess = tf.InteractiveSession()
2017-05-26 15:13:51.797769: W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE instructions, but these are available on your machine and could speed up CPU computations.
2017-05-26 15:13:51.797917: W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-26 15:13:51.798052: W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE3 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-26 15:13:51.798187: W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-26 15:13:51.798318: W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-26 15:13:51.798467: W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-05-26 15:13:51.798600: W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-05-26 15:13:51.798753: W c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2017-05-26 15:13:52.632990: I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:887] Found device 0 with properties:
name: GeForce GTX 850M
major: 5 minor: 0 memoryClockRate (GHz) 0.8625
pciBusID 0000:01:00.0
Total memory: 2.00GiB
Free memory: 1.65GiB
2017-05-26 15:13:52.633100: I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:908] DMA: 0                                                                                       2017-05-26 15:13:52.636607: I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:918] 0:   Y                                                                                       2017-05-26 15:13:52.637097: I c:\tf_jenkins\home\workspace\release-win\device\gpu\os\windows\tensorflow\core\common_runtime\gpu\gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 850M, pci bus id: 0000:01:00.0) 

这些属于警告信息,表明pip默认安装的包没有使用CPU加速,自己通过源码编译可以实现,可以忽略,具体参考这个回答:
https://github.com/tensorflow/tensorflow/issues/7778

参考文档

1. http://blog.csdn.net/u010099080/article/details/53418159
2. http://blog.csdn.net/jasonzzj/article/details/53490674
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Win10下部署TensorFlow以及一些避坑小指南 的相关文章

随机推荐