Richtige Art und Weise, den Würfelwert zu implementierenPython

Python-Programme
Guest
 Richtige Art und Weise, den Würfelwert zu implementieren

Post by Guest »

Ich möchte den folgenden Würfel-Score implementieren:
Image

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
Meine Verwirrung besteht darin, dass ich nicht weiß, ob ich über die Achsen 2 und 3 summieren soll, nur um TP, FP und FN zu berechnen und den Würfelwert wie in der aktuellen Implementierung zu berechnen, oder ob ich das tun muss Summiere über die Achsen 1, 2 und 3 und führe dann eine mittlere Reduktion über den Stapel (Testbilder) durch, um die Würfelpunktzahl zu erhalten?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post