Windows下编译TensorFlow1.3 C++ library及创建一个简单的TensorFlow C++程序

       由于最近比较忙,一直到假期才有空,因此将自己学到的知识进行分享。如果有不对的地方,请指出,谢谢!目前深度学习越来越火,学习、使用tensorflow的相关工作者也越来越多。最近在研究tensorflow线下采用 python 脚本训练出模型, 利用freeze_graph工具输出.pb图文件,之后再线上生产环境windows平台上用C++代码直接调用预先训练好的模型完成预测的工作。因为目前tensorflow提供的C++的API比较少,所以参考了以上几篇已有的日志,做个总结。这里编译出TensorFlow C++ library,遇到的坑进行填埋。之后的调用C++library相对比较简单,可以参考链接部分。

Step1:Windows 10必备环境准备

1.1安装VS2015

1.2安装Swigwin-3.0.12,其可执行文件地址为 D:/lib/swigwin-3.0.12/swig.exe

1.4安装python3.5,安装时注意选择将路径添加到环境变量。

1.5安装CMake-3.8.0 ,安装时注意选择将路径添加到环境变量。

1.6安装Git,用于在编译过程中从GitHub上下载依赖项。

1.7 将GitHub上TensorFlow的master分支 下载并解压到文件夹D:\tf中,编辑文件tensorflow/tensorflow/contrib/cmake/CMakeLists.txt,将第87行至93行修改如下:

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()

总结下这一部分的三个重要地址:

Python可执行文件地址为:D:/lib/Python35/python.exe;
Python库文件地址为:D:/lib/Python35/libs/python35.lib(release版本), D:/lib/Python35/libs/python35_d.lib(Debug版本);
Swigwin可执行文件地址:D:/lib/swigwin-3.0.12/swig.exe。

 

Step2:编译 TensorFlow shared lib

2.1 打开文件夹 D:\tf\tensorflow-master\tensorflow\contrib\cmake, 新建文件夹\build。以管理员身份运行 开始 \ 所有程序 \ Visual Studio 2015\Visual Studio Tools\ Developer Command Prompt for VS 2015,输入powershell,使用命令切换到新建的build文件夹下。使用指令1用于build编译项目。可以看到,使用的就是上一部分保存的三个路径。当命令行中出现Generating done,说明build成功。

# 指令1(release)
cmake .. -A x64 -DCMAKE_BUILD_TYPE=Release -DSWIG_EXECUTABLE=D:/lib/swigwin-3.0.12/swig.exe-DPYTHON_EXECUTABLE=D:/lib/Python35/python.exe -DPYTHON_LIBRARIES=D:/lib/Python35/libs/python35.lib -Dtensorflow_BUILD_SHARED_LIB=ON# 指令1(debug)
cmake .. -A x64 -DCMAKE_BUILD_TYPE=Debug -DSWIG_EXECUTABLE=D:/lib/swigwin-3.0.12/swig.exe-DPYTHON_EXECUTABLE=D:/lib/Python35/python.exe -DPYTHON_LIBRARIES=D:/lib/Python35/libs/python35_d.lib-Dtensorflow_BUILD_SHARED_LIB=ON

或者,这里我用cmake-gui界面进行cmake编译出release和debug版本,如下图所示:

 

2.2 命令行中输入指令2开始编译,在编译过程中保持网络畅通,有15个依赖库需要下载。编译过程中可能出现一些警告,不用担心,只要没有错误编译就可以通过。

# 指令2(release)
MSBuild /p:Configuration=Release ALL_BUILD.vcxproj
# 指令2(debug)
MSBuild /p:Configuration=Debug ALL_BUILD.vcxproj

 

2.3 但是,我在自己的电脑上编译失败,并出现了90个错误,这些错误都指向了两文件:

D:\tf\tensorflow-master\tensorflow\contrib\cmake\build\re2\src\re2\re2\testing\re2_test.cc
D:\tf\tensorflow-master\tensorflow\contrib\cmake\build\re2\src\re2\re2\testing\search_test.cc

 

re2包提供了正则表达式的功能,文件夹testing中是测试文件,在TensorFlow运行过程中其实不需要这些测试功能,所以可以通过禁止编译re2的测试部分来移除这些错误。

Step3:排除错误

3.1对出错的工程文件夹下,修改文件D:\tf\tensorflow-master\tensorflow\contrib\cmake\build\re2\src\re2\CMakeLists.txt(一定要FQ下载到),将第16行修改为:

option(RE2_BUILD_TESTING “enable testing for RE2” OFF)

,并保存。

3.2 重新进行编译(步骤2.2)。在编译过程中,会出现错误:

C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.CppCommon.targets(171,5): error MSB6006: “cmd.exe”已退
出,代码为 1。 [D:\tensorflow-master\tensorflow\contrib\cmake\build\grpc.vcxproj]

这个应该是没有成功下载grpc,这个应该是个bug,给的链接不对(多次编译都编不过去),百度了下grpc对我现在的应用暂时用不多(不了解可以百度gprc)因此,暂时不需要grpc,也就不下载,编辑文件tensorflow/tensorflow/contrib/cmake/CMakeLists.txt,将第23行修改如下:

option(tensorflow_ENABLE_GRPC_SUPPORT "Enable gRPC support" OFF)

3.3重新cmake 和Build编译项目,并重新进行编译,重复步骤2.1,2.2和3.1。可能会出现错误:

fatal error C1060: compiler is out of heap space

这应该是跟自己的硬件相关,我自己的电脑配置不太好。出现这个错误,首先等待编译过程的完成,然后重复运行指令2。大概重复运行两三次这个问题就没有了。每次重复运行前,需等待整个编译过程完成。

也可能会出现这样的错误(Debug编译出现这样的错误):

这可能在实际中编译采用的是x86,并没有真正采用x64的环境进行编译,因此打开命令窗口Developer Command Prompt for VS 2015,输入powershell,使用命令切换到新建的build文件夹下,输入指令3,然后在输入指令2。

# 指令3
set PreferredToolArchitecture=x64

3.4 编译到最后会出现错误(debug版本比较严格)符号重载的错误,如下图所示:

error C2678: binary '<': no operator found which takes a left-hand operand of type IndicesRowIterator

 修改对应的文件夹下的文件,我这里是D:\tf\tensorflow-master-ngrpc\tensorflow\contrib\boosted_trees\lib\utils\sparse_column_iterable.cc,增加到文件中,增加的内容如下所示:

bool operator<( const IndicesRowIterator& other ) const {
QCHECK_LT( iter_, other.iter_ );
return ( row_idx_ < other.row_idx_ );
}

最终编译成功。出现需要的相应的release和debug版本的lib和动态链接库

Step4:在Windows上编译和运行一个简单的TensorFlow C++ 程序

4.1在vs2015中创建一个新的工程solution,并在对应的文件中输入如下:

#include <vector>
#include <eigen/Dense>#include "matmul.h"
#include "tensorflow/core/public/session.h"
#include "tensorflow/cc/ops/standard_ops.h"using namespace tensorflow;// Build a computation graph that takes a tensor of shape [?, 2] and
// multiplies it by a hard-coded matrix.
GraphDef CreateGraphDef()
{Scope root = Scope::NewRootScope();auto X = ops::Placeholder(root.WithOpName("x"), DT_FLOAT, ops::Placeholder::Shape({ -1, 2 }));auto A = ops::Const(root, { { 3.f, 2.f },{ -1.f, 0.f } });auto Y = ops::MatMul(root.WithOpName("y"), A, X, ops::MatMul::TransposeB(true));GraphDef def;TF_CHECK_OK(root.ToGraphDef(&def));return def;
}int main()
{GraphDef graph_def = CreateGraphDef();// Start up the session
  SessionOptions options;std::unique_ptr<Session> session(NewSession(options));TF_CHECK_OK(session->Create(graph_def));// Define some data.  This needs to be converted to an Eigen Tensor to be// fed into the placeholder.  Note that this will be broken up into two// separate vectors of length 2: [1, 2] and [3, 4], which will separately// be multiplied by the matrix.std::vector<float> data = { 1, 2, 3, 4 };auto mapped_X_ = Eigen::TensorMap<Eigen::Tensor<float, 2, Eigen::RowMajor>>(&data[0], 2, 2);auto eigen_X_ = Eigen::Tensor<float, 2, Eigen::RowMajor>(mapped_X_);Tensor X_(DT_FLOAT, TensorShape({ 2, 2 }));X_.tensor<float, 2>() = eigen_X_;std::vector<Tensor> outputs;TF_CHECK_OK(session->Run({ { "x", X_ } }, { "y" }, {}, &outputs));// Get the result and print it outTensor Y_ = outputs[0];std::cout << Y_.tensor<float, 2>() << std::endl;session->Close();getchar();
}

 

4.2在对应的头文件中输入如下:

#pragma once#define COMPILER_MSVC
#define NOMINMAX

 

4.3在vs2015中,属性配置界面中,include Directories:

D:\tf\tensorflow-master-ngrpc\tensorflow\contrib\cmake\build\Debug
D:\tf\tensorflow-master-ngrpc\tensorflow\contrib\cmake\build\external\nsync\public
D:\tf\tensorflow-master-ngrpc\tensorflow\contrib\cmake\build\protobuf\src\protobuf\src
D:\tf\tensorflow-master-ngrpc\tensorflow\contrib\cmake\build\external\eigen_archive
D:\tf\tensorflow-master-ngrpc\tensorflow\contrib\cmake\build
D:\tf\tensorflow-master-ngrpc
D:\tf\tensorflow-master-ngrpc\third_party\eigen3

4.4在vs2015中,属性配置界面中,Additional Library Directories(release版本):

D:\tf\tensorflow-master-ngrpc\tensorflow\contrib\cmake\build\Release

4.5在vs2015中,属性配置界面中,Linker Settings(release版本):

tensorflow.lib

4.6编译和运行程序,运行结果如下所示:

 7 17
-1 -3

参考:

https://joe-antognini.github.io/machine-learning/build-windows-tf

http://quqixun.com/?p=785

http://blog.csdn.net/longji/article/details/72760409

http://blog.csdn.net/rockingdingo/article/details/75452711

https://github.com/tensorflow/tensorflow/issues

https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/cmake

https://stackoverflow.com/questions/42603407/how-to-compile-tensor-flow-with-sse-and-and-avx-instructions-on-windows

 

转载于:https://www.cnblogs.com/jliangqiu2016/p/7642471.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/428699.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

665C. Simple Strings

C. Simple Strings&#xff1a;题目 思路&#xff1a;简单的离谱&#xff0c;就相同就换就行&#xff0c;哪有dp的事啊#include <bits/stdc.h> using namespace std; typedef long long ll; vector<ll> a((int)6e5); vector<ll> b((int)6e5); string str; i…

798B. Mike and strings

B. Mike and strings&#xff1a;题目 思路&#xff1a;纯暴力题&#xff0c;不想写&#xff0c;copy了别人代码#include<iostream> #include<string> using namespace std; void change(string &s){ss.substr(1)s.substr(0,1); } int main(){int n;cin>&g…

jquery ajax 数据传输

在 form表单中&#xff0c;需要发送给后台的是一串长数据&#xff0c;后台才能接受&#xff0c;而用户则只需要输入字符串中的一部分&#xff0c;这种情况下&#xff0c;就需要将用户输入内容&#xff0c;和剩余部分进行拼串&#xff0c;然后添加进 formData 中传输。另一种情况…

1315B. Homecoming

B. Homecoming&#xff1a;题目 思路&#xff1a;从后往前遍历就行了#include <bits/stdc.h> using namespace std; typedef long long ll; vector<ll> a((int)6e5); vector<ll> b((int)6e5); string str; int main() {int t;cin >> t;while (t--){in…

jquery datatable 获取当前分页的数据

使用jquery datatable 遇到分页分别求和时&#xff0c;找了半天才找到获取当前分页数据的方法&#xff0c;以此总结 var table$(#example).DataTable( {"pagingType": "full_numbers",} ); table.columns({page:current}).data();//当前页面里的数据 转载于…

870C. Maximum splitting

C. Maximum splitting&#xff1a;题目 这凭啥1300分&#xff1f;#include <bits/stdc.h> using namespace std; typedef long long ll; vector<ll> a((int)6e5); vector<ll> b((int)6e5); string str; int main() {int t;cin>>t;while (t--){int n;c…

useGeneratedKeys的用法

<!-- useGeneratedKeys"true"把新增加的主键赋值到自己定义的keyProperty&#xff08;id&#xff09;中 --> <insert id"insert" useGeneratedKeys"true" keyProperty"id" parameterType""> 转载于:https://www…

1040B. Shashlik Cooking

B. Shashlik Cooking&#xff1a;题目 思路&#xff0c;如果所有烤肉都可以一次就行&#xff0c;如果不行就去前面调整一下#include <bits/stdc.h> using namespace std; typedef long long ll; vector<ll> a((int)6e5); vector<ll> b((int)6e5); int main(…

粉红小猪中有一个叫“快乐小鸡”的游戏

最近在学习svg,书看了一本 然后再找了个框架 snap.svg 摸索着写了个游戏给女儿玩。哈哈。即涨知识又娱乐。 转载于:https://www.cnblogs.com/goldli/p/7649898.html

626B. Cards

B. Cards&#xff1a;题目 竟然需要按照顺序输出&#xff0c;淦&#xff01;#include <bits/stdc.h> using namespace std; typedef long long ll; vector<ll> a((int)6e5); vector<ll> b((int)6e5); string str; int main() {int a 0, b 0, c 0;int n;c…

gcc 4.9编译

参考 http://blog.csdn.net/hzhxxx/article/details/28634893转载于:https://www.cnblogs.com/rabitvision/p/7650706.html

234C. Weather

C. Weather:题目 为什么直接交代码不行啊&#xff0c;非要加两句话&#xff0c;有没有大佬说说&#xff1f;#include <bits/stdc.h> using namespace std; // typedef long long ll; vector<int> a((int)3e5); vector<int> b((int)3e5), c((int)3e5); int m…

idea .defaultMessage

idea .defaultMessage 等同于eclipse里的getDefaultMessage idea只能能获取到bean设置NotBlank等message响应到jsp的key&#xff0c;不能在jsp里获取validationMessage properties文件里的value 那么我们就不要value只要key 转载于:https://www.cnblogs.com/duneF/p/7654780.h…

653B. Bear and Compressing

B. Bear and Compressing&#xff1a;题目 纯纯暴力DFS&#xff0c;和dp有啥关系&#xff01;不想写了&#xff0c;copy一份&#xff0c;写的真精简。#include <bits/stdc.h> using namespace std; int n, m; string a[40], b[40]; int f[10][10][2];int dfs(int pos, i…

【vue系列之二】详解vue-cli 2.0配置文件

上次给大家分享的是用vue-cli快速搭建vue项目&#xff0c;虽然很省时间和精力&#xff0c;但想要真正搞明白&#xff0c;我们还需要对其原理一探究竟。 大家拿到一个项目&#xff0c;要快速上手&#xff0c;正确的思路是这样的&#xff1a; 首先&#xff0c;如果在项目有readme…

698A. Vacations

A. Vacations&#xff1a;题目 题意&#xff1a;每天有四种可能性&#xff0c;锻炼和比赛分别两种可能性相乘。不能连续锻炼或者连续比赛&#xff0c;问最少休息几天。 思路&#xff1a;dp或者贪心都行。贪心写法 #include <bits/stdc.h> using namespace std; vector&…

1526B. I Hate 1111

B. I Hate 1111&#xff1a;题目 这题目太搞了呀&#xff0c;和dp感觉关系不大&#xff0c;数学题啊首先要知道&#xff0c;11和111能构成后面所有的棍子数。 n a*111b*11;最快做法 //算是规律&#xff1f; #include <bits/stdc.h> using namespace std; int main() {…

4. Spring 如何通过 XML 文件配置Bean,以及如何获取Bean

在 Spring 容器内拼凑 bean 叫做装配。装配 bean 的时候&#xff0c;你是在告诉容器&#xff0c;需要哪些 bean &#xff0c;以及容器如何使用依赖注入将它们配合在一起。 理论上&#xff0c;bean 装配的信息可以从任何资源获得&#xff0c;包括属性文件&#xff0c;关系数据库…

1195C. Basketball Exercise

C. Basketball Exercise&#xff1a;题目 经典简单dp&#xff0c;考虑前两天的就行#include <bits/stdc.h> using namespace std; typedef long long ll; vector<int> a((int)6e5); vector<int> b((int)6e5); ll dp[(int)5e5][3]; int main() {int n;cin>…

基于用户击键特征的身份鉴别系统

简单来说&#xff0c;我们要做的就是一种通过用户敲击键盘的习惯进行身份鉴别的系统。国内外之前有一些相关研究&#xff0c;但是通常是数千条数据训练&#xff0c;而且不能随意改变敲击的字符串&#xff0c;或者是有的要求采用带有压力传感器的键盘&#xff0c;难以实用和推广…