Вывод и получение результата (массива) с помощью модели tflite на С++C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Вывод и получение результата (массива) с помощью модели tflite на С++

Сообщение Anonymous »

Я пытаюсь преобразовать код с Python в C++. При выводе кода на С++ я не могу понять, как получить результат (массив). Код Python и частичный код C++ следующие:
Информация:
  • Модель: tflite
  • формат выходных_данных (Python):
  • формат результата (Python):
Код Python:
import numpy as np
import tensorflow as tf
import cv2
import torch

# Load TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="./model.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

img = cv2.imread("image.jpg")
tensor_input = np.array([img])
tensor_input = np.float32(tensor_input)
interpreter.set_tensor(input_details[0]['index'], tensor_input)

interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
result = torch.from_numpy(output_data)

Код C++:
#include
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/kernels/register.h"

#include
#include
#include
#include

int main() {

// Load Model
std::unique_ptr model = tflite::FlatBufferModel::BuildFromFile("model.tflite");

// Initiate Interpreter
std::unique_ptr interpreter;
tflite::ops::builtin::BuiltinOpResolver resolver;
tflite::InterpreterBuilder(*model.get(), resolver)(&interpreter);

// Allocate tensors.
if (interpreter->AllocateTensors() != kTfLiteOk)
{
fprintf(stderr, "Failed to allocate tensor\n");
exit(-1);
}

// Configure the interpreter
interpreter->SetAllowFp16PrecisionForFp32(true);
interpreter->SetNumThreads(1);

// Get Input Tensor Dimensions
int input = interpreter->inputs()[0];
auto height = interpreter->tensor(input)->dims->data[1];
auto width = interpreter->tensor(input)->dims->data[2];
auto channels = interpreter->tensor(input)->dims->data[3];

// Load Input Image
cv::Mat image;
auto image = cv::imread("image.jpg");

// Copy image to input tensor
image.convertTo(image, CV_32F, 1.0 / 255.0); // Normalize to [0, 1]
std::memcpy(interpreter->typed_input_tensor(0), image.data, image.total() * image.elemSize());

// Inference
interpreter->Invoke();

// How to get the reult from interpreter and print the Tensor outcome?

return 0;

}


Подробнее здесь: https://stackoverflow.com/questions/785 ... model-in-c
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»