File size: 7,562 Bytes
4c254bc d5deb12 4c254bc d5deb12 4c254bc d5deb12 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
---
title: detectors
emoji: π§
colorFrom: pink
colorTo: pink
sdk: static
pinned: true
license: apache-2.0
---
# π§ Detectors
Package to accelerate research on generalized out-of-distribution (OOD) detection.
Under development. Please report any issues or bugs [here](https://github.com/edadaltocg/detectors/issues).
## Stats
[](https://github.com/edadaltocg/detectors/graphs/commit-activity)
[](https://github.com/edadaltocg/detectors/actions/workflows/build.yml)
[](http://detectors.readthedocs.io/?badge=latest)
[](https://doi.org/10.5281/zenodo.7883080)
## What is it?
This library is aimed at assisting researchers in the field of generalized OOD detection. It is inspired by [HF's Transformers](https://https://github.com/huggingface/transformers) and features implementations of baselines, metrics, and data sets that allow researchers to perform meaningful benchmarking and development of ood detection methods. It features:
- `methods`: more than 20 detection methods implemented.
- `pipelines`: evaluating OOD detectors on popular benchmarks, such as MNIST, CIFAR, and ImageNet benchmarks with random seed support for reproducibility.
- `datasets`: OOD datasets implemented with md5 checksums and without the need to download them manually.
- `models`: model architectures totally integrated with [`timm`](https://github.com/huggingface/pytorch-image-models).
- `eval`: implementation of fast OOD evaluation metrics.
- Several aggregation methods for multi-layer OOD detection.
- Pipelines for open set recognition and covariate drift detection.
## Installation
Please follow the instructions [here](https://pytorch.org/get-started/locally/) to install PyTorch. Installing PyTorch with CUDA support is strongly recommended.
```bash
pip install detectors
```
To install the latest version from the source:
```bash
git clone https://github.com/edadaltocg/detectors.git
cd detectors
pip install --upgrade pip setuptools wheel
pip install -e .
```
Also, you have easy access to the Python scripts from the examples:
```bash
cd examples
```
## Examples
The following examples show how to use the library and how it can be integrated into your research. For more examples, please check the [documentation](https://detectors.readthedocs.io/en/latest/use_cases/).
### Running a benchmark
The following example shows how to run a benchmark.
```python
import detectors
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = detectors.create_model("resnet18_cifar10", pretrained=True)
model = model.to(device)
test_transform = detectors.create_transform(model)
pipeline = detectors.create_pipeline("ood_benchmark_cifar10", transform=test_transform)
method = detectors.create_detector("msp", model=model)
pipeline_results = pipeline.run(method)
print(pipeline.report(pipeline_results["results"]))
```
We recommend running benchmarks on machines equipped with large RAM and GPUs with 16GB of memory or larger to leverage large batch sizes and faster inference.
### Creating a detector
The following example shows how to create a detector. The only requirement is that the method takes an input `x` and returns a score.
```python
import torch
import detectors
@detectors.register_detector("awesome_detector")
def awesome_detector(x: torch.Tensor, model, **kwargs):
# Do something awesome with the model and the input
return scores
# Instantiating the detector
method = detectors.create_detector("awesome_detector", model=model)
```
Alternatively, you can use the `Detector` class to create a detector that requires some initialization or state to be fitted before being called (e.g., Mahalanobis detector):
```python
import torch
import detectors
@detectors.register_detector("awesome_detector")
class AwesomeDetector(detectors.Detector):
def __init__(self, model, **kwargs):
self.model = model
def __call__(self, x: torch.Tensor, **kwargs):
# Do something awesome with the model and the input
return scores
# Instantiating the detector
method = detectors.create_detector("awesome_detector", model=model)
```
Check the [documentation](https://detectors.readthedocs.io/en/latest/use_cases/) for more information.
### Listing available resources
The following example shows how to list all available resources in the library.
```python
import detectors
# list all available models (same as timm.list_models)
print(detectors.list_models())
# list all available models with a specific pattern
print(detectors.list_models("*cifar*"))
# list all available datasets
print(detectors.list_datasets())
# list all available detectors
print(detectors.list_detectors())
# list all available pipelines
print(detectors.list_pipelines())
```
## FAQ over specific documents
**Methods**
- [Documentation](https://detectors.readthedocs.io/en/latest/use_cases/)
**Pipelines**
- [Documentation](https://detectors.readthedocs.io/en/latest/use_cases/)
**Pypi**
- [Website](https://pypi.org/project/detectors)
## Contributing
As an open-source project in a rapidly developing field, we are open to contributions, whether in the form of a new feature, improved infra, or better documentation.
See the [contributing guidelines](https://github.com/edadaltocg/detectors/blob/master/CONTRIBUTING.md) for instructions on how to make your first contribution to `detectors`.
### Thanks to all our contributors
<a href="https://github.com/edadaltocg/detectors/graphs/contributors">
<img src="https://contributors-img.web.app/image?repo=edadaltocg/detectors" />
</a>
## Contact
Concerning this package, its use, and bugs, use the [issue page](https://github.com/edadaltocg/detectors/issues) of the [ruptures repository](https://github.com/edadaltocg/detectors). For other inquiries, you can contact me [here](https://edadaltocg.github.io/contact/).
## Important links
- [Documentation](http://detectors.readthedocs.io/)
- [Pypi package index](https://pypi.python.org/pypi/detectors)
- [Github repository](https://github.com/edadaltocg/detectors)
## Limitations
- This library is only compatible with PyTorch models.
- This library has implemented only computer vision pipelines and datasets.
## Citing detectors
The detection of Out-of-Distribution (OOD) has created a new way of securing machine intelligence, but despite its many successes, it can be difficult to understand due to the various methods available and their intricate implementations. The fast pace of research and the wide range of OOD methods makes it challenging to navigate the field, which can be a problem for those who have recently joined the field or want to deploy OOD detection. The library we have created aims to lower these barriers by providing a resource for researchers of any background to understand the methods available, how they work, and how to be successful with OOD detection.
If you find this repository useful, please consider giving it a star π and citing it as below:
```bibtex
@software{detectors2023,
author = {Eduardo Dadalto},
title = {Detectors: a Python Library for Generalized Out-Of-Distribution Detection},
url = {https://github.com/edadaltocg/detectors},
doi = {https://doi.org/10.5281/zenodo.7883596},
month = {5},
year = {2023}
}
``` |