-
Notifications
You must be signed in to change notification settings - Fork 74.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Subclassing tf.keras.models.Model save_model saves h5py but not h5/hdf5 file types #29545
Comments
@Ryandry1st I tried reproducing the issue but looks code snippet is incomplete. Please provide minimal code snippet to reproduce issue reported here. Also provide the error message that you have got. Thanks! |
The only code not provided was the actual data for the train and test data objects. It is possible to reproduce with the MNIST/ fashion MNIST or really any other dataset, simply change the 10 from the line NotImplementedError: Saving the model to HDF5 format requires the model to be a Functional model or a Sequential model. It does not work for subclassed models, because such models are defined via the body of a Python method, which isn't safely serializable. Consider saving to the Tensorflow SavedModel format (by setting save_format="tf") or using The entire traceback is attached in the text file. |
The point of this issue is that if it cannot be safely serialized then saving as an h5py either is handling it improperly, and should raise an error similar to the above, OR it works as it should with h5py (based on only this one test case and limited testing besides checking weights and accuracy) and the documentation should include this information, and probably adopt the method/recommend it. |
Any luck on this issue? One option for the data is of course just randomly generating x and y values. It does not matter what data is fed into the network, only that it is a trained subclassed network that is attempted to save |
The code snippet you have provided looks incomplete. Can you please check with latest tf-2.0--nightly version? |
Here is a more basic code snippet that throws the error. I do want to stress though that the only difference is that I am providing some randomly generated x and y data, and removing layers from the custom model for simplicity. There is no real new code or any ideas and it was no stretch to reach the warning using the previous code, and using random data as was suggested a month ago.
You will see that this throws the error, but the following would not.
So somewhere along the way it should either be adopted, the error removed, or it should throw a warning using this file format as well. |
I also tried using the preview with the more complicated code and found that it throws a new error, which appears to be due to improperly recalling custom objects. This may be some of the premise behind the warning, but it did work when using the beta version and no longer works with the nightly preview.
|
In TensorFlow 2.0, you should save the model with (The documentation describes the argument on this page: https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/keras/Model#save) I have an internal change that should fix the ValueError ( |
Issue reported in: tensorflow#30808 Similar issue: tensorflow#29545 PiperOrigin-RevId: 263390265
I agree with the save format, my question comes down to the use of h5py, which seems to allow the model to be saved, and it is able to load the model correctly then too. So the question is, should it not allow h5py save format with subclassed models, or if it does work correctly, then shouldn't this be a standard? |
Calling |
That is fine, however, that does not solve the actual issue. The issue is that there is no error or warning if this method is used to save the model, even though it explicitly states that you should not be able to save a subclassed models, yet it clearly allows it if you change the saved model extension to be ".h5py". So regardless of what is occurring in the background, there is a problem that subclassed models are being saved, unless this can be adopted for all instances. |
This problem has not been solved in rc0, and should be reopened. |
I would like to get a better understanding of the issue is. Perhaps docstring for the save_format is unclear? Currently, it says:
|
The NotImplementedError error from an earlier post above states that HDF5 cannot save out subclassed models. The SavedModel format is able to handle these models, so you should not get an error. |
I see, so the SavedModel format is able to handle these models. I believe this should be documented then in the warning, as the warning seems to suggest that there is not a clear method for saving these models outside of saving the weights independently from the model, which is obviously more complicated and prone to error |
Specifying that the SavedModel format is able to handle custom models sounds reasonable and would remove the ambiguity. I'll add that to the warning message. Thanks for the suggestion! |
Thank you for clearing that up, appreciate it! |
Now what is the best way to save model created in subclassed ? Were there some examples for how to save and load models ? thank you! |
I understand that so long as you use the save model format, which is done by calling |
Closing this issue since I understand it to be resolved by updating the warning, but please let me know if I'm mistaken. The current warning is clearly mentions what to do to save subclass model. Please check the gist here. Current Warning message is as follows |
Hi @Ryandry1st, may I have help on this similar issue please, using tf==2.2.0 I have tried
and loaded back with
still i get
and I tried with tf=2.0.0 version, getting same error, I need of help please |
Found it, Thanks
didn't work for me First we have to save_weights from the built model
Then
This work perfectly in TensorFlow==2.2.0 |
Glad you got that figured out @hanzigs, the way you are doing it is one that I use as well when I have custom classes/layers which require the subclassing method. It seems like with the definition of base_config it should work to save the entire model as a SavedModel format, but this has not been my experience. As you said, I just save the weights and then instantiate a new model and load the weights. |
@Ryandry1st Similar issue, I'm trying to save a subclass model and convert it to TensorFlowJS layers model format. This only works if I'm able to get the model to be saved as h5 but I get the error about not being able to save a non-sequential or functional model. The insistence for me to have the layers model format is so I can retrain using TFJS. Any suggestions? |
I believe you cannot save the model in its entirety as an h5 format because this ignores the information of the control flow/ops that are in the subclasses model. Instead, you need to save the weights in h5 format then make a new model the same way, train_on_batch as shown above, and then load the weights. I have not had to deploy a subclasses model in TF JS so I could not say if this is usable or not. |
Hi, Adding to @hanzigs , When I train the model and the save the weights using, model.save_weights(path) And load it in another session using, model1 = DCN( ) The prediction got in the first session from model and the prediction got from the second session model1 is completely different. How do I solve this ?? NOTE: If I save the model using, Then I get the below warning. WARNING:absl:Found untraced functions such as ranking_layer_call_fn, ranking_layer_call_and_return_conditional_losses, dense_layer_call_fn, dense_layer_call_and_return_conditional_losses, ranking_layer_call_fn while saving (showing 5 of 10). These functions will not be directly callable after loading. This warning cannot be ignored because when I load the model and try to evaluate it with the same test set , I get the following |
What should I do if I get this error when saving the model using ModelCheckpoint with save_weights_only=False ? The error: Thanks |
I'm doing something similar but it didnt work for me! |
Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template
System information
Describe the current behavior
-Using tf 2.0.0b-gpu on google colab.
While using the subclassing API for a subclassed layer and model, I was unable to use the model.save_model() function for h5 or hdf5 file types, but I could successfully save and load the model if it was saved as h5py file type. In the toy example being used it worked correctly, although this may not be the case. Note that the get_config method was implemented in the custom layer and custom model.
Describe the expected behavior
Either the save_model should always work (I believe this is a feature goal) and the documentation should reflect this, or if the save is likely to produce incorrect results it should raise an error and the documentation should continue to suggest that custom models can only be saved with the save_weights feature.
Code to reproduce the issue
Other info / logs
This will raise an error that only sequential or functional models can be saved
model.save('custom_model.h5')
The text was updated successfully, but these errors were encountered: