Skip to content

fix(anchors): allow flexible anchor size specification across feature maps (issue #2135) - #9596

Open
jlaportebot wants to merge 1 commit into
pytorch:mainfrom
jlaportebot:fix/anchor-distribution-2135
Open

fix(anchors): allow flexible anchor size specification across feature maps (issue #2135)#9596
jlaportebot wants to merge 1 commit into
pytorch:mainfrom
jlaportebot:fix/anchor-distribution-2135

Conversation

@jlaportebot

Copy link
Copy Markdown

Summary

This PR addresses issue #2135 by allowing users to specify anchor sizes and aspect ratios that apply to ALL feature maps, rather than requiring one sizes tuple per feature map.

Problem

The original AnchorGenerator API required users to specify sizes as a tuple of tuples, where each inner tuple corresponds to a specific feature map:

This was confusing because users expected to specify sizes that apply to all feature maps (as stated in the docs: "sizes[i] and aspect_ratios[i] can have an arbitrary number of elements").

Solution

Added a new flexible API that accepts flat tuples:

The module auto-detects which API is being used and expands flat tuples to all feature maps in the forward pass.

Changes

  1. anchor_utils.py: Modified AnchorGenerator to support both APIs:

    • Detects flat vs nested tuples in
    • Expands single tuples to all feature maps in
    • Maintains full backward compatibility
  2. rpn.py: Updated RPN to handle new anchor format (list of lists per image/feature level)

  3. retinanet.py: Updated RetinaNet to handle new anchor format

  4. fcos.py: Updated FCOS to handle new anchor format

  5. test_anchor_distribution.py: New comprehensive test suite (10 tests)

  6. test_models_detection_anchor_utils.py: Updated existing tests for compatibility

Testing

  • All existing detection tests pass
  • New tests cover: single tuple expansion, backward compatibility, mixed usage, error handling
  • Verified FasterRCNN, RetinaNet, FCOS, SSD all work correctly

Backward Compatibility

Fully backward compatible - existing code using tuple-of-tuples syntax continues to work unchanged.

Closes #2135

… maps (issue pytorch#2135)

- Modified AnchorGenerator to accept flat sizes/aspect_ratios tuples that apply to all feature maps
- New API: AnchorGenerator(sizes=(32, 64, 128), aspect_ratios=(0.5, 1.0, 2.0)) applies all sizes to all feature maps
- Legacy API: AnchorGenerator(sizes=((32,), (64,), (128,)), aspect_ratios=((0.5, 1.0, 2.0),) * 3) unchanged
- Updated RPN, RetinaNet, and FCOS to handle new anchor format (list of lists per image/feature level)
- Added comprehensive tests for new flexible API and backward compatibility

Closes pytorch#2135
@pytorch-bot

pytorch-bot Bot commented Aug 9, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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 added the cla signed label Aug 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anchors not being properly distributed across feature maps?

1 participant