keras-team / tf-keras

The TensorFlow-specific implementation of the Keras API, which was the default Keras from 2019 to 2023.
Apache License 2.0
63 stars 28 forks source link

KerasCV - Cannot save model from model.save(PATH) #247

Closed slavazhulanov closed 1 year ago

slavazhulanov commented 1 year ago

keras 2.11.0 keras-cv 0.4.2

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.

image image

sushreebarsa commented 1 year ago

@slavazhulanov I tried to replicate this issue and didn't face the error, could you please check this gist and confirm the same. Please let me know if I am missing something to replicate the issue. Thank you!

slavazhulanov commented 1 year ago

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

SuryanarayanaY commented 1 year ago

Hi @slavazhulanov ,

As the tutorial using are using keras.callbacks.ModelCheckpoint(CHECKPOINT_PATH, save_weights_only=True) in the callbacks, we can only save this model weights using model.save_weights() .

To use model.save() with the same above call back you should use it like below. model.save(CHECKPOINT_PATH,include_optimizer=None,save_traces=None)

Please refer to attached gist.

Thanks!

slavazhulanov commented 1 year ago

Hi @SuryanarayanaY,

Your reply solved my problem! Thank you very much)

slavazhulanov commented 1 year ago

@SuryanarayanaY But when I tried to make a prediction I got this error:

Cannot call custom layer RetinaNet of type <class 'keras.saving.legacy.saved_model.load.keras_cv>RetinaNet'>, because the call function was not serialized to the SavedModel.Please try one of the following methods to fix this issue:

(1) Implement `get_config` and `from_config` in the layer/model class, and pass the object to the `custom_objects` argument when loading the model. For more details, see: https://www.tensorflow.org/guide/keras/save_and_serialize

(2) Ensure that the subclassed model or layer overwrites `call` and not `__call__`. The input shape and dtype will be automatically recorded when the object is called, and used when saving. To manually specify the input shape/dtype, decorate the call function with `@tf.function(input_signature=...)`.

Call arguments received by layer 'RetinaNet' (type keras_cv>RetinaNet):
  • unused_args=('tf.Tensor(shape=(None, 640, 640, 3), dtype=float32)',)
  • unused_kwargs={'training': 'False'}`

Check this

SuryanarayanaY commented 1 year ago

Hi @slavazhulanov ,

If your model has custom objects or custom layers then you need to define get_config and from_config methods in the Model.Then during model.save() you need to pass an argument custom_objects with dictionary of custom objects.

Please refer to attached source for a minimal example.

You may also refer to this keras issue-17383 for an example.You need to recreate the model accordingly.

Thanks!

google-ml-butler[bot] commented 1 year ago

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

github-actions[bot] commented 1 year ago

This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.

google-ml-butler[bot] commented 1 year ago

Are you satisfied with the resolution of your issue? Yes No