首页 > 解决方案 > 已解决 - Tensorflow Lite 模型:输入和输出数组的形状不兼容

问题描述

我目前正在开发一个可以识别 UNO 卡的 Tensorflow Lite 图像分类器应用程序。但是当我在ImageClassifier类中运行浮动模型时,出现了问题。

错误是下一个:

java.lang.IllegalArgumentException: Cannot copy from a TensorFlowLite tensor (Identity) with shape [1, 10647, 4] to a Java object with shape [1, 15].

这是引发该错误的代码:

tflite.run(imgData, labelProbArray);

这就是我创建 imgData 和 labelProbArray 的方式:

private static final int DIM_BATCH_SIZE = 1;
private static final int DIM_PIXEL_SIZE = 3; //r+g+b = 1+1+1

static final int DIM_IMG_SIZE_X = 416;
static final int DIM_IMG_SIZE_Y = 416;

imgData = ByteBuffer.allocateDirect(DIM_BATCH_SIZE * DIM_IMG_SIZE_X * DIM_IMG_SIZE_Y * DIM_PIXEL_SIZE * 4); //The last value because size of float is 4
labelProbArray = new float[1][labelList.size()]; // {1, 15}

您可以检查.tflite文件的输入和输出资源。

我知道您应该为输出值创建一个缓冲区,但我尝试导入它但没有奏效: import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;

有任何想法吗?非常感谢您阅读我^^

编辑 v2:

感谢yyoon,我意识到我没有用metadata填充我的模型,所以我在我的 cmd 中运行了这一行:

python ./metadata_writer_for_image_classifier_uno.py \ --model_file=./model_without_metadata/custom.tflite \ --label_file=./model_without_metadata/labels.txt \ --export_directory=model_with_metadata

在此之前,我用我的数据修改了这个文件:

_MODEL_INFO = {
    "custom.tflite":
        ModelSpecificInfo(
            name="UNO image classifier",
            version="v1",
            image_width=416,
            image_height=416,
            image_min=0,
            image_max=255,
            mean=[127.5],
            std=[127.5],
            num_classes=15)
}

又出现了一个错误:

ValueError: The number of output tensors (2) should match the number of output tensor metadata (1)

Idk 为什么我的模型有 2 个张量输出...

解决方案

那是因为模型不对。我只是尝试使用另一个模型,它可以工作。

标签: tensorflowtensorflow-liteimage-classification

解决方案


推荐阅读