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

[Fix]: Resolve Error Exporting RetinaNet for Single Class Case with CrossEntropyLoss in MMDeploy #2827

Merged

Conversation

chuzhdontcode
Copy link
Contributor

Fixes #2787

This pull request addresses an issue encountered when exporting single-stage detectors (e.g., RetinaNet and detectors inheriting SingleStageDetector) from MMDetection using MMDeploy to ONNX format. The problem stems from improper handling of softmax scores, resulting in an IndexError during export.

Motivation

The root cause of the issue was the repeated slicing ([:, :, :-1]) of the scores tensor—first on scores and then again during the parsing of max_scores from nms_pre_score. This inadvertently excluded the last class index for single-stage detectors, causing an IndexError during the torch.max operation for single-class models because the scores tensor became empty. In a multi-class setting, this also excluded the last-index class from the softmax computation.

The goal of this PR is to correct the handling of softmax scores for both single-class and multi-class configurations during export. More details can be found in issue #2787.

Modification

The following changes have been made:

  • Removed the unnecessary slicing of background class after the softmax operation (scores[:, :, :-1]) in the base_dense_head__predict_by_feat function (Line 116).
  • The slicing is now only applied to the nms_pre_score, ensuring that the background class is handled correctly without causing export errors.

This fix ensures compatibility with single-class configurations for models like RetinaNet when exporting to ONNX.

BC-breaking (Optional)

This modification does not introduce any backward compatibility issues. The change is limited to the export behavior and does not affect the functionality of multi-class models.

Use cases (Optional)

This PR resolves a bug related to exporting single-stage models like RetinaNet to ONNX format using MMDeploy. No new features are introduced.

Checklist

  1. Pre-commit or other linting tools have been used to fix any potential lint issues.
  2. The modification has been covered by unit tests to ensure correctness in both single-class and multi-class scenarios.
  3. The PR has been tested with all supported versions of downstream projects, including MMDetection and MMDeploy, ensuring no regressions in multi-class configurations.
  4. Documentation and example tutorials have been updated to reflect the correct behavior of the export function.

/cc @grimoire

…for single-class models

The softmax function previously sliced out the background class, which
caused an IndexError in single-class model export. This update removes
the slicing of background class scores, fixing the issue when exporting
RetinaNet with single-class configurations.

- Updated `base_dense_head__predict_by_feat` to prevent IndexError when
handling single-class outputs.
- This change is particularly relevant for RetinaNet model exports.
@CLAassistant
Copy link

CLAassistant commented Sep 28, 2024

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@grimoire grimoire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@grimoire grimoire merged commit 3f8604b into open-mmlab:main Sep 30, 2024
21 of 26 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.

[Bug] Error Exporting RetinaNet for Single Class Case with CrossEntropyLoss in MMDeploy
3 participants