目前tensorflow官方暂不支持windows下debug模式编译,网上几乎全是release模式编译的资料。Debug模式下tensorflow官方配置存在一些问题,致使默认的方法编译不经过。html
1、环境配置:node
1 VS2015 update3python
2 Swigwinios
3 python3.5:安装时选择安装python debug包,将安装路径添加环境变量;c++
4 CMake:将安装路径添加环境变量;git
5 git:将安装路径添加环境变量;github
6 Tensorflow源码:从Github上下载r1.3.0[1]shell
2、预编译windows
1 修改Cmakelist.txt函数
打开文件 ~/tensorflow/contrib/cmake/CMakeLists.txt,找到相似代码并修改[2][3]:
if (tensorflow_OPTIMIZE_FOR_NATIVE_ARCH)
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_OPT_ARCH_NATIVE_SUPPORTED)
if (COMPILER_OPT_ARCH_NATIVE_SUPPORTED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
else()
CHECK_CXX_COMPILER_FLAG("/arch:AVX" COMPILER_OPT_ARCH_AVX_SUPPORTED)
if(COMPILER_OPT_ARCH_AVX_SUPPORTED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX")
endif()
endif()
endif()
2 CMake预编译
(1)新建文件夹
在 ~\tensorflow\contrib\cmake\ 新建 build 文件夹
(2)管理员身份运行开发人员命令提示符
(3)输入powershell,切换到build文件夹
(4)输入命令,预编译项目
输入如下命令,路径替换为实际路径便可,python的库选择debug库,须要在安装python时勾选debug安装包。
Tensorflow1.3版本:
cmake .. -A x64 -DCMAKE_BUILD_TYPE=Debug -DSWIG_EXECUTABLE=D:/yangkun/tensorflow/swigwin-3.0.12/swig.exe -DPYTHON_EXECUTABLE=C:/Users/IPC-Station/AppData/Local/Programs/Python/Python35/python.exe -DPYTHON_LIBRARIES=C:/Users/IPC-Station/AppData/Local/Programs/Python/Python35/libs/python35_d.lib -Dtensorflow_BUILD_SHARED_LIB=ON -Dtensorflow_ENABLE_GRPC_SUPPORT=OFF
Tensorflow1.9,1.10版本:
cmake .. -A x64 -DCMAKE_BUILD_TYPE=Debug -DSWIG_EXECUTABLE=D:/yangkun/tensorflow/swigwin-3.0.12/swig.exe -DPYTHON_EXECUTABLE=C:/Users/IPC-Station/AppData/Local/Programs/Python/Python35/python.exe -DPYTHON_LIBRARIES=C:/Users/IPC-Station/AppData/Local/Programs/Python/Python35/libs/python35_d.lib -Dtensorflow_BUILD_SHARED_LIB=ON -Dtensorflow_ENABLE_GRPC_SUPPORT=ON
3、使用VS2015编译
1 准备文件
文件下载可能失败,可本身提早下载相应文件并拷贝到 ~\tensorflow\contrib\cmake\build\downloads 文件夹下
2 使用MSBuild编译
(1)输入以下命令行编译[4][5][6]
msbuild /p:Configuration=Debug /p:Platform=x64 /m:1 /p:CL_MPCount=1 /p:PreferredToolArchitecture=x64
ALL_BUILD.vcxproj
使用MSBuild命令行成功命令以下:
【1】(未改MP,tensorflow1.3):[5]
cmake .. -A x64 -DCMAKE_BUILD_TYPE=Debug -DSWIG_EXECUTABLE=D:/yangkun/tensorflow/swigwin-3.0.12/swig.exe -DPYTHON_EXECUTABLE=C:/Users/IPC-Station/AppData/Local/Programs/Python/Python35/python.exe -DPYTHON_LIBRARIES=C:/Users/IPC-Station/AppData/Local/Programs/Python/Python35/libs/python35_d.lib -Dtensorflow_BUILD_SHARED_LIB=ON -Dtensorflow_ENABLE_GRPC_SUPPORT=OFF
msbuild /p:Configuration=Debug /p:Platform=x64 /m:1 /p:PreferredToolArchitecture=x64 ALL_BUILD.vcxproj
【2】(未改MP,tensorflow1.3):[10-15]
cmake .. -A x64 -DCMAKE_BUILD_TYPE=Debug -DSWIG_EXECUTABLE=D:/yangkun/tensorflow/swigwin-3.0.12/swig.exe -DPYTHON_EXECUTABLE=C:/Users/IPC-Station/AppData/Local/Programs/Python/Python35/python.exe -DPYTHON_LIBRARIES=C:/Users/IPC-Station/AppData/Local/Programs/Python/Python35/libs/python35_d.lib -Dtensorflow_BUILD_SHARED_LIB=ON -Dtensorflow_ENABLE_GRPC_SUPPORT=OFF
msbuild /p:Configuration=Debug /p:Platform=x64 /m:1 /p:PreferredToolArchitecture=x64 tensorflow.sln
【3】(未改MP, tensorflow1.9 or tensorflow1.10):[11]
cmake .. -A x64 -T host=x64 -DCMAKE_BUILD_TYPE=Debug -DSWIG_EXECUTABLE=E:/Research/CNNT/1Tensorflow/swigwin-3.0.12/swig.exe -DPYTHON_EXECUTABLE=C:/Users/yk_ya/AppData/Local/Programs/Python/Python35/python.exe -DPYTHON_LIBRARIES=C:/Users/yk_ya/AppData/Local/Programs/Python/Python35/libs/python35_d.lib -Dtensorflow_BUILD_SHARED_LIB=ON -Dtensorflow_ENABLE_GRPC_SUPPORT=ON
msbuild /p:Configuration=Debug /p:Platform=x64 /m:1 /p:CL_MPCount=1 /p:PreferredToolArchitecture=x64 ALL_BUILD.vcxproj
注意,若是编译tensorflow1.3以上版本,先进行四 Error中的第3步和第4步。
4、Error
1 在编译tensorflow1.3时出现re2报错,错误信息:error C2001: 常量中有换行符[16]:
(1) 用 VS 打开 ~\tensorflow\contrib\cmake\build\re2\src\re2 中的 RE2.sln
(2)分别将 re2_test.cc 和 search_test.cc 的编码方式修改掉:
双击打开cc文件→单击菜单栏中的文件(F)→高级保存选项(文件下拉菜单中没有高级保存选项的请自行百度一下,设置一下就有了),改为UTF-8带签名,而后保存。(貌似亦可生成RE2的ALL_BUILD)
(3)从新输入命令编译
2 各个版本均会出现的错误:"fatal error C1060: compiler is out of heap space":
错误缘由:编译器堆栈空间不足,通常出如今tf_core_kernels项目中。
修改方法:
(1)若是用命令行编译,则指定相应的并行数目,如/m:1 /p:CL_MPCount=1,可适当增长以节省编译时间
(2)将 "CMakeLists.txt" 和 "tf_core_kernels.cmake" 中的 /MP 改成 /MP2[7][8],/MP默认采用全部核数[10]
(3)若是采用IDE编译时,则修改工具中的最大并行项目数(如改成1),并在对应项目(通常tf_core_kernels)的多处理器编译选项中选择否。
3 编译tensorflow1.10出现错误:grpc++_unsecure.lib(client_context.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in print_model_analysis.obj
错误缘由:在debug下调用了release 版本的 grpc 库
修改方法:将文件 ~\tensorflow\contrib\cmake\external\grpc.cmake文件中的Release替换为Debug
4 编译tensorflow1.10出现错误:LINK : fatal error LNK1189: library limit of 65535 objects exceeded
错误缘由:tensorflow.dll 导出的函数太多,超过了 65535 这个限制
修改方法:修改 ~\tensorflow\contrib\cmake\tools\create_def_file.py 中的 EXCLUDE_RE 变量。修改成[17]:
EXCLUDE_RE = re.compile(r"RTTI|deleting destructor|::internal::|::`anonymous namespace'::|<lambda_[0-9a-z]+>|" r"std::_Vector_iterator<|std::_Vector_const_iterator<|std::_Vector_alloc<|" r"std::_Deque_iterator<|std::_Deque_alloc<|" r"std::_Tree_iterator<|std::_Tree_const_iterator<|std::_Tree_unchecked_const_iterator<|std::_Tree_comp_alloc<|std::_Tree_node<|" r"std::_List_iterator<|std::_List_const_iterator<|std::_List_unchecked_const_iterator<|std::_List_alloc<|" r"std::_Iterator012<|std::_Compressed_pair<" )
Ref:
[1] https://github.com/tensorflow/tensorflow/tree/r1.3
[2] http://www.javashuo.com/article/p-eurztsuk-ce.html
[3] https://blog.csdn.net/xierhacker/article/details/80957125
[6] https://docs.microsoft.com/zh-cn/visualstudio/msbuild/msbuild-command-line-reference?view=vs-2015
[8] https://github.com/tensorflow/tensorflow/search?l=CMake&q=%2FMP&type=&utf8=%E2%9C%93
[9] https://github.com/tensorflow/tensorflow/issues/15393
[10] https://docs.microsoft.com/en-us/previous-versions/bb385193(v=vs.140)
[11] https://github.com/tensorflow/tensorflow/issues/11096
[12] https://github.com/tensorflow/tensorflow/issues/14801
[13] https://github.com/tensorflow/tensorflow/issues/11771
[14] https://github.com/tensorflow/tensorflow/issues/10799
[15] http://twonightmare.blogspot.com/2017/08/tensorflow-gpu-build-for-windows-for.html
[16] https://blog.csdn.net/yuejisuo1948/article/details/84197534
[17] https://icedream2linxi.github.io/blog/2018/07/08/Windows下编译Tensorflow-C++-API-v1.8-Debug版动态库
Others:
[18] https://zhuanlan.zhihu.com/p/29029860
[19] https://joe-antognini.github.io/machine-learning/build-windows-tf
[20] https://blog.csdn.net/whunamikey/article/details/82143334
[21] http://www.voidcn.com/article/p-fobvxpld-bmr.html
[22] https://github.com/tensorflow/tensorflow/issues/18931
[23] https://netoken27.blogspot.com/2019/02/tensorflow-110python.html