get_config missing while loading previously saved model without custom layers

Question:

I have a problem with loading the previously saved model.

This is my save:

def build_rnn_lstm_model(tokenizer, layers):
    model = tf.keras.Sequential([
        tf.keras.layers.Embedding(len(tokenizer.word_index) + 1, layers,input_length=843),
        tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(layers, kernel_regularizer=l2(0.01), recurrent_regularizer=l2(0.01), bias_regularizer=l2(0.01))),
        tf.keras.layers.Dense(layers, activation='relu', kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01)),
        tf.keras.layers.Dense(layers/2, activation='relu', kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01)),
        tf.keras.layers.Dense(1, activation='sigmoid')
    ])
    model.summary()
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy',f1,precision, recall])
    print("Layers: ", len(model.layers))
    return model

model_path = str(Path(__file__).parents[2]) + os.path.sep + 'model'
data_train_sequence, data_test_sequence, labels_train, labels_test, tokenizer = get_training_test_data_local()
model = build_rnn_lstm_model(tokenizer, 32)
model.fit(data_train_sequence, labels_train, epochs=num_epochs, validation_data=(data_test_sequence, labels_test))
model.save(model_path + os.path.sep + 'auditor_model', save_format='tf')

After this I can see that auditor_model is saved in model directory.

now I would like to load this model with:

model = tf.keras.models.load_model(model_path + os.path.sep + 'auditor_model')

but I get:

ValueError: Unable to restore custom object of type _tf_keras_metric
currently. Please make sure that the layer implements get_configand
from_config when saving. In addition, please use the
custom_objects arg when calling load_model().

I have read about custom_objects in TensorFlow docs but I don’t understand how to implement it while I use no custom layers but the predefined ones.

Could anyone give me a hint how to make it work? I use TensorFlow 2.2 and Python3

Asked By: Mithrand1r

||

Answers:

Your example is missing the definition of f1, precision and recall functions. If the builtin metrics e.g. 'f1' (note it is a string) do not fit your usecase you can pass the custom_objects as follows:

def f1(y_true, y_pred):
    return 1

model = tf.keras.models.load_model(path_to_model, custom_objects={'f1':f1})
Answered By: SimonFojtu

You have a custom metric f1.
If you want to evaluate your model on test data after saving it and if you need the f1 score for test data you can try this:

model = tf.keras.models.load_model(path_to_model, custom_objects={‘f1’:f1}, compile=False)

// In the custom_objects map define all the custom objects along with
appropriate name

model.compile(metrics=[‘accuracy’,f1,precision, recall])

model.evaluate() …

Answered By: balzkowiz