focalLossLayer

Create focal loss layer using focal loss function for object detection

Description

A focal loss layer predicts object classes using focal loss. Focal loss is useful for object detection when imbalance exists between foreground and background classes. To compensate for class imbalance, the focal loss function multiplies the cross entropy function with a modulating factor that increases the sensitivity of the network to misclassified observations.

Creation

Description

layer = focalLossLayer(alpha,gamma) creates a focal loss layer for object detection networks, setting the balancing and focusing parameters of the loss function. alpha sets the Alpha property. gamma sets the Gamma property.

example

layer = focalLossLayer(___, Name,Value) sets properties of the focal loss layer by using one or more name-value pair arguments. Enclose each property name in quotes.

For example, focalLossLayer(2,0.1,'Name','focalloss') creates a focal loss layer with the name 'focalloss' and the specified balancing and focusing parameters.

Properties

expand all

Balancing parameter of the focal loss function, specified as a positive real number. The Alpha value scales the loss function linearly and is typically set to 0.25. If you decrease Alpha, increase Gamma.

Focusing parameter of the focal loss function, specified as a positive real number. Increasing the value of Gamma increases the sensitivity of the network to misclassified observations.

Object classes that the single shot object detector (SSD) is trained to detect, specified as a string vector, categorical vector, cell array of character vectors, or 'auto'. When you set Classes to 'auto', the classes are automatically set at training time. When you specify a string vector or cell array of character vectors, then the elements of Classes are sorted according to the output of the categories function.

Data Types: string | categorical | cell | char

Layer name, specified as a character vector or a string scalar. To include a layer in a layer graph, you must specify a nonempty unique layer name. If you train a series network with the layer and Name is set to '', then the software automatically assigns a name to the layer at training time.

Data Types: char | string

Examples

collapse all

Specify class names.

classes = ["Vehicle","Background"];

Specify the balancing paramete, and focusing parameter of the focal loss function. Create a focal loss layer named "focallosslayer" for the two classes, displaying results.

alpha = 2;
gamma = 0.1;
layer = focalLossLayer(alpha,gamma, ...
    'Classes',classes,'Name','focallosslayer')
layer = 
  FocalLossLayer with properties:

            Name: 'focallosslayer'

   Hyperparameters
           Gamma: 2
           Alpha: 0.1000
         Classes: [2x1 categorical]
    LossFunction: 'focalLoss'

More About

expand all

References

[1] Lin, Tsung-Yi, Priya Goyal, Ross Girshick, Kaiming He, and Piotr Dollar. "Focal Loss for Dense Object Detection." In 2017 IEEE® International Conference on Computer Vision (ICCV), 2999–3007. Venice: IEEE, 2017. https://doi.org/10.1109/ICCV.2017.324.

Introduced in R2020a