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

编程入门 行业动态 更新时间:2024-10-27 08:38:02

Win10下部署TensorFlow以及一些避坑小<a href=https://www.elefans.com/category/jswz/34/1769915.html style=指南"/>

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

第一步,下载Anaconda3

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

/

我们下载了支持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 pipUsing cached pip-9.0.1-py2.py3-none-any.whl
Installing collected packages: pipFound existing installation: pip 8.1.2Uninstalling pip-8.1.2:Successfully uninstalled pip-8.1.2
Successfully installed pip-9.0.1

第三步,安装CUDA

1.检查兼容性

首先要确保自己的显卡支持CUDA,这个需要是NVIDIA的显卡,可以去这个网址查查

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

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

下载地址如下:

选好自己的操作系统,我的是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(官方建议版本),需要简单注册一下,下载地址如下:

注册、认证、改密码三个步骤,大约需要3-5分钟左右,如果觉得麻烦,也可以去百度网盘下载,文件不是很大,地址如下:
链接: 密码: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: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-gpuDownloading 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: protobufRunning setup.py bdist_wheel for protobuf ... doneStored 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-gpuFound existing installation: pyparsing 2.1.4Uninstalling pyparsing-2.1.4:Successfully uninstalled pyparsing-2.1.4Found 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 pipDownloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)100% |████████████████████████████████| 1.3MB 562kB/s
Installing collected packages: pipFound existing installation: pip 8.1.1Uninstalling 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

找到一篇文章,专门研究这个问题的,文章链接是

更新一下setuptools

C:\Users\yuhul>pip install setuptools --ignore-installed
Collecting setuptoolsUsing 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-gpuUsing 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-gpuFound existing installation: Werkzeug 0.11.11DEPRECATION: 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_helperreturn importlib.import_module(mname)File "D:\Anaconda3\lib\importlib\__init__.py", line 126, in import_modulereturn _bootstrap._gcd_import(name[level:], package, level)File "<frozen importlib._bootstrap>", line 986, in _gcd_importFile "<frozen importlib._bootstrap>", line 969, in _find_and_loadFile "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlockedFile "<frozen importlib._bootstrap>", line 666, in _load_unlockedFile "<frozen importlib._bootstrap>", line 577, in module_from_specFile "<frozen importlib._bootstrap_external>", line 906, in create_moduleFile "<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_helperreturn importlib.import_module('_pywrap_tensorflow_internal')File "D:\Anaconda3\lib\importlib\__init__.py", line 126, in import_modulereturn _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_tensorflowFile "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_helperreturn importlib.import_module(mname)File "D:\Anaconda3\lib\importlib\__init__.py", line 126, in import_modulereturn _bootstrap._gcd_import(name[level:], package, level)File "<frozen importlib._bootstrap>", line 986, in _gcd_importFile "<frozen importlib._bootstrap>", line 969, in _find_and_loadFile "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlockedFile "<frozen importlib._bootstrap>", line 666, in _load_unlockedFile "<frozen importlib._bootstrap>", line 577, in module_from_specFile "<frozen importlib._bootstrap_external>", line 906, in create_moduleFile "<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_helperreturn importlib.import_module('_pywrap_tensorflow_internal')File "D:\Anaconda3\lib\importlib\__init__.py", line 126, in import_modulereturn _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named '_pywrap_tensorflow_internal'Failed to load the native TensorFlow runtime.See  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: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: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: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: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: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: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: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: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: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: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: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:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 850M, pci bus id: 0000:01:00.0) 

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

参考文档

1. 
2. 

更多推荐

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

本文发布于:2024-02-11 20:40:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1683352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指南   TensorFlow   避坑小

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!