TexasInstruments / edgeai-mmdetection

This repository has been moved. The new location is in https://github.com/TexasInstruments/edgeai-tensorlab
https://github.com/TexasInstruments/edgeai
Other
0 stars 0 forks source link

run_detection_export.sh, ImportError: cannot import name 'build_model_from_cfg' from 'xmmdet.core' #1

Closed fiefdx closed 3 years ago

fiefdx commented 3 years ago

PRIME-Z390-A:~/work/edgeai-mmdetection-master$ ./run_detection_export.sh /home/haitao/work/test_venv/lib/python3.8/site-packages/mmcv/utils/registry.py:251: UserWarning: The old API of register_module(module, force=False) is deprecated and will be removed, please use the new API register_module(name=None, force=False, module=None) instead. warnings.warn( Traceback (most recent call last): File "./scripts/export_pytorch2onnx.py", line 35, in from xmmdet.tools import pytorch2onnx File "/home/haitao/work/edgeai-mmdetection-master/xmmdet/tools/pytorch2onnx.py", line 11, in from xmmdet.core import (build_model_from_cfg, generate_inputs_and_wrap_model, ImportError: cannot import name 'build_model_from_cfg' from 'xmmdet.core' (/home/haitao/work/edgeai-mmdetection-master/xmmdet/core/init.py) Done.

mathmanu commented 3 years ago

This is may be due to a mismatch in the version of mmdet installed and our code.

The setup.sh refers to 2.16.0: https://github.com/TexasInstruments/edgeai-mmdetection/blob/master/setup.sh

We have a modification of pytorch2onnx.py in our repo: https://github.com/TexasInstruments/edgeai-mmdetection/blob/master/xmmdet/tools/pytorch2onnx.py#L11

But it is not matching with pytorch2onnx.py in v2.16.0 or mmdetection: https://github.com/open-mmlab/mmdetection/blob/v2.16.0/tools/deployment/pytorch2onnx.py#L12

This difference might have happened when upgrading edgeai-mmdetection to support the new version v2.16.0 of mmdetection.

Can you modify the import line in pytorch2onnx.py as per pytorch2onnx.py in mmdetection v2.16.0 and let me know if it works. Please post the code changes as well once you make it work. If you face difficulty in making it work, let us know.

Thank you.

fiefdx commented 3 years ago

there is no build_model_from_cfg, generate_inputs_and_wrap_model in xmmdet.core, but, the mmdet has them, I am not sure if xmmdet should have the modified version of these modules or just the import code is wrong.

mathmanu commented 3 years ago

I have done the changes and pushed a commit. Please pull and try now. Let me know how it goes.

fiefdx commented 3 years ago

@mathmanu thanks, and, I try ssd-lite_mobilenet_fpn example run export script, get another errors:

$ . ./run_detection_export.sh 
/home/haitao/work/test_venv/lib/python3.8/site-packages/mmcv/utils/registry.py:251: UserWarning: The old API of register_module(module, force=False) is deprecated and will be removed, please use the new API register_module(name=None, force=False, module=None) instead.
  warnings.warn(
Exporting onnx+proto model for: ./configs/ssd/ssd-lite_mobilenet_fpn.py @ 2021-11-01_10-52-53 with input_size (512, 512)
/home/haitao/work/edgeai-mmdetection-master/xmmdet/tools/pytorch2onnx.py:310: UserWarning: Arguments like `--mean`, `--std`, `--dataset` would be         parsed directly from config file and are deprecated and         will be removed in future releases.
  warnings.warn('Arguments like `--mean`, `--std`, `--dataset` would be \
/home/haitao/work/test_venv/lib/python3.8/site-packages/mmdet-2.17.0-py3.8.egg/mmdet/models/builder.py:51: UserWarning: train_cfg and test_cfg is deprecated, please specify them in model
  warnings.warn(
Traceback (most recent call last):
  File "/home/haitao/work/test_venv/lib/python3.8/site-packages/mmcv/utils/registry.py", line 52, in build_from_cfg
    return obj_cls(**args)
  File "/home/haitao/work/edgeai-mmdetection-master/xmmdet/models/backbones/mobilenetv2.py", line 265, in __init__
    super().__init__(InvertedResidual, model_config)
  File "/home/haitao/work/edgeai-mmdetection-master/xmmdet/models/backbones/mobilenetv2.py", line 172, in __init__
    super().__init__(init_cfg)
NameError: name 'init_cfg' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/haitao/work/test_venv/lib/python3.8/site-packages/mmcv/utils/registry.py", line 52, in build_from_cfg
    return obj_cls(**args)
  File "/home/haitao/work/test_venv/lib/python3.8/site-packages/mmdet-2.17.0-py3.8.egg/mmdet/models/detectors/single_stage.py", line 32, in __init__
    self.backbone = build_backbone(backbone)
  File "/home/haitao/work/test_venv/lib/python3.8/site-packages/mmdet-2.17.0-py3.8.egg/mmdet/models/builder.py", line 20, in build_backbone
    return BACKBONES.build(cfg)
  File "/home/haitao/work/test_venv/lib/python3.8/site-packages/mmcv/utils/registry.py", line 212, in build
    return self.build_func(*args, **kwargs, registry=self)
  File "/home/haitao/work/test_venv/lib/python3.8/site-packages/mmcv/cnn/builder.py", line 27, in build_model_from_cfg
    return build_from_cfg(cfg, registry, default_args)
  File "/home/haitao/work/test_venv/lib/python3.8/site-packages/mmcv/utils/registry.py", line 55, in build_from_cfg
    raise type(e)(f'{obj_cls.__name__}: {e}')
NameError: MobileNetV2Lite: name 'init_cfg' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./scripts/export_pytorch2onnx.py", line 56, in <module>
    pytorch2onnx.main(args)
  File "/home/haitao/work/edgeai-mmdetection-master/xmmdet/tools/pytorch2onnx.py", line 337, in main
    model = build_model_from_cfg(args.config, args.checkpoint,
  File "/home/haitao/work/test_venv/lib/python3.8/site-packages/mmdet-2.17.0-py3.8.egg/mmdet/core/export/pytorch2onnx.py", line 93, in build_model_from_cfg
    model = build_detector(cfg.model, test_cfg=cfg.get('test_cfg'))
  File "/home/haitao/work/test_venv/lib/python3.8/site-packages/mmdet-2.17.0-py3.8.egg/mmdet/models/builder.py", line 58, in build_detector
    return DETECTORS.build(
  File "/home/haitao/work/test_venv/lib/python3.8/site-packages/mmcv/utils/registry.py", line 212, in build
    return self.build_func(*args, **kwargs, registry=self)
  File "/home/haitao/work/test_venv/lib/python3.8/site-packages/mmcv/cnn/builder.py", line 27, in build_model_from_cfg
    return build_from_cfg(cfg, registry, default_args)
  File "/home/haitao/work/test_venv/lib/python3.8/site-packages/mmcv/utils/registry.py", line 55, in build_from_cfg
    raise type(e)(f'{obj_cls.__name__}: {e}')
NameError: SingleStageDetector: MobileNetV2Lite: name 'init_cfg' is not defined
Done.
fiefdx commented 3 years ago

/home/haitao/work/edgeai-mmdetection-master/xmmdet/models/backbones/mobilenetv2.py", line 172

class MobileNetV2LiteBase(torch.nn.Module):
    def __init__(self, BlockBuilder, model_config):
        """
        MobileNet V2 main class

        Args:
            num_classes (int): Number of classes
            width_mult (float): Width multiplier - adjusts number of channels in each layer by this amount
            inverted_residual_setting: Network structure
            round_nearest (int): Round the number of channels in each layer to be a multiple of this number
            Set to 1 to turn off rounding
        """
        super().__init__(init_cfg) # the init_cfg is the error

        self.model_config = model_config
        self.num_classes = self.model_config.num_classes

        assert not (init_cfg and pretrained), \
            'init_cfg and pretrained cannot be setting at the same time'
        if isinstance(pretrained, str):
            warnings.warn('DeprecationWarning: pretrained is deprecated, '
                          'please use "init_cfg" instead')
            self.init_cfg = dict(type='Pretrained', checkpoint=pretrained)
mathmanu commented 3 years ago

The upgrading to v2.16.0 was not complete and that was the reason for this issue as well.

I have now pushed the changes and it should work now - I have tried movilenetv2 and and regnet SSDmodels. Have not tried other models - if you face issues in other models let us know.

fiefdx commented 3 years ago

thanks