Skip to content

Instantly share code, notes, and snippets.

View farhanone's full-sized avatar

Farhan Rasheed farhanone

  • Linköping University WASP
  • Sweden
View GitHub Profile
@ilmonteux
ilmonteux / segmentation_metrics.py
Last active January 18, 2023 22:41
Semantic segmentation metrics in Keras and Numpy. IoU, Dice in both soft and hard variants. Mean metrics for multiclass prediction. See https://ilmonteux.github.io/2019/05/10/segmentation-metrics.html for discussion
import numpy as np
import keras.backend as K
import tensorflow as tf
def metrics_np(y_true, y_pred, metric_name, metric_type='standard', drop_last = True, mean_per_class=False, verbose=False):
"""
Compute mean metrics of two segmentation masks, via numpy.
IoU(A,B) = |A & B| / (| A U B|)
Dice(A,B) = 2*|A & B| / (|A| + |B|)