FastSurferCNN.utils.metrics¶
- class FastSurferCNN.utils.metrics.DiceScore(num_classes, class_ids=None, device=None, output_transform=<function DiceScore.<lambda>>)[source]¶
Accumulate the components of the Dice coefficient for all pairs of classes.
For each pair of a true class and a predicted class, the overlap and the sum of both sizes are added up over all updates. compute turns them into a Dice confusion matrix, whose diagonal holds the Dice score of each class.
- Parameters:
- num_classes
int Number of classes.
- class_idsarray_like,
optional Label value of each class, num_classes of them. Defaults to 0, …, num_classes - 1.
- device
strortorch.device,optional Device to move the predictions and labels to in update. None leaves them where they are.
- output_transform
callable(),optional Called with the elements of the tuple passed to update, returns the predicted and the true labels. The default takes the class with the highest score along dimension 1 of the prediction. None uses the tuple as it is.
- num_classes
Methods
compute([per_class, class_idxs])Compute the Dice score.
reset()Reset the state of the object.
update(output)Update the internal state based on the output.
- compute(per_class=False, class_idxs=None)[source]¶
Compute the Dice score.
- Parameters:
- per_class
bool, default=False Whether to return the Dice score of each class instead of their mean.
- class_idxs
np.ndarray,optional Indices into class_ids of the classes to compute it for. All classes by default.
- per_class
- Returns:
floatornp.ndarrayThe mean Dice score over the classes, or with per_class the Dice score of each class.
np.ndarrayThe Dice confusion matrix of the classes.
- FastSurferCNN.utils.metrics.dice_score(pred, gt, validate=True)[source]¶
Calculates the Dice overlap between pred and gt (best 1).
- Parameters:
- pred
np.ndarray Predicted image.
- gt
np.ndarray Ground truth image.
- validate
bool If True, use the scipy implementation. If False, use the numpy implementation.
- pred
- Returns:
floatDice overlap between pred and gt, 1 for identical images and 0 for no overlap.
- FastSurferCNN.utils.metrics.hd(result, reference, voxelspacing=None, connectivity=1)[source]¶
Computes the (symmetric) Hausdorff Distance (HD) between the binary objects in two images. It is defined as the maximum surface distance between the objects.
- Parameters:
- resultarray_like
Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else.
- referencearray_like
Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else.
- voxelspacing
floator sequenceoffloats,optional The voxelspacing in a distance unit i.e. spacing of elements along each dimension. If a sequence, must be of length equal to the input rank; if a single number, this is used for all axes. If not specified, a grid spacing of unity is implied.
- connectivity
int The neighbourhood/connectivity considered when determining the surface of the binary objects. This value is passed to
scipy.ndimage.morphology.generate_binary_structureand should usually be \(> 1\). Note that the connectivity influences the result in the case of the Hausdorff distance.
- Returns:
- hd
float The symmetric Hausdorff Distance between the object(s) in
resultand the object(s) inreference. The distance unit is the same as for the spacing of elements along each dimension, which is usually given in mm.- hd50
float The 50th percentile of the Hausdorff Distance.
- hd95
float The 95th percentile of the Hausdorff Distance.
- hd
See also
assdAverage Symmetric Surface Distance, computes the average symmetric surface distance.
asdAverage Surface Distance, computes the average surface distance.
Notes
This is a real metric. The binary images can therefore be supplied in any order.
- FastSurferCNN.utils.metrics.hd95(result, reference, voxelspacing=None, connectivity=1)[source]¶
Computes the 95th percentile of the Hausdorff Distance.
Computes the 95th percentile of the (symmetric) Hausdorff Distance (HD) between the binary objects in two images. Compared to the Hausdorff Distance, this metric is slightly more stable to small outliers and is commonly used in Biomedical Segmentation challenges.
- Parameters:
- result
Any Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else.
- referencearray_like
Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else.
- voxelspacing
floator sequenceoffloats,optional The voxelspacing in a distance unit i.e. spacing of elements along each dimension. If a sequence, must be of length equal to the input rank; if a single number, this is used for all axes. If not specified, a grid spacing of unity is implied.
- connectivity
int The neighbourhood/connectivity considered when determining the surface of the binary objects. This value is passed to
scipy.ndimage.morphology.generate_binary_structureand should usually be \(> 1\). Note that the connectivity influences the result in the case of the Hausdorff distance.
- result
- Returns:
- hd95
float The 95th percentile of the symmetric Hausdorff Distance between the object(s) in
`result`and the object(s) in`reference`. The distance unit is the same as for the spacing of elements along each dimension, which is usually given in mm.
- hd95
See also
hdComputes the symmetric Hausdorff Distance.
Notes
This is a real metric. The binary images can therefore be supplied in any order.
- FastSurferCNN.utils.metrics.iou_score(pred_cls, true_cls, nclass=79)[source]¶
Compute the intersection-over-union score.
Both inputs should be categorical (as opposed to one-hot).
- Parameters:
- pred_cls
torch.Tensor Network prediction (categorical).
- true_cls
torch.Tensor Ground truth (categorical).
- nclass
int Number of classes (Default value = 79).
- pred_cls
- Returns:
np.ndarrayAn array containing the intersection for each class.
np.ndarrayAn array containing the union for each class.
- FastSurferCNN.utils.metrics.precision_recall(pred_cls, true_cls, nclass=79)[source]¶
Calculate recall (TP/(TP + FN) and precision (TP/(TP+FP) per class.
- Parameters:
- pred_cls
torch.Tensor Network prediction (categorical).
- true_cls
torch.Tensor Ground truth (categorical).
- nclass
int Number of classes (Default value = 79).
- pred_cls
- Returns:
np.ndarrayAn array containing the number of true positives for each class.
np.ndarrayAn array containing the sum of true positives and false negatives for each class.
np.ndarrayAn array containing the sum of true positives and false positives for each class.