Load customized metrics failed

Viewed 52

I have a customized metrics called f1_metric,

and I'm trying to load my model with this customized metrics with following code

def f1_metric(y_true, y_pred):
    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


metric_dict = {
    'f1_metric':f1_metric
}

model = tf.keras.models.load_model(models_save,custom_objects = metric_dict)

but I'm still running into the situation where error shows ValueError: Unknown metric function:f1_metric

Can anyone help me with this?

Thank you!

1 Answers

Try doing the following:

x = f1_metric
metric_dict = {'x':f1_metric}

If that does not work, try updating Keras and TensorFlow on your device and run the code again.

If that fails too, refer https://stackoverflow.com/a/51706192/10399281

Related