當前位置:網站首頁>【論文筆記】EPSANet: An Efficient Pyramid Squeeze Attention Block on Convolutional Neural Network

【論文筆記】EPSANet: An Efficient Pyramid Squeeze Attention Block on Convolutional Neural Network

2022-05-15 07:05:25m0_61899108

論文

論文題目:EPSANet: An Efficient Pyramid Squeeze Attention Block on Convolutional Neural Network

收錄:CVPR2021

論文地址:https://arxiv.org/abs/2105.14447

項目地址:GitHub - murufeng/EPSANet

問題&解决

使用通道注意力和空間注意力可以提昇性能。如,SE模塊,能以較低成本提昇性能,但僅考慮通道信息,忽略了空間信息;BAM和CBAM結合空間和通道注意力來豐富注意力圖。但仍有兩個問題:

  • 如何有效捕獲和利用不同尺度的特征圖的空間信息來豐富特征空間;
  • 通道或空間注意力只能有效捕捉局部信息,難以建立long-range的通道依賴關系。

現有方法雖能解决上述問題,但會增加模型複雜度和計算開銷。論文提出一個低成本且有效的注意力模塊——金字塔壓縮注意力(Pyramid Squeeze Attention,PSA),旨在以較低模型複雜度學習注意力權重,並有效整合局部注意力和全局注意力以建立long-range長期通道依賴關系。PSA模塊可以處理多尺度的輸入特征圖的空間信息並且能够有效地建立多尺度通道注意力間的長期依賴關系。然後,將PSA 模塊替換掉ResNet網絡Bottleneck中的3x3卷積,其餘保持不變,得到新的EPSA(efficient pyramid split attention)模塊。基於EPSA block論文構建了一個新的骨幹網絡EPSANet。它既可以提供强有力的多尺度特征錶示能力。EPSANet在圖像識別中的Top-1 Acc大幅度優於現有技術,而且在計算參數量上有更加高效。

主要貢獻

  • 提出一種新的Efficient Pyramid Squeeze Attention(EPSA)塊,有效提取更細粒度的多尺度空間信息,並發展long-range遠程通道依賴性;靈活可擴展,適用於各種網絡架構。
  • 提出EPSANet主幹網絡,可以學習更豐富的多尺度特征錶示並自適應地中心校准跨維度的通道注意力權重。
  • EPSANet在ImageNet和COCO數據集上的圖像分類、目標檢測、實例分割取得很好的結果。

方法

通道注意機制允許網絡選擇性地加權每個通道的重要性,從而生成更多信息輸出。

SE模塊

SE塊由兩部分組成:Squeeze壓縮和Excitation激勵,分別用於編碼全局信息和自適應重新校准通道關系。通道數據使用全局平均池化GAP來生成,將全局空間信息嵌入到通道描述中。全局平均池化公式為:

 再用兩個全連接層組合通道間的線性信息,幫助通道高維和低維信息的交互。c-th通道權重計算公式:

 分別錶示兩個全連接層,前一個降維,後一個維度恢複。

PSA模塊

本文主要是建立一個更高效的通道注意力機制。為此,提出了一種新的金字塔壓縮注意力(PSA)模塊。PSA模塊主要通過四個步驟實現:

  • 首先,利用SPC模塊來對通道進行切分,然後針對每個通道特征圖上的空間信息進行多尺度特征提取;
  • 第二,利用SEWeight模塊提取不同尺度特征圖的通道注意力,得到每個不同尺度上的通道注意力向量;
  • 第三,利用Softmax對多尺度通道注意力向量進行特征重新校准,得到新的多尺度通道交互後的注意力權重。
  • 第四,對重新校准的權重和相應的特征圖按元素進行點乘操作,輸出得到一個多尺度特征信息注意力加權之後的特征圖。該特征圖多尺度信息錶示能力更豐富。

SPC模塊

PSA模塊中實現多尺度特征提取最重要的模塊就是SPC。 

 其中,

 然後將多尺度的特征圖進行拼接後得到特征圖F。

EPSANet

EPSA block結構如最右圖所示,主要將ResNet中bottleneck部分的3x3卷積替換為PSA module。 

基於EPSA block,論文提出一種新的骨幹網絡架構:EPSANet,並且根據PSA module中的分組卷積的大小,具體網絡結構配置如下:

實驗

細節

結果

直接看圖就行,網絡、主幹、參數,性能比較,比較一清二楚。

ImageNet 圖像分類

 COCO2017 目標檢測

 COCO2017 實例分割

主要代碼

 SE_weight_module.py

import torch.nn as nn

class SEWeightModule(nn.Module):

    def __init__(self, channels, reduction=16):
        super(SEWeightModule, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.fc1 = nn.Conv2d(channels, channels//reduction, kernel_size=1, padding=0)
        self.relu = nn.ReLU(inplace=True)
        self.fc2 = nn.Conv2d(channels//reduction, channels, kernel_size=1, padding=0)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        out = self.avg_pool(x)
        out = self.fc1(out)
        out = self.relu(out)
        out = self.fc2(out)
        weight = self.sigmoid(out)

        return weight

epsanet.py

import torch
import torch.nn as nn
import math
from .SE_weight_module import SEWeightModule

def conv(in_planes, out_planes, kernel_size=3, stride=1, padding=1, dilation=1, groups=1):
    """standard convolution with padding"""
    return nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride,
                     padding=padding, dilation=dilation, groups=groups, bias=False)

def conv1x1(in_planes, out_planes, stride=1):
    """1x1 convolution"""
    return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)

class PSAModule(nn.Module):

    def __init__(self, inplans, planes, conv_kernels=[3, 5, 7, 9], stride=1, conv_groups=[1, 4, 8, 16]):
        super(PSAModule, self).__init__()
        self.conv_1 = conv(inplans, planes//4, kernel_size=conv_kernels[0], padding=conv_kernels[0]//2,
                            stride=stride, groups=conv_groups[0])
        self.conv_2 = conv(inplans, planes//4, kernel_size=conv_kernels[1], padding=conv_kernels[1]//2,
                            stride=stride, groups=conv_groups[1])
        self.conv_3 = conv(inplans, planes//4, kernel_size=conv_kernels[2], padding=conv_kernels[2]//2,
                            stride=stride, groups=conv_groups[2])
        self.conv_4 = conv(inplans, planes//4, kernel_size=conv_kernels[3], padding=conv_kernels[3]//2,
                            stride=stride, groups=conv_groups[3])
        self.se = SEWeightModule(planes // 4)
        self.split_channel = planes // 4
        self.softmax = nn.Softmax(dim=1)

    def forward(self, x):
        batch_size = x.shape[0]
        x1 = self.conv_1(x)
        x2 = self.conv_2(x)
        x3 = self.conv_3(x)
        x4 = self.conv_4(x)

        feats = torch.cat((x1, x2, x3, x4), dim=1)
        feats = feats.view(batch_size, 4, self.split_channel, feats.shape[2], feats.shape[3])

        x1_se = self.se(x1)
        x2_se = self.se(x2)
        x3_se = self.se(x3)
        x4_se = self.se(x4)

        x_se = torch.cat((x1_se, x2_se, x3_se, x4_se), dim=1)
        attention_vectors = x_se.view(batch_size, 4, self.split_channel, 1, 1)
        attention_vectors = self.softmax(attention_vectors)
        feats_weight = feats * attention_vectors
        for i in range(4):
            x_se_weight_fp = feats_weight[:, i, :, :]
            if i == 0:
                out = x_se_weight_fp
            else:
                out = torch.cat((x_se_weight_fp, out), 1)

        return out


class EPSABlock(nn.Module):
    expansion = 4

    def __init__(self, inplanes, planes, stride=1, downsample=None, norm_layer=None, conv_kernels=[3, 5, 7, 9],
                 conv_groups=[1, 4, 8, 16]):
        super(EPSABlock, self).__init__()
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        # Both self.conv2 and self.downsample layers downsample the input when stride != 1
        self.conv1 = conv1x1(inplanes, planes)
        self.bn1 = norm_layer(planes)
        self.conv2 = PSAModule(planes, planes, stride=stride, conv_kernels=conv_kernels, conv_groups=conv_groups)
        self.bn2 = norm_layer(planes)
        self.conv3 = conv1x1(planes, planes * self.expansion)
        self.bn3 = norm_layer(planes * self.expansion)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        identity = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample is not None:
            identity = self.downsample(x)

        out += identity
        out = self.relu(out)
        return out


class EPSANet(nn.Module):
    def __init__(self,block, layers, num_classes=1000):
        super(EPSANet, self).__init__()
        self.inplanes = 64
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layers(block, 64, layers[0], stride=1)
        self.layer2 = self._make_layers(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layers(block, 256, layers[2], stride=2)
        self.layer4 = self._make_layers(block, 512, layers[3], stride=2)
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = nn.Linear(512 * block.expansion, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def _make_layers(self, block, planes, num_blocks, stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion,
                          kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(planes * block.expansion),
            )

        layers = []
        layers.append(block(self.inplanes, planes, stride, downsample))
        self.inplanes = planes * block.expansion
        for i in range(1, num_blocks):
            layers.append(block(self.inplanes, planes))

        return nn.Sequential(*layers)

    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        x = x.view(x.size(0), -1)
        x = self.fc(x)

        return x


def epsanet50():
    model = EPSANet(EPSABlock, [3, 4, 6, 3], num_classes=1000)
    return model

def epsanet101():
    model = EPSANet(EPSABlock, [3, 4, 23, 3], num_classes=1000)
    return model

參考博客

EPSANet: 一種高效的多尺度通道注意力機制,主要提出了金字塔分割注意力模塊,即插即用,效果顯著,已開源! - 知乎

真的寫得太詳細了!!!

版權聲明
本文為[m0_61899108]所創,轉載請帶上原文鏈接,感謝
https://cht.chowdera.com/2022/135/202205142322538951.html

隨機推薦