# 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

### Syntax

``layer = focalLossLayer``
``layer = focalLossLayer(Name,Value)``

### Description

example

````layer = focalLossLayer` creates a focal loss layer for deep learning networks. For information on how to use focal loss layer in an object detection network, see Create 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. 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 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.

`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 a `Pixel` `Classification` `Layer` 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 `Pixel` `Classification` `Layer` with the `Focal` `Loss` `Layer` 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 `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: Layers: [40x1 nnet.cnn.layer.Layer] Connections: [41x2 table] InputNames: {'ImageInputLayer'} OutputNames: {'focalloss'} ```

Display the network.

`analyzeNetwork(lgraph);`

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

Introduced in R2020a