Skip to content
Discussion options

You must be logged in to vote

@maciejzj — your analysis is spot on. Zero-value pixels cause division by zero in both SAM and ERGAS.

Workaround 1 — Mask out zero regions:

import torch
from torchmetrics.functional.image import spectral_angle_mapper

# Create mask of valid pixels
valid_mask = (target.sum(dim=1, keepdim=True) != 0)  # (N, 1, H, W)
preds_masked = preds * valid_mask
target_masked = target * valid_mask

# Use reduction="none" and average over valid pixels only
sam_map = spectral_angle_mapper(preds_masked, target_masked, reduction="none")
valid_pixels = valid_mask.squeeze(1)
sam_score = sam_map[valid_pixels].mean()

Workaround 2 — Add epsilon (simpler but slightly changes values):

eps = 1e-8
sam_score = spectr…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Borda
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants