windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib

 目录

0.最终环境

1.安装 vs2015、cuda9.0、python3.5+

2.安装 MSYS2

3.安装 Bazel

4.下载tensorflow-v1.12

5.修改文件配置

6.使用powershell进行配置与编译

7.vs无法解析的外部符号与powershell编译出现无法解析的外部符号错误

8.准备tensorflow的dll、lib、include

8.1 简版tensorflow的dll、lib、include

9.调用模型测试程序

10.尝试编译debug版本

11.遇到的问题

12.参考


这篇博客写着,是因为windows+bazel+tensorflow-r1.12(GPU)编译生成dll与lib,上一篇,虽然编译成功了,但是在使用tensorflow\examples\label_image\main.cc怎么都不能成功生成解决方案,因为缺很多链接库。所以写了这篇。

其中,我已经编译好的tensorflow-v1.12.0 dll,lib,include,如何出现缺链接库的话,自己可以将错误的符号加入tf_exported_symbols_msvc.lds文件中,自己编译生成自己的dll与lib文件:

下载链接:

tensorflow-v1.12.0 dll,lib,include

提取码:

0ugf
   
   
   
   

0.最终环境


   
   
   
   
  1. win10
  2. vs2015
  3. cuda9 .0
  4. cudnn7 .6
  5. python3 .5 .2
  6. tensorflow-v1 .12 .0
  7. bazel0 .22 .0
  8. msys2-x86_64-20190524

1.安装 vs2015、cuda9.0、python3.5+

2.安装 MSYS2

http://www.msys2.org/下载,

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第1张图片

其中这个最好按照默认路径安装,否则编译时没过几秒钟,可能会出现下面的问题:

/usr/bin/env: 'python': No such file or directory windows
   
   
   
   

 所有安装步骤都默认一路next下去,安装完成以后,需要配置:

  • 将目录C:\msys64和C:\msys64\usr\bin 加入系统环境变量path
  • 打开cmd.exe,输入一下命令(有时可能安装不了,多试几次好了):
pacman -Syuu patch
   
   
   
   

3.安装 Bazel

在github下载Bazel,https://github.com/bazelbuild/bazel/releases,这里我下载的bazel-0.22.0-windows-x86_64.exe

将下载好的 bazel-0.22.1-windows-x86_64.exe复制到C:\msys64下,更名为bazel.exe。

配置bazel:新建环境变量:BAZEL_SH,BAZEL_VC ,BAZEL_VS

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第2张图片

 

4.下载tensorflow-v1.12

git与直接下载都可以,我是直接下载的压缩包。

5.修改文件配置

此处参考https://github.com/guikarist/tensorflow-windows-build-script

1.将tensorflow-1.12.0.zip解压到tensorflow-1.12.0目录下,并将目录下的tensorflow-1.12.0文件夹重命名为source;

2.下载tensorflow-windows-build-script的压缩包,解压,将其中的patchs与build.ps.1复制到tensorflow-1.12.0目录下;

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第3张图片

3.将patchs下的eigen_half.patch复制到tensorflow-1.12.0\source\third_party下;

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第4张图片

 4.将patchs下的tf_exported_symbols_msvc.lds复制到tensorflow-1.12.0\source\tensorflow下,这里使用我编译使用过的tf_exported_symbols_msvc.lds替换;

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第5张图片

 5.可以使用Notepad++修改build.ps1文件如下(后面可能会出现Copy-Item命令的问题);

修改 line154-182:


   
   
   
   
  1. # Apply patches to source.
  2. if ( $buildVersion -eq "v1.11.0") {
  3. # Eigen Patch for v1.11.0
  4. git apply --ignore-space-change --ignore-white "..\patches\eigen.1.11.0.patch"
  5. Copy-Item ..\patches\eigen_half.patch third_party\
  6. } elseif ( $buildVersion -eq "v1.12.0") {
  7. # Eigen Patch for v1.12.0
  8. git apply --ignore-space-change --ignore-white "..\patches\eigen.1.12.0.patch"
  9. Copy-Item ..\patches\eigen_half.patch third_party\
  10. } elseif ( $buildVersion -eq "v1.13.1") {
  11. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  12. (Invoke-WebRequest https://github.com/tensorflow/tensorflow/commit/ec727016282383aacf9d26386b01f6bdbd65b14b.patch).Content | git apply -v --ignore-space-change --ignore-white
  13. }
  14. if ( $BuildCppAPI) {
  15. if ( $buildVersion -eq "v1.11.0") {
  16. # C++ Symbol Patch for v1.11.0
  17. git apply --ignore-space-change --ignore-white "..\patches\cpp_symbol.1.11.0.patch"
  18. Copy-Item ..\patches\tf_exported_symbols_msvc.lds tensorflow\
  19. } elseif ( $buildVersion -eq "v1.12.0") {
  20. # C++ Symbol Patch for v1.12.0
  21. git apply --ignore-space-change --ignore-white "..\patches\cpp_symbol.1.12.0.patch"
  22. Copy-Item ..\patches\tf_exported_symbols_msvc.lds tensorflow\
  23. } elseif ( $buildVersion -eq "v1.13.1") {
  24. # C++ Symbol Patch for v1.13.1
  25. git apply --ignore-space-change --ignore-white "..\patches\cpp_symbol.1.13.1.patch"
  26. Copy-Item ..\patches\tf_exported_symbols_msvc.lds tensorflow\
  27. }
  28. }

为:


   
   
   
   
  1. 为:
  2. # Apply patches to source.
  3. if ( $buildVersion -eq "v1.11.0") {
  4. # Eigen Patch for v1.11.0
  5. git apply --ignore-space-change --ignore-white "..\patches\eigen.1.11.0.patch"
  6. Copy-Item ..\patches\eigen_half.patch third_party\
  7. } elseif ( $buildVersion -eq "v1.12.0") {
  8. # Eigen Patch for v1.12.0
  9. git apply --ignore-space-change --ignore-white "..\patches\eigen.1.12.0.patch"
  10. #Copy-Item ..\patches\eigen_half.patch third_party\
  11. } elseif ( $buildVersion -eq "v1.13.1") {
  12. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  13. (Invoke-WebRequest https://github.com/tensorflow/tensorflow/commit/ec727016282383aacf9d26386b01f6bdbd65b14b.patch).Content | git apply -v --ignore-space-change --ignore-white
  14. }
  15. if ( $BuildCppAPI) {
  16. if ( $buildVersion -eq "v1.11.0") {
  17. # C++ Symbol Patch for v1.11.0
  18. git apply --ignore-space-change --ignore-white "..\patches\cpp_symbol.1.11.0.patch"
  19. Copy-Item ..\patches\tf_exported_symbols_msvc.lds tensorflow\
  20. } elseif ( $buildVersion -eq "v1.12.0") {
  21. # C++ Symbol Patch for v1.12.0
  22. git apply --ignore-space-change --ignore-white "..\patches\cpp_symbol.1.12.0.patch"
  23. #Copy-Item ..\patches\tf_exported_symbols_msvc.lds tensorflow\
  24. } elseif ( $buildVersion -eq "v1.13.1") {
  25. # C++ Symbol Patch for v1.13.1
  26. git apply --ignore-space-change --ignore-white "..\patches\cpp_symbol.1.13.1.patch"
  27. Copy-Item ..\patches\tf_exported_symbols_msvc.lds tensorflow\
  28. }
  29. }

6.使用powershell进行配置与编译

1.打开powershell

输入下面命令,这是bazel编译的选项,为不同的语言(python、c、c++)其//后对应的都不一样:

$parameterString = "--config=opt --config=cuda --define=no_tensorflow_py_deps=true --copt=-nvcc_options=disable-warnings //tensorflow:libtensorflow_cc.so --verbose_failures"
   
   
   
   

bazel编译的选项,为不同的语言(python、c、c++)其//后对应的都不一样,参考https://github.com/guikarist/tensorflow-windows-build-script:

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第6张图片

然后输入:


   
   
   
   
  1. .\build.ps1 `
  2. -BazelBuildParameters $parameterString `
  3. -BuildCppAPI -ReserveSource

会出现build.ps1错误:无法加载文件 ******.ps1,因为在此系统中禁止执行脚本,参考https://www.cnblogs.com/zhaozhan/archive/2012/06/01/2529384.html,解决输入一下命令:

set-executionpolicy remotesigned
   
   
   
   

下面执行开始需要互动配置了如下(如果出错重新编译的话,最好删除编译目录下,建的venv文件夹):


   
   
   
   
  1. (venv) PS D:\Users\tensorflow-1.12.0> .\build.ps1 `
  2. >> -BazelBuildParameters $parameterString `
  3. >> -BuildCppAPI -ReserveSource
  4. Select a Tensorflow version:
  5. [1] 1 - v1.13.1 [2] 2 - v1.12.0 [3] 3 - v1.11.0 [S] Select another version [?] 帮助 (默认值为“1”): 2
  6. ...
  7. Starting local Bazel server and connecting to it...
  8. .
  9. INFO: Starting clean.
  10. WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown".
  11. You have bazel 0.15.0 installed.
  12. Do you wish to build TensorFlow with Apache Ignite support? [Y/n]: n
  13. No Apache Ignite support will be enabled for TensorFlow.
  14. Do you wish to build TensorFlow with XLA JIT support? [y/N]: n
  15. No XLA JIT support will be enabled for TensorFlow.
  16. Do you wish to build TensorFlow with ROCm support? [y/N]: n
  17. No ROCm support will be enabled for TensorFlow.
  18. Do you wish to build TensorFlow with CUDA support? [y/N]: y
  19. CUDA support will be enabled for TensorFlow.
  20. Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]:
  21. Please specify the location where CUDA 9.0 toolkit is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0]: D:\Users\CUDA9.0
  22. Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]:
  23. Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is D:/Users/CUDA9.0]:
  24. Please specify a list of comma-separated Cuda compute capabilities you want to build with.
  25. You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
  26. Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 3.5,7.0]: 5.0
  27. Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]:
  28. Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]: y
  29. Eigen strong inline overridden.
  30. Starting local Bazel server and connecting to it...
  31. .....................
  32. WARNING: The following configs were expanded more than once: [cuda]. For repeatable flags, repeats are counted twice and may lead to unexpected behavior.

编译好以后,将

 

7.vs无法解析的外部符号与powershell编译出现无法解析的外部符号错误

出现下面问题都按这个步骤做,只不过第4步中,vs是加上,powershell可能会加上或者去掉tf_exported_symbols_msvc.lds文件EXPORTS中的链接:

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第7张图片

1.如果是vs无法解析的外部符号,这样解决,错误与解决方案如下(为了能清):

  • 错误:
无法解析的外部符号 "public: virtual __cdecl tensorflow::internal::LogMessage::~LogMessage(void)" (??1LogMessage@internal@tensorflow@@UEAA@XZ),该符号在函数 "public: void __cdecl tensorflow::internal::LogMessage::`vbase destructor'(void)" (??_DLogMessage@internal@tensorflow@@QEAAXXZ) 中被引用huawei_video_tensorflow
   
   
   
   
  • 解决方法:函数调用后与(该符号该符号在函数)前括号内的内容添加
??1LogMessage@internal@tensorflow@@UEAA@XZ
   
   
   
   

复制到tensorflow-1.12.0\source\tensorflow\tf_exported_symbols_msvc.lds文件的EXPORTS最后面。

2.如果powershell编译出现无法解析的外部符号错误

  • 错误:
无法解析的外部符号 "public: virtual __cdecl tensorflow::internal::LogMessage::~LogMessage(void)" (??1LogMessage@internal@tensorflow@@UEAA@XZ),该符号在函数 "public: void __cdecl tensorflow::internal::LogMessage::`vbase destructor'(void)" (??_DLogMessage@internal@tensorflow@@QEAAXXZ) 中被引用
   
   
   
   
  • 解决方法:(该符号该符号在函数)前括号内的内容,从tensorflow-1.12.0\source\tensorflow\tf_exported_symbols_msvc.lds文件中所有包含的部分全部删除:
??1LogMessage@internal@tensorflow@@UEAA@XZ

   
   
   
   

最后不断解决重复解决遇到的头文件与无法解析外部符号问题,终于编译工程并能够正常运行程序了。

通过这个步骤,在另外一台机器上也测试成功了:

 

8.准备tensorflow的dll、lib、include

新建tensorflow文件夹,然后在目录下再新建两个目录,dll与lib

dll:将上面图片中路径的libtensorflow_cc.so改名为tensorflow_cc.dll,放到tensorflow\dll目录下(vs调试时会调用的文件,路径要加入到环境变量中,或者也可以将文件加到当前工程运行的release目录下)

lib:将上面图片路径下的liblibtensorflow_cc.so.ifso改名为tensorflow_cc.lib,放到tensorflow\lib目录下 (路径要加入到vs包含库目录中,或将文件放到当前工程运行的release目录下,并添加库目录)

include:将bazel_source目录下,不管是快捷方式(可能会复制不全,可以去上图中目录下,找到对应文件,反正一定要复制全,不要有空目录,不然就会缺失头文件,报错),还是文件内的文件,都需要一起复制到tensorflow下。

下面是我自己编译好的tensorflow-v1.12.0 dll,lib,include,如何出现缺链接库的话,自己可以将错误的符号加入tf_exported_symbols_msvc.lds文件中,自己编译生成自己的dll与lib文件:

提取码:

0ugf
   
   
   
   

可以使用Dependency Walker工具查看该DLL:

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第8张图片

8.1 简版tensorflow的dll、lib、include

此处感谢cjAlgthm!!!

1.首先给出文件结构图:


   
   
   
   
  1. libtensorflow
  2. ├─bin
  3. │ tensorflow_cc.dll
  4. ├─include
  5. │ ...
  6. └─lib
  7. tensorflow_cc.lib

 2.下载链接:

链接:libtensorflow
提取码:rgzf 
 

3.使用

--解压后,将libtensorflow/include加入包含目录;

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第9张图片

--将lib路径包含到库目录,也可以放到对应工程的生成目录中,比如x64/Release/,同样也需要包含到库目录下;(这里我是放到对应工程的生成目录下,使用的相对路径)

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第10张图片

 且需要放到附加依赖项中:

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第11张图片

--将dll放到对应工程的生成目录中,比如x64/Release/,或者将路径添加到系统环境变量中也可以。

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第12张图片

4.如何整理此环境,可以参考

https://blog.csdn.net/carbon06/article/details/82588150

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第13张图片

5.对于想更改tensorflow_cc名的

请注意:


   
   
   
   
  1. 不要改,很麻烦,lib里面已经将dll名称写固定了;
  2. 想尝试的,备份!!!lib修改后,有可能损坏。

6.demo测试结果

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第14张图片

7.对tensorflow的库做了如下修改

修改了下面三个文件:

将这几个文件中的min与max修改成下面这样:


   
   
   
   
  1. string_view.h
  2. line491 -493
  3. private:
  4. static constexpr size_type kMaxSize =
  5. std::numeric_limits::max();
  6. 改为
  7. private:
  8. static constexpr size_type kMaxSize =
  9. ( std::numeric_limits::max)();

 

 

9.调用模型测试程序

这部分主要参考,以及博主的cv版本代码。

下面是我自己的批处理测试单张图像代码:


   
   
   
   
  1. #define COMPILER_MSVC
  2. #define NOMINMAX
  3. #define PLATFORM_WINDOWS // 指定使用tensorflow/core/platform/windows/cpu_info.h
  4. #include
  5. #include
  6. #include"tensorflow/core/public/session.h"
  7. #include "tensorflow/core/platform/env.h"
  8. #include
  9. using namespace tensorflow;
  10. using namespace cv;
  11. using std:: cout;
  12. using std:: endl;
  13. int main() {
  14. const std:: string model_path = "E:/c++work/tf_testdemo/tf_testdemo/latest_model_MobileUNet_0603.pb"; // tensorflow模型文件,注意不能含有中文
  15. const std:: string image_path = "E:/c++work/tf_testdemo/tf_testdemo/001_00045.png"; // 待inference的图片grace_hopper.jpg
  16. // 设置输入图像
  17. cv::Mat img = cv::imread(image_path);
  18. cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
  19. //resize(img, img, cv::Size(512, 512),0,0, INTER_NEAREST);
  20. int height = img.rows;
  21. int width = img.cols;
  22. int depth = img.channels();
  23. // 图像预处理
  24. img = (img - 0) / 255.0;
  25. img.convertTo(img, CV_32F);
  26. // 取图像数据,赋给tensorflow支持的Tensor变量中
  27. const float* source_data = ( float*)img.data;
  28. tensorflow:: Tensor input_tensor(DT_FLOAT, TensorShape({ 1, height, width, depth })); //这里只输入一张图片,参考tensorflow的数据格式NHWC
  29. auto input_tensor_mapped = input_tensor.tensor< float, 4>(); // input_tensor_mapped相当于input_tensor的数据接口,“4”表示数据是4维的。后面取出最终结果时也能看到这种用法
  30. // 把数据复制到input_tensor_mapped中,实际上就是遍历opencv的Mat数据
  31. for ( int i = 0; i < height; i++) {
  32. const float* source_row = source_data + (i * width * depth);
  33. for ( int j = 0; j < width; j++) {
  34. const float* source_pixel = source_row + (j * depth);
  35. for ( int c = 0; c < depth; c++) {
  36. const float* source_value = source_pixel + c;
  37. input_tensor_mapped( 0, i, j, c) = *source_value;
  38. //printf("%d");
  39. }
  40. }
  41. }
  42. // 初始化tensorflow session
  43. Session* session;
  44. Status status = NewSession(SessionOptions(), &session);
  45. if (!status.ok()) {
  46. std:: cerr << status.ToString() << endl;
  47. return -1;
  48. }
  49. else {
  50. cout << "Session created successfully" << endl;
  51. }
  52. // 读取二进制的模型文件到graph中
  53. tensorflow::GraphDef graph_def;
  54. status = ReadBinaryProto(Env::Default(), model_path, &graph_def);
  55. if (!status.ok()) {
  56. std:: cerr << status.ToString() << endl;
  57. return -1;
  58. }
  59. else {
  60. cout << "Load graph protobuf successfully" << endl;
  61. }
  62. // 将graph加载到session
  63. status = session->Create(graph_def);
  64. if (!status.ok()) {
  65. std:: cerr << status.ToString() << endl;
  66. return -1;
  67. }
  68. else {
  69. cout << "Add graph to session successfully" << endl;
  70. }
  71. // 输入inputs,“ x_input”是我在模型中定义的输入数据名称
  72. std:: vector< std::pair< std:: string, tensorflow::Tensor>> inputs = {
  73. { "input_x:0", input_tensor },
  74. };
  75. // 输出outputs
  76. //tensorflow::Tensor outputs(DT_FLOAT, TensorShape({ 1, height, width, depth }));
  77. std:: vector outputs;
  78. //批处理识别
  79. for ( int k = 0; k < 200; k++) {
  80. double start = clock();
  81. // 运行会话,计算输出"x_predict",即我在模型中定义的输出数据名称,最终结果保存在outputs中
  82. status = session->Run(inputs, { "outimage:0" }, {}, &outputs);
  83. if (!status.ok()) {
  84. std:: cerr << status.ToString() << endl;
  85. return -1;
  86. }
  87. else {
  88. cout << "Run session successfully" << endl;
  89. }
  90. // 下面进行输出结果的可视化
  91. // 下面进行输出结果的可视化
  92. tensorflow::Tensor output = std::move(outputs.at( 0)); // 模型只输出一个结果,这里首先把结果移出来(也为了更好的展示)
  93. auto out_shape = output.shape(); // 这里的输出结果为1x4x16
  94. auto out_val = output.tensor< float, 4>(); // 与开头的用法对应,4代表结果的维度
  95. cv:: Mat out_image(height, width, CV_32FC(depth));
  96. float* data = ( float*)out_image.data;
  97. for ( int y = 0; y < height; ++y)
  98. {
  99. float* dataRow = data + (y * width * depth);
  100. for ( int x = 0; x < width; ++x)
  101. {
  102. float* dataPixel = dataRow + (x * depth);
  103. for ( int c = 0; c < depth; ++c)
  104. {
  105. float* dataValue = dataPixel + c;
  106. *dataValue = out_val( 0, y, x, c);
  107. }
  108. }
  109. }
  110. cv::imshow( "outimage", out_image);
  111. cv::waitKey( 1);
  112. double finish = clock();
  113. double duration = ( double)(finish - start) / CLOCKS_PER_SEC;
  114. cout << "spend time:" << duration << endl;
  115. }
  116. /*cv::cvtColor(out_image, out_image, cv::COLOR_RGB2BGR);
  117. cv::cvtColor(out_image_final, out_image_final, cv::COLOR_RGB2BGR);
  118. cv::imwrite("E:/c++work/tf_testdemo/tf_testdemo/001_00045_out.png", out_image*255);
  119. cv::imwrite("E:/c++work/tf_testdemo/tf_testdemo/001_00045_out_final.png", out_image_final * 255);*/
  120. system( "PAUSE");
  121. }

环境配置:

包含目录:

E:\Users\tensorflow\bazel-source;E:\Users\tensorflow\bazel-source\external\org_tensorflow;E:\Users\tensorflow\bazel-source\external\protobuf_archive\src;E:\Users\tensorflow\bazel-source\external\com_google_absl;E:\Users\tensorflow\bazel-source\external\eigen_archive;E:\Users\tensorflow\bazel-source\bazel-out\x64_windows-opt\genfiles;E:\Users\tensorflow;E:\Opencv-vs2015\opencv342\include;E:\Opencv-vs2015\opencv342\include\opencv;E:\Opencv-vs2015\opencv342\include\opencv2;
   
   
   
   

E:\Users\tensorflow\bazel-source;E:\Users\tensorflow\bazel-source\external\org_tensorflow;E:\Users\tensorflow\bazel-source\external\protobuf_archive\src;E:\Users\tensorflow\bazel-source\external\com_google_absl;E:\Users\tensorflow\bazel-source\external\eigen_archive;E:\Users\tensorflow\bazel-source\bazel-out\x64_windows-opt\genfiles;E:\Users\tensorflow;E:\Opencv-vs2015\opencv342\include;E:\Opencv-vs2015\opencv342\include\opencv;E:\Opencv-vs2015\opencv342\include\opencv2;

库目录:

E:\Opencv-vs2015\opencv342\lib;E:\c++work\tf_testdemo\x64\Debug;
   
   
   
   

链接器输入附加依赖项:

opencv_world342d.lib;opencv_world342.lib;tensorflow_cc.lib;
   
   
   
   

10.尝试编译debug版本

因为项目需要,我自己也尝试了编译tensorflow的debug版本:


   
   
   
   
  1. $parameterString = "-c fastbuild --config=cuda --define=no_tensorflow_py_deps=true --copt=-nvcc_options=disable-warnings //tensorflow:libtensorflow_cc.so --verbose_failures"
  2. .\build.ps1 `
  3. -BazelBuildParameters $parameterString `
  4. -BuildCppAPI -ReserveSource -ReserveVenv


   
   
   
   
  1. $parameterString = "-c dbg--config=cuda --define=no_tensorflow_py_deps=true --copt=-nvcc_options=disable-warnings //tensorflow:libtensorflow_cc.so --verbose_failures"
  2. .\build.ps1 `
  3. -BazelBuildParameters $parameterString `
  4. -BuildCppAPI -ReserveSource -ReserveVenv

但是卡在了下面的错误

tensorflow::io::GetTempFilename
   
   
   
   

这个错误相关的github issue有这两个issue1,issue2和我自己开了一个issue,但是都建议编译release version。所以这里我暂停了继续编译debug版本。

11.遇到的问题

1.c++运行相同的pb模型,使用gpu,测试单张图像速度比python慢?

python测试单张图像20ms,c++测试单张图像26ms;

相关issue:issue3,issue4.

2.编译时遇到max的问题:

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第15张图片

windows+bazel+tensorflow-v1.12.0(GPU)编译生成dll与lib_第16张图片


   
   
   
   
  1. string_view.h
  2. line491 -493
  3. private:
  4. static constexpr size_type kMaxSize =
  5. std::numeric_limits::max();
  6. 改为
  7. private:
  8. static constexpr size_type kMaxSize =
  9. ( std::numeric_limits::max)();

 

 其他出错的头文件部分一一修改。

12.参考

[1]编译windows tensorflowv1.11-1.13 dll release version

[2]cv输入+tensorflow dll测试

[3]Tensorflow C++的Tensor和OpenCV的Mat相互转换

[4]tensorflow_c++_api_1

[5]tensorflow_c++_api_2

[6]Mat <=> Tensor方法2

这里非常感谢TerryBryant,给我解答问题,应用部分主要参考他的;也非常感谢guikarist分享的编译方法。

 

你可能感兴趣的:(AI,tensorflow)