Segformer, can't save checkpoint in saved_model format
See original GitHub issueThanks for this repo !
Issue description:
- initalise your segformer model
- try to save it.
Error:
File "/home/maxime/anaconda3/envs/tf/lib/python3.9/site-packages/transformers/models/segformer/modeling_tf_segformer.py", line 547, in serving *
output = self.call(inputs)
File "/home/maxime/anaconda3/envs/tf/lib/python3.9/site-packages/transformers/models/segformer/modeling_tf_segformer.py", line 753, in call *
batch_size = shape_list(encoder_hidden_states[-1])[0]
KeyError: -1
Minimum reproducting code:
import os
import tensorflow as tf
from transformers import TFSegformerForSemanticSegmentation
model = TFSegformerForSemanticSegmentation.from_pretrained(
"nvidia/mit-b0",
num_labels=2,
id2label={1:"1", 2:"2"},
ignore_mismatched_sizes=True, # Will ensure the segmentation specific components are reinitialized.
)
model.summary(line_length=250)
tf.keras.models.save_model(
model, os.path.join("/tmp", model.name), include_optimizer=False
)
Expected behavior
The checkpoint should save correctly
System Info
Ubuntu 20.04, Python 3.9, TF 2.9.1, Nvidia Titan
Who can help?
Issue Analytics
- State:
- Created a year ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Saving Checkpoints in SavedModel format - Stack Overflow
I am using tensorflow 1.15, I have checkpoints saved from training the model. What I want to do is save the model in...
Read more >Models - Hugging Face
A path to a directory containing model weights saved using ... optional, defaults to False ) — If the model has to be...
Read more >Save and load models | TensorFlow Core
Save and load models ; Setup. Installs and imports; Get an example dataset; Define a model ; Save checkpoints during training. Checkpoint callback ......
Read more >PyTorch-Transformers
Each model is accompanied by their saving/loading methods, either from a local ... Loading from a TF checkpoint file instead of a PyTorch...
Read more >Segformer, can't save checkpoint in saved_model format issue ...
... can't save checkpoint in saved_model format. Thanks for this repo ! Issue description: initalise your segformer model; try to save it. Error:....
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
Hi @joihn - there are some general difficulties when saving Hugging Face models as SavedModel. This is a general issue with any model where the model and layers are implemented by subclassing in Keras - SavedModel doesn’t really have a good way to completely save and load those models (although you can save one or more model traces through SavedModel, this isn’t usually what people want unless they’re trying to export to TFLite or something!)
Instead, we recommend that users save weights only, and if they want to save the entire model, to use the
save_pretrained
method, which will save the weights along with a config that will make it loadable with theload_pretrained
method.Concretely, this means doing the following things:
ModelCheckpoint
, setsave_weights_only
toTrue
.model.save
with eithermodel.save_weights
ormodel.save_pretrained
@joihn Glad to hear you were able to save with
save_pretrained
and thanks for responding so quickly @sayakpaul!I’ll defer the issue to our TF gurus @Rocketknight1 @gante regarding compatibility with keras saving.