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.

Can't save a full model using torch.save (at least with faster-RCNN)

See original GitHub issue

It is not possible to save a full model using default settings of torch.save (see stack trace below). This is because of the implementation of remove_internal_model_transforms, which uses inner functions in its implementation. The default pickle module does not support inner functions.

Workaround: use the dill module instead, which does support inner functions.

Suggested fix: It does not look as if the internal functions are necessary. If there were moved to standard functions, then the default pickle module should work. torch.save(model, 'mod.pth', pickle_module=pickle) causes an error.

torch.save(model, 'mod.pth', pickle_module=dill) is a workaround.

To Reproduce

torch.save(model, 'mod1-full.pth', pickle_module=pickle) results in:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-50f3761f4f3c> in <module>
----> 1 torch.save(model, 'mod1-full.pth', pickle_module=pickle)

~/anaconda3/envs/dlm/lib/python3.8/site-packages/torch/serialization.py in save(obj, f, pickle_module, pickle_protocol, _use_new_zipfile_serialization)
    370         if _use_new_zipfile_serialization:
    371             with _open_zipfile_writer(opened_file) as opened_zipfile:
--> 372                 _save(obj, opened_zipfile, pickle_module, pickle_protocol)
    373                 return
    374         _legacy_save(obj, opened_file, pickle_module, pickle_protocol)

~/anaconda3/envs/dlm/lib/python3.8/site-packages/torch/serialization.py in _save(obj, zip_file, pickle_module, pickle_protocol)
    474     pickler = pickle_module.Pickler(data_buf, protocol=pickle_protocol)
    475     pickler.persistent_id = persistent_id
--> 476     pickler.dump(obj)
    477     data_value = data_buf.getvalue()
    478     zip_file.write_record('data.pkl', data_value, len(data_value))

AttributeError: Can't pickle local object 'remove_internal_model_transforms.<locals>.noop_normalize'

Relevant definition:

def remove_internal_model_transforms(model: GeneralizedRCNN):
    def noop_normalize(image: Tensor) -> Tensor:
        return image

    def noop_resize(
        image: Tensor, target: Optional[Dict[str, Tensor]]
    ) -> Tuple[Tensor, Optional[Dict[str, Tensor]]]:
        return image, target

    model.transform.normalize = noop_normalize
    model.transform.resize = noop_resize

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
tfriedelcommented, Jun 22, 2021

I just ran into this issue in icevision 0.8.0. While actually saving worked, loading didn’t. I was using model_type = models.mmdet.retinanet backbone = model_type.backbones.resnet50_fpn_1x

The workaround with dill worked for me.

0reactions
FraPochetticommented, Dec 18, 2021

We put together a new inference API. This works now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Saving and Loading Models - PyTorch
This save/load process uses the most intuitive syntax and involves the least amount of code. Saving a model in this way will save...
Read more >
How do I save a trained model in PyTorch? - Stack Overflow
There are two main approaches for serializing and restoring a model. The first (recommended) saves and loads only the model parameters: torch.
Read more >
Saving and Loading the Best Model in PyTorch - DebuggerCafe
Learn about saving and loading the best model in PyTorch and how running tests on the best model gives better deep learning results....
Read more >
How to Save and Load Models in PyTorch - Wandb
The easiest way to save the entire model with the least amount of code. The saving and loading API is more intuitive.
Read more >
How shouild I save model weights in Pytorch?
PyTorch has a state_dict which stores the state of the model (in this case, the neural network) at any point in time. Saving...
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