
Wobei N die Anzahl der Testbilder ist.
Die Ausgabe y_pred hat die Form (batch_size,channel,height,width, wobei y_pred eine binäre Maskenausgabe für ein semantisches Segmentierungsproblem (Vordergrund vs. Hintergrund) darstellt, also der Kanal = 1. Ich bin verwirrt darüber Implementierung. Hier ist meine aktuelle Implementierung:
Code: Select all
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