Dice Coefficient and IoU

2025. 9. 25. 12:51·Programming/ML
728x90
728x90

Dice coefficient (or dice score)와 IoU는 대표적인 Set based Similarity 임.


Dice Coefficient

Segmentation의 결과를 측정하는데 사용되는 metric.
(harmonic mean에 해당: binary segmentation의 경우, 사실상, F1 score임.)

 

$$ \begin{aligned}\text{Dice Coef.}&=\frac{2\text{Intersection}}{\text{Union+Intersection}}\\&=\dfrac{2(S_g \cap S_p)}{|S_g|+|S_p|}\\&=\frac{2TP}{(TP+FN)+(TP+FP)}\\&=\frac{2}{\frac{(TP+FN)}{TP}+\frac{(TP+FP)}{T}}\\&=\frac{2}{\frac{1}{Recall}+\frac{1}{Precision}}\\&=\text{F-1 Score}\end{aligned} $$

  • $S_g$ : Segmentation Ground Truth
  • $S_p$ : Segmentation Prediction

 

Segmentation의 결과가 얼마나 정확한지를 나타낼때 많이 사용함.

1이 나올 경우, 완벽하게 segmentation이 된 것이고, 0은 완전히 틀린 것에 해당.


주의사항

foreground (positive class) 위주로만 계산됨

$S_g$가 1인 게 foreground이므로, 0인 background는 아무리 많은 pixel에서 맞게 나와도 Dice score에 크게 반영되지 않음.

의료 영상에서 병변(lesion) 영역이 매우 작을 때, background를 다 맞추더라도 Dice는 낮게 나올 수 있음 (병변을 맞추어야 점수가 올라감).

  • 클래스 불균형(class imbalance)이 심한 경우, background가 압도적으로 많고 이를 맞추는 건 중요하지 않다면 Dice가 더 신뢰할 수 있는 지표가 됨.
  • 반대로 background가 segmentation의 중요한 평가 요소라면 Dice만으로는 부족

 

Dice Loss

이를 이용한 Dice Loss도 있으며 식은 다음과 같음.

$$ DL(p,\hat{p})=1-\dfrac{2p\hat{p}+1}{p+\hat{p}+1} $$

  • 분모가 0이 되는 것을 방지하기 위해 분자와 분모에 1을 더해줌.

Dice coef.의 특징인 foreground만 고려되는 약점을 그대로 가짐.

즉, Cross Entropy에 비해, foreground 고려가 더 많이된다는 단점이 존재.

때문에, Cross Entropy와 합쳐서 loss를 사용하는 경우가 많음.


Intersection over Union (IoU)

Segmentation에 사용되는 metric.

Jaccard similarity (or Jaccard Index) 라고 불리기도 함.

$$ \begin{aligned}\text{IoU}&=\frac{\text{Intersection}}{\text{Union}}\\&=\frac{TP}{FP+TP+FN}\end{aligned} $$

# Dice coefficient, IoU Metric

def iou_cpu(y_true, y_pred):
  y_true_f = np.ndarray.flatten(y_true)
  y_pred_f = np.ndarray.flatten(y_pred)

  intersection = np.sum(y_true_f * y_pred_f)
  return (intersection + 1) / (np.sum(y_true_f) + np.sum(y_pred_f) - intersection + 1)

def dice_coef_cpu(y_true, y_pred):
  y_true_f = np.ndarray.flatten(y_true)
  y_pred_f = np.ndarray.flatten(y_pred)

  intersection = np.sum(y_true_f * y_pred_f)
  return (2. * intersection + 1) / (np.sum(y_true_f) + np.sum(y_pred_f) + 1)

참고자료

F1/Dice-Score vs IoU

 

F1/Dice-Score vs IoU

I was confused about the differences between the F1 score, Dice score and IoU (intersection over union). By now I found out that F1 and Dice mean the same thing (right?) and IoU has a very similar

stats.stackexchange.com

 

[PyTorch] Dice coefficient 을 PyTorch로 구현하기

 

[PyTorch] Dice coefficient 을 PyTorch로 구현하기

안녕하세요, 이번 포스팅에서는 image segmentation 분야에서 자주 사용되는 metric인 Dice coefficient를 PyTorch로 구현해보겠습니다. 또한 이 dice coefficient를 loss로 활용하는 법도 살펴봅니다. Dice coefficient d

deep-learning-study.tistory.com

 

2025.09.25 - [Programming/ML] - Similarity Metrics

 

Similarity Metrics

1. 거리 기반 (Distance-based) SimilarityEuclidean distance (L2) : $|x-y|_2$, 가장 일반적인 거리 척도.Manhattan distance (L1) : $|x-y|_1$, 절댓값 합. 희소 데이터에 강건.Minkowski distance : Lp 일반화. $p=1 → L1$, $p=2 → L2$.

dsaint31.tistory.com

 

 

728x90

'Programming > ML' 카테고리의 다른 글

Subgradient 와 Gradient Descent  (0) 2025.11.02
Bias-Variance Tradeoff  (0) 2025.10.30
Similarity Metrics  (0) 2025.09.25
[ML] Tensor: Scalar, Vector, Matrix.  (0) 2025.02.03
[ML] Nearest Neighbor Search: k-d Tree  (0) 2024.11.25
'Programming/ML' 카테고리의 다른 글
  • Subgradient 와 Gradient Descent
  • Bias-Variance Tradeoff
  • Similarity Metrics
  • [ML] Tensor: Scalar, Vector, Matrix.
dsaint31x
dsaint31x
    반응형
    250x250
  • dsaint31x
    Dsaint31's blog
    dsaint31x
  • 전체
    오늘
    어제
    • 분류 전체보기 (787)
      • Private Life (15)
      • Programming (206)
        • DIP (116)
        • ML (35)
      • Computer (120)
        • CE (54)
        • ETC (33)
        • CUDA (3)
        • Blog, Markdown, Latex (4)
        • Linux (9)
      • ... (368)
        • Signals and Systems (115)
        • Math (176)
        • Linear Algebra (33)
        • Physics (43)
        • 인성세미나 (1)
      • 정리필요. (61)
        • 의료기기의 이해 (6)
        • PET, MRI and so on. (7)
        • PET Study 2009 (1)
        • 방사선 장해방호 (5)
        • 방사선 생물학 (3)
        • 방사선 계측 (9)
        • 기타 방사능관련 (3)
        • 고시 (9)
        • 정리 (18)
      • RI (0)
      • 원자력,방사능 관련법 (2)
  • 블로그 메뉴

    • Math
    • Programming
    • SS
    • DIP
  • 링크

    • Convex Optimization For All
  • 공지사항

    • Test
    • PET Study 2009
    • 기타 방사능관련.
  • 인기 글

  • 태그

    numpy
    signal_and_system
    function
    Python
    Optimization
    cv2
    opencv
    Programming
    ML
    SIGNAL
    fourier transform
    linear algebra
    Vector
    Probability
    SS
    random
    인허가제도
    Term
    signals_and_systems
    math
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
dsaint31x
Dice Coefficient and IoU
상단으로

티스토리툴바