Skip to content

Improve RPN error messages for anchor/head count mismatches - #9599

Open
tonycoder-hub wants to merge 1 commit into
pytorch:mainfrom
tonycoder-hub:fix/issue-1539-anchor-error-message
Open

Improve RPN error messages for anchor/head count mismatches#9599
tonycoder-hub wants to merge 1 commit into
pytorch:mainfrom
tonycoder-hub:fix/issue-1539-anchor-error-message

Conversation

@tonycoder-hub

Copy link
Copy Markdown

Fixes #1539

Summary

When an AnchorGenerator is configured with a different number of anchors per
location than the RPNHead expects (or with a different number of feature
levels), the RPN previously failed with an opaque RuntimeError from
BoxCoder.decode's reshape, or with an internal AssertionError from
AnchorGenerator.grid_anchors. Neither message explained which configuration
had to change.

This change adds an early, per-feature-level validation in
RegionProposalNetwork.forward that runs immediately after the RPN head
produces its objectness and pred_bbox_deltas outputs and before the
anchor generator is invoked. For each level it checks:

  1. The number of feature levels from the head matches the number of levels the
    anchor generator was configured for.
  2. The head's anchors-per-location (read from the objectness channel count)
    matches AnchorGenerator.num_anchors_per_location() for that level.
  3. The bbox-delta tensor has exactly four regression channels per expected
    anchor on that level.

On any mismatch a clear ValueError is raised that names the offending level,
the generator's per-location counts, and the head's expected count, so users
can correct the sizes/aspect_ratios of the anchor generator or the
num_anchors of the RPN head.

Behavior

  • Valid configurations are unaffected: proposal generation, NMS, and loss
    computation are unchanged.
  • Custom RPN heads are supported because head anchor counts are derived from
    the output tensor shapes, not from private head attributes.
  • The previous aggregate total-count check was removed because per-level
    matching strictly implies total matching.

Verification

  • All three focused mismatch regressions passed:
    • test_rpn_anchor_count_mismatch
    • test_rpn_anchor_count_mismatch_per_level_cancellation
    • test_rpn_anchor_level_count_mismatch
  • test/test_models_detection_utils.py passed (14 tests; six pre-existing
    warnings).
  • git diff --check passed.

AI assistance

This change was produced with Claude Code (alwaysday1_max through
super-relay), then independently tested and reviewed before submission.

@pytorch-bot

pytorch-bot Bot commented Aug 12, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9599

Note: Links to docs will display an error until the docs builds have been completed.

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla

meta-cla Bot commented Aug 12, 2026

Copy link
Copy Markdown

Hi @tonycoder-hub!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

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.

Improve error message when number of anchors mismatch

1 participant