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
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
'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 |