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.

how to save the model in jit format(torch.script)

See original GitHub issue

Hi guys, i am having the trained model it was saved by using torch.save() function. i am trying to save the trained model in torch.jit.save () function i cant able to do. The error which i faced is :

Traceback (most recent call last):
  File "train.py", line 504, in <module>
    train()
  File "train.py", line 385, in train
    yolact_net.save_weights(save_path(epoch, iteration))
  File "/home/reorder/SAI_SUBRAMANI/Programs/MAIN/YOLACT/yolact-master/yolact.py", line 483, in save_weights
    torch.jit.save(self.state_dict(), path)
  File "/home/reorder/.local/lib/python3.6/site-packages/torch/jit/__init__.py", line 153, in save
    m.save(f, _extra_files=_extra_files)
AttributeError: 'collections.OrderedDict' object has no attribute 'save'

changes i did , batch size = 1 and cuda = True. as they mentioned that Pytorch DataParallel still doesn’t support JIT Script Modules, i used batch size =1 and the training started to use torch.jit.ScriptModule and torch.jit.script_method.After the training completed it through error when it tries to save

suggest some ideas,struggling more than 3 days.If any way to convert the pretrained model to jit format is its there help me.

System configuration:

ubuntu:18.04 python : python3.6.9 pytorch :1.4.0 cuda_version: 10.2 Driver :440.64.00

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

3reactions
saisubramanicommented, Jun 25, 2020

hi @mchmu

  1. First you need to identify, what is the return datatype of the model returning, if it return tensor means no problem.If its in different datatype try to convert into tensor, because it will leads a error when we trace model using torch script.
  2. then once you changed the output datatype as tensor datatype,you can trace the model using torch_script.trace function by feeding the pretrained model, and save the traced model in torch.jit.save() 3.Now the model is converted into torch_script format. try this steps if you need sample script for tracing, let me know.
1reaction
saisubramanicommented, Jun 23, 2020

hi @mchmu , i founded why this error occurs. This is due to the output tensors returning a dict values. can i know what you are trying to do?. If you have the trained model then you can convert the trained model which was trained in pytorch to torch_script. Its a simple step.

Read more comments on GitHub >

github_iconTop Results From Across the Web

torch.jit.save — PyTorch 1.13 documentation
Save an offline version of this module for use in a separate process. The saved module serializes all of the methods, submodules, parameters,...
Read more >
Using TorchScript to serialize and deploy model
TorchScript is a way to create serializable and optimizable models from PyTorch code. It allows users to saved their models from a Python...
Read more >
How to convert your PyTorch model to TorchScript | djl
There are two ways to convert your model to TorchScript: tracing and scripting. We will only demonstrate the first one, tracing, but you...
Read more >
PyTorch JIT and TorchScript - Towards Data Science
TorchScript saves/loads modules into an archive format. This archive is a standalone representation of the model and can be loaded into an ...
Read more >
Saving PyTorch model with no access to model class code
I think the simplest thing is to use trace = torch.jit.trace(model, typical_input) and then torch.jit.save(trace, path) .
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