2

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...

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Gaia
  • 31
  • 5
  • Did you use this [ImageClassifier](https://www.tensorflow.org/lite/inference_with_metadata/task_library/image_classifier) class provided by TFLite Task Library? If so, can you make sure that your model has the [correct metadata](https://www.tensorflow.org/lite/convert/metadata)? – yyoon Mar 13 '21 at 07:52
  • Yeah, I used the official ImageClassifier and I modified it. And no, I didn't have metadata, but I don't know how to do it. You can check the edit post again to see the details about it. Tysm for ur reply. – Gaia Mar 13 '21 at 10:44
  • Thanks for updating the question. I believe the `No module named 'tensorflow'` error can go away once you run `pip install tensorflow` from the command line, before running the metadata writer script. Can you try that? – yyoon Mar 14 '21 at 02:36
  • OMG, I thought I had installed tensorflow but not... I'm so dumb. Thank you so much. Now I have another error, I edited the post. It seems the reason it's the way I made the _.tflite_ file or the data in the files, because it's so strange that it have 2 outputs identity and identity_1... Is it normal? Meanwhile, I'm going to try with another model to see if everything it's ok. – Gaia Mar 15 '21 at 11:23
  • For a typical image classification model for TFLite (e.g., mobilenet series), having two output tensors is not normal. You might want to check what exact parameters you're using when converting the model into tflite format. – yyoon Mar 15 '21 at 15:35
  • I tried with another Image Classifier float model and the result was this: `ValueError: The model info for, fruits.tflite, is not defined yet.`. I search for help but I didn't found nothing. Could u help me? plsss – Gaia Mar 15 '21 at 19:27
  • Not knowing where that custom model came from, I can't really help. Can you try one of the models officially hosted on tfhub? For example: https://tfhub.dev/s?deployment-format=lite&module-type=image-classification&network-architecture=mobilenet-v2 – yyoon Mar 16 '21 at 14:25
  • Thank you sooo much. It works! – Gaia Mar 24 '21 at 18:46

0 Answers0