Skip to content

7951 anchors are not centered on grid cells#8475

Open
hachoj wants to merge 4 commits intoProject-MONAI:devfrom
hachoj:7951-anchors-are-not-centered-on-grid-cells
Open

7951 anchors are not centered on grid cells#8475
hachoj wants to merge 4 commits intoProject-MONAI:devfrom
hachoj:7951-anchors-are-not-centered-on-grid-cells

Conversation

@hachoj
Copy link
Copy Markdown

@hachoj hachoj commented Jun 9, 2025

This pull request resolves issue #7951 by correctly centering anchor boxes within their grid cells.

Description

Changes Made:

  • Modified the anchor generation logic in monai/apps/detection/utils/anchor_utils.py to add a stride // 2 offset.
  • Refactored the corresponding unit test in tests/apps/detection/utils/test_anchor_box.py to validate this new, correct behavior against the torchvision baseline by accounting for the offset.

Fixes #7951

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • Breaking change (fix or new feature that would cause existing functionality to change).
  • New tests added to cover the changes.
  • Integration tests passed locally by running ./runtests.sh -f -u --net --coverage.
  • Quick tests passed locally by running ./runtests.sh --quick --unittests --disttests.
  • In-line docstrings updated.
  • Documentation updated, tested make html command in the docs/ folder.
  • [ ]

hachoj added 3 commits June 8, 2025 17:49
Signed-off-by: hachoj <hjchojnowski@gmail.com>
Signed-off-by: hachoj <hjchojnowski@gmail.com>
Signed-off-by: hachoj <hjchojnowski@gmail.com>
@hachoj hachoj marked this pull request as ready for review June 9, 2025 23:26
@ericspod
Copy link
Copy Markdown
Member

Hi @hachoj thanks for the contribution, it looks fine to me as far as I can tell. Could you please check that this works with the luna16 tutorial example? If so I think we're good. @Can-Zhao I think you were the original author of this file, any thoughts?

@Cado87
Copy link
Copy Markdown
Contributor

Cado87 commented Apr 23, 2026

Hi @ericspod

I ran a smoke test of the LUNA16 detection tutorial pipeline with this patch applied (MONAI commit 8f6e2c7, PyTorch
2.10.0, Tesla T4 GPU).

What was tested:

  • Verified the anchor fix is active: with a 2×2×2 grid and stride=4, the first anchor center is now at (2.0, 2.0,
    2.0) instead of (0.0, 0.0, 0.0)
  • Ran luna16_training.py (fold 0, 3 epochs, synthetic volumes matching the fold-0 datasplit paths) — training
    completed without errors, loss was finite and decreasing
  • Ran luna16_testing.py on the saved checkpoint — inference processed 67 images, produced 94 predicted boxes with
    scores in a valid range

The full pipeline (data loading → ATSS matching → training → sliding window inference → box post-processing) runs
without issues with the centered anchors.

Note: since real LUNA16 data was not used, detection quality metrics are not meaningful here. The test only
validates that the fix does not break the tutorial pipeline. With real data, a full training run would be needed to
confirm the expected improvement in detection accuracy from properly centered anchors.

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.

Anchors are not centered on grid cells

3 participants