Need help with focal-loss-keras?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

234 Stars 60 Forks 26 Commits 7 Opened issues


Binary and Categorical Focal loss implementation in Keras.

Services available


Need anything else?

Contributors list

# 53,748
10 commits

Focal Loss


focal loss down-weights the well-classified examples. This has the net effect of putting more training emphasis on that data that is hard to classify. In a practical setting where we have a data imbalance, our majority class will quickly become well-classified since we have much more data for it. Thus, in order to insure that we also achieve high accuracy on our minority class, we can use the focal loss to give those minority class examples more relative weight during training.

The focal loss can easily be implemented in Keras as a custom loss function.


Compile your model with focal loss as sample:


model.compile(loss=[binaryfocalloss(alpha=.25, gamma=2)], metrics=["accuracy"], optimizer=adam)


model.compile(loss=[categoricalfocalloss(alpha=[[.25, .25, .25]], gamma=2)], metrics=["accuracy"], optimizer=adam)

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.

Convert a trained keras model into an inference tensorflow model

If you use the @amir-abdi's code to convert a trained keras model into an inference tensorflow model, you have to serialize nested functions. In order to serialize nested functions you have to install dill in your anaconda environment as follow:

conda install -c anaconda dill

then modify adding this piece of code after the imports:

import dill
custom_object = {'binary_focal_loss_fixed': dill.loads(dill.dumps(binary_focal_loss(gamma=2., alpha=.25))),
                 'categorical_focal_loss_fixed': dill.loads(dill.dumps(categorical_focal_loss(gamma=2., alpha=[[.25, .25, .25]]))),
                 'categorical_focal_loss': categorical_focal_loss,
                 'binary_focal_loss': binary_focal_loss}

and modify the beginning of load_model method as follow:
if not Path(input_model_path).exists():
    raise FileNotFoundError(
        'Model file `{}` does not exist.'.format(input_model_path))
    model = keras.models.load_model(input_model_path, custom_objects=custom_object)
    return model


We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.