dlr: HACK: argmax to convert [64,128,128, 2] → [64,128,128]

This commit is contained in:
Starbeamrainbowlabs 2023-03-03 21:41:26 +00:00
parent 94a32e7144
commit 3d051a8874
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 11 additions and 26 deletions

View file

@ -18,7 +18,7 @@ import tensorflow as tf
from lib.dataset.dataset_mono import dataset_mono
from lib.ai.components.LossCrossEntropyDice import LossCrossEntropyDice
from lib.ai.components.MetricDice import dice_coefficient
from lib.ai.components.MetricDice import metric_dice_coefficient
from lib.ai.components.MetricSensitivity import sensitivity
from lib.ai.components.MetricSpecificity import specificity
@ -189,7 +189,7 @@ if PATH_CHECKPOINT is None:
loss=loss_fn,
metrics=[
"accuracy",
dice_coefficient,
metric_dice_coefficient,
tf.keras.metrics.MeanIoU(num_classes=2),
sensitivity, # How many true positives were accurately predicted
specificity # How many true negatives were accurately predicted?

View file

@ -14,32 +14,17 @@ def dice_coefficient(y_true, y_pred):
Returns:
tf.Tensor: The computed Dice coefficient.
"""
y_true = tf.cast(y_true, dtype=tf.float32)
y_pred = tf.cast(y_pred, dtype=tf.float32)
y_pred = tf.math.sigmoid(y_pred)
numerator = 2 * tf.reduce_sum(y_true * y_pred)
denominator = tf.reduce_sum(y_true + y_pred)
return numerator / denominator
class MetricDice(tf.keras.metrics.Metric):
"""An implementation of the dice loss function.
@source
Args:
smooth (float): The batch size (currently unused).
"""
def __init__(self, name="dice_coefficient", smooth=100, **kwargs):
super(MetricDice, self).__init__(name=name, **kwargs)
self.param_smooth = smooth
def call(self, y_true, y_pred):
ground_truth = tf.cast(y_true, dtype=tf.float32)
prediction = tf.cast(y_pred, dtype=tf.float32)
return dice_coef(ground_truth, prediction, smooth=self.param_smooth)
def get_config(self):
config = super(MetricDice, self).get_config()
config.update({
"smooth": self.param_smooth,
})
return config
def metric_dice_coefficient(y_true, y_pred):
y_pred = tf.math.argmax(y_pred)
return dice_coefficient(y_true, y_pred)