简体   繁体   English

在 tensorflow-lite C++ 中输入图像

[英]Input Image in tensorflow-lite C++

I am trying to move a Python+Keras model to Tensorflow Lite with C++ for an embedded platform.我正在尝试使用 C++ 将 Python+Keras 模型移动到 Tensorflow Lite 以用于嵌入式平台。 I don't know how to pass the image data to the interpreter properly.我不知道如何正确地将图像数据传递给解释器。

I have the following working python code:我有以下工作 python 代码:

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).图像的输入形状是 (1, 224, 224, 3)。 I need the equivalent C++ code for the same.我需要相同的等效 C++ 代码。 How do I translate this?我如何翻译这个?

I have the following C++ code upto now:到目前为止,我有以下 C++ 代码:

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

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参考https : //www.tensorflow.org/lite/microcontrollers/get_started

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM