losses

This module defines some custom loss functions and metrics that are used to train and evaluate a U-net-like tf.Keras.model. This require the input to be a tensor.

Note: Some of these metrics are implemented in ct_segnet.stats to receive numpy.array as inputs.

ct_segnet.model_utils.losses.IoU(y_true, y_pred)[source]
Returns

intersection over union accuracy

Parameters
  • y_true (tensor) – Ground truth tensor of shape (batch_size, n_rows, n_cols, n_channels)

  • y_pred (tensor) – Predicted tensor of shape (batch_size, n_rows, n_cols, n_channels)

ct_segnet.model_utils.losses.acc_ones(y_true, y_pred)[source]
Returns

accuracy in predicting ones = TP/(TP + FN)

Parameters
  • y_true (tensor) – Ground truth tensor of shape (batch_size, n_rows, n_cols, n_channels)

  • y_pred (tensor) – Predicted tensor of shape (batch_size, n_rows, n_cols, n_channels)

ct_segnet.model_utils.losses.acc_zeros(y_true, y_pred)[source]
Returns

accuracy in predicting zero values = TN/(TN + FP)

Parameters
  • y_true (tensor) – Ground truth tensor of shape (batch_size, n_rows, n_cols, n_channels)

  • y_pred (tensor) – Predicted tensor of shape (batch_size, n_rows, n_cols, n_channels)

ct_segnet.model_utils.losses.focal_loss(y_true, y_pred)[source]
Returns

loss value

Focal loss is defined here: https://arxiv.org/abs/1708.02002 Using this provides improved fidelity in unbalanced datasets: Tekawade et al. https://doi.org/10.1117/12.2540442

Parameters
  • y_true (tensor) – Ground truth tensor of shape (batch_size, n_rows, n_cols, n_channels)

  • y_pred (tensor) – Predicted tensor of shape (batch_size, n_rows, n_cols, n_channels)

ct_segnet.model_utils.losses.my_binary_crossentropy(y_true, y_pred)[source]
Returns

loss value

This is my own implementation of binary cross-entropy. Nothing special.

Parameters
  • y_true (tensor) – Ground truth tensor of shape (batch_size, n_rows, n_cols, n_channels)

  • y_pred (tensor) – Predicted tensor of shape (batch_size, n_rows, n_cols, n_channels)

ct_segnet.model_utils.losses.weighted_crossentropy(y_true, y_pred)[source]
Returns

loss value

Weighted cross-entropy allows prioritizing accuracy in a certain class (either 1s or 0s).

Parameters
  • y_true (tensor) – Ground truth tensor of shape (batch_size, n_rows, n_cols, n_channels)

  • y_pred (tensor) – Predicted tensor of shape (batch_size, n_rows, n_cols, n_channels)