0

I am training multiple models (on Google Colab) and saving them using joblib. I am then downloading these files and loading them using joblib, but I am getting the following error;

Unable to restore custom object of type _tf_keras_metric

I am compiling the models using metrics=['accuracy', specificity, tf.keras.metrics.Precision()], where specificity is a custom metric function from https://www.sabinasz.net/unbalanced-classes-machine-learning/.

mobilenet_model = MobileNetV3Small(include_top=True, weights=None, classes=2, input_shape=(100, 100, 3))
mobilenet_model.compile(loss='binary_crossentropy', metrics=['accuracy', specificity, tf.keras.metrics.Precision()])
trained_model  = mobilenet_model.fit(X_train, y_train)

joblib.dump(trained_model, 'mobilenet_model.joblib')

I tried to load them using keras.models.load_model but I get a Error opening file (File signature not found) which according to Error opening file in H5PY (File signature not found) means the file is corrupt (I imagine that the way joblib saves models is not compatible with keras's save_model function).

I tried using keras.models.save_model to save a file and passing in custom_objects={'specificity': specificity, 'precision': tf.keras.metrics.Precision()}, but the model seems to be saved without the pretrained weights because when I evaluate it, it only has 0.5 accuracy, when it was getting 0.9 when I trained it.

So what can I do?

Bernard Borg
  • 1,225
  • 1
  • 5
  • 18

0 Answers0