# Spectral Angle Mapper¶

## Module Interface¶

class torchmetrics.image.SpectralAngleMapper(reduction='elementwise_mean', **kwargs)[source]

Spectral Angle Mapper determines the spectral similarity between image spectra and reference spectra.

It works by calculating the angle between the spectra, where small angles between indicate high similarity and high angles indicate low similarity.

As input to forward and update the metric accepts the following input

As output of forward and compute the metric returns the following output

• sam (Tensor): if reduction!='none' returns float scalar tensor with average SAM value over sample else returns tensor of shape (N,) with SAM values per sample

Parameters:
• reduction (Optional[Literal['elementwise_mean', 'sum', 'none']]) –

a method to reduce metric score over labels.

• 'elementwise_mean': takes the mean (default)

• 'sum': takes the sum

• 'none' or None: no reduction will be applied

• kwargs (Any) – Additional keyword arguments, see Advanced metric settings for more info.

Returns:

Tensor with SpectralAngleMapper score

Example

>>> from torch import rand
>>> from torchmetrics.image import SpectralAngleMapper
>>> preds = rand([16, 3, 16, 16])
>>> target = rand([16, 3, 16, 16])
>>> sam = SpectralAngleMapper()
>>> sam(preds, target)
tensor(0.5914)

plot(val=None, ax=None)[source]

Plot a single or multiple values from the metric.

Parameters:
Return type:
Returns:

Figure and Axes object

Raises:

ModuleNotFoundError – If matplotlib is not installed

>>> # Example plotting single value
>>> from torch import rand
>>> from torchmetrics.image import SpectralAngleMapper
>>> preds = rand([16, 3, 16, 16])
>>> target = rand([16, 3, 16, 16])
>>> metric = SpectralAngleMapper()
>>> metric.update(preds, target)
>>> fig_, ax_ = metric.plot()

>>> # Example plotting multiple values
>>> from torch import rand
>>> from torchmetrics.image import SpectralAngleMapper
>>> preds = rand([16, 3, 16, 16])
>>> target = rand([16, 3, 16, 16])
>>> metric = SpectralAngleMapper()
>>> values = [ ]
>>> for _ in range(10):
...     values.append(metric(preds, target))
>>> fig_, ax_ = metric.plot(values)


## Functional Interface¶

torchmetrics.functional.image.spectral_angle_mapper(preds, target, reduction='elementwise_mean')[source]

Universal Spectral Angle Mapper.

Parameters:
• preds (Tensor) – estimated image

• target (Tensor) – ground truth image

• reduction (Literal['elementwise_mean', 'sum', 'none', None]) –

a method to reduce metric score over labels.

• 'elementwise_mean': takes the mean (default)

• 'sum': takes the sum

• 'none' or None: no reduction will be applied

Return type:

Tensor

Returns:

Tensor with Spectral Angle Mapper score

Raises:
• TypeError – If preds and target don’t have the same data type.

• ValueError – If preds and target don’t have BxCxHxW shape.

Example

>>> from torch import rand
>>> from torchmetrics.functional.image import spectral_angle_mapper
>>> preds = rand([16, 3, 16, 16],)
>>> target = rand([16, 3, 16, 16])
>>> spectral_angle_mapper(preds, target)
tensor(0.5914)


References

[1] Roberta H. Yuhas, Alexander F. H. Goetz and Joe W. Boardman, “Discrimination among semi-arid landscape endmembers using the Spectral Angle Mapper (SAM) algorithm” in PL, Summaries of the Third Annual JPL Airborne Geoscience Workshop, vol. 1, June 1, 1992.