inisis / brocolli

Everything in Torch Fx
MIT License
341 stars 63 forks source link

KeyError: 'onnx::Range' while converting YOLOX-Darknet53 model. #41

Closed kv55 closed 2 years ago

kv55 commented 2 years ago

I was trying to convertYOLOx-Darknet53 and got the KeyError: 'onnx::Range'. I tried with the latest docker image. Doesn't it support conversion of YOLOX- Darknet53?

inisis commented 2 years ago

I was trying to convertYOLOx-Darknet53 and got the KeyError: 'onnx::Range'. I tried with the latest docker image. Doesn't it support conversion of YOLOX- Darknet53?

Maybe it's the same problem with #39, you can provide YOLOx-Darknet53 link here, I'll try it ASAP

kv55 commented 2 years ago

I tried with the latest docker image " yaphets4desmond/brocolli:v2.0" and got the same error. Here is the link to YOLOx-Darknet53 model, https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_darknet.pth

inisis commented 2 years ago

I tried with the latest docker image " yaphets4desmond/brocolli:v2.0" and got the same error. Here is the link to YOLOx-Darknet53 model, https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_darknet.pth

Hi, I tested the code, brocolli currently doesn't support Focus op, so you cannot convert yolox to caffe by using brocolli, it will be fixed soon.

inisis commented 2 years ago

I tried with the latest docker image " yaphets4desmond/brocolli:v2.0" and got the same error. Here is the link to YOLOx-Darknet53 model, https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_darknet.pth

If you want to convert yolox-darknet53, which is yolov3, you can set model.head.decode_in_inference = False, then it will be all fine.

inisis commented 2 years ago

Hi, you can refer to this https://github.com/open-mmlab/mmdeploy/blob/f0c110e6e60454e82bf47c184d6d61b7846f6c58/mmdeploy/codebase/mmdet/models/backbones.py#L10 and rewrite focus forward function, and other types of YoloX model can be converted.

kv55 commented 2 years ago

I tried with the latest docker image " yaphets4desmond/brocolli:v2.0" and got the same error. Here is the link to YOLOx-Darknet53 model, https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_darknet.pth

If you want to convert yolox-darknet53, which is yolov3, you can set model.head.decode_in_inference = False, then it will be all fine.

I tried with "decode_in_inference = False", but got the same error: File "/root/brocolli/converter/pytorch/pytorch_caffe_parser.py", line 134, in gen_IR node_type = layer_map[onnx_node_type] KeyError: 'onnx::Range' I tried with the latest docker image yaphets4desmond/brocolli:v1.0.

inisis commented 2 years ago

I tried with the latest docker image " yaphets4desmond/brocolli:v2.0" and got the same error. Here is the link to YOLOx-Darknet53 model, https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_darknet.pth

If you want to convert yolox-darknet53, which is yolov3, you can set model.head.decode_in_inference = False, then it will be all fine.

I tried with "decode_in_inference = False", but got the same error: File "/root/brocolli/converter/pytorch/pytorch_caffe_parser.py", line 134, in gen_IR node_type = layer_map[onnx_node_type] KeyError: 'onnx::Range' I tried with the latest docker image yaphets4desmond/brocolli:v1.0.

  1. you have to clone your yolox under custom_models folder
  2. add pythonpath export PYTHONPATH=/root/brocolli/custom_models/YOLOX/:$PYTHONPATH
  3. rewrite Focus forward function in /root/brocolli/custom_models/YOLOX/yolox/models/network_blocks.py#95

    def forward(self, x):
        # shape of x (b,c,w,h) -> y(b,4c,w/2,h/2)
        # patch_top_left = x[..., ::2, ::2]
        # patch_top_right = x[..., ::2, 1::2]
        # patch_bot_left = x[..., 1::2, ::2]
        # patch_bot_right = x[..., 1::2, 1::2]
        # x = torch.cat(
        #     (
        #         patch_top_left,
        #         patch_bot_left,
        #         patch_top_right,
        #         patch_bot_right,
        #     ),
        #     dim=1,
        # )
    
        batch_size, c, h, w = x.shape
        assert h % 2 == 0 and w % 2 == 0, f'focus for yolox needs even feature\
            height and width, got {(h, w)}.'
    
        x = x.reshape(batch_size, c * h, 1, w)
        _b, _c, _h, _w = x.shape
        g = _c // 2
        # fuse to ncnn's shufflechannel
        x = x.view(_b, g, 2, _h, _w)
        x = torch.transpose(x, 1, 2).contiguous()
        x = x.view(_b, -1, _h, _w)
    
        x = x.reshape(_b, c * h * w, 1, 1)
    
        _b, _c, _h, _w = x.shape
        g = _c // 2
        # fuse to ncnn's shufflechannel
        x = x.view(_b, g, 2, _h, _w)
        x = torch.transpose(x, 1, 2).contiguous()
        x = x.view(_b, -1, _h, _w)
    
        x = x.reshape(_b, c * 4, h // 2, w // 2)
    
        return self.conv(x)
  4. write test.py like this
    
    from bin.pytorch2caffe import Runner

from yolox.exp import get_exp from yolox.models.network_blocks import SiLU from yolox.utils import replace_module

exp = get_exp("custom_models/YOLOX/exps/default/yolox_tiny.py", "yolox_tiny") model = exp.get_model() model.eval()

model = replace_module(model, nn.SiLU, SiLU) model.head.decode_in_inference = False

runner = Runner("yolox", model, [1, 3, exp.test_size[0], exp.test_size[1]], 13, True) runner.pyotrch_inference()
runner.convert() runner.caffe_inference() runner.check_result()


5. python test.py and it will be all fine.
kv55 commented 2 years ago

It worked . Thanks a lot for your support.