The categorical focal loss discussed in this paper:

categorical_focal_loss(y_true, y_pred, gamma = 2, alpha = 0.25)

Arguments

y_true

True labels (Tensor)

y_pred

Predictions (Tensor of the same shape as y_true)

gamma

focusing parameter for modulating factor (1-p). Default = 2.0.

alpha

weighing factor in balanced cross entropy. Default = 0.25.

Value

function value

Details

https://arxiv.org/pdf/1708.02002.pdf

and ported from this implementation:

https://github.com/umbertogriffo/focal-loss-keras/blob/master/losses.py

Used to handle imbalanced classes .

Author

Tustison NJ

Examples

library( ANTsRNet ) library( keras ) model <- createUnetModel2D( c( 64, 64, 1 ) )
#> Error in py_discover_config(required_module, use_environment): Python specified in RETICULATE_PYTHON (/Users/ntustison/anaconda3/envs/antsx/bin/python3) does not exist
metric_categorical_focal_gain <- custom_metric( "categorical_focal_gain", categorical_focal_gain( alpha = 0.25, gamma = 2.0 ) )
#> Error in value[[3L]](cond): The R function's signature must not contains esoteric Python-incompatible constructs. Detailed traceback: #> Python specified in RETICULATE_PYTHON (/Users/ntustison/anaconda3/envs/antsx/bin/python3) does not exist
model %>% compile( loss = categorical_focal_loss( alpha = 0.25, gamma = 2.0 ), optimizer = optimizer_adam( lr = 0.0001 ), metrics = c( metric_categorical_focal_gain ) )
#> Error in compile(., loss = categorical_focal_loss(alpha = 0.25, gamma = 2), optimizer = optimizer_adam(lr = 1e-04), metrics = c(metric_categorical_focal_gain)): object 'model' not found