Main Content

focalLossLayer

Create focal loss layer using focal loss function

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

example

layer= focalLossLayercreates a focal loss layer for deep learning networks. For information on how to use focal loss layer in an object detection network, seeCreate SSD Object Detection Network.

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.

Properties

expand all

Balancing parameter of the focal loss function, specified as a positive real number. TheAlphavalue scales the loss function linearly and is typically set to0.25. If you decreaseAlpha, increaseGamma.

Focusing parameter of the focal loss function, specified as a positive real number. Increasing the value ofGammaincreases 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 setClassesto'auto', the classes are automatically set at training time. When you specify a string vector or cell array of character vectors, then the elements ofClassesare sorted according to the output of thecategoriesfunction.

Data Types:string|categorical|cell|char

Layer name, specified as a character vector or a string scalar. ForLayerarray input, thetrainNetwork,assembleNetwork,layerGraph, anddlnetworkfunctions automatically assign names to layers with name''.

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.

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: Layers: [100x1 nnet.cnn.layer.Layer] Connections: [113x2 table] InputNames: {'data'} OutputNames: {'classification'}

Display the output layer of the network. The output layer of the DeepLab v3+ network is aPixelClassificationLayerthat 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 outputPixelClassificationLayerwith theFocalLossLayerto 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 usingunet3dLayersfunction. 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)

Figure contains an axes object. The axes object contains an object of type graphplot.

Create a focal loss layer and replace theSegmentation-Layerin 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: Layers: [40x1 nnet.cnn.layer.Layer] Connections: [41x2 table] InputNames: {'ImageInputLayer'} OutputNames: {'focalloss'}

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 2017IEEE®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