_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 |
Source code in fastestimator\fastestimator\backend\_dice_score.py
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 |
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 |
Raises:
Type | Description |
---|---|
AssertionError
|
If |
Source code in fastestimator\fastestimator\backend\_dice_score.py
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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
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
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 |
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