1、ASPP模型结构
空洞空间卷积池化金字塔(atrous spatial pyramid pooling (ASPP))通过对于输入的特征以不同的采样率进行采样,即从不同尺度提取输入特征,然后将所获取的特征进行融合,得到最终的特征提取结果。
2、SENET结构
通道注意力机制(SENET)将尺度为HXWXC尺度大小的特征图通过全局平均池化进行压缩,只保留通道尺度上的大小C,即转换为1X1XC,之后再进行压缩,relu与还原,最后使用simoid进行激活,将各个通道的值转化为0~1范围内,相当于将各个通道的特征转换为权重值。
SENET代码如下:
import torchimport torch.nn as nnimport torch.nn.functional as F# tensor=torch.ones(size=(2,1280,32,32))# print(tensor)class SE_Block(nn.Module): # Squeeze-and-Excitation block def __init__(self, in_planes): super(SE_Block, self).__init__() self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.conv1 = nn.Conv2d(in_planes, in_planes // 16, kernel_size=1) self.relu = nn.ReLU() self.conv2 = nn.Conv2d(in_planes // 16, in_planes, kernel_size=1) self.sigmoid = nn.Sigmoid() def forward(self, x): x = self.avgpool(x) x = self.conv1(x) x = self.relu(x) x = self.conv2(x) out = self.sigmoid(x) return out
(如果要直接使用下面的SE_ASPP改进代码,建议将这块代码新建py文件保存,然后在SE_ASPP所在python中导入SE_Block类)
3、改进ASPP:SE_ASPP结构
即把SENET产生的权重值与原本输入的各个特征进行相乘,作为输入特征。代码如下
class SE_ASPP(nn.Module): ##加入通道注意力机制 def __init__(self, dim_in, dim_out, rate=1, bn_mom=0.1): super(SE_ASPP, self).__init__() self.branch1 = nn.Sequential( nn.Conv2d(dim_in, dim_out, 1, 1, padding=0, dilation=rate, bias=True), nn.BatchNorm2d(dim_out, momentum=bn_mom), nn.ReLU(inplace=True), ) self.branch2 = nn.Sequential( nn.Conv2d(dim_in, dim_out, 3, 1, padding=6 * rate, dilation=6 * rate, bias=True), nn.BatchNorm2d(dim_out, momentum=bn_mom), nn.ReLU(inplace=True), ) self.branch3 = nn.Sequential( nn.Conv2d(dim_in, dim_out, 3, 1, padding=12 * rate, dilation=12 * rate, bias=True), nn.BatchNorm2d(dim_out, momentum=bn_mom), nn.ReLU(inplace=True), ) self.branch4 = nn.Sequential( nn.Conv2d(dim_in, dim_out, 3, 1, padding=18 * rate, dilation=18 * rate, bias=True), nn.BatchNorm2d(dim_out, momentum=bn_mom), nn.ReLU(inplace=True), ) self.branch5_conv = nn.Conv2d(dim_in, dim_out, 1, 1, 0, bias=True) self.branch5_bn = nn.BatchNorm2d(dim_out, momentum=bn_mom) self.branch5_relu = nn.ReLU(inplace=True) self.conv_cat = nn.Sequential( nn.Conv2d(dim_out * 5, dim_out, 1, 1, padding=0, bias=True), nn.BatchNorm2d(dim_out, momentum=bn_mom), nn.ReLU(inplace=True), ) # print('dim_in:',dim_in) # print('dim_out:',dim_out) self.senet=SE_Block(in_planes=dim_out*5) def forward(self, x): [b, c, row, col] = x.size() conv1x1 = self.branch1(x) conv3x3_1 = self.branch2(x) conv3x3_2 = self.branch3(x) conv3x3_3 = self.branch4(x) global_feature = torch.mean(x, 2, True) global_feature = torch.mean(global_feature, 3, True) global_feature = self.branch5_conv(global_feature) global_feature = self.branch5_bn(global_feature) global_feature = self.branch5_relu(global_feature) global_feature = F.interpolate(global_feature, (row, col), None, 'bilinear', True) feature_cat = torch.cat([conv1x1, conv3x3_1, conv3x3_2, conv3x3_3, global_feature], dim=1) # print('feature:',feature_cat.shape) seaspp1=self.senet(feature_cat) #加入通道注意力机制 # print('seaspp1:',seaspp1.shape) se_feature_cat=seaspp1*feature_cat result = self.conv_cat(se_feature_cat) # print('result:',result.shape) return result
Reference
[1].Y. Sun, Y. Yang, G. Yao, F. Wei and M. Wong, “Autonomous Crack and Bughole Detection for Concrete Surface Image Based on Deep Learning,” in IEEE Access, vol. 9, pp. 85709-85720, 2021, doi: 10.1109/ACCESS.2021.3088292.
[2].J. Hu, L. Shen and G. Sun, “Squeeze-and-Excitation Networks,” 2018 IEEE/CVF Conference on Computer Vision and Pattern Recognition, 2018, pp. 7132-7141, doi: 10.1109/CVPR.2018.00745.
来源地址:https://blog.csdn.net/qq_45014374/article/details/127507120