4

I have a tensorflow keras model with custom losses. After trianing, I want to store the model using model.save(path) and in another python script load the model for prediction only using model = tf.keras.models.load_model(model_path, compile=False).

Without compile=False, tensorflow will complain about the missing loss function. Calling model.prediction however will result in a Model object has no attribute 'loss' error message. I would like to call model.predict without needing to specify the loss again.

Is there a solution to save/load a tf.keras.Model without the custom loss to using the model for prediction?

Code

Since it was asked, the model trains on multiple outputs / losses and I define the losses with lambdas to capture weightings etc. This looks like this:

losses = [lambda y_true, y_pred: util.weighted_mse_loss(y_true, y_pred, tf.square(gain_weight)), 
        lambda y_true, y_pred: util.weighted_mse_loss(y_true, y_pred, tf.square(Rd_weight)), 
        lambda y_true, y_pred: util.pole_zero_loss(y_true, y_pred, r_weight, w_weight),
        lambda y_true, y_pred: util.pole_zero_loss(y_true, y_pred, r_weight, w_weight)]


model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=10E-4),
    loss=losses)
ruhig brauner
  • 943
  • 1
  • 13
  • 35
  • Only for prediction I'd save/load weights. And not the entire model. – Fariborz Ghavamian May 19 '20 at 15:35
  • Could you please show your code? I have a model with custom loss, but `model = tf.keras.models.load_model(model_path, compile=False)` `y_pred = model.predict(X_test)` works well. – Yoskutik May 19 '20 at 16:27
  • @Yoskutik I think the issue is that I use lambdas for my loss. I'll add it to the question – ruhig brauner May 26 '20 at 11:09
  • Have you tried call `load_model` with `compile=False`, and after that compile the model with whatever losses you want? – Yoskutik May 26 '20 at 11:13
  • 1
    @Yoskutik Yes, that's basically what I do right now but since I need to define a loss, I might as well redefine the loss I use. It's a workaround for an issue that shouldn't require a loss at all. – ruhig brauner May 26 '20 at 11:19

1 Answers1

0

As mentioned here keras has tf.keras.models.load_model(filepath, custom_objects=None, compile=True) function. If you have custom loss, you can use:

model = keras.models.load_model('model.h5', custom_objects={'my_loss': my_loss})

Where 'my_loss' is your loss' name, and my_loss is the function.

Yoskutik
  • 1,859
  • 2
  • 17
  • 43
  • I've seen this but it seems that I need to store the loss function here as well. I might as well recompile with the loss (which I currently do to prevent the issue). However, since I only want to predict and don't need a loss, I'm really searching for a method were I don't have to worry about the loss after saving the model. – ruhig brauner May 19 '20 at 15:53