Task2-网络设计

写在前面
感谢datawhale的小伙伴用心编写提供了这么好的教程:动手学CV-Pytorch
再推荐一下这篇文章: 目标检测中的Anchor机制回顾,前面的历史溯源参考了这篇文章的开头。这篇文章所在的系列《图解目标检测》也肥肠不错!

0.学习内容及任务描述

学习两块原理性的内容:锚框 和 目标检测的网络结构设计
分别对应《动手学CV-Pytorch》的3.3和3.4两节:
锚框 & 模型结构

学习任务:
了解锚框的概念及作用
学习锚框的生成代码
学习本教程所介绍的网络结构,包括特征提取模块和输出模块
学习边界框的编解码,并思考其作用
思考anchor是如何在网络中发挥作用的。

1. 锚框

1.1 关于锚框

Anchor机制首先由Faster RCNN提出,但是这个思路已在RCNN中得到使用(bounding box)。后续大量流行的模型如SSD(称之为prior bounding box),Mask RCNN,RetinaNet等等都是建立在anchor基础上的,而yolo-v3,yolo-v5等模型尽管对anchor进行了一些调整,但出发点依然不变,还是从anchor的三个任务和参数化坐标出发。Anchor 和 prior bounding box 实际上是一个概念的不同叫法。

目标检测中最初的解决方案是遍历图片上每一个可能的目标框,再对其进行分类和微调。当然不可能将图片上面的每一个部分都送入网络中进行检测,因此需要锚框来缩小检测范围。

锚框的作用: 解决如何定义哪些位置是候选目标框的问题。

1.1.1 设置不同尺度的锚框

为了覆盖更多可能的情况,在图中的同一个位置,会设置几个不同尺度的锚框(不同大小,不同长宽比)
在这里插入图片描述

可以看到,通过设置不同的尺度的锚框,就有更高的概率出现对于目标物体有良好匹配度的锚框(体现为高IoU)。

1.1.2 锚框与特征图的对应

除了不同尺度,我们肯定要将锚框铺在图片中不同位置上面。但是通过遍历原图每个像素来设置的锚框就完全没必要,太多了。

如下图所示。一个224x224的图片,假设每个位置设置3个不同尺寸的锚框,那么就有224x224x3=150528个,但是如果我们不去遍历原图,而是去遍历原图下采样得到的feature map呢?
以vgg16的backbone为例,下采样了5次,得到7x7的feature map,那就只需要得到7x7x3=147个先验,这样的设置大大减少了锚框的数量,同时也能覆盖大多数情况。

在这里插入图片描述

因此,我们就将锚框的设置位置与特征图建立一一对应的关系。而且,通过建立这种映射关系,我们可以通过特征图,直接一次性的输出所有锚框的类别信息以及坐标信息,而不是想前面一直描述的那样,每个候选框都去独立的进行一次分类的预测,这样太慢了。

1.1.3 锚框类别信息的确定

对于预先铺设的锚框,先要给出这些框的类别信息,才能让模型学着去预测每个锚框是否对应着一个目标物体。

但是这些锚框中有很多是和图片中我们要检测的目标完全没有交集或者有很小的交集,如何划分他们是目标还是背景呢?

我们的做法是,设定一个IoU阈值,例如IoU=0.5,与图片中目标的IoU<0.5的锚框,这些框我们将其划分为背景,IoU>=0.5的被归到目标锚框,通过这样划分,得到供模型学习的ground truth信息
在这里插入图片描述

1.2 锚框之如何生成

锚框的生成方式通常采用预先定义的尺度大小scale和高宽比ratio生成。

  • scale:anchor原始大小(通常是正方形的,设初始大小为 a ∗ a a * a aa
  • ratio:调剂anchor的长宽比例,从而生成各种形状的anchor。指生成的anchor的h和w相对于原始正方形anchor边长的比例平方。
    h e i g h t = r ∗ a height = \sqrt{r} * a height=r a
    w i d t h = 1 r ∗ a width = \frac1 {\sqrt{r}}*a width=r 1a

废话不多说,上代码

"""
设置细节介绍:
1. 离散程度 fmap_dims = 7: VGG16最后的特征图尺寸为 7*7
2. 在上面的举例中我们是假设了三种尺寸的锚框,然后遍历坐标。在锚框生成过程中,锚框的尺寸是提前设置好的,本教程为特征图上每一个cell定义了共9种不同大小和形状的候选框(3种尺度*3种长宽比=9)

生成过程:
0. cx, cy表示中心点坐标
1. 遍历特征图上每一个cell,i+0.5是为了从坐标点移动至cell中心,/fmap_dims目的是将坐标在特征图上归一化
2. 这个时候我们已经可以在每个cell上各生成一个框了,但是这个不是我们需要的,我们称之为base_prior_bbox基准框。
3. 根据我们在每个cell上得到的长宽比1:1的基准框,结合我们设置的3种尺度obj_scales和3种长宽比aspect_ratios就得到了每个cell的9个锚框。
4. 最终结果保存在prior_boxes中并返回。

需要注意的是,这个时候我们的到的锚框是针对特征图的尺寸并归一化的,因此要映射到原图计算IOU或者展示,需要:
img_prior_boxes = prior_boxes * 图像尺寸
"""

def create_prior_boxes():
        """
        Create the 441 prior (default) boxes for the network, as described in the tutorial.
        VGG16最后的特征图尺寸为 7*7
        我们为特征图上每一个cell定义了共9种不同大小和形状的候选框(3种尺度*3种长宽比=9)
        因此总的候选框个数 = 7 * 7 * 9 = 441
        return: prior boxes in center-size coordinates, a tensor of dimensions (441, 4)
        """
        fmap_dims = 7 
        obj_scales = [0.2, 0.4, 0.6]
        aspect_ratios = [1., 2., 0.5]

        prior_boxes = []
        for i in range(fmap_dims):
            for j in range(fmap_dims):
                cx = (j + 0.5) / fmap_dims
                cy = (i + 0.5) / fmap_dims

                for obj_scale in obj_scales:
                    for ratio in aspect_ratios:
                        prior_boxes.append([cx, cy, obj_scale * sqrt(ratio), obj_scale / sqrt(ratio)])

        prior_boxes = torch.FloatTensor(prior_boxes).to(device)  # (441, 4)
        prior_boxes.clamp_(0, 1)  # (441, 4)

        return prior_boxes

将锚框可视化展示结果,为了便于观看,仅展示特征图中间那个cell对应的先验框。
这里为了对比,我们设置两组obj_scales尺度参数。

  • obj_scales = [0.1, 0.2, 0.3]

这里的参数是归一化的,0.1代表anchor的基准大小为原图长/宽的0.1那么大。

在这里插入图片描述

可以看到,我们在图片中心得到了各个尺度和宽高比的先验框。

  • obj_scales = [0.2, 0.4, 0.6]
    在这里插入图片描述

这里对比两组不同的尺度设置,需要注意一个小问题,那就是越界,可以看到第二组可视化部分蓝色和绿色的先验框都超出图片界限了,越靠近四周的位置的先验框越容易越界,那么这个问题怎么处理呢?
这里我们一般用图片尺寸将越界的先验框进行截断,保留未越界的,截断越界的。比如某个先验框左上角坐标是(-5,-9),那么就截断为(0,0),某个先验框右下角坐标是(324,134),当我们的图片大小为(224,224)时,就将其截断为(224,134)。

对应于代码中是这行,prior_boxes.clamp_(0, 1),由于进行了归一化,所以使用0-1进行截断。(括号内参数为截断边界)

2. 结构模型

教程里所介绍的网络称其为Tiny_Detector,是为了本教程特意设计的网络,而并不是某个经典的目标检测网络。如果一定要溯源的话,由于代码是由一个外国的开源SSD教程改编而来,因此很多细节上也更接近SSD网络,可以认为是一个简化后的版本,目的是便于入门。

下面就开始介绍Tiny_Detector的模型结构

2.1 以VGG16作为backbone

为了使结构简单易懂,使用VGG16作为backbone,即完全采用vgg16的结构作为特征提取模块,只是去掉fc6和fc7两个全连接层,如下图所示

在这里插入图片描述

对于网络的输入尺寸的确定,由于vgg16的ImageNet预训练模型是使用224x224尺寸训练的,因此我们的网络输入也固定为224x224,和预训练模型尺度保持一致可以更好的发挥其作用。通常来说,这样的网络输入大小,对于检测网络来说还是偏小,在完整的进行完本章的学习后,不妨尝试下将输入尺度扩大,看看会不会带来更好的效果。

特征提取模块对应代码模块在model.py中的VGGBase类进行了定义:

class VGGBase(nn.Module):                                                                                                                                         
    """
    VGG base convolutions to produce feature maps.
    完全采用vgg16的结构作为特征提取模块,丢掉fc6和fc7两个全连接层。
    因为vgg16的ImageNet预训练模型是使用224×224尺寸训练的,因此我们的网络输入也固定为224×224
    """

    def __init__(self):
        super(VGGBase, self).__init__()

        # Standard convolutional layers in VGG16
        self.conv1_1 = nn.Conv2d(3, 64, kernel_size=3, padding=1)  # stride = 1, by default
        self.conv1_2 = nn.Conv2d(64, 64, kernel_size=3, padding=1)
        self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2)    # 224->112

        self.conv2_1 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
        self.conv2_2 = nn.Conv2d(128, 128, kernel_size=3, padding=1)
        self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2)    # 112->56

        self.conv3_1 = nn.Conv2d(128, 256, kernel_size=3, padding=1)
        self.conv3_2 = nn.Conv2d(256, 256, kernel_size=3, padding=1)
        self.conv3_3 = nn.Conv2d(256, 256, kernel_size=3, padding=1)
        self.pool3 = nn.MaxPool2d(kernel_size=2, stride=2)    # 56->28

        self.conv4_1 = nn.Conv2d(256, 512, kernel_size=3, padding=1)
        self.conv4_2 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
        self.conv4_3 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
        self.pool4 = nn.MaxPool2d(kernel_size=2, stride=2)    # 28->14

        self.conv5_1 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
        self.conv5_2 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
        self.conv5_3 = nn.Conv2d(512, 512, kernel_size=3, padding=1)
        self.pool5 = nn.MaxPool2d(kernel_size=2, stride=2)    # 14->7

        # Load pretrained weights on ImageNet
        self.load_pretrained_layers()


    def forward(self, image):
        """
        Forward propagation.

        :param image: images, a tensor of dimensions (N, 3, 224, 224)
        :return: feature maps pool5
        """
        out = F.relu(self.conv1_1(image))  # (N, 64, 224, 224)
        out = F.relu(self.conv1_2(out))  # (N, 64, 224, 224)
        out = self.pool1(out)  # (N, 64, 112, 112)

        out = F.relu(self.conv2_1(out))  # (N, 128, 112, 112)
        out = F.relu(self.conv2_2(out))  # (N, 128, 112, 112)
        out = self.pool2(out)  # (N, 128, 56, 56)

        out = F.relu(self.conv3_1(out))  # (N, 256, 56, 56)
        out = F.relu(self.conv3_2(out))  # (N, 256, 56, 56)
        out = F.relu(self.conv3_3(out))  # (N, 256, 56, 56)
        out = self.pool3(out)  # (N, 256, 28, 28)

        out = F.relu(self.conv4_1(out))  # (N, 512, 28, 28)
        out = F.relu(self.conv4_2(out))  # (N, 512, 28, 28)
        out = F.relu(self.conv4_3(out))  # (N, 512, 28, 28)
        out = self.pool4(out)  # (N, 512, 14, 14)

        out = F.relu(self.conv5_1(out))  # (N, 512, 14, 14)
        out = F.relu(self.conv5_2(out))  # (N, 512, 14, 14)
        out = F.relu(self.conv5_3(out))  # (N, 512, 14, 14)
        out = self.pool5(out)  # (N, 512, 7, 7)

        # return 7*7 feature map                                                                                                                                  
        return out


    def load_pretrained_layers(self):
        """
        we use a VGG-16 pretrained on the ImageNet task as the base network.
        There's one available in PyTorch, see https://pytorch.org/docs/stable/torchvision/models.html#torchvision.models.vgg16
        We copy these parameters into our network. It's straightforward for conv1 to conv5.
        """
        # Current state of base
        state_dict = self.state_dict()
        param_names = list(state_dict.keys())

        # Pretrained VGG base
        pretrained_state_dict = torchvision.models.vgg16(pretrained=True).state_dict()
        pretrained_param_names = list(pretrained_state_dict.keys())

        # Transfer conv. parameters from pretrained model to current model
        for i, param in enumerate(param_names):  
            state_dict[param] = pretrained_state_dict[pretrained_param_names[i]]

        self.load_state_dict(state_dict)
        print("\nLoaded base model.\n")

Tiny_Detector特征提取层输出的是7x7的feature map,下面要在feature_map上设置对应的anchor,配置如下:

  • 将原图均匀分成7x7个cell
  • 3种不同的尺度:0.2, 0.4, 0.6
  • 3种不同的长宽比:1:1, 1:2, 2:1

因此,对这 7x7 的 feature map 设置了对应的 7x7x9 个anchor框,其中每一个cell有9个anchor框。下图便是中间的cell对应生成的9个anchor

在这里插入图片描述
对于每个anchor,我们需要预测两类信息,一个是这个anchor的类别信息,一个是物体的边界框信息。

使用VOC数据集时,类别信息由21类别的得分组成(20个类别 + 一个背景类),模型最终会选择预测得分最高的类作为边界框对象的类别。

边界框信息是指,我们大致知道了当前anchor中包含一个物体的情况下,如何对anchor进行微调,使得最终能够准确预测出物体的bbox。
在这里插入图片描述
这两种预测我们分别成为分类头(类别信息预测)和回归头(边界框信息微调),那么分类头预测和回归头预测是怎么得到的?

其实只需在7x7的feature map后,接上两个3x3的卷积层,即可分别完成分类和回归的预测

2.2 分类头和回归头

2.2.1 边界框编码&解码

Tiny_Detector并不是直接预测目标框,而是回归对于anchor要进行多大程度的调整,才能更准确的预测出边界框的位置。那么我们的目标就是需要找一种方法来量化计算这个偏差。

对于一只狗的目标边界框和先验框的示例如下图所示:

在这里插入图片描述
我们的模型要预测anchor与目标框的偏移,并且这个偏移会进行某种形式的归一化,这个过程我们称为边界框的编码

这里我们使用的是与SSD完全一致的编码方法,具体公示表达如下:
g c x = c x − c x ^ w ^ g_{cx} = \frac {c_x - \hat {c_x}} {\hat {w}} gcx=w^cxcx^
g c y = c y − c y ^ y ^ g_{cy} = \frac {c_y - \hat {c_y}} {\hat {y}} gcy=y^cycy^
g w = l o g ( w w ^ ) g_{w} = log(\frac w {\hat {w}}) gw=log(w^w)
g h = l o g ( h h ^ ) g_{h} = log(\frac h {\hat {h}}) gh=log(h^h)

模型预测并输出的是这个编码后的偏移量 ( g c x , g c y , g w , g h ) {(g_{cx},g_{cy},g_{w},g_{h})} (gcxgcygwgh) 。最终只要再依照公式反向进行解码,就可以得到预测的目标框的信息。
目标框编码与解码的代码如下

def cxcy_to_gcxgcy(cxcy, priors_cxcy):
    """ 
    Encode bounding boxes (that are in center-size form) w.r.t. the corresponding prior boxes (that are in center-size form).

    For the center coordinates, find the offset with respect to the prior box, and scale by the size of the prior box.
    For the size coordinates, scale by the size of the prior box, and convert to the log-space.

    In the model, we are predicting bounding box coordinates in this encoded form.

    :param cxcy: bounding boxes in center-size coordinates, a tensor of size (n_priors, 4)
    :param priors_cxcy: prior boxes with respect to which the encoding must be performed, a tensor of size (n_priors, 4)
    :return: encoded bounding boxes, a tensor of size (n_priors, 4)
    """

    # The 10 and 5 below are referred to as 'variances' in the original SSD Caffe repo, completely empirical
    # They are for some sort of numerical conditioning, for 'scaling the localization gradient'
    # See https://github.com/weiliu89/caffe/issues/155
    return torch.cat([(cxcy[:, :2] - priors_cxcy[:, :2]) / (priors_cxcy[:, 2:] / 10),  # g_c_x, g_c_y
                      torch.log(cxcy[:, 2:] / priors_cxcy[:, 2:]) * 5], 1)  # g_w, g_h


def gcxgcy_to_cxcy(gcxgcy, priors_cxcy):
    """ 
    Decode bounding box coordinates predicted by the model, since they are encoded in the form mentioned above.

    They are decoded into center-size coordinates.

    This is the inverse of the function above.

    :param gcxgcy: encoded bounding boxes, i.e. output of the model, a tensor of size (n_priors, 4)
    :param priors_cxcy: prior boxes with respect to which the encoding is defined, a tensor of size (n_priors, 4)
    :return: decoded bounding boxes in center-size form, a tensor of size (n_priors, 4)
    """

    return torch.cat([gcxgcy[:, :2] * priors_cxcy[:, 2:] / 10 + priors_cxcy[:, :2],  # c_x, c_y
                      torch.exp(gcxgcy[:, 2:] / 5) * priors_cxcy[:, 2:]], 1)  # w, h

2.2.2 分类头与回归头预测

按照前面的介绍,对于输出7x7的feature map上的每个先验框我们想预测:

1)边界框的一组21类分数,其中包括VOC的20类和一个背景类。

2)边界框编码后的偏移量 ( g c x , g c y , g w , g h ) {(g_{cx},g_{cy},g_{w},g_{h})} (gcxgcygwgh)

为了得到我们想预测的类别和偏移量,我们需要在feature map后分别接上两个卷积层:

1)一个分类预测的卷积层(黄色),采用 3x3 卷积核,padding 和 stride 都为 1,每个 anchor 需要分配 21 个卷积核,每个位置有 9 个 anchor,因此需要 21x9 个卷积核。

2)一个定位预测卷积层(蓝色),每个位置使用 3x3 卷积核,padding 和 stride 都为 1,每个 anchor 需要分配 4 个卷积核,因此需要 4x9 个卷积核。

回归头和分类头的输出feature map大小 7x7 保持不变。下图直观给出这些卷积上的输出:
在这里插入图片描述

我们真正关心的是第三维度通道数,即每个cell中9个anchor预测编码偏移量,把其具体的展开可以看到如下图所示:
在这里插入图片描述

也就是说,最终回归头的输出有36个通道,其中每4个值就对应了一个anchor的编码后偏移量的预测,这样的4个值的预测共有9组,因此通道数是36。

分类头可以用同样的方式理解,下图为 每个cell中9个anchor预测分类得分:

在这里插入图片描述

分类头和回归头结构的定义代码如下:

class PredictionConvolutions(nn.Module):
    """ 
    Convolutions to predict class scores and bounding boxes using feature maps.

    The bounding boxes (locations) are predicted as encoded offsets w.r.t each of the 441 prior (default) boxes.
    See 'cxcy_to_gcxgcy' in utils.py for the encoding definition.
    这里预测坐标的编码方式完全遵循的SSD的定义

    The class scores represent the scores of each object class in each of the 441 bounding boxes located.
    A high score for 'background' = no object.
    """

    def __init__(self, n_classes):
        """ 
        :param n_classes: number of different types of objects
        """
        super(PredictionConvolutions, self).__init__()

        self.n_classes = n_classes

        # Number of prior-boxes we are considering per position in the feature map
        # 9 prior-boxes implies we use 9 different aspect ratios, etc.
        n_boxes = 9 

        # Localization prediction convolutions (predict offsets w.r.t prior-boxes)
        self.loc_conv = nn.Conv2d(512, n_boxes * 4, kernel_size=3, padding=1)

        # Class prediction convolutions (predict classes in localization boxes)
        self.cl_conv = nn.Conv2d(512, n_boxes * n_classes, kernel_size=3, padding=1)

        # Initialize convolutions' parameters
        self.init_conv2d()


    def init_conv2d(self):
        """
        Initialize convolution parameters.
        """
        for c in self.children():
            if isinstance(c, nn.Conv2d):
                nn.init.xavier_uniform_(c.weight)
                nn.init.constant_(c.bias, 0.)


    def forward(self, pool5_feats):
        """
        Forward propagation.

        :param pool5_feats: conv4_3 feature map, a tensor of dimensions (N, 512, 7, 7)
        :return: 441 locations and class scores (i.e. w.r.t each prior box) for each image
        """
        batch_size = pool5_feats.size(0)

        # Predict localization boxes' bounds (as offsets w.r.t prior-boxes)
        l_conv = self.loc_conv(pool5_feats)  # (N, n_boxes * 4, 7, 7)
        l_conv = l_conv.permute(0, 2, 3, 1).contiguous()  
        # (N, 7, 7, n_boxes * 4), to match prior-box order (after .view())
        # (.contiguous() ensures it is stored in a contiguous chunk of memory, needed for .view() below)
        locs = l_conv.view(batch_size, -1, 4)  # (N, 441, 4), there are a total 441 boxes on this feature map

        # Predict classes in localization boxes
        c_conv = self.cl_conv(pool5_feats)  # (N, n_boxes * n_classes, 7, 7)
        c_conv = c_conv.permute(0, 2, 3, 1).contiguous()  # (N, 7, 7, n_boxes * n_classes), to match prior-box order (after .view())
        classes_scores = c_conv.view(batch_size, -1, self.n_classes)  # (N, 441, n_classes), there are a total 441 boxes on this feature map

        return locs, classes_scores

按照上面的介绍,我们的模型输出的 shape 应该为:

  • 分类头 batch_size x 7 x 7 x 189
  • 回归头 batch_size x 7 x 7 x 36

但是为了方便后面的处理,我们肯定更希望每个 anchor 的预测独自成一维,也就是:

  • 分类头 batch_size x 441 x 21
  • 回归头 batch_size x 441 x 4

441是因为我们的模型定义了总共441=7x7x9个先验框,这个转换对应了这两行代码:

locs = l_conv.view(batch_size, -1, 4)
classes_scores = c_conv.view(batch_size, -1, self.n_classes)

这个过程的可视化如下图所示。
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值