As a result of the recent busy, until the holidays are empty, so will learn from their own knowledge to share. If there is a wrong place, please point out, thank you! At present the deep study is getting more and more fire, the related worker who learns, uses TensorFlow more and more. Recently, a Python script was used to train the model under the TensorFlow line, and the Freeze_graph tool was used to output the. PB diagram file, and then the online production environment on the Windows platform directly invokes the pre-trained model with C + + code to complete the forecast work. Because the tensorflow provided by the C + + API is relatively small, so refer to the above several existing logs, do a summary. This compiles the TensorFlow C + + library and encounters the pits for landfill. After the call c++library is relatively simple, you can refer to the link section.
Step1:windows 10 Necessary environment preparation
1.1 Installing VS2015
1.2 Install the Swigwin-3.0.12 with the executable file address of D:/lib/swigwin-3.0.12/swig.exe
1.4 Install python3.5, note that you choose to add the path to the environment variable during installation.
1.5 Install CMake-3.8.0, note that you choose to add the path to the environment variable during installation.
1.6 install git for downloading dependencies from GitHub during the compilation process.
1.7 Download and extract the Master branch of TensorFlow on GitHub to the folder D:\TF, edit the file tensorflow/tensorflow/contrib/cmake/cmakelists.txt, Change line 87th to 93 as follows:
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 ()
Summarize the three important addresses in this section:
The Python executable file address is: d:/lib/python35/Python.exe;python library file address: D:/lib/python35/libs/python35.lib (Release version), D :/lib/python35/libs/python35_d.lib (Debug version); Swigwin executable file address: D:/lib/swigwin-3.0. /swig.exe.
STEP2: Compiling TensorFlow shared lib
2.1 Open Folder D:\tf\tensorflow-master\tensorflow\contrib\cmake, create new Folder \build. Run as Administrator start \ All Programs \ Visual Studio 2015\visual Studio tools\ Developer Command Prompt for VS, enter Powershe ll, use the command to switch to the new build folder. Use directive 1 to build the project. You can see that you are using the three paths saved in the previous section. When generating done appears on the command line, the build succeeds.
# instruction 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# Directive 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
Or, here I use the Cmake-gui interface to CMake compile release and debug versions, as shown in:
2.2 Command Line input instruction 2 to start compiling, in the compilation process to keep the network unblocked, there are 15 dependent libraries need to download. There may be some warnings during compilation, not to worry, as long as there is no error compiling.
/p:configuration=/p:configuration=debug all_build.vcxproj
2.3 However, I failed to compile on my own computer and there were 90 errors that point to the two files:
D:\tf\tensorflow-Master\tensorflow\contrib\cmake\build\re2\src\re2\re2\testing\re2_test.ccd:\tf\tensorflow -master\tensorflow\contrib\cmake\build\re2\src\re2\re2\testing\search_test.cc
The Re2 package provides the functionality of the regular expression, which is the test file in the folder testing, which is not required in the TensorFlow run, so you can remove these errors by banning the compilation of the Re2 test section.
STEP3: Troubleshooting Errors
3.1 To the wrong project folder, modify the file D:\tf\tensorflow-master\tensorflow\contrib\cmake\build\re2\src\re2\ CMakeLists.txt (be sure to FQ download to), modify the 16th line to:
for RE2 "OFF)
, and save.
3.2 Re-compile (step 2.2). During the compilation process, an error occurs:
C:\Program Files (x86) \msbuild\microsoft.cpp\v4. 0\v140\microsoft.cppcommon.targets (171,51. [D:\tensorflow-master\tensorflow\contrib\cmake\build\grpc.vcxproj]
This should be not successful download grpc, this should be a bug, the link to the wrong (multiple compilation is not the past), Baidu, the next grpc to my current application of the temporary use not much (do not know can Baidu GPRC) Therefore, temporarily do not need grpc, also do not download, edit the file Tensorflow/tensorflow/contrib/cmake/cmakelists.txt, change line 23rd to the following:
" Enable GRPC Support " OFF)
3.3 Re-cmake and build the project and re-compile, repeating steps 2.1,2.2 and 3.1. Errors may occur:
is out of heap space
This should be related to your own hardware, my own computer configuration is not very good. This error occurs, first wait for the completion of the compilation process, and then run command 2 repeatedly. The problem is no more than about two or three runs. Wait for the entire compilation process to complete before each run repeats.
This error can also occur (such errors occur with debug compilation):
This could actually be compiled using x86, which is not actually compiled with the x64 environment, so open the command window Developer commands Prompt for VS, enter PowerShell, Use the command switch to the new Build folder, enter Directive 3, and then enter command 2.
# instruction 3 (release) Set Preferredtoolarchitecture=x64
3.4 Compile to the end of the error (the debug version is strictly) the symbolic overload error, as shown in:
' < ' operator found which takes a left-hand operand of type Indicesrowiterator
Modify the file under the corresponding folder, here I am D:\tf\tensorflow-master-ngrpc\tensorflow\contrib\boosted_trees\lib\utils\sparse_column_ iterable.cc, add the contents to the file, as follows:
BOOL operator Const Const {qcheck_lt (iter_, other.iter_); return (Row_idx_ < other.row_idx_);}
The final compilation succeeds. The appropriate release and debug versions of Lib and the dynamic-link library appear as needed
STEP4: Compiling and running a simple TensorFlow C + + program on Windows
4.1 Create a new project solution in vs2015, and enter the following in the corresponding file:
#include <vector>#include<eigen/Dense>#include"matmul.h"#include"Tensorflow/core/public/session.h"#include"Tensorflow/cc/ops/standard_ops.h"using namespaceTensorFlow;//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::P laceholder (Root. Withopname ("x"), Dt_float, Ops::P laceholder::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)); returndef;}intMain () {graphdef graph_def=creategraphdef (); //Start up the sessionsessionoptions options; Std::unique_ptr<Session>session (NewSession (options)); TF_CHECK_OK (Session-Create (graph_def)); //Define some data. This needs to being converted to a Eigen Tensor to be//fed into the placeholder. Note that this would be broken//separate vectors of length 2: [1, 2] and [3, 4], which'll 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 Enter the following in the corresponding header file:
#pragma once#define compiler_msvc#define Nominmax
4.3 in vs2015, in the property configuration interface, include Directories:
D:\tf\tensorflow-master-Ngrpc\tensorflow\contrib\cmake\build\debugd:\tf\tensorflow-master-ngrpc\ Tensorflow\contrib\cmake\build\external\nsync\publicD:\tf\tensorflow-master-ngrpc\ Tensorflow\contrib\cmake\build\protobuf\src\protobuf\srcd:\tf\tensorflow-master-ngrpc\tensorflow\ Contrib\cmake\build\external\eigen_archived:\tf\tensorflow-master-ngrpc\tensorflow\contrib\cmake\ Buildd:\tf\tensorflow-master-ngrpcd:\tf\tensorflow-master-ngrpc\third_party\eigen3
4.4 In vs2015, in the property configuration interface, Additional Library directories (release version):
D:\tf\tensorflow-master-ngrpc\tensorflow\contrib\cmake\build\Release
4.5 in vs2015, in the property configuration interface, Linker Settings (release version):
Tensorflow.lib
4.6 Compile and run the program, and the results are as follows:
7 1 -3
Reference:
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
Windows compiles TensorFlow1.3 C + + library and creates a simple TensorFlow C + + program