windows环境VS2015 Debug模式编译Tensorflow(CPU)C++ 源码

目前tensorflow官方暂不支持windows下debug模式编译,网上几乎全是release模式编译的资料。Debug模式下tensorflow官方配置存在一些问题,导致默认的方法编译不通过。

 

一、环境配置:

VS2015 update3

Swigwin

python3.5:安装时选择安装python debug包,将安装路径添加环境变量;

CMake:将安装路径添加环境变量;

git:将安装路径添加环境变量;

6 Tensorflow源码:从Github上下载r1.3.0[1]

 

二、预编译

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

 

三、使用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步。

 

四、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] https://www.cnblogs.com/jliangqiu2016/p/7642471.html

[3] https://blog.csdn.net/xierhacker/article/details/80957125

[4] https://stackoverflow.com/questions/35974304/c-how-to-limit-visual-studios-multi-processor-compilation

[5] https://icedream2linxi.github.io/blog/2018/07/08/Windows%E4%B8%8B%E7%BC%96%E8%AF%91Tensorflow-C++-API-v1.8-Release%E7%89%88%E5%8A%A8%E6%80%81%E5%BA%93

[6] https://docs.microsoft.com/zh-cn/visualstudio/msbuild/msbuild-command-line-reference?view=vs-2015

[7] https://stackoverflow.com/questions/50155807/tensorflow-cpu-performance-anaconda-vs-custom-build-cmake-on-windows

[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

 

转载于:https://www.cnblogs.com/YO-OY/p/11067600.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值