Some custom objects are not being serialized with push_to_hub_keras
See original GitHub issueSelf-contained code example:
from huggingface_hub import from_pretrained_keras
model = from_pretrained_keras("keras-io/vit-small-ds")
Error
/usr/local/lib/python3.7/dist-packages/keras/utils/generic_utils.py in class_and_config_for_serialized_keras_object(config, module_objects, custom_objects, printable_module_name) 560 if cls is None: 561 raise ValueError( –> 562 f’Unknown {printable_module_name}: {class_name}. Please ensure this ’ 563 'object is passed to the
custom_objects
argument. See ’ 564 ‘https://www.tensorflow.org/guide/keras/save_and_serialize’ValueError: Unknown optimizer: Addons>AdamW. Please ensure this object is passed to the
custom_objects
argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Serializing Custom Objects in Java - Stack Overflow
I have a method that is requiring me to serialize objects of a custom class 'Fraction'. When I attempt ...
Read more >Serialization and saving - Keras
The computation graph of custom objects such as custom layers is not included in the saved file. At loading time, Keras will need...
Read more >Serializing Objects that meet Custom Criteria with Jackson
We can notice how the second email address, hidden, has not been serialized. Finally, in this story, we've learned how to serialize objects...
Read more >Attempt to de-reference a null object while serializing a ...
Im running into an de-reference a null object error while trying to serialize a list of objects into JSON for a POST method....
Read more >Custom objects - Oracle Help Center
Custom objects allow you to store additional data in a scalable manner and link that data to a contact or account record. You...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I agree with what @ariG23498 wrote about custom layers, its flexibility (which makes it hard to automate) can be a boon. And the creators of custom layers are mostly power user anyways, in my experience 😄
For completeness of discussion, there is a point yet to be addressed in this discussion. The error that @osanseviero originally points at can be avoided by importing
tfa
, which is needed to load the optimizer. In other words:Digging deeper, we can see that
push_to_hub_keras
usestf.keras.models.save_model
under the hood (here). We might want to set itsinclude_optimizer
argument toFalse
, which removes the optimizer object before serialization, preventing errors like this from optimizers that are not in the standardtensorflow
library.What do you think?
EDIT: at the very least, we can throw a warning when the user is pushing a model to the hub with these kind of optimizers.
Hey @osanseviero @merveenoyan
I was successful in uploading the custom objects with
push_to_hub_keras
API. The main steps are:get_config
method for all the custom layers.augmentation
layers inside the model. TensorFlow2.7
has a problem with serializing the augmentation layers. Here I have used themap
function to map thetf.data
and used the augmentation as a preprocessing step rather than inside the mode.Colab Notebook used
Colab Notebook
Usage of the pre-trained
vit-ds-small
modelNote: You have to use the test augmentation to preprocess the input images before sending it to the model.