
Где N — количество тестовых изображений.
Выходные данные y_pred имеют форму (batch_size,channel,height,width, где y_pred представляет собой выходные данные двоичной маски для проблемы семантической сегментации (передний план и фон), поэтому канал = 1. Я смущен относительно Вот моя текущая реализация:
Код: Выделить всё
def dice_score(y_true, y_pred):
"""
Compute the Dice score for binary segmentation tasks with a single channel.
Args:
y_true (Tensor): Ground truth tensor with shape (batch_size, 1, height, width).
y_pred (Tensor): Predicted tensor with shape (batch_size, 1, height, width).
Returns:
dice (float): Dice score for the entire batch.
"""
threshold = 0.5
epsilon = 1e-8 # Small constant to avoid division by zero
# Binarize predictions
y_pred = tf.cast(y_pred > threshold, tf.float32)
# Compute TP, FP, FN
TP = tf.reduce_sum(y_pred * y_true, axis=[2, 3]) # Sum over spatial dimensions (height, width)
FP = tf.reduce_sum(y_pred * (1 - y_true), axis=[2, 3]) # False positives
FN = tf.reduce_sum((1 - y_pred) * y_true, axis=[2, 3]) # False negatives
# Compute Dice score per batch element
dice_per_batch = (2 * TP + epsilon) / (2 * TP + FP + FN + epsilon)
# Average Dice score over the batch
dice_score = tf.reduce_mean(dice_per_batch)
return dice_score
Подробнее здесь: https://stackoverflow.com/questions/793 ... dice-score
Мобильная версия