SamsungLabs / iterdet

[S+SSPR2020] IterDet: Iterative Scheme for Object Detection in Crowded Environments
https://arxiv.org/abs/2005.05708
Mozilla Public License 2.0
210 stars 39 forks source link

Can we export model to onnx ? #21

Closed hoangkhoiLE closed 4 years ago

hoangkhoiLE commented 4 years ago

Hi,

Thank for your excellent work. I saw that you have a file pytorch2onnx.py in ./tools. But when i used it, i got an error:

File "/mmdet/models/detectors/two_stage.py", line 88, in forward_dummy

x = self.extract_feat(img)

TypeError: extract_feat() missing 1 required positional argument: 'history'

Please help me in this case how to find and add "history".

Best regard

hoangkhoiLE commented 4 years ago

Detail of error:

image

filaPro commented 4 years ago

Hi @hoangkhoiLE,

Unfortunately I'm not familiar with onnx and this script is here as we forked from original mmdetection. In this particular place the problem is that we overwrite forward_train method, but not forward_dummy. Looks like it also need a one line modification to use history argument.

hoangkhoiLE commented 4 years ago

Thank you for your answer. I try to overwrite "forward_dummy" by the way you do with "foward_train". But it is not working, i'm looking forward to your suggestion to modify it. Thank a lot.

Here is how i tried:

def forward_dummy(self, img, history=None):
    """Used for computing network flops.

    See `mmdetection/tools/get_flops.py`
    """
    outs = ()
    # backbone
    x = self.extract_feat(img,history)
    # rpn
    if self.with_rpn:
        rpn_outs = self.rpn_head(x)
        outs = outs + (rpn_outs, )
    proposals = torch.randn(1000, 4).to(img.device)
    # roi_head
    roi_outs = self.roi_head.forward_dummy(x, proposals)
    outs = outs + (roi_outs, )
    return outs

image

Best regard.

filaPro commented 4 years ago

Looks like here input_data should be a tuple ((1, 3, w, h), (1, 1, w, h)). Now it contains only image and the history is None, so you are getting this NoneType exception.

hoangkhoiLE commented 4 years ago

Thank for your response. I will try to fix it.

rh-ia commented 3 years ago

Dear @filaPro @hoangkhoiLE ,

I was able to generate an .onnx model file by overwriting forward_dummy as follows, inspired by your discussion:

def forward_dummy(self, img, history=None):
    """Used for onnx model file.
    See `mmdetection/tools/pytorch2onnx.py`
    """
    if not history:
        height, width = img.shape[2:]
        history = torch.zeros((1, 1, height, width), device=img.device)
    x = self.extract_feat(img, history)
    outs = self.bbox_head(x)
    return outs

Which I wrote in mmdet/models/detectors/iterdet_retinanet.py.

KR