如何设置Tensorflow Lite C ++的输入

智妍公园

我正在尝试使用TensorflowLite模型测试简单的tensorflow lite c ++代码。它得到两个浮点数并做异或。但是,当我更改输入时,输出不会更改。我猜这行interpreter->typed_tensor<float>(0)[0] = x是错误的,因此输入没有正确应用。我应该如何更改代码才能工作?

这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include "tensorflow/contrib/lite/kernels/register.h"
#include "tensorflow/contrib/lite/model.h"
#include "tensorflow/contrib/lite/string_util.h"
#include "tensorflow/contrib/lite/tools/mutable_op_resolver.h"

int main(){
        const char graph_path[14] = "xorGate.lite";
        const int num_threads = 1;
        std::string input_layer_type = "float";
        std::vector<int> sizes = {2};
        float x,y;

        std::unique_ptr<tflite::FlatBufferModel> model(
                tflite::FlatBufferModel::BuildFromFile(graph_path));

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

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

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

        if(num_threads != 1){
                interpreter->SetNumThreads(num_threads);
        }

        int input = interpreter->inputs()[0];
        interpreter->ResizeInputTensor(0, sizes);

        if(interpreter->AllocateTensors() != kTfLiteOk){
                printf("Failed to allocate tensors\n");
                exit(0);
        }

        //read two numbers

        std::printf("Type two float numbers : ");
        std::scanf("%f %f", &x, &y);
        interpreter->typed_tensor<float>(0)[0] = x;
        interpreter->typed_tensor<float>(0)[1] = y;

        printf("hello\n");
        fflush(stdout);
        if(interpreter->Invoke() != kTfLiteOk){
                std::printf("Failed to invoke!\n");
                exit(0);
        }
        float* output;
        output = interpreter->typed_output_tensor<float>(0);
        printf("output = %f\n", output[0]);
        return 0;
}

这是我运行代码时出现的消息。

root@localhost:/home# ./a.out 
nnapi error: unable to open library libneuralnetworks.so
Type two float numbers : 1 1
hello
output = 0.112958
root@localhost:/home# ./a.out 
nnapi error: unable to open library libneuralnetworks.so
Type two float numbers : 0 1
hello
output = 0.112958
智妍公园

通过更改解决

interpreter->typed_tensor<float>(0)[0] = x;

interpreter->typed_input_tensor<float>(0)[0] = x;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将OpenCV Mat设置为Tensorflow Lite输入和输出?

如何知道Tensorflow Lite模型的输入/输出功能信息?

在Tensorflow-lite中输入具有动态尺寸的图像

TensorFlow Lite:toco_convert用于任意大小的输入张量

Tensorflow Lite:关于tflite文件中的输入形状

用于可变大小输入的 Tensorflow Lite

TensorFlow Lite C ++ API示例进行推理

无论输入如何,Tensorflow lite模型输出始终提供相同的输出

如何在输入按钮上使用 Material Design Lite

如何使用 Makefile 设置输入文件?(C)

如何在(Android Studio)NDK(C / C ++ API)中运行Tensorflow-Lite推理?

Tensorflow C API占位符/输入变量设置

仅当输入大小> 1时,TensorFlow Lite Android才会在GPU计算上崩溃

如何解释tensorflow lite c ++中的输出张量数据打包?

如何使用C ++概念(“ concepts lite”)支持构建gcc?

C 代码错误(从 Raspberry Pi 零 W 读取 GPIO 输入),在 Jessie 上编译但不在 Buster Lite 上编译

使用tensorflow lite micro ESP32将输入复制到CNN的缓冲区中

如何使用TensorFlow Lite进行批处理?

如何基于模型 Tensorflow lite 进行预测?

如何在 Xamarin.Android 中为 TensorFlow Lite .tflite 文件设置 aaptOptions

Tensorflow js VS Tensorflow Lite

如何使用Tensorflow张量设置Keras层的输入?

如何使用C ++将输入输入到已加载的Tensorflow模型中

C ++如何防止无效输入?

如何输入命令c++

带有 NativeScript 的 Tensorflow lite

在Tensorflow Lite C API中注册自定义运算符

为TensorFlow Lite C ++编写read_jpeg和Decode_jpeg函数

Caffe C ++在输入层中设置数据