How to add metadata to a tensorflow lite image recongition model Densenet121 with pretrained weights from imagenet?

42 Views Asked by At

I'm trying to insert metadata to a tflite DenseNet121 model to use it in an Android App, by having as an input an image and get as output the predictions of top 5 recognitions. My model is built and trained with the following code in python:


    base_model = DenseNet121(weights="imagenet", include_top=False, input_shape=(224, 224, 3))

    model = tf.keras.Sequential([
        base_model,
        layers.GlobalAveragePooling2D(),
        layers.Dense(128, activation='relu'),
        layers.Dense(len(class_names), name='outputs')  # Adjust units based on your number of labels
    ])

    model.compile(optimizer='adam',
                  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                  metrics=['accuracy'])

    model.summary()

    history = model.fit(
        train_ds,
        validation_data=val_ds,
        epochs=epochs,
        callbacks=[save_best_val_acc_CB]
    )

this outputs a tensorflow keras model, which then i convert into tflite, following the next code:

# Load model
model = load_model('testModel.keras')

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

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

I have followed the Tensorflow tutorial on how to build these here: Tensorflow Image Classification

I have tested both keras and tflite model and predictions seems legit: Prediction results

I also checked the signature list from my tflite model which is the following: Signature list

According to Tensorflow tutorials on adding metadata here, they have a code in this repository supposed to work but it doesn't on this model. I have tried it on other simpler models and it does work, but not for the DenseNet121. When i tried adding metadata it returns only 1 label and the predictions are totally wrong. The following picture shows the model parameters i use on metadata addition code: Model info Do i need to change the parameters that are populated in the model? Does it has to do with input and output labels in signature list that are different?

0

There are 0 best solutions below