CerebNet.utils.metrics

class CerebNet.utils.metrics.DiceScore(num_classes, class_ids=None, device=None, one_hot=False, output_transform=<function DiceScore.<lambda>>)[source]

Accumulating the component of the dice coefficient i.e. the union and intersection.

Args:
op (callable): a callable to update accumulator. Method’s signature is (accumulator, output).

For example, to compute arithmetic mean value, op = lambda a, x: a + x.

output_transform (callable, optional): a callable that is used to transform the

Engine’s process_function’s output into the form expected by the metric. This can be useful if, for example, you have a multi-output model and you want to compute the metric with respect to one of the outputs.

device (str of torch.device, optional): device specification in case of distributed computation usage.

In most of the cases, it can be defined as “cuda:local_rank” or “cuda” if already set torch.cuda.set_device(local_rank). By default, if a distributed process group is initialized and available, device is set to cuda.

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.

reset()[source]

Reset the state of the object.

update(output)[source]

Update the internal state based on the output.

Parameters:
outputtuple of torch.Tensor

Tuple of predictions and labels.

CerebNet.utils.metrics.dice_score(pred, gt)[source]

Calculates the Dice Similarity between pred and gt.

CerebNet.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.

voxelspacingfloat or sequence of floats, 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.

connectivityint

The neighbourhood/connectivity considered when determining the surface of the binary objects. This value is passed to scipy.ndimage.morphology.generate_binary_structure and should usually be \(> 1\). Note that the connectivity influences the result in the case of the Hausdorff distance.

Returns:
hdfloat

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.

hd50float

The 50th percentile of the Hausdorff Distance.

hd95float

The 95th percentile of the Hausdorff Distance.

See also

assd

Average Symmetric Surface Distance, computes the average symmetric surface distance.

asd

Average Surface Distance, computes the average surface distance.

Notes

This is a real metric. The binary images can therefore be supplied in any order.

CerebNet.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:
resultAny

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.

voxelspacingfloat or sequence of floats, 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.

connectivityint

The neighbourhood/connectivity considered when determining the surface of the binary objects. This value is passed to scipy.ndimage.morphology.generate_binary_structure and should usually be \(> 1\). Note that the connectivity influences the result in the case of the Hausdorff distance.

Returns:
hd95float

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.

See also

hd

Computes the symmetric Hausdorff Distance.

Notes

This is a real metric. The binary images can therefore be supplied in any order.

CerebNet.utils.metrics.volume_similarity(pred, gt)[source]

Calculate the Volume Similarity between pred and gt.