# sigmoid_focal_loss¶

paddle.fluid.layers.sigmoid_focal_loss(x, label, fg_num, gamma=2.0, alpha=0.25)[source]

Sigmoid Focal Loss Operator.

Focal Loss is used to address the foreground-background class imbalance existed on the training phase of many computer vision tasks. This OP computes the sigmoid value for each element in the input tensor x, after which focal loss is measured between the sigmoid value and target label.

The focal loss is given as followed:

$\begin{split}\mathop{loss_{i,\,j}}\limits_{i\in\mathbb{[0,\,N-1]},\,j\in\mathbb{[0,\,C-1]}}=\left\{ \begin{array}{rcl} - \frac{1}{fg\_num} * \alpha * {(1 - \sigma(x_{i,\,j}))}^{\gamma} * \log(\sigma(x_{i,\,j})) & & {(j +1) = label_{i,\,0}} \\ - \frac{1}{fg\_num} * (1 - \alpha) * {\sigma(x_{i,\,j})}^{ \gamma} * \log(1 - \sigma(x_{i,\,j})) & & {(j +1)!= label_{i,\,0}} \end{array} \right.\end{split}$

We know that

$\sigma(x_j) = \frac{1}{1 + \exp(-x_j)}$
Parameters
• x (Variable) – A 2-D tensor with shape $$[N, C]$$ represents the predicted categories of all samples. $$N$$ is the number of all samples responsible for optimization in a mini-batch, for example, samples are anchor boxes for object detection and $$N$$ is the total number of positive and negative samples in a mini-batch; Samples are images for image classification and $$N$$ is the number of images in a mini-batch. $$C$$ is the number of classes (Notice: excluding background). The data type of x is float32 or float64.

• label (Variable) – A 2-D tensor with shape $$[N, 1]$$ represents the target labels for classification. $$N$$ is the number of all samples responsible for optimization in a mini-batch, each sample has one target category. The values for positive samples are in the range of $$[1, C]$$, and the values for negative samples are 0. The data type of label is int32.

• fg_num (Variable) – A 1-D tensor with shape [1] represents the number of positive samples in a mini-batch, which should be obtained before this OP. The data type of fg_num is int32.

• gamma (int|float) – Hyper-parameter to balance the easy and hard examples. Default value is set to 2.0.

• alpha (int|float) – Hyper-parameter to balance the positive and negative example. Default value is set to 0.25.

Returns

A 2-D tensor with shape $$[N, C]$$, which is the focal loss of each element in the input tensor x.

Return type

Variable(the data type is float32 or float64)

Examples

import paddle.fluid as fluid

input = fluid.data(name='data', shape=[10,80], dtype='float32')
label = fluid.data(name='label', shape=[10,1], dtype='int32')
fg_num = fluid.data(name='fg_num', shape=[1], dtype='int32')
loss = fluid.layers.sigmoid_focal_loss(x=input,
label=label,
fg_num=fg_num,
gamma=2.0,
alpha=0.25)