fix: optimal threshold computation#32
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request fixes the optimal threshold computation in the OptimalF1 metric for the edge case when only a single target class is present. The fix uses a combined approach of torch.nextafter and a fixed epsilon value to ensure numerical correctness across different floating-point precisions.
Changes:
- Enhanced threshold computation for single-target scenarios using
torch.nextaftercombined with fixed epsilon - Added validation to detect and raise an error for infinite threshold values
- Updated version to 0.7.0.dev151 and added changelog entry
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/anomalib/utils/metrics/optimal_f1.py | Improved threshold computation with torch.nextafter and epsilon combination for single-target case, with infinity check |
| pyproject.toml | Version bump to 0.7.0.dev151 |
| CHANGELOG.md | Added changelog entry for the fix |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # than the maximum (or minimum) score. This ensure correctness for lower precisions. | ||
| # Combined method is to avoid very small shifts around zero. | ||
| _inf = torch.tensor(torch.inf, dtype=current_preds.dtype, device=current_preds.device) | ||
| optimal_f1_score = torch.tensor(1.0) |
There was a problem hiding this comment.
The optimal_f1_score tensor is created without specifying dtype or device, which may lead to inconsistency with other tensors in the computation. Consider creating it with the same dtype and device as current_preds for consistency: torch.tensor(1.0, dtype=current_preds.dtype, device=current_preds.device).
| optimal_f1_score = torch.tensor(1.0) | |
| optimal_f1_score = torch.tensor(1.0, dtype=current_preds.dtype, device=current_preds.device) |
Description
Provide a summary of the modification as well as the issue that has been resolved.