Reputation: 149
I have issues with tensforflow when I try to load a model which contains more than 1 metric. It gives the following error:
ValueError: Unable to restore custom object of type _tf_keras_metric currently. Please make sure that the layer implements
get_config
andfrom_config
when saving. In addition, please use thecustom_objects
arg when callingload_model()
.
I have tried to search for solutions, but I can only find examples with 1 metric, which also works for me, but I need mulitple metrics. Hope you guys can help!
My code:
METRICS = [
keras.metrics.BinaryAccuracy(threshold = 0.5),
tfa.metrics.HammingLoss(mode='multilabel'),
tfa.metrics.F1Score(num_classes=4, threshold=0.5)
]
VOCAB_SIZE = 25000
encoder = tf.keras.layers.experimental.preprocessing.TextVectorization(max_tokens=VOCAB_SIZE, output_mode='int', pad_to_max_tokens = True)
encoder.adapt(X_train)
model = tf.keras.Sequential([encoder,
tf.keras.layers.Embedding(input_dim=len(encoder.get_vocabulary())+1, output_dim=64, mask_zero=True),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.GlobalAveragePooling1D(),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(32, activation='sigmoid', activity_regularizer=tf.keras.regularizers.L2(0.005)),
tf.keras.layers.Dense(4, activation='sigmoid')
])
model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=False),
optimizer=tf.keras.optimizers.Adam(learning_rate = 0.0005),
metrics= METRICS),
history = model.fit(X_train, label_train,
epochs=2,
validation_split = 0.2,
batch_size = 32,
verbose = 1,
shuffle = True)
# --- Save trained model --- #
model.save('CNN_model_fit_1.2', save_format = 'tf')
# --- Load model --- #
from keras.models import load_model
def BinaryAccuracy(label_test, test_predictions):
return 1
def HammingLoss(label_test, test_predictions):
return 1
def F1Score(label_test, test_predictions):
return 1
model_new = load_model("CNN_model_fit_1.2", custom_objects={'binary_accuracy':BinaryAccuracy,'hamming_loss':HammingLoss,'f1_score':F1Score})
model_new.get_weights()
pred = model_new.predict(X_test)
Upvotes: 1
Views: 431
Reputation: 314
I cannot test your code because you have not provided shape and size of X_train
but here is an idea:
Tensorflow addons is a separate library from Tensorflow , hence its metrics can be thought of as custom objects.
#Add tf.metrics before the name of the metrics
model_new = load_model("CNN_model_fit_1.2", custom_objects={'hamming_loss': tfa.metrics.HammingLoss,'f1_score': tfa.metrics.F1Score})
Upvotes: 2