Skip to content

Add masked loss support for region-of-interest driven registration#67

Merged
rohitrango merged 1 commit intomaskedlossfrom
copilot/sub-pr-66
Jan 29, 2026
Merged

Add masked loss support for region-of-interest driven registration#67
rohitrango merged 1 commit intomaskedlossfrom
copilot/sub-pr-66

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 29, 2026

Enables registration to be driven only by specific regions of interest by computing loss exclusively where masks are non-zero. Both fixed and moving images accept an optional mask channel.

Implementation

  • Masked loss variants: CC, MI, FusedCC, FusedMI now support masked=True parameter
    • Last channel treated as mask; remaining channels as image data
    • Mask combination modes: "mult" (element-wise product) or "max"
    • Batch-wise MI processing handles variable mask sizes per sample
  • Shared utilities (maskedutils.py): mask extraction, combination, and weighted loss reduction
  • Registration integration: All registration classes (Greedy, Affine, Rigid, SYN, Moments) updated
    • loss_type="masked_cc" or "masked_mse" prefix activates masked mode
    • Gaussian smoothing in multi-resolution pipeline applies only to image channels, preserving mask
  • Helper functions: generate_image_mask_allones() and apply_mask_to_image() for easy mask channel concatenation

Usage

from fireants.io.imagemask import apply_mask_to_image, generate_image_mask_allones
from fireants.registration.greedy import GreedyRegistration

# Append mask as last channel (shape: [N, C+1, ...])
fixed_with_mask = apply_mask_to_image(fixed_img, generate_image_mask_allones(fixed_img))
moving_with_mask = apply_mask_to_image(moving_img, roi_mask)

reg = GreedyRegistration(
    scales=[2, 1],
    iterations=[100, 50],
    fixed_images=BatchedImages([fixed_with_mask]),
    moving_images=BatchedImages([moving_with_mask]),
    loss_type="masked_cc",  # masked variant
    optimizer_lr=0.5
)

Testing

  • 2D and 3D masked registration fidelity tests
  • Synthetic data: axis-aligned vs rotated squares with diagonal half-plane mask
  • Documentation with visual comparisons of masked vs unmasked results

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Add support for masked losses Add masked loss support for region-of-interest driven registration Jan 29, 2026
Copilot AI requested a review from rohitrango January 29, 2026 18:20
@rohitrango rohitrango marked this pull request as ready for review January 29, 2026 18:25
@rohitrango rohitrango merged commit 086dbb0 into maskedloss Jan 29, 2026
1 check passed
@rohitrango rohitrango deleted the copilot/sub-pr-66 branch January 29, 2026 18:25
rohitrango added a commit that referenced this pull request Jan 30, 2026
* made sure all tests still pass

Signed-off-by: rohitrango <rohit.rango@gmail.com>

* added preliminary keypoint support

Signed-off-by: rohitrango <rohit.rango@gmail.com>

* scipy tests

Signed-off-by: rohitrango <rohit.rango@gmail.com>

* fix few bugs here

Signed-off-by: rohitrango <rohit.rango@gmail.com>

* refactor smoothing to account for image and mask

Signed-off-by: rohitrango <rohit.rango@gmail.com>

* masked losses test cases

Signed-off-by: rohitrango <rohit.rango@gmail.com>

* testcases pass

Signed-off-by: rohitrango <rohit.rango@gmail.com>

* add docs

Signed-off-by: rohitrango <rohit.rango@gmail.com>

* Add masked loss support for region-of-interest driven registration (#67)

Initial plan

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>

* ignore debug folders

Signed-off-by: rohitrango <rohit.rango@gmail.com>

* added keypoints docs

Signed-off-by: rohitrango <rohit.rango@gmail.com>

* update copyright

Signed-off-by: rohitrango <rohit.rango@gmail.com>

* docs

Signed-off-by: rohitrango <rohit.rango@gmail.com>

---------

Signed-off-by: rohitrango <rohit.rango@gmail.com>
Signed-off-by: Rohit Jena <rohitrango@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
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.

2 participants