Changes required for using non-quantized tflite files in CustomImageClassifier.java [proposed Label]:mlkit
I want to use a non-quantized tflite model. So, I made the following changes as mentioned here to CustomImageClassifier.java to overcome Cannot convert an TensorFlowLite tensor with type FLOAT32 to a Java object of type [[B (which is compatible with the TensorFlowLite type UINT8) . The code was written for quantised models and I'm trying to use an unquantised model.
**Added these two lines**
private static final int IMAGE_MEAN = 128;
private static final float IMAGE_STD = 128.0f;
**Changes made**
Line 161 : byte[][] labelProbArray = task.getResult().<byte[][]>getOutput(0);
.............................................................
float[][] labelProbArray = task.getResult().<float[][]>getOutput(0);
Line 222 : new AbstractMap.SimpleEntry<>(mLabelList.get(i), (labelProbArray[0][i]&0xff) / 255.0f));
.............................................................
new AbstractMap.SimpleEntry<>(mLabelList.get(i), (labelProbArray[0][i])));
Line 198 : imgData.put((byte) ((val >> 16) & 0xFF));
imgData.put((byte) ((val >> 8) & 0xFF));
imgData.put((byte) (val & 0xFF));
.............................................................
imgData.putFloat((((val >> 16) & 0xFF)-IMAGE_MEAN)/IMAGE_STD);
imgData.putFloat((((val >> 8) & 0xFF)-IMAGE_MEAN)/IMAGE_STD);
imgData.putFloat(((val & 0xFF)-IMAGE_MEAN)/IMAGE_STD);
But now I'm getting the following error: java.nio.BufferOverflowException at line 198 : imgData.putFloat((((val >> 16) & 0xFF)-IMAGE_MEAN)/IMAGE_STD);
When I'm changing DIM_BATCH_SIZE = 1 to 4 (reference) including the above changes, I'm getting
Cannot convert an TensorFlowLite tensor with type FLOAT32 to a Java object of type [[B (which is compatible with the TensorFlowLite type UINT8)
When I'm multiplying imgData size by 4(reference), I'm getting
Input 0 should have 150528 bytes, but found 602112 bytes.
cc @gkaldev @isaisachen
@gkaldev @isaisachen Does MLKit right now works only for quantised tflite models?
@duplex143 Nope, non-quantized model is also supported. It looks like a usage issue. Also, Saw you posted the same question on StackOverflow and our expert is helping you there already. Let's continue discussion on StackOverflow instead? https://stackoverflow.com/questions/50923996/changes-required-for-using-non-quantized-tflite-files-in-mainactivity-java#50923996