Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pysot model.pth to onnx occurred error #125

Closed
rollben opened this issue Jul 23, 2019 · 30 comments
Closed

pysot model.pth to onnx occurred error #125

rollben opened this issue Jul 23, 2019 · 30 comments

Comments

@rollben
Copy link

rollben commented Jul 23, 2019

torch_out = torch.onnx.export(net,dummy_input,"siamrpn_alex_dwxcorr.pth",export_params=True)
when export siamrpn_alex_dwxcorr.pth to onnx the error is occurred.

error position-> :template = data['template'].cuda()

builtins.IndexError: too many indices for tensor of dimension 4

why this happened and how to solve it

@rollben
Copy link
Author

rollben commented Aug 10, 2019

anybody ever try convert pysot siamrpn pretrained xxxx.pth model format to other counterpart format like xxxx.onnx and is there any successful experience

@lb1100
Copy link
Contributor

lb1100 commented Aug 10, 2019

We override forward, which requires a dict as input. And the dict requires labels. I think you'll need to write a wrapper class which only takes template and search as input, and the predict tensor as outputs (without loss).

@rollben
Copy link
Author

rollben commented Aug 11, 2019

@lb1100 thanks for response, do you mean write a new ModelBuilder class to replace the original one used in training and the rewrite ModelBuilder class only include 'template' and 'search' as input data of dict type for forward and output only give cls and loc predict tensor ? And it is merely for inference ? Also the downloaded pysot pretrained xxxx. pth could adopt this new write ModelBuilder class directly convert to xxxx.onnx or not? I confused why torch.onnx.export would call this override forward but demo.py not. Would I need other extra work if want convert trained xxxx.pth model to xxxx.onnx for inference purpose?
And any gap between my understand and your meaning, Thanks!

@lb1100
Copy link
Contributor

lb1100 commented Aug 11, 2019

During inference, we don't directly call forward (in both demo.py and test.py). You can refer to tracker.py, we call these two functions template and search instead of forward .

@root12321
Copy link

do you solve this problem ?

@root12321
Copy link

@rollben do you solve this problem ?

@full915
Copy link

full915 commented Aug 12, 2019 via email

@rollben
Copy link
Author

rollben commented Aug 13, 2019

@lb1100 In which case the override forward will be called directly .I don't quite make sense why the process of format conversion to onnx would call the override forward. where the input is a img e.g. torch tensor (1,3, 511,511) , and the tensor set as dummy_input for torch.onnx.export(model ,dummy_input, xxxx.onnx) .you know if the override forward is called , the error of course would happen , because the required data of override forward requires a dict not a tuple and so it can not find 'template' , so how to modify it to avoid conflict in inference.

@lb1100
Copy link
Contributor

lb1100 commented Aug 14, 2019

torch.onnx.export will call model.forward to obtain the computation graph to convert model. You should write a wrapper class like this (untested).

class ConvertModel(nn.Module):
    def __init__(self, model):
        
        self.model = model

    def forward(self, template, search):
        zf = self.model.backbone(template)
        xf = self.model.backbone(search)
        cls, loc = self.model.rpn_head(zf, xf)
        return cls, loc

@rollben
Copy link
Author

rollben commented Aug 14, 2019

@lb1100 OK got it ,appreciated.

@root12321
Copy link

@rollben do you want to use onnx-> trt ,do inference?

@rollben
Copy link
Author

rollben commented Aug 21, 2019

Not limit to TRT, there is multiple choices depends on the hardware you wanna the model to populate.

@root12321
Copy link

@rollben any suggestion? i just know tensorrt to accelerate model ,but i have some question, i dont know how to transform torch.view() to trt code and how to transform F.con2d() to trt code.

@rollben
Copy link
Author

rollben commented Aug 28, 2019

I train SiamRPN++ with backbone with resnet50 and Multirpn mode and generate checkpoint_e1.pth and then I use the checkpoint_e7.pth to generate the onnx model through class of ConvertModel and
torch.onnx.export(new_model,dummy_input,"siamrpn_r50_l234_dwxcorr.onnx",export_params=True)
but came across critical errors as follows:

Traceback (most recent call last):
File "/home/superspace/PycharmProjects/Pysot_ONNX/tools/demo_model.py", line 138, in
main()
File "/home/superspace/PycharmProjects/Pysot_ONNX/tools/demo_model.py", line 97, in main
torch.onnx.export(new_model,, dummy_input,"siam_r50_pth_e7_coco.onnx") ## add for debug """graph, params,"""
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/onnx/init.py", line 25, in export
return utils.export(*args, **kwargs)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/onnx/utils.py", line 131, in export
strip_doc_string=strip_doc_string)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/onnx/utils.py", line 363, in _export
_retain_param_name, do_constant_folding)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/onnx/utils.py", line 266, in _model_to_graph
graph, torch_out = _trace_and_get_graph_from_model(model, args, training)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/onnx/utils.py", line 225, in _trace_and_get_graph_from_model
trace, torch_out = torch.jit.get_trace_graph(model, args, _force_outplace=True)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/jit/init.py", line 231, in get_trace_graph
return LegacyTracedModule(f, _force_outplace, return_inputs)(*args, **kwargs)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/nn/modules/module.py", line 493, in call
result = self.forward(*input, **kwargs)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/jit/init.py", line 294, in forward
out = self.inner(*trace_inputs)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/nn/modules/module.py", line 491, in call
result = self._slow_forward(*input, **kwargs)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/nn/modules/module.py", line 481, in _slow_forward
result = self.forward(*input, **kwargs)
File "/home/superspace/PycharmProjects/Pysot_ONNX/pysot/models/model_converter.py", line 40, in forward
cls, loc = self.model.rpn_head(zf, xf)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/nn/modules/module.py", line 491, in call
result = self._slow_forward(*input, **kwargs)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/nn/modules/module.py", line 481, in _slow_forward
result = self.forward(*input, **kwargs)
File "/home/superspace/PycharmProjects/Pysot_ONNX/pysot/models/head/rpn.py", line 111, in forward
c, l = rpn(z_f, x_f)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/nn/modules/module.py", line 491, in call
result = self._slow_forward(*input, **kwargs)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/nn/modules/module.py", line 481, in _slow_forward
result = self.forward(*input, **kwargs)
File "/home/superspace/PycharmProjects/Pysot_ONNX/pysot/models/head/rpn.py", line 90, in forward
cls = self.cls(z_f, x_f)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/nn/modules/module.py", line 491, in call
result = self._slow_forward(*input, **kwargs)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/nn/modules/module.py", line 481, in _slow_forward
result = self.forward(*input, **kwargs)
File "/home/superspace/PycharmProjects/Pysot_ONNX/pysot/models/head/rpn.py", line 76, in forward
kernel = self.conv_kernel(kernel)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/nn/modules/module.py", line 491, in call
result = self._slow_forward(*input, **kwargs)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/nn/modules/module.py", line 481, in _slow_forward
result = self.forward(*input, **kwargs)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/nn/modules/container.py", line 92, in forward
input = module(input)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/nn/modules/module.py", line 491, in call
result = self._slow_forward(*input, **kwargs)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/nn/modules/module.py", line 481, in _slow_forward
result = self.forward(*input, **kwargs)
File "/home/rollben/miniconda3/envs/pysot/lib/python3.7/site-packages/torch/nn/modules/conv.py", line 338, in forward
self.padding, self.dilation, self.groups)

RuntimeError: Given groups=1, weight of size 256 256 3 3, expected input[1, 512, 15, 15] to have 256 channels, but got 512 channels instead

Process finished with exit code 1

I am confused that that why no error happened during train period, but do at forward when convert the trained model of siamrpn_r50_l234_dwxcorr_16gpu of checkpoint_e7.pth to onnx format ?

@lb1100 Do you have any idea about this weird problem?

@lb1100
Copy link
Contributor

lb1100 commented Aug 29, 2019

If you didn't modify my code, it seems that you need to add neck forward after backbone.

@lb1100
Copy link
Contributor

lb1100 commented Aug 29, 2019

@rollben do you want to use onnx-> trt ,do inference?

I think convert to trt will be a little difficult. Correlation layer is not supported. You will have to split the network in to several parts if you want to use trt.

@rollben
Copy link
Author

rollben commented Aug 30, 2019

@lb1100 I solve it and thanks for your reminder.

As for your mention of correlation layer is not yet supported by trt , besides the internal compute structure, I am also curious about whether the following warning would be a barrier of convert onnx to trt model ? Do you have same point of view or just thinks it doesn't matter , it's just a expected ultimate output for onnx model .so what's your opinion?

Warning as follows:(when convert pytorch pth format model to onnx one)
pysot/core/xcorr.py:46: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
out = F.conv2d(x, kernel, groups=batch*channel)

@root12321
Copy link

@lb1100 OK ,thank you

@ghost
Copy link

ghost commented Sep 3, 2019

@lb1100
can you give the code of CAM of channels?
thanks

@root12321
Copy link

why alexnet model size is not same to alexnetlegacy model size ,i saw the model structure both same ,why the model size is not same ?
@lb1100

@LiyaoTang
Copy link

My experience: you should script the xcorr part and use the scripted model before tracing it to avoid warning.

@ZhiyuanChen
Copy link
Collaborator

Uploading image.png…
Since no further question is asked, I'm closing it for now.
Please reopen it or create a new issue if you have further question.

@shupinghu
Copy link

hi, did you transform pysot model.pth to onnx successfully? I have meet the same problem with you, could you please give me some help? email: [email protected]

@shupinghu
Copy link

/pysot/pysot/core/xcorr.py:46: TracerWarning: Converting a tensor to a Python integer might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
out = F.conv2d(x, kernel, groups=batch*channel)
does this warning matter?

@shupinghu
Copy link

My code:
import torch
import torchvision.models as models
import torch.nn as nn
import argparse
import torchvision
import torch.onnx

from collections import OrderedDict
from pysot.core.config import cfg
from pysot.models.model_builder import ModelBuilder

parser = argparse.ArgumentParser(description='trans demo')
parser.add_argument('--config', type=str, help='config file')
parser.add_argument('--snapshot', type=str, help='model name')
args = parser.parse_args()

cfg.merge_from_file(args.config)
cfg.CUDA = torch.cuda.is_available() and cfg.CUDA
device = torch.device('cuda' if cfg.CUDA else 'cpu')

class ConvertModel(nn.Module):
def init(self, model0):
super(ConvertModel, self).init()
self.model = model0

def forward(self, template, search):
    zf = self.model.backbone(template)
    xf = self.model.backbone(search)
    cls, loc = self.model.rpn_head(zf, xf)
    return cls, loc

载入预训练的型

model0 = ModelBuilder()
model0.load_state_dict(torch.load(args.snapshot, map_location=lambda storage, loc: storage.cpu()))

model0.eval()
print(model0)
model = ConvertModel(model0)

x = torch.randn(1, 3, 127, 127)
z = torch.randn(1, 3, 287, 287)

torch_out = torch.onnx._export(model, (x,z), "model.onnx", export_params=True)

@zeleniyslonik
Copy link

zeleniyslonik commented May 23, 2020

Hello,
Has anyone tried to convert SiamMask to ONNX?
I am trying to achieve this using shupinghu's code with minor editing, but I end up with the following error:
RuntimeError: Unsupported: ONNX export of Slice with dynamic inputs. DynamicSlice is a deprecated experimental op. Please use statically allocated variables or export to a higher opset version.

Here is the code.

import torch
import torchvision.models as models
import torch.nn as nn
import argparse
import torchvision
import torch.onnx

from collections import OrderedDict
from pysot.core.config import cfg
from pysot.models.model_builder import ModelBuilder

parser = argparse.ArgumentParser(description='trans demo')
parser.add_argument('--config', type=str, help='config file')
parser.add_argument('--snapshot', type=str, help='model name')
args = parser.parse_args()

cfg.merge_from_file(args.config)
cfg.CUDA = torch.cuda.is_available() and cfg.CUDA
device = torch.device('cuda' if cfg.CUDA else 'cpu')


class ConvertModel(nn.Module):
    def __init__(self, model):
        super(ConvertModel, self).__init__()
        self.model = model

    def forward(self, template, search):
        zf = self.model.backbone(template)
        xf = self.model.backbone(search)

        if cfg.MASK.MASK:
            zf = zf[-1]
            self.model.xf_refine = xf[:-1]
            xf = xf[-1]
        if cfg.ADJUST.ADJUST:
            zf = self.model.neck(zf)
            xf = self.model.neck(xf)
        mask, self.mask_corr_feature = self.model.mask_head(zf, xf)
        cls, loc = self.model.rpn_head(zf, xf)

        return cls, loc, mask

model0 = ModelBuilder()
model0.load_state_dict(torch.load(args.snapshot, map_location=lambda storage, loc: storage.cpu()))

model0.eval()
# print(model0)
model = ConvertModel(model0)

x = torch.randn(1, 3, 127, 127)
z = torch.randn(1, 3, 255, 255)

torch_out = torch.onnx._export(model, (x,z), "onnx-ized.onnx", export_params=True)

Can anyone help me with that?

@ZhiyuanChen
Copy link
Collaborator

#368

@ZhiyuanChen ZhiyuanChen reopened this May 29, 2020
@GOBish
Copy link

GOBish commented Jun 25, 2020

I have been following this thread, has anyone been able to successfully convert the model to tensorrt or onnx?

@rollben did you get it to work, especially interested if you were able to convert tot trt.

thanks

@blacklong28
Copy link

change this, add opset_version = 11 ,maybe work
torch_out = torch.onnx._export(model, (x,z), "onnx-ized.onnx", export_params=True,opset_version = 11)

@mhm0902
Copy link

mhm0902 commented Jan 27, 2021

pysot/pysot/models/neck/neck.py:25: TracerWarning: Converting a tensor to a Python index might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
x = x[:, :, l:r, l:r]
to onnx warnning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests