Metrics¶
Mean Dice¶

monai.metrics.
compute_meandice
(y_pred, y, include_background=True, to_onehot_y=False, mutually_exclusive=False, sigmoid=False, other_act=None, logit_thresh=0.5)[source]¶ Computes Dice score metric from full size Tensor and collects average.
 Parameters
y_pred (
Tensor
) – input data to compute, typical segmentation model output. it must be onehot format and first dim is batch, example shape: [16, 3, 32, 32].y (
Tensor
) – ground truth to compute mean dice metric, the first dim is batch. example shape: [16, 1, 32, 32] will be converted into [16, 3, 32, 32]. alternative shape: [16, 3, 32, 32] and set to_onehot_y=False to use 3class labels directly.include_background (
bool
) – whether to skip Dice computation on the first channel of the predicted output. Defaults to True.to_onehot_y (
bool
) – whether to convert y into the onehot format. Defaults to False.mutually_exclusive (
bool
) – if True, y_pred will be converted into a binary matrix using a combination of argmax and to_onehot. Defaults to False.sigmoid (
bool
) – whether to add sigmoid function to y_pred before computation. Defaults to False.other_act (
Optional
[Callable
]) – callable function to replace sigmoid as activation layer if needed, Defaults toNone
. for example: other_act = torch.tanh.logit_thresh (
float
) – the threshold value used to convert (for example, after sigmoid if sigmoid=True) y_pred into a binary matrix. Defaults to 0.5.
 Raises
ValueError – When
sigmoid=True
andother_act is not None
. Incompatible values.TypeError – When
other_act
is not anOptional[Callable]
.ValueError – When
sigmoid=True
andmutually_exclusive=True
. Incompatible values.
 Returns
Dice scores per batch and per class, (shape [batch_size, n_classes]).
Note
 This method provides two options to convert y_pred into a binary matrix
when mutually_exclusive is True, it uses a combination of
argmax
andto_onehot
,when mutually_exclusive is False, it uses a threshold
logit_thresh
(optionally with asigmoid
function before thresholding).

class
monai.metrics.
DiceMetric
(include_background=True, to_onehot_y=False, mutually_exclusive=False, sigmoid=False, other_act=None, logit_thresh=0.5, reduction=<MetricReduction.MEAN: 'mean'>)[source]¶ Compute average Dice loss between two tensors. It can support both multiclasses and multilabels tasks. Input logits y_pred (BNHW[D] where N is number of classes) is compared with ground truth y (BNHW[D]). Axis N of y_preds is expected to have logit predictions for each class rather than being image channels, while the same axis of y can be 1 or N (onehot format). The include_background class attribute can be set to False for an instance of DiceLoss to exclude the first category (channel index 0) which is by convention assumed to be background. If the nonbackground segmentations are small compared to the total image size they can get overwhelmed by the signal from the background so excluding it in such cases helps convergence.
 Parameters
include_background (
bool
) – whether to skip Dice computation on the first channel of the predicted output. Defaults to True.to_onehot_y (
bool
) – whether to convert y into the onehot format. Defaults to False.mutually_exclusive (
bool
) – if True, y_pred will be converted into a binary matrix using a combination of argmax and to_onehot. Defaults to False.sigmoid (
bool
) – whether to add sigmoid function to y_pred before computation. Defaults to False.other_act (
Optional
[Callable
]) – callable function to replace sigmoid as activation layer if needed, Defaults toNone
. for example: other_act = torch.tanh.logit_thresh (
float
) – the threshold value used to convert (for example, after sigmoid if sigmoid=True) y_pred into a binary matrix. Defaults to 0.5.reduction (
Union
[MetricReduction
,str
]) – {"none"
,"mean"
,"sum"
,"mean_batch"
,"sum_batch"
,"mean_channel"
,"sum_channel"
} Define the mode to reduce computation result of 1 batch data. Defaults to"mean"
.
 Raises
ValueError – When
sigmoid=True
andother_act is not None
. Incompatible values.
Area under the ROC curve¶

monai.metrics.
compute_roc_auc
(y_pred, y, to_onehot_y=False, softmax=False, other_act=None, average=<Average.MACRO: 'macro'>)[source]¶ Computes Area Under the Receiver Operating Characteristic Curve (ROC AUC). Referring to: sklearn.metrics.roc_auc_score.
 Parameters
y_pred (
Tensor
) – input data to compute, typical classification model output. it must be OneHot format and first dim is batch, example shape: [16] or [16, 2].y (
Tensor
) – ground truth to compute ROC AUC metric, the first dim is batch. example shape: [16, 1] will be converted into [16, 2] (where 2 is inferred from y_pred).to_onehot_y (
bool
) – whether to convert y into the onehot format. Defaults to False.softmax (
bool
) – whether to add softmax function to y_pred before computation. Defaults to False.other_act (
Optional
[Callable
]) – callable function to replace softmax as activation layer if needed, Defaults toNone
. for example: other_act = lambda x: torch.log_softmax(x).average (
Union
[Average
,str
]) –{
"macro"
,"weighted"
,"micro"
,"none"
} Type of averaging performed if not binary classification. Defaults to"macro"
."macro"
: calculate metrics for each label, and find their unweighted mean.This does not take label imbalance into account.
"weighted"
: calculate metrics for each label, and find their average,weighted by support (the number of true instances for each label).
"micro"
: calculate metrics globally by considering each element of the labelindicator matrix as a label.
"none"
: the scores for each class are returned.
 Raises
ValueError – When
y_pred
dimension is not one of [1, 2].ValueError – When
y
dimension is not one of [1, 2].ValueError – When
softmax=True
andother_act is not None
. Incompatible values.TypeError – When
other_act
is not anOptional[Callable]
.ValueError – When
average
is not one of [“macro”, “weighted”, “micro”, “none”].
Note
ROCAUC expects y to be comprised of 0’s and 1’s. y_pred must be either prob. estimates or confidence values.