keras-cv icon indicating copy to clipboard operation
keras-cv copied to clipboard

bug: Cannot save model from model.save(PATH)

Open slavazhulanov opened this issue 2 years ago • 12 comments

I trained the model with this tutorial. At the end, the WEIGHTS are saved, but I need to save the MODEL. When I save the MODEL I get the error:

ValueError: Model <keras_cv.models.object_detection.retina_net.retina_net.RetinaNet object at 0x00000287F451B6D0> cannot be saved either because the input shape is not available or because the forward pass of the model is not defined.To define a forward pass, please override Model.call(). To specify an input shape, either call build(input_shape)directly, or call the model on actual data usingModel(), Model.fit(), or Model.predict(). If you have a custom training step, please make sure to invoke the forward pass in train step through Model.call, i.e. model(inputs), as opposed to model.call().

Next, I plan to convert the MODEL to ONNX format and use it in OpenCV.

slavazhulanov avatar Feb 22 '23 03:02 slavazhulanov

The error message is correct! Your model is not built yet.

To specify an input shape, either call build(input_shape) directly, or call the model on actual data using Model(), Model.fit(), or Model.predict().

jbischof avatar Feb 22 '23 04:02 jbischof

@jbischof Could you take a look and tell me what I'm doing wrong? image

slavazhulanov avatar Feb 22 '23 06:02 slavazhulanov

The behavior of the save() method changes after a call to predict()

image

slavazhulanov avatar Feb 27 '23 04:02 slavazhulanov

WARNING:absl:Found untraced functions such as retina_net_label_encoder_layer_call_fn, retina_net_label_encoder_layer_call_and_return_conditional_losses, anchor_generator_layer_call_fn, anchor_generator_layer_call_and_return_conditional_losses, multi_class_non_max_suppression_layer_call_fn while saving (showing 5 of 110). These functions will not be directly callable after loading.

slavazhulanov avatar Feb 27 '23 08:02 slavazhulanov

Hi @slavazhulanov, I tried to run the colab tutorial and I was able to save my model. Could you post your colab here, so that I can take a full look at you code?

lb-97 avatar Feb 28 '23 18:02 lb-97

Hello @lb-97 I have attached a link here. The tutorial uses .save_weights() and my error is related to .save()

slavazhulanov avatar Mar 01 '23 03:03 slavazhulanov

In callbacks, try to change save_weights_only=True to False. It should work then after rerunning the entire code. @slavazhulanov

ID6109 avatar Mar 07 '23 15:03 ID6109

@ID6109 it didn't change anything. You can try it yourself on the link to Google Colab

slavazhulanov avatar Mar 08 '23 07:03 slavazhulanov

@slavazhulanov Try to save the model as model.save(CHECKPOINT_PATH, include_optimizer = None, save_traces = None)

ID6109 avatar Mar 27 '23 15:03 ID6109

Closing as stale -- it seems like KerasCV is WAI in this case

ianstenbit avatar Aug 04 '23 17:08 ianstenbit

having the same issue when i model.save as .h5/.keras i get: ValueError: Unable to create dataset (name already exists)

when i model.save as .tf , it saves the model, but when loading it i get:

ValueError: `RetinaNet` does not accept a `loss` to `compile()`. Instead, please pass `box_loss` and `classification_loss`. `loss` will be ignored during training.

what is the problem? what is the correct way to save and load the model?

Aillian avatar Aug 07 '23 11:08 Aillian

@Aillian can you send along a colab gist replicating your issue?

jbischof avatar Aug 07 '23 16:08 jbischof