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
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 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.

@pytest.mark.parametrize("include_background", [True, False])
class TestMeanIoU(MetricTester):
"""Test class for `MeanIoU` metric."""

Copy link

Copilot AI Sep 19, 2025

Choose a reason for hiding this comment

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

The tolerance has been relaxed from 1e-4 to 1e-2 without explanation. This change could mask precision issues and should either be reverted or documented with a comment explaining why the relaxed tolerance is necessary.

Suggested change
# The tolerance has been relaxed from 1e-4 to 1e-2 due to minor numerical differences
# between the reference and implementation, likely caused by floating point precision
# or differences in third-party library computations (e.g., MONAI, PyTorch).

Copilot uses AI. Check for mistakes.

Copy link
Author

Choose a reason for hiding this comment

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

No additional comment needed here since the rationale is self-understood.

Comment on lines +235 to +238
expected_ious = [0.6667, 0.3333, 0.0]
for idx, (iou, iou_func) in enumerate(zip(miou_per_class, miou_func)):
assert torch.allclose(iou, iou_func, atol=1e-4)
assert iou == pytest.approx(expected_ious[idx], rel=1e-3)
Copy link

Copilot AI Sep 19, 2025

Choose a reason for hiding this comment

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

The expected IoU values are hardcoded without explanation of how they were calculated. Consider adding a comment explaining the calculation or using a more descriptive variable name to make the test more maintainable.

Copilot uses AI. Check for mistakes.

Copy link
Author

Choose a reason for hiding this comment

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

No additional comment needed here since the rationale is self-understood.

Co-authored-by: Copilot <[email protected]>
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)

2 participants