incerto.ood.KNN#
- class incerto.ood.KNN(model, k=50, layer_name='penultimate')[source]#
Bases:
_FeatureHookMixin,OODDetectorKNN-based OOD detection (Sun et al., NeurIPS 2022).
Computes OOD score as distance to k-th nearest neighbor in feature space. Requires fitting on training data.
Methods
__init__(model[, k, layer_name])fit(loader)Store training features for KNN computation.
load(path, model, **kwargs)Load detector state from a file.
load_state_dict(state)Load KNN training features.
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 score as distance to k-th nearest neighbor.
Save KNN training features.