Multiclass Segmentation Hausdorff Distance

Hi everyone,

I wanted to address a problem I’ve been facing for the past week. I am currently working on a U-Net multiclass segmentation model, which works fine. The input is a (192, 192, 1) MRI image of a rat’s brain, and the output is a mask of size (192, 192, 3) with the three classes being: class 0 for background, class 1 for brain, and class 2 for lesion.

The metric I have been using is the Dice score. However, I wanted to add the Hausdorff distance, but so far, I haven’t been successful. Below is the code I’ve been using:

python

Copiar código

import numpy as np
from scipy.spatial.distance import cdist

def hausdorff_distance(set1, set2):
    """
    Calculates the Hausdorff distance between two sets of points.
    """
    # Calculate the distances between each point in set1 and all points in set2
    dist1 = cdist(set1, set2, metric='euclidean')
    dist2 = cdist(set2, set1, metric='euclidean')
    
    # For each set, take the minimum distance to the other set
    max_dist1 = np.max(np.min(dist1, axis=1))  # Maximum distance from each point in set1 to set2
    max_dist2 = np.max(np.min(dist2, axis=1))  # Maximum distance from each point in set2 to set1
    
    # The Hausdorff distance is the maximum of these two distances
    return max(max_dist1, max_dist2)

def average_hausdorff_distance(y_true, y_pred, num_classes):
    """
    Calculates the Average Hausdorff Distance for multiclass segmentations.
    `y_true` and `y_pred` are the ground truth and predicted segmentation masks.
    `num_classes` is the total number of classes in the segmentation.
    """
    hausdorff_distances = []
    
    # Iterate over each class
    for class_id in range(num_classes):
        # For each class, get the pixels of that class in the ground truth and predicted masks
        true_class_points = np.argwhere(y_true[:, :, class_id] == 1)  # Class 1
        pred_class_points = np.argwhere(y_pred[:, :, class_id] == 1)  # Class 1
        
        if len(true_class_points) > 0 and len(pred_class_points) > 0:
            hd = hausdorff_distance(true_class_points, pred_class_points)
            hausdorff_distances.append(hd)
        else:
            hausdorff_distances.append(0)  # If no points, append 0.
    
    # Check if there are valid distances
    if len(hausdorff_distances) == 0:
        return 0.0  # If no distances can be calculated, return 0.
    
    return np.mean(hausdorff_distances)

No matter the modifications or setups I have tried, the error remains the same: ValueError: None values not supported.

Any ideas? I couldn’t find any specific information online.

Thank you in advance

Hi @Sara_Ortega_Espina, I have tried to execute the above function on random data it works without any error. Could you please check the data you are passing contains any Nan values in it. Thank You.