# Dice score calculation of 3D binary masks using Segment Comparison module

**URL:** <https://discourse.slicer.org/t/dice-score-calculation-of-3d-binary-masks-using-segment-comparison-module/30124>\
**Category:** Support\
**Tags:** segmentation\
**Created:** [June 19, 2023, 3:44pm UTC](https://discourse.slicer.org/t/dice-score-calculation-of-3d-binary-masks-using-segment-comparison-module/30124 "2023-06-19T15:44:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thanuja](https://avatars.discourse-cdn.com/v4/letter/t/b487fb/32.png) [@thanuja](https://discourse.slicer.org/u/thanuja)\
**Post date:** [June 19, 2023, 3:44pm UTC](https://discourse.slicer.org/t/dice-score-calculation-of-3d-binary-masks-using-segment-comparison-module/30124/1 "2023-06-19T15:44:23Z")

</div>

Hi All,

Could you please point out where can I find the source code for Segment Comparison module (Slicer version 5.2.2). I’m getting different values of dice score for 3D binary nifti masks when using 3D Slicer and the Monai framework. This is the monai function I’m using:

```auto
def calculate_dice_score(predicted, ground_truth):
      dice_metric = monai.metrics.DiceMetric(include_background=False, reduction="mean")
      # Convert the predicted and ground truth masks to PyTorch tensors
      predicted_tensor = torch.from_numpy(predicted)
      ground_truth_tensor = torch.from_numpy(ground_truth)
      # Reshape tensors to have batch size and number of channels
      predicted_tensor = predicted_tensor.unsqueeze(0).unsqueeze(0)
      ground_truth_tensor = ground_truth_tensor.unsqueeze(0).unsqueeze(0)
      # Calculate the Dice score
      dice_score = dice_metric(y_pred =predicted_tensor, y = ground_truth_tensor)
      return dice_score.item()

```

Please note that I have tried both mean and sum for reduction method and still getting different values from Slicer and Monai .

Thank you!

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [June 20, 2023, 12:58pm UTC](https://discourse.slicer.org/t/dice-score-calculation-of-3d-binary-masks-using-segment-comparison-module/30124/2 "2023-06-20T12:58:30Z")

</div>

This is the source code of the module:

> **[SlicerRT/SegmentComparison at master · SlicerRt/SlicerRT](https://github.com/SlicerRt/SlicerRT/tree/master/SegmentComparison)**
>
> master/SegmentComparison

The difference may be due to the resizing that a MONAI transform does, that’s where I’d look first.

---

<div class="post-metadata">

**Author:** ![thanuja](https://avatars.discourse-cdn.com/v4/letter/t/b487fb/32.png) [@thanuja](https://discourse.slicer.org/u/thanuja)\
**Post date:** [June 20, 2023, 5:05pm UTC](https://discourse.slicer.org/t/dice-score-calculation-of-3d-binary-masks-using-segment-comparison-module/30124/3 "2023-06-20T17:05:30Z")

</div>

Thank you for sharing the code. I’m getting same dice score value as monai for below python code as well:

```auto
# Calculate dice score of masks
def calculate_dice_score(predicted, ground_truth):
    area_sum = np.sum(predicted == 1) + np.sum(ground_truth == 1)
    if area_sum > 0:
        return np.sum(predicted[ground_truth == 1]) * 2.0 / area_sum
    else:
        return 1.0 # Will return 1.0 if both predicted and ground truth masks are empty

```

Will check the slicer code. Thanks again.

---

<div class="post-metadata">

**Author:** ![thanuja](https://avatars.discourse-cdn.com/v4/letter/t/b487fb/32.png) [@thanuja](https://discourse.slicer.org/u/thanuja)\
**Post date:** [August 29, 2023, 7:59pm UTC](https://discourse.slicer.org/t/dice-score-calculation-of-3d-binary-masks-using-segment-comparison-module/30124/4 "2023-08-29T19:59:46Z")

</div>

I’m getting the same value with Segment Comaprison module as with manual calculation. Sorry for the trouble. Thank you!
