incerto.ood.MSP

incerto.ood.MSP#

class incerto.ood.MSP(model)[source]#

Bases: OODDetector

Maximum-Softmax-Probability (MSP) baseline OOD detector.

Returns 1 - max softmax probability as the OOD score, so higher means more likely OOD. Despite its simplicity, MSP is a strong baseline.

Reference:

Hendrycks & Gimpel, “A Baseline for Detecting Misclassified and Out-of-Distribution Examples in Neural Networks”, ICLR 2017.

Parameters:

model – A trained classifier returning logits of shape (batch, n_classes).

__init__(model)#

Initialize the OOD detector with a trained model.

The model is automatically: 1. Set to eval mode 2. Has gradients disabled (requires_grad=False)

Parameters:

model – A trained PyTorch model (nn.Module)

Raises:

TypeError – If model is not an nn.Module.

Methods

__init__(model)

Initialize the OOD detector with a trained model.

load(path, model, **kwargs)

Load detector state from a file.

load_state_dict(state)

Load detector state from a dictionary.

predict(x, threshold)

Predict whether inputs are OOD using a threshold.

save(path)

Save detector state to a file (excluding the model).

score(x)

Compute OOD scores for a batch of inputs.

state_dict()

Return a dictionary containing the detector's state.

score(x)[source]#

Compute OOD scores for a batch of inputs.

Parameters:

x (Tensor) – Input batch passed to model.

Return type:

Tensor

Returns:

Tensor of shape (batch,) with values in [0, 1). Higher values indicate higher OOD likelihood.