Skip to content

_dice_score

cast

Cast y_true, epsilon to desired data type.

Parameters:

Name Type Description Default
y_true

Ground truth class labels with a shape like (Batch, C, H, W) for torch and (Batch, H, W, C) for tensorflow or numpy. dtype: int or float32 or float16.

required
epsilon

Floating point value to avoid divide by zero error.

required
dtype

Datatype to which the y_true and epsilon should be converted to.

required

Returns:

Type Description

Converted y_true and epsilon values.

Raises:

Type Description
AssertionError

If y_true are unacceptable data types. if data type is other than np.array, tensor.Tensor, tf.Tensor.

Source code in fastestimator\fastestimator\backend\_dice_score.py
def cast(y_true, epsilon, dtype):
    """
        Cast y_true, epsilon to desired data type.

        Args:
            y_true: Ground truth class labels with a shape like (Batch, C, H, W) for torch and (Batch, H, W, C) for tensorflow or numpy. dtype: int or float32 or float16.
            epsilon: Floating point value to avoid divide by zero error.
            dtype: Datatype to which the y_true and epsilon should be converted to.

        Returns:
            Converted y_true and epsilon values.

        Raises:
            AssertionError: If `y_true` are unacceptable data types. if data type is other than np.array, tensor.Tensor, tf.Tensor.
    """
    if dtype not in allowed_data_types:
        raise ValueError("Provided datatype {} is not supported, only {} data types are supported".format(
            dtype, allowed_data_types))

    if tf.is_tensor(y_true):
        return tf.cast(y_true, dtype), tf.cast(epsilon, dtype)
    elif isinstance(y_true, torch.Tensor):
        return y_true.type(dtype), torch.tensor(epsilon).type(dtype)
    elif isinstance(y_true, np.ndarray):
        return np.array(y_true, dtype=dtype), np.array(epsilon, dtype=dtype)
    else:
        raise ValueError("Unsupported tensor type.")

dice_score

Compute Dice score.

This method can be used with Numpy data:

true = np.array([[[[0, 1, 1], [1, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [1, 0, 1]]]])
pred = np.array([[[[0, 1, 0], [1, 0, 0], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [0, 0, 0]], [[0, 0, 1], [1, 0, 1], [1, 0, 1]]]])
b = fe.backend.dice_score(y_pred=pred, y_true=true)  # 0.161
b = fe.backend.dice_score(y_pred=pred, y_true=true, soft_dice=True)  # 0.161
b = fe.backend.dice_score(y_pred=pred, y_true=true, channel_average=True)  # 0.1636
b = fe.backend.dice_score(y_pred=pred, y_true=true, sample_average=True)  # 0.161

This method can be used with TensorFlow tensors:
```python
true = tf.constant([[[[0, 1, 1], [1, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [1, 0, 1]]]])
pred = tf.constant([[[[0, 1, 0], [1, 0, 0], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [0, 0, 0]], [[0, 0, 1], [1, 0, 1], [1, 0, 1]]]])
b = fe.backend.dice_score(y_pred=pred, y_true=true)  # 0.161
b = fe.backend.dice_score(y_pred=pred, y_true=true, soft_dice=True)  # 0.161
b = fe.backend.dice_score(y_pred=pred, y_true=true, channel_average=True)  # 0.1636
b = fe.backend.dice_score(y_pred=pred, y_true=true, sample_average=True)  # 0.161

This method can be used with PyTorch tensors:

true = torch.tensor([[[[0, 1, 1], [1, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [1, 0, 1]]]])
pred = torch.tensor([[[[0, 1, 0], [1, 0, 0], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [0, 0, 0]], [[0, 0, 1], [1, 0, 1], [1, 0, 1]]]])
b = fe.backend.dice_score(y_pred=pred, y_true=true)  # 0.161
b = fe.backend.dice_score(y_pred=pred, y_true=true, soft_dice=True)  # 0.161
b = fe.backend.dice_score(y_pred=pred, y_true=true, channel_average=True)  # 0.1636
b = fe.backend.dice_score(y_pred=pred, y_true=true, sample_average=True)  # 0.161

```

Parameters:

Name Type Description Default
y_pred Tensor

Prediction with a shape like (Batch, C, H, W) for torch and (Batch, H, W, C) for tensorflow or numpy. dtype: float32 or float16.

required
y_true Tensor

Ground truth class labels with a shape like y_pred. dtype: int or float32 or float16.

required
soft_dice bool

Whether to square elements. If True, square of elements is added.

False
sample_average bool

Whether to average the element-wise dice score.

False
channel_average bool

Whether to average the channel wise dice score.

False
epsilon float

floating point value to avoid divide by zero error.

1e-06

Returns:

Type Description
Tensor

The dice score between y_pred and y_true. A scalar if average_sample is True, else a tensor with the shape (Batch).

Raises:

Type Description
AssertionError

If y_true or y_pred are unacceptable data types. if data type is other than np.array, tensor.Tensor, tf.Tensor.

Source code in fastestimator\fastestimator\backend\_dice_score.py
def dice_score(y_pred: Tensor,
               y_true: Tensor,
               soft_dice: bool = False,
               sample_average: bool = False,
               channel_average: bool = False,
               epsilon: float = 1e-6) -> Tensor:
    """
    Compute Dice score.

    This method can be used with Numpy data:
    ```python
    true = np.array([[[[0, 1, 1], [1, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [1, 0, 1]]]])
    pred = np.array([[[[0, 1, 0], [1, 0, 0], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [0, 0, 0]], [[0, 0, 1], [1, 0, 1], [1, 0, 1]]]])
    b = fe.backend.dice_score(y_pred=pred, y_true=true)  # 0.161
    b = fe.backend.dice_score(y_pred=pred, y_true=true, soft_dice=True)  # 0.161
    b = fe.backend.dice_score(y_pred=pred, y_true=true, channel_average=True)  # 0.1636
    b = fe.backend.dice_score(y_pred=pred, y_true=true, sample_average=True)  # 0.161

    This method can be used with TensorFlow tensors:
    ```python
    true = tf.constant([[[[0, 1, 1], [1, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [1, 0, 1]]]])
    pred = tf.constant([[[[0, 1, 0], [1, 0, 0], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [0, 0, 0]], [[0, 0, 1], [1, 0, 1], [1, 0, 1]]]])
    b = fe.backend.dice_score(y_pred=pred, y_true=true)  # 0.161
    b = fe.backend.dice_score(y_pred=pred, y_true=true, soft_dice=True)  # 0.161
    b = fe.backend.dice_score(y_pred=pred, y_true=true, channel_average=True)  # 0.1636
    b = fe.backend.dice_score(y_pred=pred, y_true=true, sample_average=True)  # 0.161
    ```

    This method can be used with PyTorch tensors:
    ```python
    true = torch.tensor([[[[0, 1, 1], [1, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [1, 0, 1]]]])
    pred = torch.tensor([[[[0, 1, 0], [1, 0, 0], [1, 0, 1]], [[0, 1, 1], [1, 0, 1], [0, 0, 0]], [[0, 0, 1], [1, 0, 1], [1, 0, 1]]]])
    b = fe.backend.dice_score(y_pred=pred, y_true=true)  # 0.161
    b = fe.backend.dice_score(y_pred=pred, y_true=true, soft_dice=True)  # 0.161
    b = fe.backend.dice_score(y_pred=pred, y_true=true, channel_average=True)  # 0.1636
    b = fe.backend.dice_score(y_pred=pred, y_true=true, sample_average=True)  # 0.161
    ```

    ```
    Args:
        y_pred: Prediction with a shape like (Batch, C, H, W) for torch and (Batch, H, W, C) for tensorflow or numpy. dtype: float32 or float16.
        y_true: Ground truth class labels with a shape like `y_pred`. dtype: int or float32 or float16.
        soft_dice: Whether to square elements. If True, square of elements is added.
        sample_average: Whether to average the element-wise dice score.
        channel_average: Whether to average the channel wise dice score.
        epsilon: floating point value to avoid divide by zero error.

    Returns:
        The dice score between `y_pred` and `y_true`. A scalar if `average_sample` is True, else a tensor with the shape (Batch).

    Raises:
        AssertionError: If `y_true` or `y_pred` are unacceptable data types. if data type is other than np.array, tensor.Tensor, tf.Tensor.
    """
    y_true, epsilon = cast(y_true, epsilon, y_pred.dtype)

    axis = get_axis(y_true, channel_average)

    keep_dims = False
    if axis == None:
        keep_dims = True

    numerator = reduce_sum(y_true * y_pred, axis=axis, keepdims=keep_dims)

    denominator = get_denominator(y_true, y_pred, soft_dice)

    denominator = reduce_sum(denominator, axis=axis, keepdims=keep_dims)

    dice_score = (2 * numerator) / (denominator + epsilon)

    if channel_average:
        dice_score = reduce_mean(dice_score, axis=-1)

    if sample_average:
        dice_score = reduce_mean(dice_score)

    return dice_score

get_axis

Get the axis to apply reduced_sum on.

Parameters:

Name Type Description Default
y_true Tensor

Ground truth class labels with a shape like (Batch, C, H, W) for torch and (Batch, H, W, C) for tensorflow or numpy. dtype: int or float32 or float16.

required
channel_average bool

Whether to average the channel wise dice score.

required

Returns:

Type Description
Tensor

The axis on which reduce_sum needs to be applied.

Source code in fastestimator\fastestimator\backend\_dice_score.py
def get_axis(y_true: Tensor, channel_average: bool) -> Tensor:
    """
        Get the axis to apply reduced_sum on.

        Args:
            y_true: Ground truth class labels with a shape like (Batch, C, H, W) for torch and (Batch, H, W, C) for tensorflow or numpy. dtype: int or float32 or float16.
            channel_average: Whether to average the channel wise dice score.

        Returns:
            The axis on which reduce_sum needs to be applied.
    """
    dims = len(y_true.shape)
    if dims <= 2:
        return None
    else:
        input_axis = list(range(dims))
        axis = input_axis[1:]
        if channel_average:
            if tf.is_tensor(y_true) or isinstance(y_true, np.ndarray):
                axis = input_axis[1:-1]
            elif isinstance(y_true, torch.Tensor):
                axis = input_axis[2:]
            else:
                raise ValueError("Unsupported tensor type.")

        return axis

get_denominator

Calculate sum/squared sum of y_true and y_pred

Parameters:

Name Type Description Default
y_pred Tensor

Prediction with a shape like (Batch, C, H, W) for torch and (Batch, H, W, C) for tensorflow or numpy. dtype: float32 or float16.

required
y_true Tensor

Ground truth class labels with a shape like y_pred. dtype: int or float32 or float16.

required
soft_dice bool

Whether to add direct sum or square sum of inputs

required
Return

The sum or squared sum of y_pred and y_true

Source code in fastestimator\fastestimator\backend\_dice_score.py
def get_denominator(y_true: Tensor, y_pred: Tensor, soft_dice: bool) -> Tensor:
    """
        Calculate sum/squared sum of y_true and y_pred

        Args:
            y_pred: Prediction with a shape like (Batch, C, H, W) for torch and (Batch, H, W, C) for tensorflow or numpy. dtype: float32 or float16.
            y_true: Ground truth class labels with a shape like `y_pred`. dtype: int or float32 or float16.
            soft_dice: Whether to add direct sum or square sum of inputs

        Return:
            The sum or squared sum of y_pred and y_true
    """
    if soft_dice:
        return y_true**2 + y_pred**2
    else:
        return y_true + y_pred