Skip to content

fix: optimal threshold computation#32

Merged
michele-milesi merged 4 commits intoorobix-masterfrom
fix/optimal-f1-threshold-computation
Feb 20, 2026
Merged

fix: optimal threshold computation#32
michele-milesi merged 4 commits intoorobix-masterfrom
fix/optimal-f1-threshold-computation

Conversation

@michele-milesi
Copy link
Member

@michele-milesi michele-milesi commented Feb 20, 2026

Description

Provide a summary of the modification as well as the issue that has been resolved.

  • Compute Optimal Threshold with single target with combined method: fixed epsilon and torch.nextafter

Copy link

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 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.nextafter combined 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)
Copy link

Copilot AI Feb 20, 2026

Choose a reason for hiding this comment

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

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

Suggested change
optimal_f1_score = torch.tensor(1.0)
optimal_f1_score = torch.tensor(1.0, dtype=current_preds.dtype, device=current_preds.device)

Copilot uses AI. Check for mistakes.
@michele-milesi michele-milesi changed the title fix: optimal threhsold computation fix: optimal threshold computation Feb 20, 2026
@michele-milesi michele-milesi merged commit 4038509 into orobix-master Feb 20, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants