Utilities
- class pytorch_ood.utils.TensorBuffer(device='cpu')[source]
Used to buffer tensors
- Parameters:
device – device used to store buffers. Default is cpu.
- append(key, value: Tensor) Self[source]
Appends a tensor to the buffer.
- Parameters:
key – tensor identifier
value – tensor
- get(key) Tensor[source]
Retrieves tensor from the buffer
- Parameters:
key – tensor identifier
- Returns:
concatenated tensor
- pytorch_ood.utils.calc_openness(n_train, n_test, n_target)[source]
In Toward open set recognition the Openness \(\mathcal{O}\) of a problem was defined as:
\[\mathcal{O} = 1 - \sqrt{ \frac{2 \times n_{train}}{n_{test} \times n_{target}} }\]where \(n\) is the number of classes, respectively.
- Parameters:
n_train – number of classes for training
n_test – total number of classes used during testing
n_target – number of classes for classification during testing
- Returns:
Openness of the problem
- See Paper:
- pytorch_ood.utils.contains_known(labels) bool | Tensor[source]
- Returns:
True if the labels contains any ID labels
- pytorch_ood.utils.contains_known_and_unknown(labels) bool | Tensor[source]
- Returns:
True if the labels contain ID and OOD classes
- pytorch_ood.utils.contains_unknown(labels) bool | Tensor[source]
- Returns:
True if the labels contains any OOD labels
- pytorch_ood.utils.extract_features(data_loader: DataLoader, model: Callable[[Tensor], Tensor], device: str | None) Tuple[Tensor, Tensor][source]
Helper to extract outputs from model. Ignores OOD inputs.
- Parameters:
data_loader – dataset to extract from
model – neural network to pass inputs to
device – device used for calculations
- Returns:
Tuple with outputs and labels
- pytorch_ood.utils.fix_random_seed(seed: int = 12345) None[source]
Set all random seeds.
- Parameters:
seed – seed to set
Transformations
- class pytorch_ood.utils.ToUnknown[source]
Callable that returns a negative number, used in pipelines to mark specific datasets as OOD or unknown.
- class pytorch_ood.utils.TargetMapping(known: Set, unknown: Set)[source]
Maps ID (a.k.a. known) classes to labels \(\in [0,n]\), and OOD (a.k.a. unknown) classes to labels in \([-\infty, -1]\). This is required for open set simulations.
Example: If we split up a dataset so that the classes 2,3,4,9 are considered known or ID, these class labels have to be remapped to 0,1,2,3 to be able to train using cross entropy with 1-of-K-vectors. All other classes have to be mapped to values \(<0\) to be marked as OOD.
Metrics
- class pytorch_ood.utils.OODMetrics(device: str = 'cpu', mode: str = 'classification', void_label: int | None = None)[source]
Calculates various metrics used in OOD detection experiments.
The interface is similar to
torchmetrics.metrics = OODMetrics() outlier_scores = torch.Tensor([0.5, 1.0, -10]) labels = torch.Tensor([1,2,-1]) metrics.update(outlier_scores, labels) metric_dict = metrics.compute()
In
classificationmode, the inputs will be flattened, so we treat each value as an individual example. Using this mode for segmentation tasks can require a lot of memory and compute.In
segmentationmode, the inputs will be flattened along the first (batch) dimension so that the shape is \(B \times D\) afterwards. The scores will then be calculated for each sample in the batch (i.e., over \(D\) values each), and the final score will be the mean over all \(B\) samples.- Parameters:
device – where tensors should be stored
mode – either
classificationorsegmentation.void_label – label that will be ignored during score calculation
- pytorch_ood.utils.metrics.aurra(confidence: Tensor, correct: Tensor) float[source]
- See Implementation:
- Parameters:
confidence – predicted confidence values
correct – ground truth
- Returns:
score
- pytorch_ood.utils.metrics.calibration_error(confidence: Tensor, correct: Tensor, p: str = '2', beta: int = 100) float[source]
- See Implementation:
- Parameters:
confidence – predicted confidence
correct – ground truth
p – p for norm. Can be one of
1,2, orinftybeta – target bin size
- Returns:
calculated calibration error