windows10下如何进行源码编译安装tensorflow
1.获取python3.5.x
https://www.python.org/ftp/python/3.5.4/python-3.5.4-amd64.exe
2.安装python3.5.x,默认安装即可,安装后将路径添加到环境变量PATH中
笔者安装后python的路径为:C:\Users\jello\AppData\Local\Programs\Python\Python35
pip路径为:C:\Users\jello\AppData\Local\Programs\Python\Python35\Scripts
3.安装tensorflow的依赖包
pip3 install six numpy wheel
pip3 install keras_applications==1.0.6 --no-deps
pip3 install keras_preprocessing==1.0.5 --no-deps
4.获取MSYS2
http://repo.msys2.org/distrib/x86_64/msys2-x86_64-20180531.exe
5.安装MSYS2
5.1添加路径C:\msys64\usr\bin到PATH环境变量
5.2安装git patch unzip等软件
pacman -S git patch unzip
6.获取Visual C++ Build Tools 2015
https://www.microsoft.com/en-us/download/details.aspx?id=48159
7.安装Visual C++ Build Tools 2015
8.获取Microsoft Visual C++ 2015 Redistributable Update 3
https://www.microsoft.com/en-us/download/details.aspx?id=53587
9.安装Microsoft Visual C++ 2015 Redistributable Update 3 (如果安装无法进行,可能有其它版本,卸载最新版本即可,如安装2013 和2017,那么卸载2017这个版本即可)
10.获取Visual Studio 2015
https://download.microsoft.com/download/b/e/d/bedddfc4-55f4-4748-90a8-ffe38a40e89f/vs2015.3.com_enu.iso
11.安装Visual Studio 2015
12.获取bazel
https://github.com/bazelbuild/bazel/releases (从此链接选择合适的版本即可)
13.安装bazel (依赖MSYS2和Visual C++ Build Tools 2015),安装方法如下
将文件bazel-0.21.0-windows-x86_64重命名为bazel,然后将文件bazel放到C:\msys64\usr\bin目录下
14.获取tensorflow源码
git clone https://github.com/tensorflow/tensorflow.git (如果使用git无法获取源码,提示fatal: BUG: disabling cancellation: Invalid argument,那么直接去github上下载源码压缩包即可)
cd tensorflowgit checkout remotes/origin/r1.13
15.配置tensorflow
python ./configure.py
笔者的配置如下:
C:\Users\jello\tensorflow>python ./configure.py
WARNING: The following rc files are no longer being read, please transfer their contents or import their path into one of the standard rc files:
nul
WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown".
INFO: Invocation ID: 89e98bd0-d528-40ac-ac67-87aa0682b890
You have bazel 0.21.0 installed.
Please specify the location of python. [Default is C:\Users\jello\AppData\Local\Programs\Python\Python35\python.exe]:
Found possible Python library paths:
C:\Users\jello\AppData\Local\Programs\Python\Python35\lib\site-packages
Please input the desired Python library path to use. Default is [C:\Users\jello\AppData\Local\Programs\Python\Python35\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 ROCm support? [y/N]: n
No ROCm 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.
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]:
Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details.
16.构建tensorflow pip包
方法一:在cmd中构建
16.1
设置变量
set BAZEL_VS=C:\Program Files (x86)\Microsoft Visual Studio 14.0 (这个变量是用来告知bazel,Visual Studio所安装的目录)
16.2
bazel build -c opt --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-O3 //tensorflow/tools/pip_package:build_pip_package
16.3
bazel-bin\tensorflow\tools\pip_package\build_pip_package C:/tmp/tensorflow_pkg
方法二:在MSYS2中构建
16.1 启动MSYS2
16.2 禁用MSYS2路径转换
export MSYS_NO_PATHCONV=1
export MSYS2_ARG_CONV_EXCL="*"
16.3 设置环境变量
export PATH="/c/Users/jello/AppData/Local/Programs/Python/Python35:$PATH"
(将python的路径加入环境变量PATH中)
16.4 获取tensorflow源码
16.5
bazel build -c opt --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-O3 //tensorflow/tools/pip_package:build_pip_package
16.6
bazel-bin\tensorflow\tools\pip_package\build_pip_package C:/tmp/tensorflow_pkg
17.安装tensorflow
pip3 install C:/tmp/tensorflow_pkg/tensorflow-version-cp35-cp35m-win_amd64.whl
18.参考资料
https://www.tensorflow.org/install/source_windows