Search code examples
pythontensorflowkerasdeep-learningjupyter-notebook

VGG16 Transfer Learning - Unkown Metric function: f1_score error


I have a transfer learning model where I used VGG16 and then added some dense, dropout and batch_normalization layers to. I trained the model and saved it as Study3_v1.h5 using the model.save('Study3_v1.h5') command.

However, when I tried to load it using the model = tf.keras.models.load_model('Study3_v1.h5') command it gave me the error below:

ValueError: Unknown metric function: f1_score. Please ensure this object is passed to the custom_objects argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.

What I think is causing the error are the callbacks or the metrics I used before compiling and training my model, their codes are below:

def f1_score(y_true, y_pred): #taken from old keras source code
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
    precision = true_positives / (predicted_positives + K.epsilon())
    recall = true_positives / (possible_positives + K.epsilon())
    f1_val = 2*(precision*recall)/(precision+recall+K.epsilon())
    return f1_val
METRICS = [
      tf.keras.metrics.BinaryAccuracy(name='accuracy'),
      tf.keras.metrics.Precision(name='precision'),
      tf.keras.metrics.Recall(name='recall'),  
      tf.keras.metrics.AUC(name='auc'),
        f1_score,
]
lrd = ReduceLROnPlateau(monitor = 'val_loss',patience = 20,verbose = 1,factor = 0.50, min_lr = 1e-10)

mcp = ModelCheckpoint('model.h5')

es = EarlyStopping(verbose=1, patience=20)

Compiling the model:

model.compile(optimizer='Adam',loss='categorical_crossentropy',metrics=METRICS)

Training the model:

history=model.fit(train_dataset,validation_data=valid_dataset,epochs = 5,verbose = 1,callbacks=[lrd,mcp,es])

What is causing the error? And what are the custom objects that I need to add to the load_model function

Thanks in advance!


Solution

  • Because you used a custom metric. So now you need to do this:

    model = tf.keras.models.load_model('Study3_v1.h5',custom_objects={"f1_score":f1_score})
    

    Whenever you have a custom object, like a custom model, layer, metrics... you need to do this if you save your model in h5.