Build Basic program by using Caffe2 framework in C++

Kazami
2 min readJul 11, 2017

--

In this post, what we need to do is to show how to write caffe2 application in c++. In high performance application, such as trading, scientific computing, performance is critical issue. That is why we need to compute the whole graph by using c++.

In recent years, more and more AI algorithm are implemented in the embedded system device such as mobile phone. In these devices, the computing resource is limited, so it should make resource management efficiently.

Requirements:

Caffe2 should be installed. Then, the Caffe2.so would be linked.

In this tutorial, we start from writing Makefile, because it would decide the structure of the whole project.

eigen3_include = /usr/local/include/caffe2/caffe2_lib = /usr/local/lib/

These two path were depended on the platform you are working on. When the caffe2 is installed followed by the instruction, it will install in the system lib. It would not set the two path

CAFFE2_LINKFLAGS = “-L$(CAFFE2_LIB) -lglog -lgflags -lprotobuf”

The caffe2 is built with protobuf, gflags and glob lib, it is necessary to build the target with these library.

Caffe2 is built with Eigen3 template library, therefore we should add it to the include path.

CXXFLAGS = -std=c++11 -I${eigen3_include}

After finishing the makefile, we should begin to write the program. We aim to write small program to run the CNN net. It will demonstrate how to make number recognition.

In this snip of code, we would try to demonstrate the basic functionality of the workspace and blob. We create the workspace which is like session in the tensorflow, and create the blob to represent the variable in the caffe2 computation graph.

Workspace workspace;

std::vector<float> x(4 * 3 * 2);
auto tensor = workspace.CreateBlob(“my_x_variable”)->GetMutable<TensorCPU>();

In this paragraph of code, it create the blob and we make it as TensorCPU type. It would possibly make it as TensorGPU type whenever the GPU on the device is workable.

const auto blob = workspace.GetBlob(“my_x”)auto tensor = blob->GEt<Tensor<CPUContext>>();const auto& data = tensor.template data<float>();

In the above code, we fetch the x variable from blob. Then, we convert the whole blob object to the tensor object. Finally, we get the data expression by using template function.

g++ data/make_mnist_db.cc -std=c++11 -I/usr/local/include/eigen3 -L /usr/local/lib/ -lCaffe2_CPU -lprotobuf -lglog -lgflags -o gy

Finally, it print the whole bunch of x variable value.

source code: https://github.com/Knight-X/basic_caffe2_cc

--

--