Target detection series (2) - another one stage target detection model - SSD (on)

foreword

Since a whim 3 years ago, I have persisted until today. I have experienced a lot and learned a lot along the way. Especially when you encounter someone you don't understand at all and have a certain depth of field how to start. In the future, I will have the opportunity to share with you the experience of self-learning machine learning, hoping that everyone can avoid some learning detours. Deep learning, I have also learned and used it, and I have some understanding of deep learning, especially the application of deep learning in the field of computer vision. However, I feel that although I have learned, my understanding of many knowledge points is not deep enough, and I still stay on the surface. I have a lot of time recently, so I want to do in-depth research, and I want to connect these scattered knowledge points. Organize a set of data

  • Anatomy of a Deep Learning Framework
  • Target Detection
  • Semantic segmentation

The work in the past two years is mainly focused on the two computer vision tasks of target recognition and semantic segmentation. Therefore, these two main lines are mainly introduced in the sharing to introduce the application of deep learning in computer vision.

There are some common problems for object detection tasks, and we will take a look at how different networks give their own answers to these problems.

  • Positive and negative sample imbalance problem
  • Target recognition at different scales
  • How to efficiently define a loss function

one stage detection

What is one-stage object detection

I shared an article about YOLOv1 before, Target Detection Series—Deep Interpretation of YOLOv1 (1) In fact, the YOLO series of models from v1 to v5 are all one-stage target detection models. The SSD to be introduced today also belongs to the one-stage target detection model. Well, then, what is one stage? The so-called one-stage means that the model only observes (detects) the input image once, and can give the prediction result (detects the pre-defined category and its position). In fact, here one stage is relative to the previous two stage model. The two stage is to observe the image twice. After the first observation of the image, many candidate frames (regions) that may contain targets are given, and then in the second observation, target detection is performed on these candidate regions, and The candidate box is adjusted to accurately represent the target location and size.

011.png

one-stage 目标检测一般流程

在开始介绍 SSD 之前,就需要先清楚什么是 one-stage 目标检测的一般流程。one stage 目标检测是先把图像以网格形式划分为若干区域。每个小区域通常称为网格(grid cell),每个区域负责检测目标,在 YOLOv1 中,每个网格负责检测那些中心点落在该网格区域内的目标。在 SSD 以及 YOLOv2 以后版本,他们都引入锚框(anchor box)的概念,每个网格会生成一系列锚框(anchor box),然后基于这些锚框来生成预测框来负责检测目标,模型要学习的是预测框相对于锚框的偏移量。

001.png

SSD(Single Stage Detection)

首先我们在一阶段目标检测网络中没有使用候选框生成,而是使用。在 SSD 中就是将图片划分成网格,然后每一个网格创建一些锚框,这些锚框会给出一个其所属类别的预测,类别数中除了数据集提供类别外还包括了背景这个类别,例如 VOC 是 20 个类别,那么每个锚框负责是从 21 类别(将背景算为一个类别)

结合上图我们来看一看 SSD ,经过一系列卷积操作后,得到了 m × n × p m \times n \times p 特征图,其中 p 是通道数,例如 8x8 和 4x4 特征图,然后在这些特征图上应用 3x3 的卷积。对于每一个位置,拿到 k 个边界框,这些边界框具有不同大小和宽高比,这样做的目的让边界可以适应各种不同宽高比的目标。然后每一个边界框会具有类别(这里类别包括背景)的得分以及边界框相对于初始给定边界框的偏移量,也就是会有 (c+4)kmn 个边界框。

SSD 结构图

008.png

在网络结构上,SSD 和 YOLOv1 虽然都是 one-stage detector 的确有着明显的差异。YOLOv1 主干网络是一个自定义的网路结构,而且最后两层是全连接,随后 reshape 成想要的输出形状。而 SSD 是在 VGG16 基础上进行了改造,并且最后都是卷积层,并没有用到全连接层。

009.png

如上图,在 VGG16 的第 5 模块将其 Pooling 层替替换为 3x3 卷积层,然后再经过一个 Atrous/Dilated Conv 层后进行一系列卷积操作得到不同尺度的特征图。

为了有更准确的检测,输出不同尺寸的特征图还要经过一个 3×3 的卷积再进行物体检测,如上图。

016.png

这里以 Conv4_3 为例,其尺寸是 38×38×512,对其应用 3×3 的卷积输出预测。每个网格有 4 个边界框,每个边界框将要预测(类+4)个类别。因此在 Conv4_3 中,输出是 38×38×4×(c+4)。假设数据集为 VOC,有 20 个类别,再加上 1 个背景类,那么输出就是 38×38×4×(21+4)=144,400,边界框数量就是 38×38×4=5776。以此类推

  • Conv7: 19×19×6 = 2166 边界框 (这里对于网格位置 6 边界框)
  • Conv8_2: 10×10×6 = 600 边界框 (这里对于网格位置 6 边界框)
  • Conv9_2: 5×5×6 = 150 边界框 (这里对于网格位置 6 边界框)
  • Conv10_2: 3×3×4 = 36 边界框 (这里对于网格位置 4 边界框)
  • Conv11_2: 1×1×4 = 4 边界框 (这里对于网格位置 4 边界框)

现在我们把所有不同特征层的边界框加起来,共得到了 5776+2166+600+150+36+4=8732 个边界框。前面说的 YOLOv1 ,将图像划分为 7×7 的网格,每个网格有 2 个边界框,一共得到 7×7×2=98 个边界框,这与拥有 8732个边界框的 SSD 相比,YOLOv1 边界框要少的多。

损失函数

损失函数由两部分组成,分别 L c o n f L_{conf} L l o c L_{loc} ,其中 N 是匹配上的默认的边界框。

L ( x , c , l , g ) = 1 N ( L c o n f ( x , c ) + α L l o c ( x , l , g ) ) L(x,c,l,g) = \frac{1}{N}\left( L_{conf}(x,c) + \alpha L_{loc}(x,l,g) \right)
  • 这里 N 是匹配上边界框的数量
  • α \alpha 是一个权重值,这里 α \alpha 取值为 1

分类损失函数

关于分类损失函数这里使用 softmax 损失函数

L c o n f ( x , c ) = i P o s N x i j P log ( C i ^ P ) i N e g N ( C i ^ 0 ) L_{conf}(x,c) = -\sum_{i \in Pos}^N x_{ij}^P \log(\hat{C_i}^P) - \sum_{i \in Neg}^N (\hat{C_i}^0)
C i ^ p = exp ( C i P ) P exp ( C i P ) \hat{C_i}^p = \frac{\exp(C_i^P)}{\sum_{P} \exp(C_i^P)}
L l o c ( x , l , g ) = L 1 s m o o t h ( 1 2 i j x i j l i g j 2 ) L_{loc}(x,l,g) = L_1^{smooth}(\frac{1}{2} \sum_{ij} x_{ij}||l_i - g_j||^2)

这里 x i j x_{ij} 表示表示边界框和真实框的 IoU 大于 0.5 才会列入计算。

019.png

再是实际预测时候,不是直接预测边界框,而是预测边界框相对于锚框的偏移量,在训练时候也是要学到让预测边界框相对于锚框的偏移量接近真实框(GT)相对于锚框的偏移量。

在上图中,位于右上角图中,用绿色框表示真实框,在图像中,通常以左上角点作为原点,然后 x 和 y 表示目标中心点,w 和 h 表示目标宽和高。接下来在左下角图中,橘黄色框表示一个锚框,通过 IoU 计算这个锚框和真实框比较接近,如果用 y , x y^{\prime},x^{\prime} 表示锚框中心点, h , w h^{\prime},w^{\prime} 表示锚框高和宽,要学习的是锚框相对于真实框偏移量也就是 Δ x , Δ y \Delta x ,\Delta y

x = x + Δ x y = y + Δ y x = x^{\prime} + \Delta x\\ y = y^{\prime} + \Delta y

其实继续来看右下角图,并不是直接预测 Δ x , Δ y \Delta x ,\Delta y ,而是 Δ x / w , Δ y / h \Delta x/w^{\prime} ,\Delta y/h^{\prime} Δ x , Δ y \Delta x ,\Delta y 进行了归一化的处理,同对于宽高是预测 log ( w / w ) \log(w/w^{\prime}) log ( h / h ) \log(h/h^{\prime}) 其实这样做的好处就是将这些值归一化到 0 到 1 之间,便于学习和预测。

smooth L1 损失函数

这里从名字上来看

在 Fast RCNN 和 SSD 关于边界框位置回归都用到了 Smooth L1 这个损失函数,这个损失函数具有 L1 和 L2 优点,也就是首先 Smooth L1 要比 L1 收敛快,但是相对于 L2 函数对离群点又不那么敏感,梯度变化相对更小。

但L2范数的缺点是当存在离群点的时候,这些点会占loss的主要组成部分。比如说真实值为1,预测10次,有一次预测值为1000,其余次的预测值为1左右,显然loss 值主要由 1000 这个离群点占得比重比较大。所以 FastRCNN 采用稍微缓和一点绝对损失函数(smooth L1损失),是随着误差线性增长,而不是平方增长,通过上图可以看出 L1 、L2 和 Smooth L1 三者之间的关系。

017.png

0.5 x 2    i f x < 1 x 0.5    o t h e r w i s e 0.5x^2 \; if|x| < 1\\ |x| - 0.5 \; otherwise
L l o c = i j m x , y , h , w 1 i j m a t c h L 1 s m o o t h ( d m i t m i ) 2 L_{loc} = \sum_{ij} \sum_{m \in x,y,h,w} \mathbb{1}_{ij}^{match} L_1^{smooth} (d_m^i - t_m^i)^2

我正在参与掘金技术社区创作者签约计划招募活动,点击链接报名投稿

Guess you like

Origin juejin.im/post/7120552422438600712