【发布时间】:2021-07-07 23:49:28
【问题描述】:
我正在尝试实现骰子系数,因此我可以将分段图像与地面实况图像进行比较。然而,骰子系数的输出似乎不正确,因为分割后的图像与地面真实图像的相似度约为 80%。骰子系数的输出为0.13。应该在 0.8 左右。
def my_dice(img1,img2):
intersection = np.logical_and(img1, img2)
union = np.logical_or(img1, img2)
dice = (2*np.sum(intersection))/(np.sum(union)+np.sum(intersection))
return dice
由于非二进制数组(范围 0-255),我认为结果很奇怪。所以我将图像除以 255,这给了我一些浮点数。这没有帮助。所以我使用了 python astype(np.bool) 但这也没有帮助。为了清楚起见,我对一个单元格的图像进行了语义分割,仅此而已,我想使用骰子系数与它的基本事实进行比较。我使用了很多我在网上找到的方法,但没有一个能给我正确的数字。以前喜欢这个。
"""Soft dice loss calculation for arbitrary batch size, number of classes, and number of spatial dimensions.
Assumes the `channels_last` format.
# Arguments
y_true: b x X x Y( x Z...) x c One hot encoding of ground truth
y_pred: b x X x Y( x Z...) x c Network output, must sum to 1 over c channel (such as after softmax)
epsilon: Used for numerical stability to avoid divide by zero errors
"""
# skip the batch and class axis for calculating Dice score
axes = tuple(range(1, len(y_pred.shape)-1))
print(axes)
numerator = 2. * np.sum(y_pred * y_true, axes)
print(numerator)
denominator = np.sum(np.square(y_pred) + np.square(y_true), axes)
print(denominator)
return np.mean(numerator / (denominator + epsilon)) # average over classes and batch
当我使用骰子损失时,结果看起来更合理,在第二种方法的情况下,返回看起来像这样:
return 1-np.mean(numerator / (denominator + epsilon)) # average over classes and batch
但是,这显然不是我需要的。两张图片的形状都是:(224,224,1)
【问题讨论】:
标签: python image-segmentation dice coefficients semantic-segmentation