[Fix]: Resolve Error Exporting RetinaNet for Single Class Case with CrossEntropyLoss in MMDeploy #2827
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 anIndexError
during export.Motivation
The root cause of the issue was the repeated slicing (
[:, :, :-1]
) of the scores tensor—first onscores
and then again during the parsing ofmax_scores
fromnms_pre_score
. This inadvertently excluded the last class index for single-stage detectors, causing anIndexError
during thetorch.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:
scores[:, :, :-1]
) in thebase_dense_head__predict_by_feat
function (Line 116).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
/cc @grimoire