_focal_loss
focal_loss
¶
Calculate the focal loss between two tensors.
Original implementation from https://github.com/facebookresearch/fvcore/blob/master/fvcore/nn/focal_loss.py . Loss used in RetinaNet for dense detection: https://arxiv.org/abs/1708.02002.
This method can be used with TensorFlow tensors:
true = tf.constant([[1], [1], [1], [0], [0], [0]])
pred = tf.constant([[0.97], [0.91], [0.73], [0.27], [0.09], [0.03]])
b = fe.backend.focal_loss(y_pred=pred, y_true=true, gamma=None, alpha=None) #0.1464
b = fe.backend.focal_loss(y_pred=pred, y_true=true, gamma=2.0, alpha=0.25) #0.00395
This method can be used with PyTorch tensors:
true = torch.tensor([[1], [1], [1], [0], [0], [0]])
pred = torch.tensor([[0.97], [0.91], [0.73], [0.27], [0.09], [0.03]])
b = fe.backend.focal_loss(y_pred=pred, y_true=true, gamma=None, alpha=None) #0.1464
b = fe.backend.focal_loss(y_pred=pred, y_true=true, gamma=2.0, alpha=0.25) #0.004
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
Tensor
|
Ground truth class labels with shape([batch_size, d0, .. dN]), which should take values of 1 or 0. |
required |
y_pred |
Tensor
|
Prediction score for each class, with a shape like y_true. dtype: float32 or float16. |
required |
alpha |
float
|
Weighting factor in range (0,1) to balance positive vs negative examples or (-1/None) to ignore. Default = 0.25 |
0.25
|
gamma |
float
|
Exponent of the modulating factor (1 - p_t) to balance easy vs hard examples. |
2.0
|
normalize |
bool
|
Whether to normalize focal loss along samples based on number of positive classes per samples. |
True
|
shape_reduction |
str
|
|
'sum'
|
from_logits |
bool
|
Whether y_pred is logits (without sigmoid). |
False
|
sample_reduction |
str
|
'none' | 'mean' | 'sum' 'none': No reduction will be applied to the output. 'mean': The output will be averaged across batch size. 'sum': The output will be summed across batch size. |
'mean'
|
Returns:
The Focal loss between y_true
and y_pred
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in fastestimator/fastestimator/backend/_focal_loss.py
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 |
|
pytorch_focal_loss
¶
Calculate the focal loss between two tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
y_true |
Tensor
|
Ground truth class labels with shape([batch_size, d0, .. dN]), which should take values of 1 or 0. |
required |
y_pred |
Tensor
|
Prediction score for each class, with a shape like y_true. dtype: float32 or float16. |
required |
alpha |
float
|
Weighting factor in range (0,1) to balance positive vs negative examples or (-1/None) to ignore. Default = 0.25 |
0.25
|
gamma |
float
|
Exponent of the modulating factor (1 - p_t) to balance easy vs hard examples. |
2
|
from_logits |
bool
|
Whether y_pred is logits (without sigmoid). |
False
|
Returns: Loss tensor.