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

[Enhance] Enhance error information in build function #1088

Merged
merged 7 commits into from Jul 28, 2023

Conversation

HAOCHENYE
Copy link
Collaborator

@HAOCHENYE HAOCHENYE commented Apr 19, 2023

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

build_from_cfg catches all exception when building the registered object, and raise an unfriendly error. For example, an value error will be raised during building the metric, the error raise by build_from_cfg will like this:

Traceback (most recent call last):
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/build_functions.py", line 122, in build_from_cfg
    obj = obj_cls(**args)  # type: ignore
  File "/home/yehaochen/codebase/mmdetection/mmdet/models/dense_heads/retina_head.py", line 57, in __init__
    super(RetinaHead, self).__init__(
  File "/home/yehaochen/codebase/mmdetection/mmdet/models/dense_heads/anchor_head.py", line 84, in __init__
    self.loss_cls = MODELS.build(loss_cls)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/registry.py", line 548, in build
    return self.build_func(cfg, *args, **kwargs, registry=self)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/build_functions.py", line 250, in build_model_from_cfg
    return build_from_cfg(cfg, registry, default_args)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/build_functions.py", line 144, in build_from_cfg
    raise type(e)(
ValueError: class `FocalLoss` in mmdet/models/losses/focal_loss.py: real error message in FocalLoss

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/build_functions.py", line 122, in build_from_cfg
    obj = obj_cls(**args)  # type: ignore
  File "/home/yehaochen/codebase/mmdetection/mmdet/models/detectors/retinanet.py", line 19, in __init__
    super().__init__(
  File "/home/yehaochen/codebase/mmdetection/mmdet/models/detectors/single_stage.py", line 35, in __init__
    self.bbox_head = MODELS.build(bbox_head)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/registry.py", line 548, in build
    return self.build_func(cfg, *args, **kwargs, registry=self)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/build_functions.py", line 250, in build_model_from_cfg
    return build_from_cfg(cfg, registry, default_args)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/build_functions.py", line 144, in build_from_cfg
    raise type(e)(
ValueError: class `RetinaHead` in mmdet/models/dense_heads/retina_head.py: class `FocalLoss` in mmdet/models/losses/focal_loss.py: real error message in FocalLoss

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/yehaochen/codebase/mmdetection/tools/train.py", line 133, in <module>
    main()
  File "/home/yehaochen/codebase/mmdetection/tools/train.py", line 122, in main
    runner = Runner.from_cfg(cfg)
  File "/home/yehaochen/codebase/mmengine/mmengine/runner/runner.py", line 439, in from_cfg
    runner = cls(
  File "/home/yehaochen/codebase/mmengine/mmengine/runner/runner.py", line 406, in __init__
    self.model = self.build_model(model)
  File "/home/yehaochen/codebase/mmengine/mmengine/runner/runner.py", line 813, in build_model
    model = MODELS.build(model)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/registry.py", line 548, in build
    return self.build_func(cfg, *args, **kwargs, registry=self)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/build_functions.py", line 250, in build_model_from_cfg
    return build_from_cfg(cfg, registry, default_args)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/build_functions.py", line 144, in build_from_cfg
    raise type(e)(
ValueError: class `RetinaNet` in mmdet/models/detectors/retinanet.py: class `RetinaHead` in mmdet/models/dense_heads/retina_head.py: class `FocalLoss` in mmdet/models/losses/focal_loss.py: real error message in FocalLoss

The actual error message may be buried among other messages, and users may need to scroll up to locate which lines raise this error.

This PR remove this unnecessary error catching:

Traceback (most recent call last):
  File "/home/yehaochen/codebase/mmdetection/tools/train.py", line 133, in <module>
    main()
  File "/home/yehaochen/codebase/mmdetection/tools/train.py", line 122, in main
    runner = Runner.from_cfg(cfg)
  File "/home/yehaochen/codebase/mmengine/mmengine/runner/runner.py", line 439, in from_cfg
    runner = cls(
  File "/home/yehaochen/codebase/mmengine/mmengine/runner/runner.py", line 406, in __init__
    self.model = self.build_model(model)
  File "/home/yehaochen/codebase/mmengine/mmengine/runner/runner.py", line 813, in build_model
    model = MODELS.build(model)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/registry.py", line 548, in build
    return self.build_func(cfg, *args, **kwargs, registry=self)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/build_functions.py", line 221, in build_model_from_cfg
    return build_from_cfg(cfg, registry, default_args)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/build_functions.py", line 118, in build_from_cfg
    obj = obj_cls(**args)  # type: ignore
  File "/home/yehaochen/codebase/mmdetection/mmdet/models/detectors/retinanet.py", line 19, in __init__
    super().__init__(
  File "/home/yehaochen/codebase/mmdetection/mmdet/models/detectors/single_stage.py", line 35, in __init__
    self.bbox_head = MODELS.build(bbox_head)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/registry.py", line 548, in build
    return self.build_func(cfg, *args, **kwargs, registry=self)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/build_functions.py", line 221, in build_model_from_cfg
    return build_from_cfg(cfg, registry, default_args)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/build_functions.py", line 118, in build_from_cfg
    obj = obj_cls(**args)  # type: ignore
  File "/home/yehaochen/codebase/mmdetection/mmdet/models/dense_heads/retina_head.py", line 57, in __init__
    super(RetinaHead, self).__init__(
  File "/home/yehaochen/codebase/mmdetection/mmdet/models/dense_heads/anchor_head.py", line 84, in __init__
    self.loss_cls = MODELS.build(loss_cls)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/registry.py", line 548, in build
    return self.build_func(cfg, *args, **kwargs, registry=self)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/build_functions.py", line 221, in build_model_from_cfg
    return build_from_cfg(cfg, registry, default_args)
  File "/home/yehaochen/codebase/mmengine/mmengine/registry/build_functions.py", line 118, in build_from_cfg
    obj = obj_cls(**args)  # type: ignore
  File "/home/yehaochen/codebase/mmdetection/mmdet/models/losses/focal_loss.py", line 190, in __init__
    raise ValueError('real error message in FocalLoss')
ValueError: real error message in FocalLoss

You can simply find that line 190 in focal_loss.py raise this error.

Modification

Please briefly describe what modification is made in this PR.

BC-breaking (Optional)

Does the modification introduce changes that break the backward-compatibility of the downstream repos?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.

Checklist

  1. Pre-commit or other linting tools are used to fix the potential lint issues.
  2. The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  3. If the modification has potential influence on downstream projects, this PR should be tested with downstream projects, like MMDet or MMCls.
  4. The documentation has been modified accordingly, like docstring or example tutorials.

@HAOCHENYE HAOCHENYE added this to the 0.7.3 milestone Apr 21, 2023
@zhouzaida
Copy link
Member

Please provide the error message of the same error after merging into this PR.

@zhouzaida zhouzaida modified the milestones: 0.7.3, 0.7.4 Apr 28, 2023
@HAOCHENYE HAOCHENYE modified the milestones: 0.7.4, 0.7.5 May 31, 2023
@zhouzaida zhouzaida merged commit 237aee3 into open-mmlab:main Jul 28, 2023
15 of 19 checks passed
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

Successfully merging this pull request may close these issues.

None yet

2 participants