SafetyCage is a Python package for detecting misclassified predictions from machine learning models in classification tasks. It provides a unified interface for multiple statistical detection methods, enabling users to quantify prediction reliability and flag potentially incorrect outputs across different models and datasets easily.
Available on PyPI: https://pypi.org/project/safetycage/.
Full documentation is available at https://safetycage.readthedocs.io/.
Background
The idea behind safetycage is that we can find statistics on each predicted sample and compare that statistic to some statistic threshold to predict whether the sample prediction was incorrectly classified.
Description
Machine learning models can produce incorrect predictions with high confidence. SafetyCage addresses this by providing post-hoc misclassification detection methods that operate on model outputs or internal representations.
The package includes several methods:
- MSP (Maximum Softmax Probability)
- DOCTOR (Error probability estimation)
- Mahalanobis (Distance-based statistical testing)
- SPARDACUS (Projection + density estimation approach)
Each method outputs a statistic or p-value that reflects how likely a prediction is to be incorrect.
Alternatively, you can implement your own method by initializing a base class from the safetycage abstract base class, which defines how methods should be implemented.
Requirements
safetycage requires Python 3.13 or later.
Core dependencies (installed automatically): joblib, matplotlib, numpy.
Installation
pip install safetycage
# or: uv add safetycage
Some methods need extra dependencies, installed via pip install safetycage[extra] (or uv add "safetycage[extra]"):
| Extra | Uses | Adds |
|---|---|---|
red |
RED |
torch, gpytorch |
spardacus |
SPARDACUS |
statsmodels, scipy, scikit-learn, tqdm |
mahalanobis |
Mahalanobis |
statsmodels, scipy |
torch |
TorchModelModule |
torch |
Tutorials & Examples
To learn how to use safetycage, check out the examples/ directory in this repository. It contains complete integrations with runnable notebooks, including scripts to train models to test the safetycage methods on.
Changelog
See the CHANGELOG.MD for details on versioning.
Support
If you encounter issues or have questions:
- Open an issue on the repository: https://github.com/SINTEF/safetycage/issues.
- Check the safetycage-tutorials repo for examples.
Contributing
If you would like to contribute, please reach out to our safetycage team, listed below!
Authors
- Pål Vegard Bun Johnsen (palVJ)
- Joel Bjervig (joelbjervig)
- Julia Qiu (jq11)
Acknowledgment
The MSP method was introduced by Hendrycks and Gimpel in A Baseline for Detecting Misclassified and Out-of-Distribution Examples in Neural Networks.
The DOCTOR method was introduced by Granese et al. in DOCTOR: A Simple Method for Detecting Misclassification Errors.
The Mahalanobis method is described in Johnsen et al..
The SPARDACUS method is described in Johnsen et al..
A proper citation for these methods is provided in the docstring of the code using these methods.
A special thank you goes to previous co-authors of the methods we have built, Filippo Remonato, Shawn Benedict, and Albert Ndur-Osei.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project status
Active and under development!
Citation
If you use safetycage, please cite us!
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file safetycage-0.0.62.tar.gz.
File metadata
- Download URL: safetycage-0.0.62.tar.gz
- Upload date:
- Size: 35.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.7.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
74ae4deb16293d0b828dcf7974d49165d54b5522a894f9830b67260243b721af
|
|
| MD5 |
d6d79afe0cf5670a8752502b0e70d86f
|
|
| BLAKE2b-256 |
225c84dda9616c890ae9bb95b2a5d4bfcdcda84a55223a74c1098e60cc4e041c
|
File details
Details for the file safetycage-0.0.62-py3-none-any.whl.
File metadata
- Download URL: safetycage-0.0.62-py3-none-any.whl
- Upload date:
- Size: 44.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.7.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
65ac9b4fcc9f9b1168889844e347895033684c21b21c7931d90f406e69b67831
|
|
| MD5 |
4fd2f7c0a4ee77ed484c2abb739fd074
|
|
| BLAKE2b-256 |
dd22b76eff6980f74fa1c94dd8eefec2437c9410929e61647fb4f1ed646c29ab
|