目前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