why tensorflow lite model not working after converting from keras model?

Issue

This Content is from Stack Overflow. Question asked by Riyas PK

I converted a keras model in to tenserflow lite model using the code

model = tf.keras.models.load_model('liveness.model')

# Convert the model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

# Save the model.
with open('model.tflite', 'wb') as f:
  f.write(tflite_model)

But it not working and always giving same output for all inputs. I tried with android i am adding my android code as well.

bitmap = getBitmapImageWith32();

int inputSize=32;
int OUTPUT_SIZE=2;
float[][] embeedings;
int[] intValues;

ByteBuffer imgData = ByteBuffer.allocateDirect(1 * inputSize * inputSize * 3 * 4);
imgData.order(ByteOrder.nativeOrder());

bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

imgData.rewind();

int p = 0;
for (int i = 0; i < inputSize; ++i) {
    for (int j = 0; j < inputSize; ++j) {
        int pixelValue = intValues[pixel++];
        imgData.putFloat((((pixelValue >> 16) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
        imgData.putFloat((((pixelValue >> 8) & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
        imgData.putFloat(((pixelValue & 0xFF) - IMAGE_MEAN) / IMAGE_STD);
        p++;
    }
}

Object[] inputArray = {imgData};
Map<Integer, Object> outputMap = new HashMap<>();
embeedings = new float[1][OUTPUT_SIZE];
outputMap.put(0, embeedings);
tfLiteLiveness.runForMultipleInputsOutputs(inputArray, outputMap);

Log.e("first val", String.valueOf(embeedings[0][0]));
Log.e("second val", String.valueOf(embeedings[0][1]));

This is the model I am using

https://github.com/birdowl21/Face-Liveness-Detection-Anti-Spoofing-Web-App/blob/main/liveness.model



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?