Closed saisusmitha closed 1 year ago
Have you solved this problem?
Hi I took all metrics (dice, hd95, jaccard) from the medpy module: https://loli.github.io/medpy/_modules/medpy/metric/binary.html
To generate the uncertainty maps, you need to store all predicted segmentation masks of your ensemble in a list: Ensemble=[segmentation_prediction_1, ...., segmentation_prediction_n], where n is the number of samples in your ensemble. Then you run the following code:
for i in range(len(Ensemble)):
prediction = torch.where(Ensemble[i] > 0.5, 1, 0).float() # a binary mask is obtained via thresholding
score = dice_score(prediction[0,...].cpu(), ground_truth_segmentation.cpu()) # we compute the dice scores for all samples separately
print('Dice score of sample' + str(i), score)
E = torch.where(torch.cat(Ensemble) > 0.5, 1, 0).float()
var = torch.var(E, dim=0) # pixel-wise variance map over the ensemble
mean = torch.mean(E, dim=0) # pixel-wise mean map over the ensemble
mean_prediction = torch.where(mean > 0.5, 1, 0).float()
mean_score = dice_score(mean_prediction.cpu(), ground_truth_segmentation.cpu()) # Here we predict the Dice score for the mean map
print('Dice score on the mean map', mean_score)
plt.style.use("default")
plt.imshow(mean[0, ...].cpu(), vmin=0, vmax=1, cmap="gray") # We plot the mean map
plt.tight_layout()
plt.axis("off")
plt.show()
plt.style.use("default")
plt.imshow(var[0, ...].cpu(), vmin=0, vmax=1, cmap="jet") # We plot the variance map
plt.tight_layout()
plt.axis("off")
plt.show()
Hi I took all metrics (dice, hd95, jaccard) from the medpy module: https://loli.github.io/medpy/_modules/medpy/metric/binary.html
To generate the uncertainty maps, you need to store all predicted segmentation masks of your ensemble in a list: Ensemble=[segmentation_prediction_1, ...., segmentation_prediction_n], where n is the number of samples in your ensemble. Then you run the following code:
for i in range(len(Ensemble)): prediction = torch.where(Ensemble[i] > 0.5, 1, 0).float() # a binary mask is obtained via thresholding score = dice_score(prediction[0,...].cpu(), ground_truth_segmentation.cpu()) # we compute the dice scores for all samples separately print('Dice score of sample' + str(i), score) E = torch.where(torch.cat(Ensemble) > 0.5, 1, 0).float() var = torch.var(E, dim=0) # pixel-wise variance map over the ensemble mean = torch.mean(E, dim=0) # pixel-wise mean map over the ensemble mean_prediction = torch.where(mean > 0.5, 1, 0).float() mean_score = dice_score(mean_prediction.cpu(), ground_truth_segmentation.cpu()) # Here we predict the Dice score for the mean map print('Dice score on the mean map', mean_score) plt.style.use("default") plt.imshow(mean[0, ...].cpu(), vmin=0, vmax=1, cmap="gray") # We plot the mean map plt.tight_layout() plt.axis("off") plt.show() plt.style.use("default") plt.imshow(var[0, ...].cpu(), vmin=0, vmax=1, cmap="jet") # We plot the variance map plt.tight_layout() plt.axis("off") plt.show()
Thanks for your generous help. I think you can also push it to your GitHub repository for easy learning for beginners, FYI.
@jaceqin Hi. did you write the exact code for finding scores? if yes kindly share
@JuliaWolleb Can you tell me where to add codes for all metrics (dice, hd95, Jaccard) from the medpy module. If they are already written in the codes kindly let me know where to check and if they does not exist in the given repository mention where and how to add these metric codes.
#Copyright (C) 2013 Oskar Maier
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# author Oskar Maier
# version r0.1.1
# since 2014-03-13
# status Release
# build-in modules
# third-party modules
import numpy
from scipy.ndimage import _ni_support
from scipy.ndimage.morphology import distance_transform_edt, binary_erosion, \
generate_binary_structure
from scipy.ndimage.measurements import label, find_objects
from scipy.stats import pearsonr
import SimpleITK as sitk
import scipy.spatial
# own modules
# code
def dc(result, reference):
r"""
Dice coefficient
Computes the Dice coefficient (also known as Sorensen index) between the binary
objects in two images.
The metric is defined as
.. math::
DC=\frac{2|A\cap B|}{|A|+|B|}
, where :math:`A` is the first and :math:`B` the second set of samples (here: binary objects).
Parameters
----------
result : array_like
Input data containing objects. Can be any type but will be converted
into binary: background where 0, object everywhere else.
reference : array_like
Input data containing objects. Can be any type but will be converted
into binary: background where 0, object everywhere else.
Returns
-------
dc : float
The Dice coefficient between the object(s) in ```result``` and the
object(s) in ```reference```. It ranges from 0 (no overlap) to 1 (perfect overlap).
Notes
-----
This is a real metric. The binary images can therefore be supplied in any order.
"""
result = numpy.atleast_1d(result.astype(numpy.bool))
reference = numpy.atleast_1d(reference.astype(numpy.bool))
intersection = numpy.count_nonzero(result & reference)
size_i1 = numpy.count_nonzero(result)
size_i2 = numpy.count_nonzero(reference)
try:
dc = 2. * intersection / float(size_i1 + size_i2)
except ZeroDivisionError:
dc = 0.0
return dc
def jc(result, reference):
"""
Jaccard coefficient
Computes the Jaccard coefficient between the binary objects in two images.
Parameters
----------
result: array_like
Input data containing objects. Can be any type but will be converted
into binary: background where 0, object everywhere else.
reference: array_like
Input data containing objects. Can be any type but will be converted
into binary: background where 0, object everywhere else.
Returns
-------
jc: float
The Jaccard coefficient between the object(s) in `result` and the
object(s) in `reference`. It ranges from 0 (no overlap) to 1 (perfect overlap).
Notes
-----
This is a real metric. The binary images can therefore be supplied in any order.
"""
result = numpy.atleast_1d(result.astype(numpy.bool))
reference = numpy.atleast_1d(reference.astype(numpy.bool))
intersection = numpy.count_nonzero(result & reference)
union = numpy.count_nonzero(result | reference)
jc = float(intersection) / float(union)
return jc
def precision(result, reference):
"""
Precison.
Parameters
----------
result : array_like
Input data containing objects. Can be any type but will be converted
into binary: background where 0, object everywhere else.
reference : array_like
Input data containing objects. Can be any type but will be converted
into binary: background where 0, object everywhere else.
Returns
-------
precision : float
The precision between two binary datasets, here mostly binary objects in images,
which is defined as the fraction of retrieved instances that are relevant. The
precision is not symmetric.
See also
--------
:func:`recall`
Notes
-----
Not symmetric. The inverse of the precision is :func:`recall`.
High precision means that an algorithm returned substantially more relevant results than irrelevant.
References
----------
.. [1] http://en.wikipedia.org/wiki/Precision_and_recall
.. [2] http://en.wikipedia.org/wiki/Confusion_matrix#Table_of_confusion
"""
result = numpy.atleast_1d(result.astype(numpy.bool))
reference = numpy.atleast_1d(reference.astype(numpy.bool))
tp = numpy.count_nonzero(result & reference)
fp = numpy.count_nonzero(result & ~reference)
try:
precision = tp / float(tp + fp)
except ZeroDivisionError:
precision = 0.0
return precision
def recall(result, reference):
"""
Recall.
Parameters
----------
result : array_like
Input data containing objects. Can be any type but will be converted
into binary: background where 0, object everywhere else.
reference : array_like
Input data containing objects. Can be any type but will be converted
into binary: background where 0, object everywhere else.
Returns
-------
recall : float
The recall between two binary datasets, here mostly binary objects in images,
which is defined as the fraction of relevant instances that are retrieved. The
recall is not symmetric.
See also
--------
:func:`precision`
Notes
-----
Not symmetric. The inverse of the recall is :func:`precision`.
High recall means that an algorithm returned most of the relevant results.
References
----------
.. [1] http://en.wikipedia.org/wiki/Precision_and_recall
.. [2] http://en.wikipedia.org/wiki/Confusion_matrix#Table_of_confusion
"""
result = numpy.atleast_1d(result.astype(numpy.bool))
reference = numpy.atleast_1d(reference.astype(numpy.bool))
tp = numpy.count_nonzero(result & reference)
fn = numpy.count_nonzero(~result & reference)
try:
recall = tp / float(tp + fn)
except ZeroDivisionError:
recall = 0.0
return recall
def sensitivity(result, reference):
"""
Sensitivity.
Same as :func:`recall`, see there for a detailed description.
See also
--------
:func:`specificity`
"""
return recall(result, reference)
def specificity(result, reference):
"""
Specificity.
Parameters
----------
result : array_like
Input data containing objects. Can be any type but will be converted
into binary: background where 0, object everywhere else.
reference : array_like
Input data containing objects. Can be any type but will be converted
into binary: background where 0, object everywhere else.
Returns
-------
specificity : float
The specificity between two binary datasets, here mostly binary objects in images,
which denotes the fraction of correctly returned negatives. The
specificity is not symmetric.
See also
--------
:func:`sensitivity`
Notes
-----
Not symmetric. The completment of the specificity is :func:`sensitivity`.
High recall means that an algorithm returned most of the irrelevant results.
References
----------
.. [1] https://en.wikipedia.org/wiki/Sensitivity_and_specificity
.. [2] http://en.wikipedia.org/wiki/Confusion_matrix#Table_of_confusion
"""
result = numpy.atleast_1d(result.astype(numpy.bool))
reference = numpy.atleast_1d(reference.astype(numpy.bool))
tn = numpy.count_nonzero(~result & ~reference)
fp = numpy.count_nonzero(result & ~reference)
try:
specificity = tn / float(tn + fp)
except ZeroDivisionError:
specificity = 0.0
return specificity
def true_negative_rate(result, reference):
"""
True negative rate.
Same as :func:`specificity`, see there for a detailed description.
See also
--------
:func:`true_positive_rate`
:func:`positive_predictive_value`
"""
return specificity(result, reference)
def true_positive_rate(result, reference):
"""
True positive rate.
Same as :func:`recall` and :func:`sensitivity`, see there for a detailed description.
See also
--------
:func:`positive_predictive_value`
:func:`true_negative_rate`
"""
return recall(result, reference)
def positive_predictive_value(result, reference):
"""
Positive predictive value.
Same as :func:`precision`, see there for a detailed description.
See also
--------
:func:`true_positive_rate`
:func:`true_negative_rate`
"""
return precision(result, reference)
def hd1(result, reference, voxelspacing=None, connectivity=1):
"""
Hausdorff Distance.
Computes the (symmetric) Hausdorff Distance (HD) between the binary objects in two
images. It is defined as the maximum surface distance between the objects.
Parameters
----------
result : array_like
Input data containing objects. Can be any type but will be converted
into binary: background where 0, object everywhere else.
reference : array_like
Input data containing objects. Can be any type but will be converted
into binary: background where 0, object everywhere else.
voxelspacing : float or sequence of floats, optional
The voxelspacing in a distance unit i.e. spacing of elements
along each dimension. If a sequence, must be of length equal to
the input rank; if a single number, this is used for all axes. If
not specified, a grid spacing of unity is implied.
connectivity : int
The neighbourhood/connectivity considered when determining the surface
of the binary objects. This value is passed to
`scipy.ndimage.morphology.generate_binary_structure` and should usually be :math:`> 1`.
Note that the connectivity influences the result in the case of the Hausdorff distance.
Returns
-------
hd : float
The symmetric Hausdorff Distance between the object(s) in ```result``` and the
object(s) in ```reference```. The distance unit is the same as for the spacing of
elements along each dimension, which is usually given in mm.
See also
--------
:func:`assd`
:func:`asd`
Notes
-----
This is a real metric. The binary images can therefore be supplied in any order.
"""
hd1 = __surface_distances(result, reference, voxelspacing, connectivity).max()
hd2 = __surface_distances(reference, result, voxelspacing, connectivity).max()
hd = max(hd1, hd2)
return hd
def __distinct_binary_object_correspondences(reference, result, connectivity=1):
"""
Determines all distinct (where connectivity is defined by the connectivity parameter
passed to scipy's `generate_binary_structure`) binary objects in both of the input
parameters and returns a 1to1 mapping from the labelled objects in reference to the
corresponding (whereas a one-voxel overlap suffices for correspondence) objects in
result.
All stems from the problem, that the relationship is non-surjective many-to-many.
@return (labelmap1, labelmap2, n_lables1, n_labels2, labelmapping2to1)
"""
result = numpy.atleast_1d(result.astype(numpy.bool))
reference = numpy.atleast_1d(reference.astype(numpy.bool))
# binary structure
footprint = generate_binary_structure(result.ndim, connectivity)
# label distinct binary objects
labelmap1, n_obj_result = label(result, footprint)
labelmap2, n_obj_reference = label(reference, footprint)
# find all overlaps from labelmap2 to labelmap1; collect one-to-one relationships and store all one-two-many for later processing
slicers = find_objects(labelmap2) # get windows of labelled objects
mapping = dict() # mappings from labels in labelmap2 to corresponding object labels in labelmap1
used_labels = set() # set to collect all already used labels from labelmap2
one_to_many = list() # list to collect all one-to-many mappings
for l1id, slicer in enumerate(slicers): # iterate over object in labelmap2 and their windows
l1id += 1 # labelled objects have ids sarting from 1
bobj = (l1id) == labelmap2[slicer] # find binary object corresponding to the label1 id in the segmentation
l2ids = numpy.unique(labelmap1[slicer][
bobj]) # extract all unique object identifiers at the corresponding positions in the reference (i.e. the mapping)
l2ids = l2ids[0 != l2ids] # remove background identifiers (=0)
if 1 == len(
l2ids): # one-to-one mapping: if target label not already used, add to final list of object-to-object mappings and mark target label as used
l2id = l2ids[0]
if not l2id in used_labels:
mapping[l1id] = l2id
used_labels.add(l2id)
elif 1 < len(l2ids): # one-to-many mapping: store relationship for later processing
one_to_many.append((l1id, set(l2ids)))
# process one-to-many mappings, always choosing the one with the least labelmap2 correspondences first
while True:
one_to_many = [(l1id, l2ids - used_labels) for l1id, l2ids in
one_to_many] # remove already used ids from all sets
one_to_many = [x for x in one_to_many if x[1]] # remove empty sets
one_to_many = sorted(one_to_many, key=lambda x: len(x[1])) # sort by set length
if 0 == len(one_to_many):
break
l2id = one_to_many[0][1].pop() # select an arbitrary target label id from the shortest set
mapping[one_to_many[0][0]] = l2id # add to one-to-one mappings
used_labels.add(l2id) # mark target label as used
one_to_many = one_to_many[1:] # delete the processed set from all sets
return labelmap1, labelmap2, n_obj_result, n_obj_reference, mapping
def __surface_distances(result, reference, voxelspacing=None, connectivity=1):
"""
The distances between the surface voxel of binary objects in result and their
nearest partner surface voxel of a binary object in reference.
"""
result = numpy.atleast_1d(result.astype(numpy.bool))
reference = numpy.atleast_1d(reference.astype(numpy.bool))
if voxelspacing is not None:
voxelspacing = _ni_support._normalize_sequence(voxelspacing, result.ndim)
voxelspacing = numpy.asarray(voxelspacing, dtype=numpy.float64)
if not voxelspacing.flags.contiguous:
voxelspacing = voxelspacing.copy()
# binary structure
footprint = generate_binary_structure(result.ndim, connectivity)
# test for emptiness
if 0 == numpy.count_nonzero(result):
raise RuntimeError('The first supplied array does not contain any binary object.')
if 0 == numpy.count_nonzero(reference):
raise RuntimeError('The second supplied array does not contain any binary object.')
# extract only 1-pixel border line of objects
result_border = result ^ binary_erosion(result, structure=footprint, iterations=1)
reference_border = reference ^ binary_erosion(reference, structure=footprint, iterations=1)
# compute average surface distance
# Note: scipys distance transform is calculated only inside the borders of the
# foreground objects, therefore the input has to be reversed
dt = distance_transform_edt(~reference_border, sampling=voxelspacing)
sds = dt[result_border]
return sds
def __combine_windows(w1, w2):
"""
Joins two windows (defined by tuple of slices) such that their maximum
combined extend is covered by the new returned window.
"""
res = []
for s1, s2 in zip(w1, w2):
res.append(slice(min(s1.start, s2.start), max(s1.stop, s2.stop)))
return tuple(res)
def hd95(result, reference, voxelspacing=None, connectivity=1):
"""
95th percentile of the Hausdorff Distance.
Computes the 95th percentile of the (symmetric) Hausdorff Distance (HD) between the binary objects in two
images. Compared to the Hausdorff Distance, this metric is slightly more stable to small outliers and is
commonly used in Biomedical Segmentation challenges.
Parameters
----------
result : array_like
Input data containing objects. Can be any type but will be converted
into binary: background where 0, object everywhere else.
reference : array_like
Input data containing objects. Can be any type but will be converted
into binary: background where 0, object everywhere else.
voxelspacing : float or sequence of floats, optional
The voxelspacing in a distance unit i.e. spacing of elements
along each dimension. If a sequence, must be of length equal to
the input rank; if a single number, this is used for all axes. If
not specified, a grid spacing of unity is implied.
connectivity : int
The neighbourhood/connectivity considered when determining the surface
of the binary objects. This value is passed to
`scipy.ndimage.morphology.generate_binary_structure` and should usually be :math:`> 1`.
Note that the connectivity influences the result in the case of the Hausdorff distance.
Returns
-------
hd : float
The symmetric Hausdorff Distance between the object(s) in ```result``` and the
object(s) in ```reference```. The distance unit is the same as for the spacing of
elements along each dimension, which is usually given in mm.
See also
--------
:func:`hd`
Notes
-----
This is a real metric. The binary images can therefore be supplied in any order.
"""
hd1 = __surface_distances(result, reference, voxelspacing, connectivity)
hd2 = __surface_distances(reference, result, voxelspacing, connectivity)
hd95 = numpy.percentile(numpy.hstack((hd1, hd2)), 95)
return hd95
def getHausdorff(testImage, resultImage):
"""Compute the Hausdorff distance."""
# Hausdorff distance is only defined when something is detected
resultStatistics = sitk.StatisticsImageFilter()
resultStatistics.Execute(resultImage)
if resultStatistics.GetSum() == 0:
return float('nan')
# Edge detection is done by ORIGINAL - ERODED, keeping the outer boundaries of lesions. Erosion is performed in 2D
eTestImage = sitk.BinaryErode(testImage, (1, 1, 0))
eResultImage = sitk.BinaryErode(resultImage, (1, 1, 0))
hTestImage = sitk.Subtract(testImage, eTestImage)
hResultImage = sitk.Subtract(resultImage, eResultImage)
hTestArray = sitk.GetArrayFromImage(hTestImage)
hResultArray = sitk.GetArrayFromImage(hResultImage)
# Convert voxel location to world coordinates. Use the coordinate system of the test image
# np.nonzero = elements of the boundary in numpy order (zyx)
# np.flipud = elements in xyz order
# np.transpose = create tuples (x,y,z)
# testImage.TransformIndexToPhysicalPoint converts (xyz) to world coordinates (in mm)
testCoordinates = [testImage.TransformIndexToPhysicalPoint(x.tolist()) for x in
np.transpose(np.flipud(np.nonzero(hTestArray)))]
resultCoordinates = [testImage.TransformIndexToPhysicalPoint(x.tolist()) for x in
np.transpose(np.flipud(np.nonzero(hResultArray)))]
# Use a kd-tree for fast spatial search
def getDistancesFromAtoB(a, b):
kdTree = scipy.spatial.KDTree(a, leafsize=100)
return kdTree.query(b, k=1, eps=0, p=2)[0]
# Compute distances from test to result; and result to test
dTestToResult = getDistancesFromAtoB(testCoordinates, resultCoordinates)
dResultToTest = getDistancesFromAtoB(resultCoordinates, testCoordinates)
return max(np.percentile(dTestToResult, 95), np.percentile(dResultToTest, 95))
These are the metrics as described in the medpy module. Then you just import the ones you need (dc, jc, hd95), and compute the scores for the whole ensemble, as suggested in my code snippet above (https://github.com/JuliaWolleb/Diffusion-based-Segmentation/issues/26#issuecomment-1473911951)
@JuliaWolleb Thanks for the above codes. My other question is we have training and testing datasets -training folder has GT and the testing/validation folder doesn't have GT[ data extracted from https://www.med.upenn.edu/cbica/brats2020/data.html ] - so if we include these metric codes in the segmentation_sample.py [as suggested in your code snippet above] this python code works on testing data where there is no GT to run these metric codes - and also we use complete training data folder[data/training] for segmentation_train.py - In general, the scores are evaluated on validation dataset so I wanted where to include these metrics code and run on what dataset?[ did you split the data folders? ] and did you have a validation loop in segmentation_train.py for finding these metrics? if you have such a code kindly explain. Excuse me if I am understanding something wrong.
I split the training data of the BRATS dataset into training/testing. So we have GT labels also for testing. I did not include a validation loop in segmentation_train.py.
@JuliaWolleb for hd95 score we need the __surface_distances(result, reference, voxelspacing=None, connectivity=1) function and it has lines line checking result and reference for emptiness - so are these lines strictly required? is it not allowed for any sample to be or GT to be empty? for various datasets the model might output empty sample so what to do in that case? how to find hd95 score?
lines of code in __surface_distances
if 0 == numpy.count_nonzero(result):
raise RuntimeError('The first supplied array does not contain any binary object.')
if 0 == numpy.count_nonzero(reference):
raise RuntimeError('The second supplied array does not contain any binary object.')
yes, you can compute the hd95 score only for non-empty images. As described in the paper (Section 4, Table 1), we only compute the hd95 score for non-empty predictions.
Hello, where exactly is the code for generating uncertainty map and code for the scores [ HD95, Jaccard Index ] you used.