Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [v.0.7.0.dev151]

### Fixed

- Use combined torch.nextafter and fixed epsilon to select optimal threshold when only one target is present.

## [v.0.7.0.dev150]

### Updated
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "anomalib-orobix"
version = "0.7.0.dev150"
version = "0.7.0.dev151"
description = "Orobix anomalib fork"
authors = [
"Intel OpenVINO <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion src/anomalib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
# SPDX-License-Identifier: Apache-2.0

anomalib_version = "0.7.0"
custom_orobix_version = "1.5.0"
custom_orobix_version = "1.5.1"

__version__ = f"{anomalib_version}.dev{custom_orobix_version.replace('.', '')}"
16 changes: 13 additions & 3 deletions src/anomalib/utils/metrics/optimal_f1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
# SPDX-License-Identifier: Apache-2.0

"""Implementation of Optimal F1 score based on TorchMetrics."""
from typing import Optional

import warnings
from typing import Optional

import torch
from torch import Tensor
Expand Down Expand Up @@ -54,12 +55,21 @@ def compute(self) -> Tensor:

epsilon = 1e-3
if len(current_targets.unique()) == 1:
# Use torch nextafter to ensure that the threshold is higher (or smaller)
# than the maximum (or minimum) score. This ensures 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.

if current_targets.max() == 0:
self.threshold = current_preds.max() + epsilon
max_score = current_preds.max()
self.threshold = torch.max(torch.nextafter(max_score, _inf), max_score + epsilon)
else:
self.threshold = current_preds.min() - epsilon
min_score = current_preds.min()
self.threshold = torch.min(torch.nextafter(min_score, -_inf), min_score - epsilon)

if torch.isinf(self.threshold) or torch.isnan(self.threshold):
raise RuntimeError(f"Invalid value computed for the threshold: {self.threshold}.")

return optimal_f1_score
else:
Expand Down
Loading