After converting the model to .tflite and running it on Android, the accuracy drops

Hello everyone!

This is my first neural network, so there are often problems. And now there is a problem that I can’t solve.
My network produces a binary classification (patient is healthy, patient is sick). The input layer is fed 12 numeric values. I created and trained a neural network in Collab, it trained well and shows acceptable results on the validation sample (val_accuracy: 0.95
val_loss: 0.13), but after converting the model to .tflite and running it on a smartphone, it can’t predict anything.
I changed the number of layers, converted the model with tf.lite.TFLiteConverter.from_saved_model and tf.lite.TFLiteConverter.from_keras_model, viewed .tflite in Netron, tried to change the data input in Android, but nothing helped.

I think the problem is the wrong data transfer to the input layer of the tflite model in Android, but this is just a guess. And if so, please tell me how to fix the error?

This is my Colab code

raw_dataset = pd.read_csv('data.csv')
dataset = raw_dataset.copy()

train_dataset = dataset.sample(frac=0.8, random_state=0)
test_dataset = dataset.drop(train_dataset.index)

X = train_dataset.values
Y = test_dataset.values

Y = np.array(Y).astype("float32")

test_x, test_y = X[:,0:12], X[:,12]
train_x, train_y = Y[:,0:12], Y[:,12]

model = models.Sequential()
model.add(layers.Dense(64, activation = "tanh", input_dim=12))
model.add(layers.Dense(32, activation = "tanh"))
model.add(layers.Dense(16, activation = "tanh"))
model.add(layers.Dense(1, activation = "sigmoid"))

model.compile( optimizer = "adam", loss = "binary_crossentropy", metrics = ["accuracy"])

results = train_x, train_y, epochs = 100, batch_size = 10, validation_data = (test_x, test_y))

# Save model
mobilenet_save_path = "my_SavedModel", mobilenet_save_path)

# Convert the SavedModel
converter = tf.lite.TFLiteConverter.from_saved_model("my_SavedModel") # path to the SavedModel directory
tflite_model = converter.convert()

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

This is my Java code in Android

ByteBuffer byteBuffer = ByteBuffer.allocateDirect(48);

                try {
                    ModelSavedModel model = ModelSavedModel.newInstance(context);

                    // Creates inputs for reference.
                    TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 12}, DataType.FLOAT32);

                    // Runs model inference and gets result.
                    ModelSavedModel.Outputs outputs = model.process(inputFeature0);
                    TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();

                    // Releases model resources if no longer used.
                    float preResult = outputFeature0.getFloatArray()[0]*100;
                    int result = (int) preResult;
                } catch (IOException e) {
                    // TODO Handle the exception
1 Like

Hello @Van_Gil Prior to deploying the model, have you considered the following by any chance to improve generalization?

Or, perhaps, the issue here is with the Java code.

A few more sources:

1 Like

Have you found any solutions? I have the same problem: python - Poor tensorflow-lite accuracy in Android application - Stack Overflow – my model works in python but performs poorly in Android app. 1/3 of predictions are wrong, while python version predicts 100% correctly.

BTW, I wrote a script to load .tflite model in python and it works well, too, so the problem is not in .tflite file. The only problem is Android part and I have no idea how to solve it…