Talk about the DICE and IOU indicators of image segmentation

Table of contents

1 Introduction

2. The connection between dice and iou

3. Code implementation

3.1 dice

3.2 iou

3.3 test

3.4 The relationship curve between dice and iou

4. Code


1 Introduction

Both dice and iou are measures of the similarity between two sets

dice calculation formula:

IOU calculation formula:

Set comprehension for iou:

 

Iou is actually the ratio of the overlap part and the union part of the two regions, that is, the intersection/union of the two sets

The denominator of dice is not a union, because the denominator of dice is the sum of two areas, A+B = A + B - A∩B, so the denominator of dice is actually minus one A∩B, so let the numerator  A∩B (intersection) expands by 2 times

2. The connection between dice and iou

If the relationship between the two collections is divided a little more finely, this is the form:

Then A∩B = TP, A∪B = FN + TP + FP, A+B = FN + TP +TP + FP 

dice : 

 

iou : 

 

Then according to the deformation, it can be concluded that:

 

3. Code implementation

|A ∩ B| = sum of A * B = sum of products of two areas

|A| + |B| = sum of A + B = sum of two regions added

|A∪B| = |A| + |B| - |A ∩ B| = sum of intersection of two regions - sum of multiplication of two regions

3.1 dice

implementation of dice

# Dice
def Dice(pred,true):
    intersection = pred * true          # 计算交集  pred ∩ true
    temp = pred + true                  # pred + true
    smooth = 1e-8                       # 防止分母为 0
    dice_score = 2*intersection.sum() / (temp.sum() + smooth)
    return dice_score

intersection is the intersection of two areas, that is, the product of two areas

temp is the sum of the two areas, (Note: this is not a union, because the intersecting part is not subtracted)

3.2 iou

implementation of iou

# Iou
def Iou(pred,true):
    intersection = pred * true          # 计算交集  pred ∩ true
    temp = pred + true                  # pred + true
    union = temp - intersection         # 计算并集:A ∪ B = A + B - A ∩ B
    smooth = 1e-8                       # 防止分母为 0
    iou_score = intersection.sum() / (union.sum() + smooth)
    return iou_score

intersection is the intersection of two areas, that is, the product of two areas

temp is the sum of the two areas, (Note: this is not a union, because the intersecting part is not subtracted)

union is the union of two regions

3.3 test

predict:

# prediction
predict = torch.tensor([0.01,0.03,0.02,0.02,0.05,0.12,0.09,0.07,0.89,0.85,0.88,0.91,0.99,0.97,0.95,0.97]).reshape(1,1,4,4)
'''
tensor([[[[0.0100, 0.0300, 0.0200, 0.0200],
          [0.0500, 0.1200, 0.0900, 0.0700],
          [0.8900, 0.8500, 0.8800, 0.9100],
          [0.9900, 0.9700, 0.9500, 0.9700]]]])
'''

label:

# label
label = torch.tensor([0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1]).reshape(1,1,4,4)
'''
tensor([[[[0, 0, 0, 0],
          [0, 0, 0, 0],
          [1, 1, 1, 1],
          [1, 1, 1, 1]]]])
'''

Calculation results:

 

The formula shows that the relationship between dice and iou is:

Verification shows that:

Note: Some slight differences are caused by smooth

3.4 The relationship curve between dice and iou

According to the formula, the relationship between dice and iou is as follows:

The relationship curve is shown in the figure:

 

4. Code

import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'

import torch
import numpy as np
import matplotlib.pyplot as plt

# prediction
predict = torch.tensor([0.01,0.03,0.02,0.02,0.05,0.12,0.09,0.07,0.89,0.85,0.88,0.91,0.99,0.97,0.95,0.97]).reshape(1,1,4,4)
'''
tensor([[[[0.0100, 0.0300, 0.0200, 0.0200],
          [0.0500, 0.1200, 0.0900, 0.0700],
          [0.8900, 0.8500, 0.8800, 0.9100],
          [0.9900, 0.9700, 0.9500, 0.9700]]]])
'''

# label
label = torch.tensor([0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1]).reshape(1,1,4,4)
'''
tensor([[[[0, 0, 0, 0],
          [0, 0, 0, 0],
          [1, 1, 1, 1],
          [1, 1, 1, 1]]]])
'''


# Dice
def Dice(pred,true):
    intersection = pred * true          # 计算交集  pred ∩ true
    temp = pred + true                  # pred + true
    smooth = 1e-8                       # 防止分母为 0
    dice_score = 2*intersection.sum() / (temp.sum() + smooth)
    return dice_score


# Iou
def Iou(pred,true):
    intersection = pred * true          # 计算交集  pred ∩ true
    temp = pred + true                  # pred + true
    union = temp - intersection         # 计算并集:A ∪ B = A + B - A ∩ B
    smooth = 1e-8                       # 防止分母为 0
    iou_score = intersection.sum() / (union.sum() + smooth)
    return iou_score


# dice 和 iou 的换算
def dice_and_iou(x):
    y = x / (2 - x)
    return y


dice = np.arange(0,1,0.001)
iou = dice_and_iou(dice)

plt.plot(dice,iou)
plt.xlabel('dice')
plt.ylabel('iou')
plt.show()

Guess you like

Origin blog.csdn.net/qq_44886601/article/details/129418831