目标检测(Object Detection)——理解SPP Layer

一、 前言

在目标检测系列文章的上一篇R-CNN中,我们知道R-CNN在当时虽然取得了不错的成绩,但是其需要改进的地方也很多,比如算法步骤比较繁琐,需要大量的时间和内存去训练和测试模型等。除此之外,在训练和测试常见的CNN网络时,要求输入的图像有一个固定的大小,比如要求图像的输入为224*224。(网络前面的卷积层不要求输入图像的大小,后面的全连接层的输入特征数是固定的,需要固定的输入)这就要求我们在使用网络前需要对图像进行一些预处理操作,比如:裁剪(crop)、拉伸(warp)等。文章《Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition》提出来一种Spatial Pyramid Pooling(SPP)结构来解决这种问题,将这种结构放在卷积层与全连接层中间,在多个测试中取得了不错的效果。

上面需要加入一些裁剪等操作,才能得到一个固定大小的输入,下面只需要加一个SPP层就可以达到相同作用,并且还有其他优点。

二、SPP原理

SPP的原理论文里写的很简单,在这里要重新回顾一下CNN中的一些问题:

  • 当尺寸大小不同的图像输入到相同的多层卷积网络中,得到的feature map大小是不同的,数量是相同(相同的filters)。
  • 对一个一个固定的CNN,全连接层的输入是一个固定的数值(这个数值提前设置好的),这就需要使用SPP插入多层卷积和全连接层中间。

在上面两个问题的基础上,以下图为例介绍核心的思想。

SPP原理图

  1. SPP的核心在于使用多个不同尺寸sliding window pooling(上图中的蓝色4*4、青色2*2、灰色1*1窗口)对上层(卷积层)获得的feature maps 进行采样(池化,文中使用最大池化),将分别得到的结果进行合并就会得到固定长度的输出。
  2. 上图可以看出SPP层就是在前一卷积层得到的feature maps上进行了3个池化操作(实际情况根据自己设定的池化个数,控制全连接层的输入,下面会讲)。最右边的就是原图像,中间的是把图像分成大小是4的特征图,最右边的就是把图像分成大小是16的特征图。这样每一个feature map就会变成固定的21(16+4+1)个feature maps。

通俗的讲,SPP就相当于标准通道层,不管任何大小的图像,我都用一套标准的pool(文中说叫: l-level pyramid)对图像进行池化,最后组合成一列相同大小的特征,作为全连接层的输入,这一组相同大小的特征是固定的,可以提前进行计算,计算的方法和规则下面进行讲解。

三、Single-size training 计算规则

为了便于理解,我们将原文中一大段话整理为下面几个步骤,并在最后给出一个论文中的图,辅助理解。

  1. 按照传统CNN网络,对于图像的输入需要一个固定的大小,假设为224×224。
  2. 经过五个卷积层后,conv5输出的 feature maps的大小为a×a。在第一步假设输入大小为224×224的基础上,feature maps的大小为13×13。
  3. 开始使用SPP层插入在conv5层后(对应SPP原理图),SPP层中想要得到一组n×n的和的特征(比如SPP原理图中1×1、2×2、4×4........一旦确定,就固定了),文中举例用的n=3、2、1。想要得到这样的一组特征,就要使用一组sliding window 对conv5层得到的feature maps进行pooling。这里涉及sliding window的大小(win)和步长(str)计算,计算如下:

n=2,n=1以此类推(那两个计算符号分别为:向上取整和向下取整),将3个pooling后的结果合并,可以得到如下图。

上图是一个3-level pyramid pooling,sizeX是sliding window的大小,stride是步长。

四、SPP评价

考虑到SPP就是为了奔着打破一些传统网络和形式的目的搞出来的,所以优点主要有:

  • 解决输入图片大小不一造成的缺陷。
  • 由于把一个feature map从不同的尺寸进行pooling特征抽取,再聚合,提高了算法的robust和精度。
  • 图像分类、目标检测都可以用,而且效果很棒。
  • 一定程度上缓解了R-CNN耗时过多等问题。

最后,放一张论文中的图,用不同尺寸的sliding window去pooling一张图(feature map)确实可以获得更多的特征。

参考文献

[1] Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition

转自:https://zhuanlan.zhihu.com/p/39717526

其它文章:https://blog.youkuaiyun.com/hjimce/article/details/50187655

### YOLO Object Detection Head Architecture and Implementation #### Overview of YOLO Detection Heads The detection head in YOLO models plays a crucial role in predicting bounding boxes and class probabilities. In YOLOv8, which forms part of the YOLO-World framework, the model employs a specific structure that includes components like Darknet as an image encoder along with PAN (Path Aggregation Network) for multi-scale feature pyramid processing[^1]. For earlier versions such as YOLOv4, CSPDarkNet53 serves as the backbone while SPP enhances receptive fields; meanwhile, PANet acts as the neck component before reaching the final prediction layer known from YOLOv3's anchor-based head design[^2]. #### Components within the Detection Head In more detail about how these elements work together inside the detection heads: - **Bounding Box Regression**: The task involves estimating coordinates defining where objects are located within images. - **Object Embedding/Classification Scores Calculation**: This process determines what type each detected item might be alongside confidence scores indicating likelihood. For implementing this functionality efficiently across different scales present throughout input data sets without losing spatial resolution or context information between layers during forward passes through neural networks architectures designed specifically around object recognition tasks – especially those involving small-to-medium sized items placed at varying distances relative to camera viewpoints used when capturing scenes containing them - both aforementioned approaches incorporate mechanisms aimed at preserving detailed representations necessary for accurate predictions. ```python import torch.nn as nn class YOLOHead(nn.Module): def __init__(self, num_classes=80, anchors=None): super(YOLOHead, self).__init__() # Define Convolution Layers here based on chosen architecture specifics def forward(self, x): # Implement Forward Pass Logic Here Including Bounding Boxes & Class Predictions Generation pass ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值