_dice_score
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.90909083, 0.79999984, 0.79999995]]
b = fe.backend.dice_score(y_pred=pred, y_true=true, soft_dice=True) # [[0.90909083, 0.79999984, 0.79999995]]
b = fe.backend.dice_score(y_pred=pred, y_true=true, channel_average=True) # [0.83636354]
b = fe.backend.dice_score(y_pred=pred, y_true=true, sample_average=True) # [0.90909083, 0.79999984, 0.79999995]
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.9090908 , 0.79999983, 0.79999995]]
b = fe.backend.dice_score(y_pred=pred, y_true=true, soft_dice=True) # [[0.9090908 , 0.79999983, 0.79999995]]
b = fe.backend.dice_score(y_pred=pred, y_true=true, channel_average=True) # [0.83636355]
b = fe.backend.dice_score(y_pred=pred, y_true=true, sample_average=True) # [0.9090908 , 0.79999983, 0.79999995]
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.8000, 0.8000, 0.9091]]
b = fe.backend.dice_score(y_pred=pred, y_true=true, soft_dice=True) # [[0.8000, 0.8000, 0.9091]]
b = fe.backend.dice_score(y_pred=pred, y_true=true, channel_average=True) # [0.8364]
b = fe.backend.dice_score(y_pred=pred, y_true=true, sample_average=True) # [0.8000, 0.8000, 0.9091]
Args:
y_pred: Prediction with a shape like (Batch, C, ...) for torch and (Batch, ..., C) for tensorflow or numpy.
y_true: Ground truth class labels with a shape like y_pred
.
soft_dice: Whether to square elements in the denominator.
sample_average: Whether to average the dice score along the batch dimension.
channel_average: Whether to average the dice score along the channel dimension.
channel_weights: A tensor of weights (size 1xN_Channels) to apply to each channel before reduction.
mask_overlap: Whether an individual pixel can belong to only 1 class (False) or more than 1 class
(True). If False, a threshold of 0.0 can be used to binarize by whatever the maximum predicted class is.
threshold: The threshold for binarizing the prediction. Set this to 0.0 if you are using a background class. Set
to None if continuous values are desired (ex. for a loss).
empty_nan: If a target mask is totally empty (object is missing) and the prediction is also empty, should this
function return 0.0 (False) or NaN (True) for that particular mask channel.
epsilon: floating point value to avoid divide by zero error.
Returns:
The dice score between y_pred
and y_true
.
Raises:
AssertionError: If y_true
or y_pred
something other than np.array, tensor.Tensor, or tf.Tensor.
Source code in fastestimator/fastestimator/backend/_dice_score.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
|