Skip to content

Conversation

@VijayVignesh1
Copy link

@VijayVignesh1 VijayVignesh1 commented Sep 17, 2025

What does this PR do?

Fixes #2747

Before submitting
  • Was this discussed/agreed via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure to update the docs?
  • Did you write any new necessary tests?
PR review Added ignore_index to segmentation mean_iou class and function. Users can now add an index in the target which can be ignored (like 255). Added testcases to verify the same. Note: This is only for the case where input_format = "index"

Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.

Did you have fun?

Make sure you had fun coding 🙃


📚 Documentation preview 📚: https://torchmetrics--3266.org.readthedocs.build/en/3266/

@VijayVignesh1 VijayVignesh1 marked this pull request as ready for review September 17, 2025 15:11
@Borda Borda changed the title Adding ignore_index to segmentation mean_iou Adding ignore_index to segmentation mean_iou Sep 17, 2025
@codecov
Copy link

codecov bot commented Sep 17, 2025

Codecov Report

❌ Patch coverage is 0% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 32%. Comparing base (3eb05bf) to head (59744c1).

❌ Your project check has failed because the head coverage (32%) is below the target coverage (95%). You can increase the head coverage or adjust the target coverage.

❗ There is a different number of reports uploaded between BASE (3eb05bf) and HEAD (59744c1). Click for more details.

HEAD has 880 uploads less than BASE
Flag BASE (3eb05bf) HEAD (59744c1)
torch2.0.1+cpu 41 3
python3.10 166 12
Windows 27 2
cpu 236 17
torch2.8.0 14 1
macOS 41 3
python3.12 56 4
torch2.8.0+cpu 42 3
torch2.0.1 27 2
Linux 168 12
torch2.7.1+cpu 28 2
gpu 2 0
unittest 2 0
torch2.2.2+cpu 14 1
torch2.6.0+cpu 14 1
torch2.4.1+cpu 14 1
torch2.1.2+cpu 14 1
torch2.3.1+cpu 14 1
torch2.5.1+cpu 14 1
python3.9 14 1
Additional details and impacted files
@@           Coverage Diff            @@
##           master   #3266     +/-   ##
========================================
- Coverage      37%     32%     -5%     
========================================
  Files         364     349     -15     
  Lines       20096   19905    -191     
========================================
- Hits         7520    6376   -1144     
- Misses      12576   13529    +953     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mergify mergify bot added the ready label Sep 17, 2025
@Borda Borda requested a review from Copilot September 19, 2025 19:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for an ignore_index parameter to the segmentation mean_iou metric, allowing users to specify a class index (such as 255) that should be excluded from both intersection and union computations. This feature is only available when input_format="index".

  • Added ignore_index parameter to both the MeanIoU class and mean_iou functional implementation
  • Updated the internal _mean_iou_update function to filter out ignored indices before processing
  • Added comprehensive test coverage for the new functionality

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
src/torchmetrics/segmentation/mean_iou.py Added ignore_index parameter to MeanIoU class constructor and passed it to update function
src/torchmetrics/functional/segmentation/mean_iou.py Added ignore_index parameter to mean_iou function and _mean_iou_update with filtering logic
tests/unittests/segmentation/test_mean_iou.py Enhanced test suite with ignore_index parameter coverage and added specific test case
CHANGELOG.md Added changelog entry for the new feature

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Co-authored-by: Copilot <[email protected]>
) -> tuple[Tensor, Tensor]:
"""Update the intersection and union counts for the mean IoU computation."""
if ignore_index is not None and input_format == "index":
idx = target == ignore_index
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we support that also in onehot by basically just removing the row in that dimension?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could do that. However, in most cases the ignore_index value is 255 which will blow up the one-hot tensor, right? So, for one-hot tensors can we assume that the user would handle the masking?

"""Update the intersection and union counts for the mean IoU computation."""
if ignore_index is not None and input_format == "index":
idx = target == ignore_index
target, preds = target[~idx], preds[~idx]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's directly compute idx = target != ignore_index. Otherwise ~idx will create an additional intermediate tensor leading to additional memory allocation

@mergify mergify bot requested a review from a team October 16, 2025 10:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segmentation IOU compute Ignore some tagged values that don't need to be recorded (such as 255)

3 participants