fl4health.metrics.utils module¶
- align_pred_and_target_shapes(preds, targets, label_dim=None)[source]¶
If necessary, attempts to correct shape mismatches between the given tensors by inferring which one to one-hot-encode. Note that if both preds and targets have the same shape then nothing needs to be modified. If there is a mismatch, it is assumed that the labels in one of the predictions or the targets are vector encoded in some way (either one-hot or soft) while the other is label index encoded.
If one is vector encoded but not the other, then both are returned as vector encoded tensors.
NOTE: This function ASSUMES label-index encoding if the shapes are misaligned. This assumption doesn’t necessarily hold in binary classification settings where continuous values might be used to indicate the positive label by default. As such, this function should not be used for those types of tensors.
For example, consider a problem with 3 label classes with the preds vector encoded and the targets label encoded
preds = torch.Tensor([[0.1, 0.2, 0.7], [0.9, 0.1, 0.0]]) targets = torch.Tensor([[2], [1]])
preds has shape (2, 3) and targets has shape (2, 1). This function will convert targets to a one-hot-encode tensor with contents
targets = torch.Tensor([0, 0, 1], [0, 1, 0])
- Parameters:
preds (torch.Tensor) – The tensor with model predictions.
targets (torch.Tensor) – The tensor with model targets.
label_dim (int | None) – Index of the label dimension. If left as None then this method attempts to infer the label dimension if it is needed.
- Returns:
The pred and target tensors respectively now ensured to have the same shape.
- Return type:
tuple[torch.Tensor, torch.Tensor]
- infer_label_dim(tensor1, tensor2)[source]¶
Infers the label dimension given two related tensors of different shapes.
Generally useful for inferring the label dimension when one tensor is vector-encoded and the other is not. The label dimension is inferred by looking for dimensions that either are not the same size, or are not present in tensor 2.
- Parameters:
tensor1 (torch.Tensor) – The reference tensor. Must have the same number of dimensions as tensor 2, or have exactly 1 more dimension (the label dim).
tensor2 (torch.Tensor) – The non-reference tensor.
- Raises:
AssertionError – If the the label dimension cannot be inferred without ambiguity. For example if a dimension next to the label dimension has the same size.
- Returns:
Index of the dimension along tensor 1 that corresponds to the label dimension.
- Return type:
- map_label_index_tensor_to_one_hot(label_index_tensor, target_shape, label_dim)[source]¶
Maps the provided
label_index_tensor
, which has label indices at the providedlabel_dim
. In the tensor, this dimension should be “empty”, i.e. have size 1. This function uses the shape provided bytarget_shape
to expand the label indices into one-hot encoded vectors in that dimension according the the size of the target dimension atlabel_dim
intarget_shape
. For example, if label_index_tensor has shape (64, 10, 10, 1), label_dim = 3, and target_shape = (64, 10, 10, 4), then the new shape should be (64, 10, 10, 4) with [i, j, k, :] being a one-hot vector of length 4.- Parameters:
label_index_tensor (torch.Tensor) – Tensor to have label_dim dimension one-hot encoded accounting to target_shape and the indices of label_index_tensor in the label_dim
target_shape (torch.Size) – Shape we want to transform label_index_tensor to. Mainly used to establish the length of the one-hot encodings
label_dim (int) – Dimension to one-hot encode.
- Returns:
Tensor with one-hot encoded label_dim.
- Return type:
torch.Tensor