'Tensorflow Lite Model: Incompatible shapes for input and output array

I'm currently working on a Tensorflow Lite image classifier app that can recognice UNO cards. But when I'm running the float model in the class ImageClassifier, something is wrong.

The error is the next one:

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

Here's the code that throw that error:

tflite.run(imgData, labelProbArray);

And this is how I have created imgData and 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}

You can chech the inputs and ouputs of the .tflite file. Source.

I know you should create a buffer for the output values, but I tried to import this and didn't work: import org.tensorflow.lite.support.tensorbuffer.TensorBuffer;

Any ideas? Thank you so much for read me^^

Edit v2:

Thanks to yyoon I realise that I didn't populate my model with metadata, so I run this line in my 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

Before that, I modified this file with my data:

_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)
}

And another error appeared:

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

Idk why my model have 2 tensors outputs...



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source