Hi,

I'm trying to use tflite in a Bela project. I built tflite for Bela using rodrigodzf's repo: https://github.com/rodrigodzf/bela-tflite. I copied the folder into 'Bela/libraries/' so that the folder structure now looks like this:

/root/Bela/libraries/tensorflow-lite
-- build/
-- tensorflow/lite/ *.cc, *.h files

The minimal example I'm trying to run is an adapted version from https://stackoverflow.com/questions/56837288/tensorflow-lite-c-api-example-for-inference, located in /root/Bela/projects/tf-test:

#include <Bela.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"


bool setup(BelaContext *context, void *userData)
{
    std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile("linear.tflite");

	return true;
}

void render(BelaContext *context, void *userData)
{

}

void cleanup(BelaContext *context, void *userData)
{

}

and the command I am using to build this (following these instructions https://blog.bela.io/using-an-external-library-with-bela/)

make -C /root/Bela PROJECT=tf-test CPPFLAGS="-I/root/Bela/libraries/tensorflow-lite/tensorflow/lite" LDFLAGS="-L/root/Bela/libraries/tensorflow-lite/build" LDLIBS="-l/root/Bela/libraries/tensorflow-lite/build/libtensorflow-lite.a"

The compiler doesn't seem to find the library files since I get this error:

/root/Bela/projects/tf-test/render.cpp:2:10: fatal error: 'tensorflow/lite/interpreter.h' file not found
#include "tensorflow/lite/interpreter.h"
         ^

I don't know if the issue has to do with the fact that apparently tflite needs to have an add_subdirectory command in the cmake file, as said in the tflite documentation https://www.tensorflow.org/lite/guide/build_cmake and in this comment https://stackoverflow.com/a/70261676. Any ideas on how to do this? Is there any way to have a custom CMakeLists for a Bela project? Or am I missing any basic linking libraries commands?

Thank you!!

Edit: I changed the make command semicolons following this: https://forum.bela.io/d/2223-compiling-with-external-libraries/2
Edit2: there's this FindTFLite.cmake file https://github.com/rodrigodzf/DeepLearningForBela/blob/main/cmake/Modules/FindTfLite.cmake but I'm not very sure how to add it to the Bela project cmake
Edit3: I also tried copying libtensorflow-lite.a to usr/local/lib and the contents of /root/Bela/libraries/tensorflow-lite/tensorflow/lite (all of the *.h and *.cpp files) into usr/local/include/libtensorflow-lite. With this, running the command make -C /root/Bela PROJECT=tf-test LDLIBS="-llibtensorflow-lite" still gives the same error

    pelinski The compiler doesn't seem to find the library files since I get this error:

    /root/Bela/projects/tf-test/render.cpp:2:10: fatal error: 'tensorflow/lite/interpreter.h' file not found

    when you get this type of error, it means that the CPPFLAGS do not include a folder which contains tensorflow/lite/interpreter.h. You say you were using:

    CPPFLAGS="-I/root/Bela/libraries/tensorflow-lite/tensorflow/lite"

    , which makes me think that the file you are looking for is /root/Bela/libraries/tensorflow/lite/interpreter.h.
    If that is the case, then you should have

    CPPFLAGS="-I/root/Bela/libraries/tensorflow-lite/"

    in order for the #include to find the correct file. Now the C-preprocessor will be able to find the relative path 'tensorflow/lite/interpreter.h

    Can you show the output of

    find /root/Bela/libraries/tensorflow-lite

    and the content of /root/Bela/libraries/tensorflow-lite/lib.metadata (if it exists)?

      giuliomoro
      Hi Giulio,

      thanks a lot for your fast reply! You were right, cmake found the tf lite library using this command:

      make -C /root/Bela PROJECT=tf-test CPPFLAGS="-I/root/Bela/libraries/tensorflow-lite/" LDFLAGS="-L/root/Bela/libraries/tensorflow-lite/build" LDLIBS="-ltensorflow-lite"

      Then I got an error because it's not able to find the library flatbuffers, so I downloaded but the last version has compatibility issues with tensorflow 2.x (something to do with the function VerifyField). I found that I need to use version 1.12 ( https://github.com/tensorflow/tensorflow/issues/51487#issue-970332029). When compiling flatbuffers, the errors as warnings flags (-Werror) was on so I had to remove it from CMakeLists. I managed to compile the program using the following command:

      make -C /root/Bela PROJECT=tf-test CPPFLAGS="-I/root/Bela/libraries/tensorflow-lite/ -I/root//Bela/libraries/flatbuffers/include/" LDFLAGS="-L/root/Bela/libraries/tensorflow-lite/build -L/root/Bela/libraries/flatbuffers/" LDLIBS="-ltensorflow-lite -lflatbuffers"

      I will update detailed descriptions soon in case anyone wants to reproduce.