Custom Losses

class src.pipelines.tensorflow_v2.losses.custom_losses.WeightedCE(alpha=0.5, name='WeightedCE')[source]

Weighted Cross Entropy Loss

call(y_true, y_pred)[source]

Function matching the structure expected by tf.keras. This function applies the weighted cross entropy loss to the prediction and the true response.

Parameters
  • y_true (Tensor) – the true response

  • y_pred (Tensor) – the prediction

Return type

float

Returns

weighted cross entropy loss score (tf.float32)

class src.pipelines.tensorflow_v2.losses.custom_losses.FocalLossV2(alpha=0.25, gamma=2, name='FocalLossV2')[source]

Focal Loss (applied to logits)

call(y_true, y_pred)[source]

Function matching the structure expected by tf.keras. This function applies the focal loss to the prediction and the true response. The call to Focal Loss is applied at the logits for more stability when optimising.

Parameters
  • y_true (Tensor) – the true label

  • y_pred (Tensor) – the predicted label

Return type

float

Returns

focal loss score (tf.float32)

class src.pipelines.tensorflow_v2.losses.custom_losses.FocalLoss(alpha=0.25, gamma=2, name='FocalLoss')[source]

Focal Loss

call(y_true, y_pred)[source]

Function matching the structure expected by tf.keras. This function applies the focal loss to the prediction and the true response.

Parameters
  • y_true (Tensor) – the true label

  • y_pred (Tensor) – the predicted label

Return type

float

Returns

focal loss score (tf.float32)

class src.pipelines.tensorflow_v2.losses.custom_losses.SoftDiceLoss(name='SoftDiceLoss')[source]

Soft Dice Loss

call(y_true, y_pred)[source]

Function matching the structure expected by tf.keras. This function applies the weighted cross entropy loss to the prediction and the true response.

Parameters
  • y_true (Tensor) – the true response

  • y_pred (Tensor) – the prediction

Return type

float

Returns

soft dice loss score (tf.float32)