# Dice coefficient Python与Julia实现

2020年3月6日 8745点热度 1人点赞 0条评论

Hello everyone! 这篇文章将介绍Dice coefficient以及其实现

## Introduction

Dice coefficient 是 Lee R. Dice 在1945年为评估生物种群提出的一种度量方法[1]。后来不同领域的学者都将其引入到自己的专业。这里，我将介绍Dice codfficient 在图像分割领域作为评价指标的理解与实现。

### Segmentation

- 标记出所有属于背景类别的点。
- 标记出属于我们关心类别的所有点。

- based on threshold
- region growing
- interactive segmenation
- classifier
- CNN (Unet)[3]

### 一个例子

Accuracy=(TP+FN)/(TP+FN+FP+FN)

Recall (=Sensitivity)=TP/(TP+FN)

Precision=TP/(TP+FP)

=2* precision* recall/(precision+recall)

s=\frac{2|X \cap Y|}{|X|+|Y|}

This figure is copyright by ] Zou, Kelly H. et al.[2]

def dice_coef(y_true, y_pred):
smooth = 1
y_true_f = y_true.flatten()
y_pred_f = y_pred.flatten()
intersection = np.sum(y_true_f * y_pred_f)
return (2. * intersection + smooth) / (np.sum(y_true_f) + np.sum(y_pred_f) + smooth)


def dice_average_sets(y_true, y_pred):
assert y_true.shape[0]==y_pred.shape[0], "you should use same size data"
dice = []
for i in range(y_true.shape[0]):
dice.append(dice_coef(y_true[i], y_pred[i]))
print(np.mean(dice))
return dice


function dice_coef(y_true, y_pred)
smooth = 1
y_true_f = vec(y_true)
y_pred_f = vec(y_pred)
intersection = dot(y_true_f, y_pred_f)
return (2. * intersection + smooth) / (sum(y_true_f) + sum(y_pred_f) + smooth)
end

function DiceCoefficientAverage(y_true, y_pred)
if size(y_true)[1]!=size(y_pred)[1]
println("you should use same size data")
return -1
end
dice = []
for i in 1:size(y_true)[1]
append!(dice, dice_coef(y_true[i,:,:], y_pred[i,:,:]))
end
print(mean(dice))
end


## 实验

[1] Dice, Lee R. "Measures of the amount of ecologic association between species." Ecology 26.3 (1945): 297-302.
[2] Zou, Kelly H. et al. “Statistical validation of image segmentation quality based on a spatial overlap index.” Academic radiology 11 2 (2004): 178-89 .
[3] Ronneberger, Olaf, Philipp Fischer, and Thomas Brox. "U-net: Convolutional networks for biomedical image segmentation." International Conference on Medical image computing and computer-assisted intervention. Springer, Cham, 2015.

Dong Wang

I will work as a PhD student of TU Graz in Austria. My research interests include Embedded/Edge AI, federated learning, computer vision, and IoT.