Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input Image in tensorflow-lite C++

I am trying to move a Python+Keras model to Tensorflow Lite with C++ for an embedded platform. I don't know how to pass the image data to the interpreter properly.

I have the following working python code:

interpreter = tf.lite.Interpreter(model_path="model.tflite")
print(interpreter.get_input_details())
print(interpreter.get_output_details())
print(interpreter.get_tensor_details())
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
input_shape = input_details[0]['shape']
print("Input Shape ")
print(input_shape)

image_a = plt.imread('image/0_0_0_copy.jpeg')
image_a = cv2.resize(image_a,(224,224))
image_a = np.asarray(image_a)/255
image_a = np.reshape(image_a,(1,224,224,3))

input_data = np.array(image_a, dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)

interpreter.invoke()

output_data = interpreter.get_tensor(output_details[0]['index'])
print("Output Data ")
print(output_data)

The input shape for the image is (1, 224, 224, 3). I need the equivalent C++ code for the same. How do I translate this?

I have the following C++ code upto now:

int main(){

    std::unique_ptr<tflite::FlatBufferModel> model =
    tflite::FlatBufferModel::BuildFromFile('model.tflite');

    if(!model){
        printf("Failed to map model\n");
        exit(0);
    }

    tflite::ops::builtin::BuiltinOpResolver resolver;
    tflite::InterpreterBuilder builder(*model, resolver);
    std::unique_ptr<tflite::Interpreter> interpreter;

    if(!interpreter){
        printf("Failed to construct interpreter\n");
        exit(0);
    }

    tflite::PrintInterpreterState(interpreter.get());
    
    interpreter->AllocateTensors();
    interpreter->SetNumThreads(4);
    interpreter->SetAllowFp16PrecisionForFp32(true);

    if(interpreter->AllocateTensors() != kTfLiteOk){
        printf("Failed to allocate tensors\n")
    }
    LOG(INFO) << "tensors size: " << interpreter->tensors_size() << "\n";
    LOG(INFO) << "nodes size: " << interpreter->nodes_size() << "\n";
    LOG(INFO) << "inputs: " << interpreter->inputs().size() << "\n";
    LOG(INFO) << "input(0) name: " << interpreter->GetInputName(0) << "\n";

    float* input = interpreter->typed_input_tensor<float>(0);
    // Need help here

    interpreter->Invoke();

    float* output = interpreter->typed_output_tensor<float>(0);

    printf("output1 = %f\n", output[0]);
    printf("output2 = %f\n", output[1]);


    return 0;
}
like image 396
sareeb_akro Avatar asked Oct 27 '25 04:10

sareeb_akro


1 Answers

I solved the problem in this way.

Build the interpreter as usual:

// Load model
std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile(filename);
TFLITE_MINIMAL_CHECK(model != nullptr);

// Build the interpreter
tflite::ops::builtin::BuiltinOpResolver resolver;
compute_engine::tflite::RegisterLCECustomOps(&resolver);
enter code here

InterpreterBuilder builder(*model, resolver);
std::unique_ptr<Interpreter> interpreter;
builder(&interpreter);
TFLITE_MINIMAL_CHECK(interpreter != nullptr);

TFLITE_MINIMAL_CHECK(interpreter->AllocateTensors() == kTfLiteOk);

To get the input shape:

const std::vector<int>& t_inputs = interpreter->inputs();
TfLiteTensor* tensor = interpreter->tensor(t_inputs[0]);

// input size - for a cnn is four: (batch_size, h, w, channels)
input_size = tensor->dims->size;

batch_size = tensor->dims->data[0];
h = tensor->dims->data[1];
w = tensor->dims->data[2];
channels = tensor->dims->data[3];

This worked for me. I hope it will be good for you too.

Reference: https://www.tensorflow.org/lite/microcontrollers/get_started

like image 67
bruno F Avatar answered Oct 28 '25 19:10

bruno F



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!