目标检测之FPN(Feature Pyramid Net)

本文介绍FPN(Feature Pyramid Network)的概念,它源于传统图像处理中的图像金字塔思想,旨在提升模型对不同大小物体的检测能力。文章对比了多种目标检测模型的多尺度特征处理方式,详细解释了FPN如何通过上采样和特征融合实现既考虑全局特征又兼顾局部特征的目标检测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FPN思想

FPN的思想来源是传统图像处理中的图像金字塔(image pyramid),例如sift特征构建中使用的不同尺度的图像金字塔。FPN思想的主要是用来提高模型对不同大小输入图像以及目标检测问题中不同大小物体的鲁棒性。

模型结构

在这里插入图片描述

  • (a)中的图像金字塔,即将图像resize到不同的大小,然后分别对不同大小的图片使用模型进行训练和检测。这种方法的缺点在于增加了时间成本。有些算法会在测试时候采用图像金字塔。
  • SPP net,Fast RCNN,Faster RCNN是采用(b)方式,即仅采用网络最后一层的特征。
  • SSD(Single Shot Detector)采用(c)多尺度特征的方式,没有上采样过程,即从网络不同层抽取不同尺度的特征做预测,这种方式不会增加额外的计算量。作者认为SSD算法中没有用到足够低层的特征(在SSD中,最低层的特征是VGG网络的conv4_3),而在作者看来足够低层的特征对于检测小物体是很有帮助的。但是我个人认为,在这里SSD只是用了一个浅层layer的特征,进行目标检测,会缺少全局语义特征。只是用一个深层layer的特征进行目标检测会缺少局部特征细节。这才是SSD在使用多尺度特征进行目标检测的时候的主要问题
  • FPN采用(d)方式,多尺度特征融合,实现了在目标检测的时候既考虑全局特征同时又考虑局部特征。

多尺度特征融合方式

在这里插入图片描述

  • 将上层特征进行上采样得到和下层特征图同样大小的特征图,然后两个特征图进行融合得到最终的特征图,使用该特征图作为进一步处理的输入特征。

FPN效果

在这里插入图片描述

### FPNFeature Pyramid Network)模型 示例代码 实现 以下是基于 PyTorch 的 Feature Pyramid Network (FPN) 模型的一个简单实现示例。此代码展示了如何构建一个多尺度特征金字塔结构,并将其应用于目标检测任务。 ```python import torch import torch.nn as nn import torchvision.models as models class FPN(nn.Module): def __init__(self, out_channels=256): super(FPN, self).__init__() # 使用 ResNet-50 作为 Backbone 提取基础特征 resnet = models.resnet50(pretrained=True) self.layer1 = nn.Sequential(resnet.conv1, resnet.bn1, resnet.relu, resnet.maxpool, resnet.layer1) self.layer2 = resnet.layer2 self.layer3 = resnet.layer3 self.layer4 = resnet.layer4 # Lateral connections to reduce channel size self.latlayer1 = nn.Conv2d(2048, out_channels, kernel_size=1, stride=1, padding=0) self.latlayer2 = nn.Conv2d(1024, out_channels, kernel_size=1, stride=1, padding=0) self.latlayer3 = nn.Conv2d(512, out_channels, kernel_size=1, stride=1, padding=0) # Top-down pathway and smooth layers self.toplayer = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1) self.smooth1 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1) self.smooth2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1) def _upsample_add(self, x, y): """Upsample tensor `y` to match the spatial dimensions of `x`, then add them together.""" _, _, H, W = x.size() return torch.nn.functional.interpolate(y, size=(H,W), mode='bilinear', align_corners=False) + x def forward(self, x): c1 = self.layer1(x) # Output shape: [B, 256, H/4, W/4] c2 = self.layer2(c1) # Output shape: [B, 512, H/8, W/8] c3 = self.layer3(c2) # Output shape: [B, 1024, H/16, W/16] c4 = self.layer4(c3) # Output shape: [B, 2048, H/32, W/32] p4 = self.latlayer1(c4) # Apply lateral connection on C4 p3 = self._upsample_add(p4, self.latlayer2(c3)) # Upsample P4 and add it with C3 after applying a lateral layer. p2 = self._upsample_add(p3, self.latlayer3(c2)) # Repeat process for C2. p4 = self.toplayer(p4) # Smooth top-layer output using convolutional smoothing operation. p3 = self.smooth1(p3) # Smooth intermediate outputs similarly. p2 = self.smooth2(p2) # Final smoothed result at highest resolution level. return p2, p3, p4 # Return multi-scale feature maps from different levels of pyramid structure. # Example usage: if __name__ == "__main__": model = FPN(out_channels=256).cuda() # Initialize FPN module with specified number of channels per map. input_tensor = torch.randn((1, 3, 224, 224)).cuda() # Create dummy batched image data tensor shaped appropriately. features = model(input_tensor) # Pass through network; get list containing three tensors corresponding to each scale's processed version. print([f.shape for f in features]) # Print shapes just so we can verify correctness visually here too! Should see something like [(1,256,H,W)] where height & width depend upon original input size divided by powers-of-two according to respective stages within architecture design itself... ``` 上述代码实现了基本的 FPN 结构,其中使用了 ResNet-50 作为骨干网络来提取多级特征图[^1]。通过横向连接和自顶向下的路径,该架构可以生成具有相同通道数但分辨率不同的多个特征图,从而支持多尺度目标检测需求[^4]。 #### 关键点解释 - **Backbone**: 这里选择了预训练好的 ResNet-50 模型作为主干网路,负责提取原始输入图片的基础特征[^3]。 - **Lateral Connections**: 利用卷积操作减少高层特征图的维度至统一尺寸以便后续融合处理。 - **Top-Down Pathway**: 将低分辨率高语义级别的特征逐步插值放大并与更高分辨率较低层次的信息相结合形成最终输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值