Function Repository Resource:

# FocalLossLayer

A NetGraph layer implementing focal loss

Contributed by: Michael Sollami
 ResourceFunction["FocalLossLayer"][α,γ] represents a net layer that implements focal loss.

## Details and Options

ResourceFunction["FocalLossLayer"] exposes the following ports for use in NetGraph etc.:
 "Input" scalar values between 0 and 1, or arrays of these "Target" scalar values between 0 and 1, or arrays of these "Loss" real number
ResourceFunction["FocalLossLayer"] is a variant of binary cross-entropy (see CrossEntropyLossLayer) that downweights well-classified examples.
ResourceFunction["FocalLossLayer"] is defined in the 2017 paper "Focal Loss for Dense Object Detection".
The paper's authors suggest default values of 0.25 and 2 for α and γ, respectively.
ResourceFunction["FocalLossLayer"] takes the same options as NetGraph.

## Examples

### Basic Examples (4)

Create a focal loss layer:

 In[1]:=
 Out[1]=

Apply it to a given input and target:

 In[2]:=
 Out[2]=

Use FocalLossLayer with single probabilities for the input and target:

 In[3]:=
 Out[4]=

Apply FocalLossLayer with inputs and targets being matrices of binary-class probabilities:

 In[5]:=
 Out[6]=

Apply the layer to an input and target:

 In[7]:=
 Out[7]=

Use a focal loss layer during training:

 In[8]:=
 Out[11]=
 In[12]:=
 Out[12]=

### Scope (1)

Define a FocalLossLayer operating on color images:

 In[13]:=
 Out[13]=
 In[14]:=
 Out[14]=

### Properties and Relations (1)

Plot a comparison of FocalLossLayer (abbreviated as "FL") and standard CrossEntropy (abbreviated as "CE"). Note that setting γ>0 reduces the relative loss for well-classified examples (pt>.5), thus focusing on the harder examples:

 In[15]:=
 Out[736]=

Michael Sollami

## Version History

• 1.0.0 – 27 January 2020