FPN 特征金字塔网络

如图

FPN 特征金字塔网络_第1张图片 

直接上代码 

    def _upsample_add(self, x, y):
        _,_,H,W = y.size()
        # 使用 双线性插值bilinear对x进行上采样,之后与y逐元素相加
        return F.upsample(x, size=(H,W), mode='bilinear') + y

    def forward(self, x):
        # Bottom-up  自底向上   conv -> batchnmorm -> relu  ->maxpool
        c1 = F.relu(self.bn1(self.conv1(x)))
        c1 = F.max_pool2d(c1, kernel_size=3, stride=2, padding=1)

        # resnet网络
        c2 = self.layer1(c1)
        c3 = self.layer2(c2)
        c4 = self.layer3(c3)
        c5 = self.layer4(c4)

        # Top-down  自顶向下并与侧边相连


        p5 = self.toplayer(c5)  #1*1 卷积减少通道数
        p4 = self._upsample_add(p5, self.latlayer1(c4))
        p3 = self._upsample_add(p4, self.latlayer2(c3))
        p2 = self._upsample_add(p3, self.latlayer3(c2))

        # Smooth  平滑层(在融合之后还会再采用3*3的卷积核对每个融合结果进行卷积,目的是消除上 
    采样的混叠效应)
        p4 = self.smooth1(p4)
        p3 = self.smooth2(p3)
        p2 = self.smooth3(p2)
        return p2, p3, p4, p5



    ##self.latlayer1 = nn.Conv2d(1024, 256, kernel_size=1, stride=1, padding=0)
    ##self.latlayer2 = nn.Conv2d( 512, 256, kernel_size=1, stride=1, padding=0)
    ##self.latlayer3 = nn.Conv2d( 256, 256, kernel_size=1, stride=1, padding=0)

 

 

你可能感兴趣的:(目标检测)