TensorFlow Lite C++ API example for inference - c++

I am trying to get a TensorFlow Lite example to run on a machine with an ARM Cortex-A72 processor. Unfortunately, I wasn't able to deploy a test model due to the lack of examples on how to use the C++ API. I will try to explain what I have achieved so far.
Create the tflite model
I have created a simple linear regression model and converted it, which should approximate the function f(x) = 2x - 1. I got this code snippet from some tutorial, but I am unable to find it anymore.
import tensorflow as tf
import numpy as np
from tensorflow import keras
from tensorflow.contrib import lite
model = keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
xs = np.array([ -1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([ -3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)
model.fit(xs, ys, epochs=500)
print(model.predict([10.0]))
keras_file = 'linear.h5'
keras.models.save_model(model, keras_file)
converter = lite.TocoConverter.from_keras_model_file(keras_file)
tflite_model = converter.convert()
open('linear.tflite', 'wb').write(tflite_model)
This creates a binary called linear.tflite, which I should be able to load.
Compile TensorFlow Lite for my machine
TensorFlow Lite comes with a script for the compilation on machines with the aarch64 architecture. I followed the guide here to do this, even though I had to modify the Makefile slightly. Note that I compiled this natively on my target system. This created a static library called libtensorflow-lite.a.
Problem: Inference
I tried to follow the tutorial on the site here, and simply pasted the the code snippets from loading and running the model together, e.g.
class FlatBufferModel {
// Build a model based on a file. Return a nullptr in case of failure.
static std::unique_ptr<FlatBufferModel> BuildFromFile(
const char* filename,
ErrorReporter* error_reporter);
// Build a model based on a pre-loaded flatbuffer. The caller retains
// ownership of the buffer and should keep it alive until the returned object
// is destroyed. Return a nullptr in case of failure.
static std::unique_ptr<FlatBufferModel> BuildFromBuffer(
const char* buffer,
size_t buffer_size,
ErrorReporter* error_reporter);
};
tflite::FlatBufferModel model("./linear.tflite");
tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder(*model, resolver)(&interpreter);
// Resize input tensors, if desired.
interpreter->AllocateTensors();
float* input = interpreter->typed_input_tensor<float>(0);
// Fill `input`.
interpreter->Invoke();
float* output = interpreter->typed_output_tensor<float>(0);
When trying to compile this via
g++ demo.cpp libtensorflow-lite.a
I get a load of errors. Log:
root#localhost:/inference# g++ demo.cpp libtensorflow-lite.a
demo.cpp:3:15: error: ‘unique_ptr’ in namespace ‘std’ does not name a template type
static std::unique_ptr<FlatBufferModel> BuildFromFile(
^~~~~~~~~~
demo.cpp:10:15: error: ‘unique_ptr’ in namespace ‘std’ does not name a template type
static std::unique_ptr<FlatBufferModel> BuildFromBuffer(
^~~~~~~~~~
demo.cpp:16:1: error: ‘tflite’ does not name a type
tflite::FlatBufferModel model("./linear.tflite");
^~~~~~
demo.cpp:18:1: error: ‘tflite’ does not name a type
tflite::ops::builtin::BuiltinOpResolver resolver;
^~~~~~
demo.cpp:19:6: error: ‘unique_ptr’ in namespace ‘std’ does not name a template type
std::unique_ptr<tflite::Interpreter> interpreter;
^~~~~~~~~~
demo.cpp:20:1: error: ‘tflite’ does not name a type
tflite::InterpreterBuilder(*model, resolver)(&interpreter);
^~~~~~
demo.cpp:23:1: error: ‘interpreter’ does not name a type
interpreter->AllocateTensors();
^~~~~~~~~~~
demo.cpp:25:16: error: ‘interpreter’ was not declared in this scope
float* input = interpreter->typed_input_tensor<float>(0);
^~~~~~~~~~~
demo.cpp:25:48: error: expected primary-expression before ‘float’
float* input = interpreter->typed_input_tensor<float>(0);
^~~~~
demo.cpp:28:1: error: ‘interpreter’ does not name a type
interpreter->Invoke();
^~~~~~~~~~~
demo.cpp:30:17: error: ‘interpreter’ was not declared in this scope
float* output = interpreter->typed_output_tensor<float>(0);
^~~~~~~~~~~
demo.cpp:30:50: error: expected primary-expression before ‘float’
float* output = interpreter->typed_output_tensor<float>(0);
I am relatively new to C++, so I may be missing something obvious here. It seems, however, that other people have trouble with the C++ API as well (look at this GitHub issue). Has anybody also stumbled across this and got it to run?
The most important aspects for me to cover would be:
1.) Where and how do I define the signature, so that the model knows what to treat as inputs and outputs?
2.) Which headers do I have to include?
Thanks!
EDIT
Thanks to #Alex Cohn, the linker was able to find the correct headers. I also realized that I probably do not need to redefine the flatbuffers class, so I ended up with this code (minor change is marked):
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/tools/gen_op_registration.h"
auto model = tflite::FlatBufferModel::BuildFromFile("linear.tflite"); //CHANGED
tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder(*model, resolver)(&interpreter);
// Resize input tensors, if desired.
interpreter->AllocateTensors();
float* input = interpreter->typed_input_tensor<float>(0);
// Fill `input`.
interpreter->Invoke();
float* output = interpreter->typed_output_tensor<float>(0);
This reduces the number of errors greatly, but I am not sure how to resolve the rest:
root#localhost:/inference# g++ demo.cpp -I/tensorflow
demo.cpp:10:34: error: expected ‘)’ before ‘,’ token
tflite::InterpreterBuilder(*model, resolver)(&interpreter);
^
demo.cpp:10:44: error: expected initializer before ‘)’ token
tflite::InterpreterBuilder(*model, resolver)(&interpreter);
^
demo.cpp:13:1: error: ‘interpreter’ does not name a type
interpreter->AllocateTensors();
^~~~~~~~~~~
demo.cpp:18:1: error: ‘interpreter’ does not name a type
interpreter->Invoke();
^~~~~~~~~~~
How do I have to tackle these? It seems that I have to define my own resolver, but I have no clue on how to do that.

I finally got it to run. Considering my directory structure looks like this:
/(root)
/tensorflow
# whole tf repo
/demo
demo.cpp
linear.tflite
libtensorflow-lite.a
I changed demo.cpp to
#include <stdio.h>
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/tools/gen_op_registration.h"
int main(){
std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile("linear.tflite");
if(!model){
printf("Failed to mmap model\n");
exit(0);
}
tflite::ops::builtin::BuiltinOpResolver resolver;
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder(*model.get(), resolver)(&interpreter);
// Resize input tensors, if desired.
interpreter->AllocateTensors();
float* input = interpreter->typed_input_tensor<float>(0);
// Dummy input for testing
*input = 2.0;
interpreter->Invoke();
float* output = interpreter->typed_output_tensor<float>(0);
printf("Result is: %f\n", *output);
return 0;
}
Also, I had to adapt my compile command (I had to install flatbuffers manually to make it work). What worked for me was:
g++ demo.cpp -I/tensorflow -L/demo -ltensorflow-lite -lrt -ldl -pthread -lflatbuffers -o demo
Thanks to #AlexCohn for getting me on the right track!

Here is the minimal set of includes:
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/tools/gen_op_registration.h"
These will include other headers, e.g. <memory> which defines std::unique_ptr.

Related

Rcpp and protobuf: double definition of Free macro

I'm developing a C++ library and wanted to interface it with both Python and R.
The core idea is that R and Python are basically 'empty shells' used for plotting and not much else and all the core computation is done in C++, so that the interface is just a bunch of functions that run the equivalent of a C++ 'main' and return the output to R or Python instead of saving stuff to file.
My idea was to use goolge's protocol buffer in a server/client fashion: data is passed from Python (R) to C++ using a serialized message, is then deserialized in C++. Computation is run in C++ and to Python (R) is returned a list of bytes/strings corresponding each to a serialized proto object.
In Python, this works like a charm thanks to Pybind11.
However, in R I get a weird error:
/usr/include/google/protobuf/arena_impl.h:174:60: error: macro "Free" passed 3 arguments, but takes just 1
void (*block_dealloc)(void*, size_t));
I believe this issue is due to Rcpp defining a similar macro to protobuf.
The following MWE reproduces the same error.
This is the fold structure
.
├── src
│   ├── rcpp_exports.cpp
├── student.proto
└── test.R
in test.R
Rcpp::sourceCpp(file="src/rcpp_exports.cpp")
in rcpp_exports.cpp
#include <Rcpp.h>
using namespace Rcpp;
#include "student.pb.h"
// [[Rcpp::export]]
std::string foo() {
Student stud;
stud.set_grade(25);
return stud.DebugString();
}
in student.proto
syntax = "proto3";
message Student {
double grade = 1;
bool pass = 2;
}
the protobuf file is then compiled using
> protoc --proto_path=. --cpp_out=./src student.proto
and finally I run the test.R file
> Rscript test.R
Getting the following error log:
In file included from /usr/include/google/protobuf/arena.h:55:0,
from /home/mario/dev/tesi/test_rproto/src/student.pb.h:24,
from rcpp_exports.cpp:3:
/usr/include/google/protobuf/arena_impl.h:174:60: error: macro "Free" passed 3 arguments, but takes just 1
void (*block_dealloc)(void*, size_t));
^
In file included from /usr/share/R/include/R.h:91:0,
from /home/mario/R/x86_64-pc-linux-gnu-library/3.6/Rcpp/include/Rcpp/r/headers.h:63,
from /home/mario/R/x86_64-pc-linux-gnu-library/3.6/Rcpp/include/RcppCommon.h:29,
from /home/mario/R/x86_64-pc-linux-gnu-library/3.6/Rcpp/include/Rcpp.h:27,
from rcpp_exports.cpp:1:
/usr/share/R/include/R_ext/RS.h:74:37: error: expected identifier before ‘(’ token
#define Free(p) (R_chk_free( (void *)(p) ), (p) = NULL)
^
/usr/share/R/include/R_ext/RS.h:74:47: error: ‘parameter’ declared as function returning a function
#define Free(p) (R_chk_free( (void *)(p) ), (p) = NULL)
^
/usr/share/R/include/R_ext/RS.h:74:50: error: expected ‘)’ before ‘,’ token
#define Free(p) (R_chk_free( (void *)(p) ), (p) = NULL)
^
make: *** [rcpp_exports.o] Error 1
I would recommend a few things for defensive programming:
Do not flatten namespaces i.e. remove using namespace Rcpp; and call your functins explicitly with prefix.
Ask for stringent includes (which we can't retroactively make a default) by defining #define STRICT_R_HEADERS before including Rcpp.h or other R headers.
Use R functions without the remap, i.e Rf_error() by defining R_NO_REMAP but Rcpp should do that for you.
That should avoid the clash you found here by being a little liberal in what you included and how.
Lastly, we wrote RProtoBuf (and it actually had a hand in creating what is the current Rcpp too) well over a decade ago yet I never got that error message you have here. So it clearly can be avoided.

Including calib3d gives many compilation errors

I am trying to use cv::Rodrigues function in order to get a rotation matrix from a given vector of angles.
I already have these includes in my program:
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/videoio/videoio.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core.hpp"
Without adding #include "opencv2/calib3d.hpp" everything compiles just fine.
But when I add it in order to be able to use cv::Rodrigues the compilation fails with many errors.
I won't add the whole error log because it will be long and useless. But it starts with this:
<command-line>:0:9: error: expected identifier before numeric constant
<command-line>:0:9: error: expected '}' before numeric constant
<command-line>:0:9: error: expected unqualified-id before numeric constant
And after that I have many error such as :
/usr/local/include/opencv2/features2d.hpp:1106:69: error: expected primary-expression before 'int'
const Scalar& color=Scalar::all(-1), int flags=DrawMatchesFlags::DEFAULT );
^
/usr/local/include/opencv2/features2d.hpp:1130:32: error: variable or field 'drawMatches' declared void
CV_EXPORTS_W void drawMatches( InputArray img1, const std::vector<KeyPoint>& keypoints1,
^
/usr/local/include/opencv2/features2d.hpp:1130:32: error: 'InputArray' was not declared in this scope
For completeness here is the part of the code that I would like to add:
void test(cv::Matx33f& rot_mat){
float x = 50 * (M_PI/180);
float y = -10 * (M_PI/180);
float z = 0 * (M_PI/180);
cv::Matx31f rot_vec(x,y,z);
//converts to a rotation matrix
cv::Rodrigues(rot_vec, rot_mat);
}
Note: I am working on Ubuntu 15.10 with OpenCV 3.1.0 and I did add -lopencv_calib3d to my makefile.

Simple C++ exception error (MEX)

I am working on adapting some C++ code from Windows to Linux to compile as mex in Matlab. The program is designed to connect a data glove via a virtual serial port. I've never worked much at all with C++ before, so the solution this issue could very well be quite simple. In a larger file, I am getting issues caused by the following try/catch:
try
{
// You can tell which com port to use by checking windows settings under hardware
glove = new CyberGlove(portName, (int) *baudRate); // baudrate 115200
/* Return CyberGlove object */
mxArray* result = (mxArray*) mxMalloc(sizeof(CyberGlove));
*reinterpret_cast<CyberGlove*>(result) = *glove;
plhs[0] = result;
}
catch(std::runtime_error err)
{
std::cout << "Error: " << err.what() << std::endl;
}
delete glove;
When I try to compile it in Matlab, I get the following errors:
InitCyberGlove.cpp: In function ‘void mexFunction(int, mxArray**, int, const mxArray**)’:
InitCyberGlove.cpp:43:11: error: expected type-specifier
InitCyberGlove.cpp:43:30: error: expected unqualified-id before ‘err’
InitCyberGlove.cpp:43:30: error: expected ‘)’ before ‘err’
InitCyberGlove.cpp:43:30: error: expected ‘{’ before ‘err’
InitCyberGlove.cpp:43:30: error: ‘err’ was not declared in this scope
InitCyberGlove.cpp:43:33: error: expected ‘;’ before ‘)’ token
I don't think it's specifically related to the CyberGlove stuff so much as my general lack of knowledge of C++ and how exceptions work.
You need to #include <stdexcept> where std::runtime_error is declared.

Simple C++ program running errors in Xcode, Code Blocks, and Terminal

I'm trying to write a simple program to calculate numerical approximations with Euler's method and every complier I've used hasn't printed anything. Codeblocks is running an error but I think that is because the compiler isn't set up right. xCode will build it but nothing happens. When I run g++ Euler.cpp I get:
Euler.cpp:1: error: expected constructor, destructor, or type conversion before ‘<’ token
Euler.cpp: In function ‘int main()’:
Euler.cpp:13: error: ‘cin’ was not declared in this scope
Euler.cpp:19: error: ‘cout’ was not declared in this scope
Euler.cpp:19: error: ‘endl’ was not declared in this scope
I usually never have problems with simple c++ programs and fear it's something very obvious.
//
// Euler.cpp
// Numerical Approximations (Euler's Method)
//
// Created by XXXXXXXXXXXX on 6/18/12.
// Copyright (c) 2012 University of Kansas Department of Mathematics. All rights reserved.
//
#include <iostream>
using namespace std;
int main ()
{
int N=4;
//cout<<"Number of steps (N):";
//cin>>t;
float h=0.1;
//cout<<endl<<" Step size (h):";
cin>>h;
float y0=1;
//cout<<endl<<"y(0)=";
//cin>>y0;
cout<<"test!"<<endl;
float data[N][4];
int n=0;
data[0][2] = y0;
while (n<N){
data[n][0]=n;
if(n>0){
data[n][2]=data[n-1][3];
}
data[n][1]=h*n;
data[n][3] = data[n][2] + ((3 + data[n][1] - data[n][2])*h);
n++;
cout<<"n="<<n<<". tn="<<data[n][1]<<". y(n)="<<data[n][2]<<". y(n+1)="<<data[n][3] <<"."<<endl;
}
return 0;
}
It's probably something obvious but I don't see it.
It's not finding the iostream header. Do you see an error message reading something like "couldn't find header iostream" ?

Boost Date_Time problem compiling a simple program

I'm writing a very stupid program using Boost Date_Time library.
int main(int srgc, char** argv) {
using namespace boost::posix_time;
date d(2002,Feb,1); //an arbitrary date
ptime t1(d, hours(5)+nanosec(100)); //date + time of day offset
ptime t2 = t1 - minutes(4)+seconds(2);
ptime now = second_clock::local_time(); //use the clock
date today = now.date(); //Get the date part out of the time
}
Well I cannot compile it, compiler does not recognize a type...
Well I used many features of Boost libs like serialization and more... I correctly built them and, looking in my /usr/local/lib folder I can see that libboost_date_time.so is there (a good sign which means I was able to build that library)
When I compile I write the following:
g++ -lboost_date_time main.cpp
But the errors it showed me when I specify the lib are the same of those ones where I do not specify any lib.
What is this? Anyone knows?
The error is
main.cpp: In function ‘int main(int,
char**)’: main.cpp:9: error: ‘date’
was not declared in this scope
main.cpp:9: error: expected ‘;’ before
‘d’ main.cpp:10: error: ‘d’ was not
declared in this scope main.cpp:10:
error: ‘nanosec’ was not declared in
this scope main.cpp:13: error:
expected ‘;’ before ‘today’
Though I can't figure out what's ss in your code,
qualifying date and Feb as the following will make your code valid.
boost::gregorian::date
boost::date_time::Feb
Hope this helps.