pythondarttensorflowmachine-learningtensorflow-lite

TF Lite shows only zeros on prediction


I'm building a model in Tensorflow 2.10 for a classification problem. The model works fine on PC, but when converted to TF Lite, it shows only zeros in the prediction list. Model was tested on Android 13 using Flutter.

I want to repair the model but I don't see any error that would cause this result.

Code in Dart looks this:

final String column_order = List ColumnOrderData = await json.decode(column_order);
var beacons = data[0]['beacons'];
var exampleData = List<double>.filled(234, 0);

for (var i = 0; i < beacons.length; i++) {
  var beaconName = beacons[i]['beaconName'];
  double beaconRssiValue = beacons[i]['rssi'].toDouble();
  if (beaconRssiValue != 127) {
    beaconRssiValue = (beaconRssiValue * (-1)) / 105;
    int index = ColumnOrderData.indexWhere((element) => element == beaconName);
    exampleData[index] = beaconRssiValue;
  }
}

var output = List<double>.filled(4, 0);
final interpreter = await Interpreter.fromAsset('assets/model.tflite');
interpreter.run([exampleData], output.reshape([1, 4]));

This is part of model:

visible = Input(shape=(234,))
hidden1 = Dense(250, activation='relu', kernel_initializer='he_normal')(visible)
drp1 = Dropout(0.5)(hidden1)
norm1 = BatchNormalization()(drp1)
hidden2 = Dense(125, activation='relu', kernel_initializer='he_normal')(norm1)
drp2 = Dropout(0.5)(hidden2)
norm2 = BatchNormalization()(drp2)
hidden3 = Dense(80, activation='relu', kernel_initializer='he_normal')(norm2)
drp3 = Dropout(0.5)(hidden3)
norm3 = BatchNormalization()(drp3)
hidden4 = Dense(40, activation='relu', kernel_initializer='he_normal')(norm3)
drp4 = Dropout(0.5)(hidden4)
norm4 = BatchNormalization()(drp4)
out_reg = Dense(4, activation='softmax')(norm4)
model = Model(inputs=visible, outputs=[out_reg])
model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.001))

Code to convert in Python:

tf.saved_model.save(model, 'saved_models\\')
converter = tf.lite.TFLiteConverter.from_saved_model('saved_models\\')
tflite_model = converter.convert()
with open('model.tflite', 'wb') as f:
  f.write(tflite_model)

Input look like this:

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.8952380952380953, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.9523809523809523, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.8952380952380953, 0.0, 0.0, 0.9238095238095239, 0.9428571428571428, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]

And output like this:

[0.0, 0.0, 0.0, 0.0]

Solution

  • After many trail I found out that shape must be reshaped when variable is initialized var output = List.filled(4, 0).reshape([1, 4]);