Relative Mahalanobis Distance (RMD)¶
- class pytorch_ood.detector.RMD(encoder: Callable[[Tensor], Tensor] | None)[source]¶
Bases:
Mahalanobisclassification no segmentation requires fit
Implements the Relative Mahalanobis Distance (RMD) from the paper A Simple Fix to Mahalanobis Distance for Improving Near-OOD Detection.
This method calculates a class center \(\mu_y\) for each class, and a shared covariance matrix \(\Sigma\) from the data.
Additionally, it fits a background gaussian with mean \(\mu_0\) and covariance matrix \(\Sigma_0\) to all of the features and calculates outlier scores as
\[\min_k \lbrace d_k(f(x)) - d_0(f(x)) \rbrace\]where \(f(x)\) are the features of \(x\), \(d_k(z) = (z - \mu_k)^\top \Sigma^{-1} (z - \mu_k)\) is the squared Mahalanobis distance to class \(k\) and \(d_0\) is the squared Mahalanobis distance under the background gaussian.
- Input:
features
- Paper:
A Simple Fix to Mahalanobis Distance for Improving Near-OOD Detection, arXiv 2021
- Parameters:
encoder – feature extractor that maps inputs to features of shape \(B \times D\). Can be
Nonewhen usingfit_features(...)andpredict_features(...)directly.
Attributes
- property device¶
The device of the detector’s owned torch state, if one can be inferred.
- property n_classes¶
Number of classes the model is fitted for
- requires_fit = True¶
Whether
fit(...)must be called before scoring.
Methods
- fit(data_loader: DataLoader) Self[source]¶
Fit parameters of the multivariate Gaussians for the given loader. Ignores OOD Inputs.
- Parameters:
data_loader – data loader with training data
- Returns:
self
- fit_features(z: Tensor, y: Tensor) Self[source]¶
Fit parameters of the multivariate Gaussians. Ignores OOD inputs.
- Parameters:
z – features of shape \(N \times D\)
y – class labels of shape \(N\)
- Returns:
self
- predict(x: Tensor) Tensor[source]¶
- Parameters:
x – input batch, will be passed through the encoder
- Returns:
relative Mahalanobis scores of shape \(B\)
- predict_features(z: Tensor) Tensor[source]¶
Calculates the relative Mahalanobis distance directly on features.
- Parameters:
z – features of shape \(B \times D\) (or feature maps of shape \(B \times C \times H \times W\), which are spatially averaged)
- Returns:
relative Mahalanobis scores of shape \(B\)
- to(device: str | device) Self¶
Move detector-owned modules and tensor state to
device.This is a detector-level analogue of
nn.Module.to(...). It moves modules, tensors, and common container-valued state stored on the detector itself.- Parameters:
device – target device, e.g.
"cuda:0"or atorch.device- Returns:
self