Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix for empty preds or target in iou scores #2806

Merged
merged 5 commits into from
Oct 29, 2024
Merged

Conversation

SkafteNicki
Copy link
Member

@SkafteNicki SkafteNicki commented Oct 28, 2024

What does this PR do?

Fixes #2805
When either preds or target have empty boxes the score for that pair should be 0. Currently instead it is a empty tensor which then are not counted towards the global average making the scores too high.

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

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--2806.org.readthedocs.build/en/2806/

@SkafteNicki SkafteNicki added the bug / fix Something isn't working label Oct 28, 2024
@SkafteNicki SkafteNicki added this to the v1.5.x milestone Oct 28, 2024
@SkafteNicki SkafteNicki self-assigned this Oct 28, 2024
Copy link

codecov bot commented Oct 28, 2024

Codecov Report

Attention: Patch coverage is 60.86957% with 9 lines in your changes missing coverage. Please review.

Project coverage is 35%. Comparing base (4c75369) to head (2c2993c).
Report is 3 commits behind head on master.

❗ There is a different number of reports uploaded between BASE (4c75369) and HEAD (2c2993c). Click for more details.

HEAD has 296 uploads less than BASE
Flag BASE (4c75369) HEAD (2c2993c)
Linux 59 9
torch2.0.1+cpu 18 3
python3.8 6 1
cpu 88 14
macOS 17 3
python3.12 18 3
torch2.5.0 6 1
python3.10 57 9
Windows 12 2
torch2.5.0+cpu 6 1
torch2.0.1 11 2
python3.11 7 1
torch2.4.1+cu121 15 2
torch2.2.2+cpu 6 1
torch2.1.2+cpu 6 1
torch2.3.1+cpu 6 1
torch2.5.0+cu124 14 2
Additional details and impacted files
@@           Coverage Diff            @@
##           master   #2806     +/-   ##
========================================
- Coverage      69%     35%    -34%     
========================================
  Files         338     338             
  Lines       18334   18352     +18     
========================================
- Hits        12586    6387   -6199     
- Misses       5748   11965   +6217     

@mergify mergify bot added the ready label Oct 28, 2024
@yurithefury
Copy link

@SkafteNicki thanks for implementing the fix! I just noticed another potential unexpected behaviour is when both targets and preds contain empty boxes:

targets = [{'boxes': torch.empty((0, 4), device='cuda:0'), 'labels': torch.tensor([], device='cuda:0', dtype=torch.int64)}]

preds = [{'boxes': torch.empty((0, 4), device='cuda:0'), 'labels': torch.tensor([], device='cuda:0', dtype=torch.int64), 'scores': torch.tensor([], device='cuda:0')}]

iou = IntersectionOverUnion()
iou.update(preds, targets)
result = iou.compute()  

This yields an IoU of 0, but it probably should return 1

@SkafteNicki
Copy link
Member Author

@SkafteNicki thanks for implementing the fix! I just noticed another potential unexpected behaviour is when both targets and preds contain empty boxes:

targets = [{'boxes': torch.empty((0, 4), device='cuda:0'), 'labels': torch.tensor([], device='cuda:0', dtype=torch.int64)}]

preds = [{'boxes': torch.empty((0, 4), device='cuda:0'), 'labels': torch.tensor([], device='cuda:0', dtype=torch.int64), 'scores': torch.tensor([], device='cuda:0')}]

iou = IntersectionOverUnion()
iou.update(preds, targets)
result = iou.compute()  

This yields an IoU of 0, but it probably should return 1

@yurithefury I do partly not agree. When both target and preds contains empty boxes both the intersection and union of those boxes are 0, meaning that we end up trying to compute 0/0, which is undefined. It would maybe make the most sense setting it to Nan, but that would mess with average over multiple samples where only one is not defined. So there is really no right or wrong value in this case. Maybe it needs to be an argument to control?

@Borda Borda merged commit fbc7877 into master Oct 29, 2024
43 of 50 checks passed
@Borda Borda deleted the bugfix/iou_empty_average branch October 29, 2024 11:07
@yurithefury
Copy link

@SkafteNicki Good point! Allowing an option to set this value, like empty_iou_value, would give flexibility - for example, setting it to NaN, 0, 1, or just ignoring these cases altogether. This way, users could pick what fits their use case best.

Borda pushed a commit that referenced this pull request Nov 7, 2024
Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Jirka B <[email protected]>

(cherry picked from commit fbc7877)
Borda pushed a commit that referenced this pull request Nov 7, 2024
Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Jirka B <[email protected]>

(cherry picked from commit fbc7877)
Borda pushed a commit that referenced this pull request Nov 7, 2024
Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Jirka B <[email protected]>

(cherry picked from commit fbc7877)
Borda pushed a commit that referenced this pull request Nov 7, 2024
Co-authored-by: Jirka Borovec <[email protected]>
Co-authored-by: Jirka B <[email protected]>

(cherry picked from commit fbc7877)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug / fix Something isn't working ready topic: Detect
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect IntersectionOverUnion calculations when predictions or targets contain empty boxes
3 participants