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 IN labels
- pytorch_ood.utils.contains_known_and_unknown(labels) bool | Tensor[source]
- Returns:
true if the labels contain IN 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 known classes to index in \([0,n]\), unknown classes to values in \([-\infty, -1]\). Required for open set simulations.
Example: If we split up a dataset so that the classes 2,3,4,9 are considered known or IN, 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.
Target mappings have to be known at evaluation time.
- class pytorch_ood.utils.InsertCOCO(coco_dir: str, p: float = 0.1, n: int = 1, exclude_classes: List[str] | str | None = None, annotation_per_image: int = 1, ood_mask_value: int = -1, upscale: float = 1.4150357439499515, year: int = 2017, min_img_size: int = 480, download: bool = False)[source]
Transformation that inserts cropped COCO objects into images, marking the corresponding pixels of a segmentation mask as OOD.
The inserted objects can be used as synthetic OOD objects for supervised training of OOD detectors.
This was proposed in the paper Entropy Maximization and Meta Classification for Out-Of-Distribution Detection in Semantic Segmentation.
insert_coco = InsertCOCO( coco_dir="data/coco", exclude_classes=["train", "bicycle"], p=0.1 ) img, mask = insert_coco(img, mask)
- See Paper:
- Parameters:
coco_dir – Directory to store the coco dataset
p – Probability of inserting an OOD object to the image
n – Number of inserted OOD objects per image
exclude_classes – List of classes that should not be used for the OOD generation. Can also be one of
bddAnomalyorStreethazards.annotation_per_image – Number of different annotation that are used for the ood object per coco image. (E.g. if there are 2 elephants on a COCO image, if this parameter is 1, only 1 elephant is inserted)
ood_mask_value – Value of the OOD segmentation mask pixels
upscale – Upscale factor for the OOD object
year – Year of the coco dataset
min_img_size – Minimum size of the used coco image
download – Set
Trueto automatically download the COCO dataset
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.
AUROC
AUPR IN
AUPR OUT
FPR@95TPR
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