【Tensorflow】Win10以bazel方式源码编译安装tensorflow cpu版本

版权声明:本文为博主原创文章,未经作者允许请勿转载。 https://blog.csdn.net/heiheiya https://blog.csdn.net/heiheiya/article/details/88891149

环境:win10

           python3.6.3

          visual studio 2015

一、安装visual studio 2015

visual studio下载地址下载 visual studio 2015,安装安装向导安装,注意要选择安装Visual C++,这个默认是不安装的。

在下载页面“可再发行组件和生成工具”,下载并安装:

1. Microsoft Visual C++ 2015 Redistributable 更新 3

2. Microsoft 生成工具 2015 更新 3

二、安装python

这里我用anaconda的方式安装,安装过程可以参考链接【Tensorflow】(一):环境配置(Anaconda)

三、安装java 8

安装过程可以参考链接:【Android】Win10 android studio开发环境配置

四、安装tensorflow pip依赖包

pip install six numpy wheel
pip install keras_applications==1.0.5 --no-deps
pip install keras_preprocessing==1.0.3 --no-deps

五、安装bazel

bazel下载地址下载一个windows的exe版本,这里我下载的是bazel 0.11.0。

bazel-0.11.0-windows-x86_64.exe重命名为bazel.exe。

将bazel.exe所在路径加入Path环境变量。

六、安装MSYS2

msys2下载地址下载对应版本的安装包,安装过程在这个链接里也有详细说明。

将安装路径的C:\msys64\usr\bin添加到Path环境变量。

新建环境变量BAZEL_SH=C:\msys64\usr\bin\bash.exe。

新建环境变量BAZEL_VC=C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC或者BAZEL_VS=C:\Program Files (x86)\Microsoft Visual Studio 14.0。

使用cmd.exe运行以下命令:

pacman -S git patch unzip

七、安装git

【Git】Git tortoisegit配置

八、编译tensorflow源码

首先下载源码。

git clone https://github.com/tensorflow/tensorflow.git

因为这里我编译tensorflow 1.9版本,所以切换分支

cd tensorflow
git checkout r1.9

在cmd.exe执行配置脚本

python ./configure.py

相关的配置选项

WARNING: Running Bazel server needs to be killed, because the startup options are different.
You have bazel 0.11.0 installed.
Please specify the location of python. [Default is D:\software\Anaconda3\envs\mytensorflow\python.exe]:


Found possible Python library paths:
  D:\software\Anaconda3\envs\mytensorflow\lib\site-packages
Please input the desired Python library path to use.  Default is [D:\software\Anaconda3\envs\mytensorflow\lib\site-packages]

Do you wish to build TensorFlow with XLA JIT support? [y/N]: N
No XLA JIT support will be enabled for TensorFlow.

Do you wish to build TensorFlow with GDR support? [y/N]: N
No GDR support will be enabled for TensorFlow.

Do you wish to build TensorFlow with VERBS support? [y/N]: N
No VERBS support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]: N
No CUDA support will be enabled for TensorFlow.

Do you wish to build TensorFlow with MPI support? [y/N]: N
No MPI support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]:


Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: N
Not configuring the WORKSPACE for Android builds.

Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See tools/bazel.rc for more details.
        --config=mkl            # Build with MKL support.
        --config=monolithic     # Config for mostly static monolithic build.

开始bazel构建

bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

构建过程中报了Cannot open compiler generated file: '': Invalid argument的错误。解决方法请参考链接 【Tensorflow】Windows bazel编译tensorflow“Cannot open compiler generated file: '': Invalid argument”错误

编译时间比较长,成功之后输出如下信息:

编译完成之后就可以构建软件包。

bazel-bin\tensorflow\tools\pip_package\build_pip_package tmp/tensorflow_pkg

构建完成之后生成一个.whl文件。

九、安装测试

现在来安装一下这个编译好的软件包。

我在anaconda新建一个mytensorflow的虚拟环境。

conda create -n mytensorflow python=3.6

激活这个环境。

activate mytensorflow

安装软件包。

pip install D:\software\tensorflow_pkg\tensorflow-1.9.0-cp36-cp36m-win_amd64.whl

安装完成之后,测试一下功能。进入python环境,在终端输入

import tensorflow as tf
a = tf.constant(3)
b = tf.constant(4)
sess = tf.Session()
print(sess.run(a+b))

输出

证明基本功能是可用的。

猜你喜欢

转载自blog.csdn.net/heiheiya/article/details/88891149