Loss functions
binary_focal_loss(alpha=0.25, gamma=2.0)
¤
Binary form of focal loss computation.
FL(p_t) = -alpha * (1 - p_t)**gamma * log(p_t) where p = sigmoid(x), p_t = p or 1 - p depending on if the label is 1 or 0, respectively.
Example
from aucmedi.neural_network.loss_functions import *
my_loss = binary_focal_loss(alpha=0.75)
model = NeuralNetwork(n_labels=1, channels=3, loss=my_loss)
Reference - Implementation
Author: Umberto Griffo
GitHub: https://github.com/umbertogriffo
Source: https://github.com/umbertogriffo/focal-loss-keras
Reference - Publication
Focal Loss for Dense Object Detection (Aug 2017)
Authors: Tsung-Yi Lin, Priya Goyal, Ross Girshick, Kaiming He, Piotr Dollár
https://arxiv.org/abs/1708.02002
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
float
|
Class weight for positive class. |
0.25
|
gamma |
float
|
Tunable focusing parameter (γ ≥ 0). |
2.0
|
Returns:
Name | Type | Description |
---|---|---|
loss |
Loss Function
|
A TensorFlow compatible loss function. This object can be
passed to the NeuralNetwork |
Source code in aucmedi/neural_network/loss_functions.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 |
|
categorical_focal_loss(alpha, gamma=2.0)
¤
Softmax version of focal loss.
When there is a skew between different categories/labels in your data set, you can try to apply this function as a loss.
m
FL = ∑ -alpha * (1 - p_o,c)^gamma * y_o,c * log(p_o,c)
c=1
where m = number of classes, c = class and o = observation
The class_weights_list
obtained from compute_class_weights
can be provided as parameter alpha
.
Example
# Compute class weights
from aucmedi.utils.class_weights import compute_class_weights
cw_loss, cw_fit = compute_class_weights(class_ohe)
from aucmedi.neural_network.loss_functions import *
my_loss = categorical_focal_loss(alpha=cw_loss)
model = NeuralNetwork(n_labels=6, channels=3, loss=my_loss)
Reference - Implementation
Author: Umberto Griffo
GitHub: https://github.com/umbertogriffo
Source: https://github.com/umbertogriffo/focal-loss-keras
Reference - Publication
Focal Loss for Dense Object Detection (Aug 2017)
Authors: Tsung-Yi Lin, Priya Goyal, Ross Girshick, Kaiming He, Piotr Dollár
https://arxiv.org/abs/1708.02002
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha |
list of float
|
The same as weighing factor in balanced cross entropy. Alpha is used to specify the weight of different categories/labels, the size of the array needs to be consistent with the number of classes. |
required |
gamma |
float
|
Focusing parameter for modulating factor (1-p). |
2.0
|
Returns:
Name | Type | Description |
---|---|---|
loss |
Loss Function
|
A TensorFlow compatible loss function. This object can be
passed to the NeuralNetwork |
Source code in aucmedi/neural_network/loss_functions.py
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 |
|
multilabel_focal_loss(class_weights, gamma=2.0, class_sparsity_coefficient=1.0)
¤
Focal loss for multi-label classification.
Example
# Compute class weights
from aucmedi.utils.class_weights import compute_class_weights
class_weights = compute_multilabel_weights(class_ohe)
from aucmedi.neural_network.loss_functions import *
my_loss = multilabel_focal_loss(class_weights=class_weights)
model = NeuralNetwork(n_labels=6, channels=3, loss=my_loss,
activation_output="sigmoid")
Reference - Implementation
Author: Sushant Tripathy
LinkedIn: https://www.linkedin.com/in/sushanttripathy/
Source: https://github.com/sushanttripathy/Keras_loss_functions/blob/master/focal_loss.py
Reference - Publication
Focal Loss for Dense Object Detection (Aug 2017)
Authors: Tsung-Yi Lin, Priya Goyal, Ross Girshick, Kaiming He, Piotr Dollár
https://arxiv.org/abs/1708.02002
Parameters:
Name | Type | Description | Default |
---|---|---|---|
class_weights |
list of float
|
Non-zero, positive class-weights. This is used instead of alpha parameter. |
required |
gamma |
float
|
The Gamma parameter in Focal Loss. Default value (2.0). |
2.0
|
class_sparsity_coefficient |
float
|
The weight of True labels over False labels. Useful if True labels are sparse. Default value (1.0). |
1.0
|
Returns:
Name | Type | Description |
---|---|---|
loss |
Loss Function
|
A TensorFlow compatible loss function. This object can be
passed to the NeuralNetwork |
Source code in aucmedi/neural_network/loss_functions.py
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|