Skip to content

Commit

Permalink
Handle case with only background/normal images in scoring
Browse files Browse the repository at this point in the history
  • Loading branch information
willyfh committed Jan 27, 2024
1 parent f421ff9 commit d5e39f2
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/anomalib/metrics/spro.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def spro_score(predictions: torch.Tensor, targets: torch.Tensor,
predictions = predictions > threshold

score = torch.tensor(0.0)

m = 0
# Iterate for each image in the batch
for i, target in enumerate(targets):
unique_labels = torch.unique(target)
Expand Down Expand Up @@ -137,6 +137,9 @@ def spro_score(predictions: torch.Tensor, targets: torch.Tensor,

# Update score with minimum of true_pos/saturation_threshold and 1.0
score += torch.minimum(true_pos / saturation_threshold, torch.tensor(1.0))
m += 1

# Calculate the mean score
return torch.mean(score)
# If there are only backgrounds
if m == 0:
return torch.tensor(1.0)
return score / m

0 comments on commit d5e39f2

Please sign in to comment.