question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Segformer, can't save checkpoint in saved_model format

See original GitHub issue

Thanks for this repo !

Issue description:

  1. initalise your segformer model
  2. 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?

@sayakpaul @NielsRogge

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
Rocketknight1commented, Aug 15, 2022

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 the load_pretrained method.

Concretely, this means doing the following things:

  1. When using ModelCheckpoint, set save_weights_only to True.
  2. Replace model.save with either model.save_weights or model.save_pretrained
1reaction
amyerobertscommented, Aug 11, 2022

@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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found