Windows下编译tensorflow-gpu教程

这两个也要看:
https://zhuanlan.zhihu.com/p/29029860

https://zhuanlan.zhihu.com/p/34942873


Windows下编译tensorflow-gpu教程

官方文档: 
1. 在windows下安装tensorflow 
2. 在windows下编译tensorflow(cmake)

首先是系统配置:

  • OS:Windows Server 2012 R2(相当于Windows 8)
  • GPU:NVIDIA Quadro K1200

然后是各种软件版本:

  • git 2.14
  • Visual Studio 2015
  • CUDA 8.0.61
  • cuDNN 6.0
  • Anaconda3-4.2.0(此版本非最新版本,之所以使用是因为其默认python版本为3.5.2)
  • cmake 3.9.1
  • swig 3.0.12

一、安装CUDA

0. 首先需要在cuda-gpus检查电脑的GPU是否支持cuda编程,要求Compute Capability 3.0及以上。
1. 安装Visual Studio 2015(需要在CUDA之前安装好)
2. 安装CUDA toolkit 8.0(推荐下载离线版本)
3. 解压下载好的cuDNN,将其中的 binincludelib 覆盖到CUDA的安装目录

二、安装tensorflow-gpu

1. 安装anaconda
2. 进入python3.5环境,通过以下命令安装tensorflow-gpu:

pip install --ignore-installed --upgrade tensorflow-gpu

3. 如果遇到各种lib、dll缺失的问题,可以尝试通过添加对应的路径到path中来解决
  • 在CUDA_PATH后面添加bin和lib\x64路径
  • 将cudnn64_7改为cudnn64_6
4. 测试代码

如下:

#Creates a graph.
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
#Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
#Runs the op.
print sess.run(c)

三、编译tensorflow-gpu

一些说明:使用pip或anaconda等方式安装的预编译好的tensorflow没有AVX2指令集加速,通过手动编译可以更好的利用GPU。但是如果没有AVX或者GPU的话,手动编译几乎没有优势。

官方只提供了Ubuntu和Mac OS X的编译支持,在Windows下可以通过Bazel和CMake两种方式进行编译,但只是 “highly experimental”,可能会遇到各种错误。下面使用CMake来进行编译。

0. 进行编译之前安装一下软件:
  • Cmake 3.5以上
  • Git
  • swig

swig安装方法:

1.下载Swig for Windows:http://www.swig.org/download.html

2 解压 .zip 文件到目录,比如:D:\backupsoftware

3 添加环境变量到path, 比如: D:\backupsoftware\swigwin-2.0.9

4 添加环境变量 JAVA_INCLUDE 和 JAVA_BIN

JAVA_INCLUDE : Set this to the directory containing jni.h
JAVA_BIN : Set this to the bin directory containing javac.exe

Example using JDK1.3:
JAVA_INCLUDE: D:\jdk1.3\include
JAVA_BIN: D:\jdk1.3\bin

Note:D:\backupsoftware\swigwin-2.0.9 下面有 swig.exe 文件,注意这个目录。


简单测试安装是否成功:

打开Dos,在命令行执行: swig --help, 显示 Target Language Options即表明安装成功。

Windows下的额外要求:

  • Visual Studio 2015
  • Python 3.5
  • Numpy 1.11.0 or later
1. 设置环境变量

命令行执行:set PreferredToolArchitecture=x64

  • 进入目录 C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\,运行命令 vcvarsall amd64,从64位命令行编译到64位的目标平台。执行这一步可以避免编译的时候出现内存不足的错误。
  • 将CUDA dlls和cuDNN dll添加到环境变量: 
  • C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin
    cudnn-8.0-windows7-x64-v7\cuda\bin
  • 将Cmake和git添加到环境变量 
    CMake\bin
2. 下载 Tensorflow源代码, 选择版本r1.6,然后下载,
在其中的 tensorflow\tensorflow\contrib\cmake 目录下新建 build 目录,用于存放编译结果。
D:\temp> git clone https://github.com/tensorflow/tensorflow.git
D:\temp> cd tensorflow\tensorflow\contrib\cmake
D:\temp\tensorflow\tensorflow\contrib\cmake> mkdir build
D:\temp\tensorflow\tensorflow\contrib\cmake> cd build
D:\temp\tensorflow\tensorflow\contrib\cmake\build>
3. 调用cmake来产生Visual Studio solution 和project files.
D:\...\build> cmake .. -A x64 -T host=x64 -DCMAKE_BUILD_TYPE=Release ^
-DSWIG_EXECUTABLE=D:/soft/swigwin-3.0.12/swig.exe ^
-DPYTHON_EXECUTABLE=D:/ProgramData/Miniconda3/python.exe ^
-DPYTHON_LIBRARIES=D:/ProgramData/Miniconda3/libs/python36.lib ^
-Dtensorflow_ENABLE_GPU=ON ^
-DCUDNN_HOME="D:\soft\python\cudnn7"

如果编译库,需要加这个参数:-Dtensorflow_BUILD_SHARED_LIB=ON

如果有NVIDIA显卡,且安装好了cuDNN,那么可以添加以下参数:

More? -Dtensorflow_ENABLE_GPU=ON ^
More? -DCUDNN_HOME="D:\...\cudnn"

4. 调用MSBiuld来编译Tensorflow

使用以下命令产生.whl文件

D:\...\build> MSBuild /p:Configuration=Release tf_python_build_pip_package.vcxproj

还有一种命令: MSBuild /p:Configuration=Release /p:Platform=x64 tf_python_build_pip_package.vcxproj

编译成功后,whl路径;

D:\soft\tensorflow-r1.6\tensorflow\contrib\cmake\build\tf_python\dist

遇到的问题:

点击生成解决方案,编译的时间很长,我的笔记本电脑编译一次大概3到4个小时,编译到最后一般都会报错

fatal error C1060: compiler is out of heap space 没关系,等待整个工程全部编译完成(据说内存特别大的电脑不会报)

  • error C2001: newline in constant 
    打开对应的文件发现出现了乱码,google之后发现可以通过修改locale为 英语(美国) 来解决。
  • 编译过程中下载失败或者 git clone缓慢 
    使用命令 nslookup 查找失效ip(github.global.ssl.fastly.net,storage.googleapis.com等),修改host文件。完了之后 ipconfig /flushdns。当然也可以尝试手动下载到对应的目录。

我这里的报错:

  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cinttypes(20): error C2039: “imaxdiv_t”: 不是“`global namespace'”的成员 [D:\soft\python\tensorflow-master\tensorflow
\contrib\cmake\build\grpc\src\grpc\grpc_unsecure.vcxproj] [D:\soft\python\tensorflow-master\tensorflow\contrib\cmake\build\grpc.vcxproj]
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cinttypes(20): error C2873: “imaxdiv_t”: 符号不能用在 using 声明中 [D:\soft\python\tensorflow-master\tensorflow\contrib\
cmake\build\grpc\src\grpc\grpc_unsecure.vcxproj] [D:\soft\python\tensorflow-master\tensorflow\contrib\cmake\build\grpc.vcxproj]
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cinttypes(22): error C2039: “imaxabs”: 不是“`global namespace'”的成员 [D:\soft\python\tensorflow-master\tensorflow\c
ontrib\cmake\build\grpc\src\grpc\grpc_unsecure.vcxproj] [D:\soft\python\tensorflow-master\tensorflow\contrib\cmake\build\grpc.vcxproj]
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cinttypes(22): error C2873: “imaxabs”: 符号不能用在 using 声明中 [D:\soft\python\tensorflow-master\tensorflow\contrib\cm
ake\build\grpc\src\grpc\grpc_unsecure.vcxproj] [D:\soft\python\tensorflow-master\tensorflow\contrib\cmake\build\grpc.vcxproj]
  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\cinttypes(22): error C2039: “imaxdiv”: 不是“`global namespace'”的成员 

服务器上jdk_include添加了,报错:

D:\soft\tensorflow-master\tensorflow\c\python_api.cc(19): fatal error C1083: 无法打开包括文件: “tensorflow/python/framework/c
pp_shape_inference.pb.h”: No such file or directory [D:\soft\tensorflow-master\tensorflow\contrib\cmake\build\tf_c_pyth
on_api.vcxproj]

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值