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;
}