Compiling C++ code with TensorFlow without Bazel

Viewed 5546

I am looking to adapt TensorFlow and its ANN capabilities for my computational mechanics purposes where majority of my codes are in c++. Is it possible to compile in c++ without using Bazel while including TensorFlow .h files? If so, I would really appreciate an example (haven't be able to find any online so far). Thanks

Edit: I did but i am not able to follow. Let me provide an example and maybe we can go from there. I am using ubuntu 16.10, gcc (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005, and Python 2.7.12+. I have installed bazel from source and also have cloned TF repository (~/Desktop/tensorflow). Taking a slightly modified example from (https://www.tensorflow.org/api_guides/cc/guide), i have in example.cc:

#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"
#include <iostream>

int main() {
  using namespace tensorflow;
  using namespace tensorflow::ops;
  using namespace std;
  Scope root = Scope::NewRootScope();
  // Matrix A = [3 2; -1 0]
  auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f}});
  // Vector b = [3 5]
  auto b = Const(root, { {3.f, 5.f}});
  // v = Ab^T
  auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
  std::vector<Tensor> outputs;
  ClientSession session(root);
  // Run and fetch v
  TF_CHECK_OK(session.Run({v}, &outputs));
  // Expect outputs[0] == [19; -3]
  LOG(INFO) << outputs[0].matrix<float>();
  return 0;
  cout<<"compiled correctly!"<<endl;
}

It is located in ~/Desktop/tensorflow/tensorflow/cc/example. My BUILD file - also in ~/Desktop/tensorflow/tensorflow/cc/example - reads:

cc_binary(
    name = "example",
    srcs = ["example.cc"],
    deps = [
        "//tensorflow/cc:cc_ops",
        "//tensorflow/cc:client_session",
        "//tensorflow/core:tensorflow",
    ],
)

I try to compile from ~/Desktop/tensorflow using:

bazel build tensorflow/cc/example/...

This is what i get:

INFO: Found 1 target...
Target //tensorflow/cc/example:example up-to-date:
  bazel-bin/tensorflow/cc/example/example
INFO: Elapsed time: 0.381s, Critical Path: 0.00s

Then when i go to ~/Desktop/tensorflow/bazel-bin/tensorflow/cc/example and run:

./example 

I get:

2017-07-27 09:58:39.906578: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-27 09:58:39.906628: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-27 09:58:39.906636: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-07-27 09:58:39.906641: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-07-27 09:58:39.906646: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2017-07-27 09:58:39.907751: I tensorflow/cc/example/example.cc:22] 19
-3

Any help would be much appreciate it as i'm trying to wrap my hands around this. Thanks for you patience.

3 Answers

If you don't want to tinker with your environment too much there are two possibilities. One is to compile the C++ API with Floop's tensorflow_cc project and install on your system. The other possibility is my packaging project tensorflow_cpp_packaging to provide Debian packages for the C and C++ API of Tensorflow. Both projects use CMake (instead of Bazel) to support C++ compilation of your source files. Time to time, I compile the latest Tensorflow version, they are available in the release section for 64 bit CPU and Raspberry Pi.

From Tensorflow point of view, the advantage of tensorflow_cc that you can build GPU support if you want while my project can only use CPU for inference.

If you go with either project, I can still recommend my short tutorial what I wrote how to freeze a Tensorflow model in Python and load with either C or C++ API and use for inference.

Related