处理 imbalance 的方法。

目标检测中的样本不平衡问题

目标检测中的样本不平衡问题,是指对于目标检测模型,负样本的个数远多于正样本(比例约为 10000:1),同时,负样本中简单负样本的个数也远多于困难负样本。模型训练过程中,需要避免这种不平衡对模型性能的影响,以免模型将所有样本都判断为负样本。

  • 单阶段网络:目前绝大多数单阶段网络仍然基于 anchor 。网络 anchor 的正负样本和困难/简单负样本不平衡问题十分突出。
  • 两阶段网络:两阶段网络中第一阶段的 RPN 与单阶段网络类似,但第二阶段 proposal 的不平衡问题得到了很大缓解,这主要是由于 RPN 处理(top N_pre + nms + top N_pos)后,proposal 中的简单负样本大大减少,从而同时减轻了正负样本和困难/简单负样本的不平衡问题。

解决样本不平衡的方法

按比例随机采样

Faster R-CNN 中,第一阶段 RPN 采用按比例随机采样,图片 batch size 为 1,anchor batch size 为 256,正负样本比例为 1 比 1,如果正样本不足 128(256 的 1/2),则用负样本凑。

We randomly sample 256 anchors in an image to compute the loss function of a mini-batch, where the sampled positive and negative anchors have a ratio of up to 1:1. If there are fewer than 128 positive samples in an image, we pad the mini-batch with negative ones.(Faster R-CNN 3.1.3 Training RPN)

第二阶段 Fast R-CNN 同样采用按比例随机采样,正负样本比例为 1:3。

We take 25% of the RoIs from object proposals that have intersection over union (IoU) overlap with a ground truth bounding box of at least 0:5. These RoIs comprise the examples labeled with a foreground object class. The remaining RoIs are sampled from object proposals that have a maximum IoU with ground truth in the interval [0:1; 0:5), following [11].(Fast R-CNN 2.3 Mini-batch sampling)

在线难样本挖掘(OHEM, Online Hard Example Mining)

按比例随机采样控制了正负样本的比例,基本解决了正负样本不平衡的问题,但负样本的随机采样会导致简单负样本占绝大多数。SSD 在训练时加入了难样本挖掘,选出负样本中 loss 最大的进行训练,同时仍然保证正负样本比例为 1:3,以解决简单/困难负样本不平衡的问题。

Instead of using all the negative examples, we sort them using the highest confidence loss for each default box and pick the top ones so that the ratio between the negatives and positives is at most 3:1. We found that this leads to faster optimization and a more stable training.(SSD 2.2 Training Hard negative mining)

Focal Loss

Focal loss 应该是最有名的处理不平衡问题的 paper 了,文中提出了一种新的 loss 即 focal loss:

其中 $ \alpha_t $ 作用是平衡正负样本权重,$ \gamma $ 作用是平衡简单/困难负样本权重,所以 focal loss 理论上能同时解决上面提到的两方面不平衡问题。原 paper 中给出的结果,$\alpha_t = 0.25, \gamma = 2 $ 时效果最好。注意,使用 focal loss 后,不需要再保证正负样本比例,原文中使用了所有 anchor 进行训练。

We emphasize that when training RetinaNet, the focal loss is applied to all ~100k anchors in each sampled image.(Focal loss 4.1 Inference and Training Focal Loss)

IoU 平衡采样

OHEM 存在以下两个问题:

  1. 需要计算所有 anchor 的 loss,付出额外的空间和时间代价。
  2. 标注如果有 noise label,会影响模型性能。

IoU 平衡采样是 Libra R-CNN 中提出的,作者发现 60% 的困难负样本与 GT 的 IoU 都大于 0.05,而随机采样时与 GT IoU 大于 0.05 的 anchor 只占 30%,说明困难负样本不容易被选中。这个方法的核心思想是根据 anchor 与 GT 的 IoU 划分不同区间,在每个区间中进行均匀采样,保证与 GT IoU 大的负样本(很多是困难负样本)能够以较大的概率被选中。

To raise the selected probability of hard negatives, we evenly split the sampling interval into K bins according to IoU. N demanded negative samples are equally distributed to each bin. Then we select samples from them uniformly.

GHM

GHM 文章首先根据梯度的模和样本的 label 将样本分为难易不同的样本,梯度的模越接近 0 说明越简单,越接近 1 则说明越困难。文章发现对于训练好的 detection 模型,仍然存在相当比例的困难样本,需要解决的具体问题是如何适当降低这部分困难样本的权重,提升模型性能。

为此,文中首先定义了梯度密度的概念,用于刻画一个样本梯度模值附近所有样本的总数,总数越多说明样本处的密度越高。

  • 分类 loss:采用加权交叉熵,权重为梯度密度的倒数,即降低密集样本的权重,这样就同时降低了非常密集的简单样本和同样密集的困难样本的权重。
  • 回归 loss:采用改进的 Smooth L1 loss($\sqrt{d^2 + \mu ^ 2} - \mu$),类似分类 loss 同样采用加权平均,同时降低简单和困难样本的权重。

留言

2019-09-01