本文參考和綜合了多篇網絡博客文章,加以本身的實踐,最終終於在windows環境下,編譯出能夠用於C++程序調用tensorflow API的程序,並執行成功。php
考慮到網絡上關於這方面的資料還較少,特總結全過程以下,但願能幫助到有須要的碼農朋友,文中有部分文字步驟是借鑑他人文章,引用路徑在最後列出。html
1、環境準備:python
1 if (tensorflow_OPTIMIZE_FOR_NATIVE_ARCH) 2 include(CheckCXXCompilerFlag) 3 CHECK_CXX_COMPILER_FLAG("-march=native" COMPILER_OPT_ARCH_NATIVE_SUPPORTED) 4 if (COMPILER_OPT_ARCH_NATIVE_SUPPORTED) 5 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") 6 else() 7 CHECK_CXX_COMPILER_FLAG("/arch:AVX" COMPILER_OPT_ARCH_AVX_SUPPORTED) 8 if(COMPILER_OPT_ARCH_AVX_SUPPORTED) 9 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /arch:AVX") 10 endif() 11 endif() 12 endif()
2、使用CMAKE設置各項編譯參數git
3、編譯生成tensorflow庫文件github
fatal error C1060: compiler is out of heap space 不要緊,等待整個工程所有編譯完成(聽說內存特別大的電腦不會報)。
找到tf_core_kernels項目,右鍵單獨編譯,操做以下圖。
4. tf_core_kernels項目編譯成功後,再一樣對tensorflow_static做單獨編譯,最後再對tensorflow做單獨編譯。.windows
這樣tensorflow.lib和tensorflow.dll文件就能夠編譯出來了,生成的庫文件路徑在..\tensorflow\tensorflow\contrib\cmake\build\Release下。網絡
4、使用tensorflow庫文件編寫C++程序session
#pragma once #define COMPILER_MSVC #define NOMINMAX
// TestTensorFlow.cpp : 定義控制檯應用程序的入口點。 // #include "stdafx.h" #include <vector> #include <eigen/Dense> #include "TestTensorFlow.h" #include "tensorflow/core/public/session.h" #include "tensorflow/cc/ops/standard_ops.h" using namespace tensorflow; 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 out Tensor Y_ = outputs[0]; std::cout << Y_.tensor<float, 2>() << std::endl; session->Close(); getchar(); }
E:\TF Code\tensorflow\tensorflow\contrib\cmake\build\Debug E:\TF Code\tensorflow\tensorflow\contrib\cmake\build\external\nsync\public E:\TF Code\tensorflow\tensorflow\contrib\cmake\build\protobuf\src\protobuf\src E:\TF Code\tensorflow\tensorflow\contrib\cmake\build\external\eigen_archive E:\TF Code\tensorflow\tensorflow\contrib\cmake\build E:\TF Code\tensorflow E:\TF Code\tensorflow\third_party\eigen3
5. 設置預編譯選項,右鍵屬性——C/C++——預處理器,預處理器定義中加入PLATFORM_WINDOWSapp
6. 編譯TestTensorFlow項目,就能夠成功生成TestTensorFlow.exe了。工具
7.直接運行程序,會報錯,
8,把..\tensorflow\tensorflow\contrib\cmake\build\Release下的tensorflow.dll拷貝到TestTensorFlow.exe同文件夾下,再運行便可成功獲得輸出結果以下:
輸出結果有一句警告,好像是我編譯參數仍是跟CPU功能有不匹配,可是不影響執行結果,有知道如何解決的朋友能夠留言給我,謝謝。
參考: