Tensorflow 用于图像分割中训练与推理的骰子分数
我正在使用U-Net模型进行图像分割(多类),并将dice_损失作为我的损失函数进行训练。使用平滑项分别计算每个类的骰子分数,然后取所有类的平均骰子分数来计算损失。下面是我如何计算tensorflow中的骰子损失Tensorflow 用于图像分割中训练与推理的骰子分数,tensorflow,deep-learning,computer-vision,image-segmentation,medical-imaging,Tensorflow,Deep Learning,Computer Vision,Image Segmentation,Medical Imaging,我正在使用U-Net模型进行图像分割(多类),并将dice_损失作为我的损失函数进行训练。使用平滑项分别计算每个类的骰子分数,然后取所有类的平均骰子分数来计算损失。下面是我如何计算tensorflow中的骰子损失 def dice_loss(y_true, y_pred, smooth=1e-7): # y_true : shape=(BATCH_SIZE, HIEGHT, WIDTH, CHANNELS) # y_pred : shape=(BATCH_SIZE, HIEGHT, W
def dice_loss(y_true, y_pred, smooth=1e-7):
# y_true : shape=(BATCH_SIZE, HIEGHT, WIDTH, CHANNELS)
# y_pred : shape=(BATCH_SIZE, HIEGHT, WIDTH, CHANNELS)
nom = tf.reduce_sum(y_true*y_pred, axis=(1,2))
denom = tf.reduce_sum(y_true, axis=(1,2)) + tf.reduce_sum(y_pred, axis=(1,2))
dice_score = (2 * nom + smooth) / (denom + smooth)
mean_dice_score = tf.reduce_mean(dice_score, axis=-1)
return 1 - mean_dice_score
最后,取该批次所有图像的平均值作为损失
现在,在推理过程中,我以类似的方式计算骰子分数,但是每个类的骰子分数是单独报告的,而不是所有类的平均值。下面是如何计算tensorflow中单个类的骰子分数
def compute_dice_score(y_true, y_pred, num_classes, class_idx, smooth=1e-7):
# y_true : shape=(BATCH_SIZE, HIEGHT, WIDTH, CHANNELS)
# y_pred : shape=(BATCH_SIZE, HIEGHT, WIDTH, CHANNELS)
y_pred = tf.one_hot(tf.argmax(y_pred, axis=3), num_classes)
y_pred = y_pred[..., class_idx]
y_true = y_true[..., class_idx]
nom = tf.reduce_sum(y_true*y_pred, axis=(1,2))
denom = tf.reduce_sum(y_true, axis=(1,2)) + tf.reduce_sum(y_pred, axis=(1,2))
dice_score = (2 * nom + smooth) / (denom + smooth)
return dice_score
最后计算该批中所有图像的平均骰子分数
在训练过程中,我取单个图像的平均骰子分数,然后对批次进行平均,因为这对我的数据集更有效,而不是同时计算整个批次的所有骰子分数
我的问题在推理过程中