sigmoid_focal_loss

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

Focal Loss 被提出用于解决计算机视觉任务中前景-背景不平衡的问题。该OP先计算输入x中每个元素的sigmoid值,然后计算sigmoid值与类别目标值label之间的Focal Loss。

Focal Loss的计算过程如下:

sigmoid_focal_loss - 图1

其中,已知:

sigmoid_focal_loss - 图2

参数:

  • x (Variable) – 维度为

    sigmoid_focal_loss - 图3

    的2-D Tensor,表示全部样本的分类预测值。其中,第一维N是批量内参与训练的样本数量,例如在目标检测中,样本为框级别,N为批量内所有图像的正负样本的数量总和;在图像分类中,样本为图像级别,N为批量内的图像数量总和。第二维:math:C 是类别数量( 不包括背景类 )。数据类型为float32或float64。

  • label (Variable) – 维度为

    sigmoid_focal_loss - 图4

    的2-D Tensor,表示全部样本的分类目标值。其中,第一维N是批量内参与训练的样本数量,第二维1表示每个样本只有一个类别目标值。正样本的目标类别值的取值范围是 [1,C][1,C] , 负样本的目标类别值是0。数据类型为int32。

  • fg_num (Variable) – 维度为 [1][1] 的1-D Tensor,表示批量内正样本的数量,需在进入此OP前获取正样本的数量。数据类型为int32。

  • gamma (int|float) – 用于平衡易分样本和难分样本的超参数, 默认值设置为2.0。

  • alpha (int|float) – 用于平衡正样本和负样本的超参数,默认值设置为0.25。

返回: 输入x中每个元素的Focal loss,即维度为 [N,C][N,C] 的2-D Tensor。

返回类型: 变量(Variable),数据类型为float32或float64。

代码示例

  1. import numpy as np
  2. import paddle.fluid as fluid
  3. num_classes = 10 # exclude background
  4. image_width = 16
  5. image_height = 16
  6. batch_size = 32
  7. max_iter = 20
  8. def gen_train_data():
  9. x_data = np.random.uniform(0, 255, (batch_size, 3, image_height,
  10. image_width)).astype('float64')
  11. label_data = np.random.randint(0, num_classes,
  12. (batch_size, 1)).astype('int32')
  13. return {"x": x_data, "label": label_data}
  14. def get_focal_loss(pred, label, fg_num, num_classes):
  15. pred = fluid.layers.reshape(pred, [-1, num_classes])
  16. label = fluid.layers.reshape(label, [-1, 1])
  17. label.stop_gradient = True
  18. loss = fluid.layers.sigmoid_focal_loss(
  19. pred, label, fg_num, gamma=2.0, alpha=0.25)
  20. loss = fluid.layers.reduce_sum(loss)
  21. return loss
  22. def build_model(mode='train'):
  23. x = fluid.data(name="x", shape=[-1, 3, -1, -1], dtype='float64')
  24. output = fluid.layers.pool2d(input=x, pool_type='avg', global_pooling=True)
  25. output = fluid.layers.fc(
  26. input=output,
  27. size=num_classes,
  28. # Notice: size is set to be the number of target classes (excluding backgorund)
  29. # because sigmoid activation will be done in the sigmoid_focal_loss op.
  30. act=None)
  31. if mode == 'train':
  32. label = fluid.data(name="label", shape=[-1, 1], dtype='int32')
  33. # Obtain the fg_num needed by the sigmoid_focal_loss op:
  34. # 0 in label represents background, >=1 in label represents foreground,
  35. # find the elements in label which are greater or equal than 1, then
  36. # computed the numbers of these elements.
  37. data = fluid.layers.fill_constant(shape=[1], value=1, dtype='int32')
  38. fg_label = fluid.layers.greater_equal(label, data)
  39. fg_label = fluid.layers.cast(fg_label, dtype='int32')
  40. fg_num = fluid.layers.reduce_sum(fg_label)
  41. fg_num.stop_gradient = True
  42. avg_loss = get_focal_loss(output, label, fg_num, num_classes)
  43. return avg_loss
  44. else:
  45. # During evaluating or testing phase,
  46. # output of the final fc layer should be connected to a sigmoid layer.
  47. pred = fluid.layers.sigmoid(output)
  48. return pred
  49. loss = build_model('train')
  50. moment_optimizer = fluid.optimizer.MomentumOptimizer(
  51. learning_rate=0.001, momentum=0.9)
  52. moment_optimizer.minimize(loss)
  53. place = fluid.CPUPlace()
  54. exe = fluid.Executor(place)
  55. exe.run(fluid.default_startup_program())
  56. for i in range(max_iter):
  57. outs = exe.run(feed=gen_train_data(), fetch_list=[loss.name])
  58. print(outs)