LoginSignup
1
0

More than 5 years have passed since last update.

[Note] Using Tensorflow API in C++

Last updated at Posted at 2018-03-31

Before jumping into Tensorflow in C++.

I have summarised about Bazel, which google uses for building the project!
Please kindly take a look if you have interest on it.
https://qiita.com/Rowing0914/items/8e60588fa698693d20a3

Official Tutorial

// tensorflow/yourfolder/filename.cc

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

int main() {
  using namespace tensorflow;
  using namespace tensorflow::ops;
  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;
}

Put the application above into the folder you make newly under tensorflow directory.
And please make a BUILD file to notice the bazel dependencies of this application.

cc_binary(
    name = "project",
    srcs = ["code.cpp"],
    linkopts = 
        ["-lopencv_core","-lopencv_imgproc", "-lopencv_highgui", 
        "-Wl,--version-script=tensorflow/tf_version_script.lds"],
    copts = ["-I/usr/local/include/", "-O3"],
    deps = [
        "//tensorflow/core:tensorflow"
    ])

Screen Shot 2018-04-14 at 23.31.32.png

And then run the bazel!

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0