sequence_unet.metrics

Custom weighted metrics and losses for training and evaluating Sequence UNET models.

class WeightedMaskedBinaryCrossEntropy(pos_weight, neg_weight, from_logits=False, reduction='auto', name='weighted_masked_binary_crossentropy')

Bases: Loss

Version of masked_binary_crossentropy with class weights.

A version of the binary cross-entropy loss function with masked labels and class weights. Classes labelled 0 in y_true are masked, those labelled 1 correspond to 0 in y_pred and those labelled 2 to 1 in y_pred (i.e. offset by -1). Class weightings are applied after masking.Accepts predictions/true values as matrices.

pos_weight

Weight applied to positvely labelled (2) items.

Type:

float

neg_weight

Weight applied to negatively labelled (1) items.

Type:

float

from_logits

Predictions are assumed to be in logit rather than probability form.

Type:

bool

call(y_true, y_pred)

Calculate Weighted masked binary cross-entropy.

Parameters:
  • y_true (float) – True class labels. 0 < x < 1.

  • y_pred (int) – Predicted class labels. x = 0,1,2 with 0 being masked and 1,2 converted to 0,1.

Returns:

Binary cross-entropy from non-masked positions

Return type:

float

get_config()

Generate configuration dictionary for serialisation.

Generate configuration dictionary used by the Keras save_model function for serialisation.

Returns:

Configuration dictionary.

Return type:

dict

masked_accuracy(y_true, y_pred)

Zero masked binary accuracy.

A version of the binary accuracy metric with masked labels. Classes labelled 0 in y_true are masked, those labelled 1 correspond to 0 in y_pred and those labelled 2 to 1 in y_pred (i.e. offset by -1). Accepts predictions/true values as matrices.

Parameters:
  • y_true (float) – True class labels. 0 < x < 1.

  • y_pred (int) – Predicted class labels. x = 0,1,2 with 0 being masked and 1,2 converted to 0,1.

Returns:

Binary accuracy from non-masked positions

Return type:

float

masked_binary_crossentropy(y_true, y_pred)

Zero masked binary cross-entropy.

A version of the binary cross-entropy loss function with masked labels. Classes labelled 0 in y_true are masked, those labelled 1 correspond to 0 in y_pred and those labelled 2 to 1 in y_pred (i.e. offset by -1). Accepts predictions/true values as matrices.

Parameters:
  • y_true (float) – True class labels. 0 < x < 1.

  • y_pred (int) – Predicted class labels. x = 0,1,2 with 0 being masked and 1,2 converted to 0,1.

Returns:

Binary cross-entropy from non-masked positions

Return type:

float