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

Created on 11 Jun 2020  ·  8Comments  ·  Source: dbolya/yolact

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

Most helpful comment

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.

All 8 comments

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.@abhigoku10 @Ma-Dan @dbolya

System configuration:

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

@saisubramani Hi, have you resolved the problem? I'm very curious about how to solve it. I want to call the module with c++.

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.

@saisubramani yeah. I want to convert the trained model to torch_script. Then use c++ to call the model. I try to use torch.jit.trace, but failed. what shall I do? THX!

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.

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.

@saisubramani Thank you very much for your reply!
I've tried to modify it in your way, but I failed. I want to get the result of image segmentation. I check the code of YOLACT.
If I want to get the result of image segmentation, I need the model to return mask, proto, class, score, and proto. But the shape of the mask, proto, class, score, proto are not same. So, how can I put them in a tensor?
I would appreciate it if you could provide sample script for tracing. Thanks again.

@saisubramani
The error info:
Traceback (most recent call last):
File "trace_yolact.py", line 224, in
traced_model = torch.jit.trace(net, batch)
File "/usr/local/lib/python3.6/dist-packages/torch/jit/__init__.py", line 858, in trace
check_tolerance, _force_outplace, _module_class)
File "/usr/local/lib/python3.6/dist-packages/torch/jit/__init__.py", line 1007, in trace_module
check_tolerance, _force_outplace, True, _module_class)
File "/usr/local/lib/python3.6/dist-packages/torch/autograd/grad_mode.py", line 49, in decorate_no_grad
return func(args, *kwargs)
File "/usr/local/lib/python3.6/dist-packages/torch/jit/__init__.py", line 676, in _check_trace
raise TracingCheckError(*diag_info)
torch.jit.TracingCheckError: Tracing failed sanity checks!
ERROR: Graphs differed across invocations!
Graph diff:

@saisubramani can you give me the solution (trace script)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mrtechnoo picture mrtechnoo  ·  4Comments

breznak picture breznak  ·  7Comments

abhigoku10 picture abhigoku10  ·  6Comments

Zhang-O picture Zhang-O  ·  6Comments

davodogster picture davodogster  ·  10Comments