The Dice coefficient measures overlapping regions in image segmentation

learning target

The Dice coefficient and mIoU are both evaluation indicators for semantic segmentation. Today I will focus on the Dice coefficient. By the way, I will mention Dice Loss. I will have time to distinguish between two commonly used loss functions in semantic segmentation, cross entropy and Dice Loss.

The Importance of Evaluation Metrics in Semantic Segmentation

Semantic segmentation is an important task in the field of computer vision, which aims to assign each pixel in an image to a specific semantic category. Unlike other image analysis tasks such as object detection or image classification, semantic segmentation requires pixel-level labeling of images, providing more detailed and accurate scene understanding.

Evaluation metrics play a key role in semantic segmentation tasks to measure the similarity and accuracy between model predictions and real labels. The Dice coefficient is a commonly used evaluation metric for pixel-level segmentation tasks.

Dice Coefficient

The Dice coefficient can measure the degree of overlap between the predicted segmentation result and the real label, and its value ranges from 0 to 1. The closer the Dice coefficient is to 1, the better the overlap between the predicted result and the real label, and the better the performance of the model. Compared with other indicators, the Dice coefficient has advantages in dealing with imbalanced categories and small objects, so it is widely used in many semantic segmentation tasks.

The calculation formula of Dice coefficient is as follows:

Dice=\frac{2*(before\bigcap true)}{before\bigcup true}

Among them, pred is a set of predicted values, true is a set of real values, and the denominator is the intersection of pred and true. Multiplying by 2 is because the intersection has an overlapping part of the two, that is, there are common elements, and the denominator is the union of pred and true . 

(1) The dot product of the predicted segmentation map and the real segmentation map:

Approximate  (pred⋂true)  as the dot product between the predicted graph pred and the true graph true:

(pred\bigcap true)=\begin{bmatrix} 0.01 &0.03 &0.08 &0.05 \\ 0.02 &0.14 &0.21 &0.06 \\ 0.92 &0.89 &0.90 &0.96 \\ 0.94&0 .88 &0.97 & 0.92 \end{bmatrix} * \begin{bmatrix} 0 &0 & 0 &0 \\ 0 &0 & 0 &0 \\ 1 &1 & 1 &1 \\ 1 &1 &1 &1 \end{bmatrix}

(2) Add elements after dot multiplication:

\begin{bmatrix} 0 &0 &0 &0 \\ 0 &0 &0 &0 \\ 0.92 &0.89 &0.90 &0.96 \\ 0.94&0.88 &0.97 & 0.92 \end{bmatrix}=>0.92+0.89+...+0.92=7.38

For the problem of binary classification, the real segmentation map has only two values ​​of 0 and 1 encoded by One-Hot, so all pixels that are not activated in the true segmentation map in the pred segmentation map can be effectively cleared. For activated pixels, the contribution of low-confidence predicted pixels in the final result can be reduced, and higher values ​​​​will result in higher Dice coefficients.

(3) To calculate the union of molecules, just take the addition of elements directly:

pred=\begin{bmatrix} 0.01 &0.03 &0.08 &0.05 \\ 0.02 &0.14 &0.21 &0.06 \\ 0.92 &0.89 &0.90 &0.96 \\ 0.94&0.88 &0.97 & 0.92 \end{bmatrix}=>0.01+0.03+...+0.92=7.98

true= \begin{bmatrix} 0 &0 & 0 &0 \\ 0 &0 & 0 &0 \\ 1 &1 & 1 &1 \\ 1 &1 &1 &1 \end{bmatrix}=>8

Dice loss

The relationship between Dice loss and Dice coefficient is: DiceLoss = 1 - DiceCoefficient, thus the formula of Dice Loss is:

Dice \ Loss=1-\frac{2*(pred\bigcap true)}{pred\bigcup true}

When the dice value goes up, the dice loss goes down, and when we get the maximum value of dice, the loss value is 0, which means the model is perfect.

pytorch implements Dice coefficient and Dice Loss

# Dice系数 Dice Loss
# 已放入 pyzjr 中,方便调用,版本>= 1.1.7之后可见
import torch

def Dice_coeff(pred, true, reduce_batch_first=False, epsilon=1e-6):
    #  二分类Dice系数
    assert pred.size() == true.size()
    assert pred.dim() == 3 or not reduce_batch_first

    if pred.dim() == 2 or not reduce_batch_first:
        sum_dim = (-1, -2)
    else:
        sum_dim=(-1, -2, -3)

    inter = 2 * (pred * true).sum(dim=sum_dim)
    sets_sum = pred.sum(dim=sum_dim) + true.sum(dim=sum_dim)
    sets_sum = torch.where(sets_sum == 0, inter, sets_sum)

    dice = (inter + epsilon) / (sets_sum + epsilon)
    return dice.mean()


def multiclass_Dice_coeff(pred, true, reduce_batch_first=False, epsilon=1e-6):
    #  多分类Dice系数
    return Dice_coeff(pred.flatten(0, 1), true.flatten(0, 1), reduce_batch_first, epsilon)


def Dice_Loss(pred, true, multiclass=False):
    #  Dice损失
    fn = multiclass_Dice_coeff if multiclass else Dice_coeff
    return 1 - fn(pred, true, reduce_batch_first=True)

reference article

The Difference Between Dice and Dice Loss

Metric evaluation index-Dice and loss function-Dice Loss - Know (zhihu.com)

Scientific research mapping-commonly used image segmentation indicators (Dice, Iou, Hausdorff) and their calculation_How to calculate the dice coefficient of image segmentation_CV Exchange Conference Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/m0_62919535/article/details/132657931