Metrics

biapy.engine.metrics.jaccard_index_numpy(y_true, y_pred)[source]

Define Jaccard index.

Parameters:
  • y_true (N dim Numpy array) – Ground truth masks. E.g. (num_of_images, x, y, channels) for 2D images or (volume_number, z, x, y, channels) for 3D volumes.

  • y_pred (N dim Numpy array) – Predicted masks. E.g. (num_of_images, x, y, channels) for 2D images or (volume_number, z, x, y, channels) for 3D volumes.

Returns:

jac – Jaccard index value.

Return type:

float

biapy.engine.metrics.jaccard_index_numpy_without_background(y_true, y_pred)[source]

Define Jaccard index excluding the background class (first channel).

Parameters:
  • y_true (N dim Numpy array) – Ground truth masks. E.g. (num_of_images, x, y, channels) for 2D images or (volume_number, z, x, y, channels) for 3D volumes.

  • y_pred (N dim Numpy array) – Predicted masks. E.g. (num_of_images, x, y, channels) for 2D images or (volume_number, z, x, y, channels) for 3D volumes.

Returns:

jac – Jaccard index value.

Return type:

float

class biapy.engine.metrics.jaccard_index(num_classes, device, t=0.5, torchvision_models=False)[source]

Bases: object

class biapy.engine.metrics.instance_metrics(num_classes, metric_names, device, torchvision_models=False)[source]

Bases: object

class biapy.engine.metrics.CrossEntropyLoss_wrapper(num_classes, torchvision_models=False)[source]

Bases: object

biapy.engine.metrics.dice_loss(y_true, y_pred)[source]

Dice loss.

Based on image_segmentation.ipynb.

Parameters:
  • y_true (Tensor) – Ground truth masks.

  • y_pred (Tensor) – Predicted masks.

Returns:

loss – Loss value.

Return type:

Tensor

biapy.engine.metrics.bce_dice_loss(y_true, y_pred)[source]

Loss function based on the combination of BCE and Dice.

Based on image_segmentation.ipynb.

Parameters:
  • y_true (Numpy array) – Ground truth.

  • y_pred (Numpy array) – Predictions.

Returns:

loss – Loss value.

Return type:

Tensor

biapy.engine.metrics.weighted_bce_dice_loss(w_dice=0.5, w_bce=0.5)[source]

Loss function based on the combination of BCE and Dice weighted.

Inspired by https://medium.com/@Bloomore post.

Parameters:
  • w_dice (float, optional) – Weight to be applied to Dice loss.

  • w_bce (float, optional) – Weight to be applied to BCE.

Returns:

loss – Loss value.

Return type:

Tensor

biapy.engine.metrics.voc_calculation(y_true, y_pred, foreground)[source]

Calculate VOC metric value.

Parameters:
  • y_true (4D Numpy array) – Ground truth masks. E.g. (num_of_images, x, y, channels).

  • y_pred (4D Numpy array) – Predicted masks. E.g. (num_of_images, x, y, channels).

  • foreground (float) – Foreground Jaccard index score.

Returns:

voc – VOC score value.

Return type:

float

biapy.engine.metrics.binary_crossentropy_weighted(weights)[source]

Custom binary cross entropy loss. The weights are used to multiply the results of the usual cross-entropy loss in order to give more weight to areas between cells close to one another.

Based on unet_weights.py.

Parameters:

weights (float) – Weigth to multiply the BCE value by.

Returns:

loss – Loss value.

Return type:

Tensor

class biapy.engine.metrics.instance_segmentation_loss(weights=(1, 0.2), out_channels='BC', mask_distance_channel=True, n_classes=2)[source]

Bases: object

biapy.engine.metrics.detection_metrics(true, pred, tolerance=10, voxel_size=(1, 1, 1), return_assoc=False, verbose=False)[source]

Calculate detection metrics based on

Parameters:
  • true (List of list) – List containing coordinates of ground truth points. E.g. [[5,3,2], [4,6,7]].

  • pred (4D Tensor) – List containing coordinates of predicted points. E.g. [[5,3,2], [4,6,7]].

  • tolerance (optional, int) – Maximum distance far away from a GT point to consider a point as a true positive.

  • voxel_size (List of floats) – Weights to be multiply by each axis. Useful when dealing with anysotropic data to reduce the distance value on the axis with less resolution. E.g. (1,1,0.5).

  • return_assoc (bool, optional) – To return two dataframes containing the gt points association and the FP.

  • verbose (bool, optional) – To print extra information.

Returns:

metrics – List containing precision, accuracy and F1 between the predicted points and ground truth.

Return type:

List of strings

class biapy.engine.metrics.dfcan_loss(device, max_val=1)[source]

Bases: Module

forward(input, target)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

biapy.engine.metrics.n2v_loss_mse(y_pred, y_true)[source]
biapy.engine.metrics.MaskedAutoencoderViT_loss(y_pred, y_true, model)[source]

imgs: [N, 3, H, W] pred: [N, L, p*p*3] mask: [N, L], 0 is keep, 1 is remove,

class biapy.engine.metrics.L1_wrapper[source]

Bases: object

class biapy.engine.metrics.MSE_wrapper[source]

Bases: object

class biapy.engine.metrics.SSIM_wrapper[source]

Bases: object