TensorFlow Lite C++ API example for inference

前端 未结 2 1616
野趣味
野趣味 2021-02-06 02:05

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 exampl

2条回答
  •  青春惊慌失措
    2021-02-06 02:55

    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 
    #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 model = tflite::FlatBufferModel::BuildFromFile("linear.tflite");
    
        if(!model){
            printf("Failed to mmap model\n");
            exit(0);
        }
    
        tflite::ops::builtin::BuiltinOpResolver resolver;
        std::unique_ptr interpreter;
        tflite::InterpreterBuilder(*model.get(), resolver)(&interpreter);
    
        // Resize input tensors, if desired.
        interpreter->AllocateTensors();
    
        float* input = interpreter->typed_input_tensor(0);
        // Dummy input for testing
        *input = 2.0;
    
        interpreter->Invoke();
    
        float* output = interpreter->typed_output_tensor(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!

提交回复
热议问题