Main Content

focalLossLayer

(To be removed) Create focal loss layer using focal loss function

Since R2020a

The FocalLossLayer object will be removed in a future release. Use the trainnet (Deep Learning Toolbox) function and specify the loss as the focalCrossEntropy function. For more information, see Version History.

Description

A focal loss layer predicts object classes using focal loss. Add the focal loss layer to train an object detection, semantic segmentation, or a classification network 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 creates a focal loss layer for deep learning networks.

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('Name','focalloss') creates a focal loss layer with the name 'focalloss' and the specified balancing and focusing parameters.

example

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 network 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 string scalar. For Layer array input, the trainnet (Deep Learning Toolbox) and dlnetwork (Deep Learning Toolbox) functions automatically assign names to layers with the name "".

The FocalLossLayer object stores this property as a character vector.

Data Types: char | string

Examples

collapse all

Specify class names.

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

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

layer = focalLossLayer(Classes=classes,Name="focallosslayer")
layer = 
  FocalLossLayer with properties:

            Name: 'focallosslayer'

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

Create a DeepLab v3+ network based on ResNet-18.

imageSize = [480 640 3];
numClasses = 5;
network = "resnet18";
lgraph = deeplabv3plusLayers(imageSize,numClasses,network,DownsamplingFactor=16)
lgraph = 
  LayerGraph with properties:

     InputNames: {'data'}
    OutputNames: {'classification'}
         Layers: [100x1 nnet.cnn.layer.Layer]
    Connections: [113x2 table]

Display the output layer of the network. The output layer of the DeepLab v3+ network is a PixelClassificationLayer that uses cross-entropy loss to predict the categorical label for every pixel in an input 2-D image.

lgraph.Layers(end)
ans = 
  PixelClassificationLayer with properties:

            Name: 'classification'
         Classes: 'auto'
    ClassWeights: 'none'
      OutputSize: 'auto'

   Hyperparameters
    LossFunction: 'crossentropyex'

Replace the output PixelClassificationLayer with the FocalLossLayer to handle imbalanced classes in data.

layer = focalLossLayer("Name","focalloss");
lgraph = replaceLayer(lgraph,"classification",layer);

Display the network.

analyzeNetwork(lgraph);

Create a 3-D U-Net network for semantic segmentation by using the unet3dLayers function. Set the encoder-decoder depth to 2 and specify the number of output channels for the first convolution layer as 16.

imageSize = [128 128 128 3];
numClasses = 5;
lgraph = unet3dLayers(imageSize,numClasses, ...
    EncoderDepth=2,NumFirstEncoderFilters=16);
figure
plot(lgraph)

Create a focal loss layer and replace the Segmentation-Layer in the network with the focal loss layer. The layer predicts the categorical label for every voxel in an input 3-D volume.

layer = focalLossLayer(Name="focalloss");
lgraph = replaceLayer(lgraph,"Segmentation-Layer",layer)
lgraph = 
  LayerGraph with properties:

     InputNames: {'ImageInputLayer'}
    OutputNames: {'focalloss'}
         Layers: [40x1 nnet.cnn.layer.Layer]
    Connections: [41x2 table]

Display the network.

analyzeNetwork(lgraph);

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.

Extended Capabilities

Version History

Introduced in R2020a

collapse all

R2024a: focalLossLayer will be removed

The focalLossLayer object will be removed in a future release. Follow these steps to update your code:

  • Define your network as a dlnetwork (Deep Learning Toolbox) object. You can use functions such as addLayers (Deep Learning Toolbox) and connectLayers (Deep Learning Toolbox) to build the network. Do not include output layers in the network.

  • Train the network using the trainnet (Deep Learning Toolbox) function and specify the loss using the focalCrossEntropy function. For example, this code trains a dlnetwork network called net using the training data images.

    netTrained = trainnet(images,net,@focalCrossEntropy,options);