草庐IT

SENet代码复现+超详细注释(PyTorch)

路人贾\'ω\' 2023-05-27 原文

在卷积网络中通道注意力经常用到SENet模块,来增强网络模型在通道权重的选择能力,进而提点。关于SENet的原理和具体细节,我们在上一篇已经详细的介绍了:经典神经网络论文超详细解读(七)——SENet(注意力机制)学习笔记(翻译+精读+代码复现)

接下来我们来复现一下代码。

因为SENet不是一个全新的网络模型,而是相当于提出了一个即插即用的高性能小插件,所以代码实现也是比较简单的。本文是在ResNet基础上加入SEblock模块进行实现ResNet_SE50。


 一、SENet结构组成介绍

 上图为一个SEblock,由SEblock块构成的网络叫做SENet;可以基于原生网络,添加SEblock块构成SE-NameNet,如基于AlexNet等添加SE结构,称作SE-AlexNet、SE-ResNet等

SE块与先进的架构Inception、ResNet的结合效果


 

原理:通过一个全局平均池化层加两个全连接层以及全连接层对应激活【ReLU和sigmoid】组成的结构输出和输入特征同样数目的权重值,也就是每个特征通道的权重系数,学习一个通道的注意力出来,用于决定哪些通道应该重点提取特征,哪些部分放弃。

 SE块详细过程

1.首先由 Inception结构 或 ResNet结构处理后的C×W×H特征图开始,通过Squeeze操作对特征图进行全局平均池化(GAP),得到1×1×C 的特征向量

2.紧接着两个 FC 层组成一个 Bottleneck 结构去建模通道间的相关性:

  (1)经过第一个FC层,将C个通道变成 C/ r​ ,减少参数量,然后通过ReLU的非线性激活,到达第二个FC层

  (2)经过第二个FC层,再将特征通道数恢复到C个,得到带有注意力机制的权重参数

3.最后经过Sigmoid激活函数,最后通过一个 Scale 的操作来将归一化后的权重加权到每个通道的特征上。


  二、SEblock的具体介绍

 Sequeeze:Fsq操作就是使用通道的全局平均池化,将包含全局信息的W×H×C 的特征图直接压缩成一个1×1×C的特征向量,即将每个二维通道变成一个具有全局感受野的数值,此时1个像素表示1个通道,屏蔽掉空间上的分布信息,更好的利用通道间的相关性。
具体操作:对原特征图50×512×7×7进行全局平均池化,然后得到了一个50×512×1×1大小的特征图,这个特征图具有全局感受野。


Excitation :基于特征通道间的相关性,每个特征通道生成一个权重,用来代表特征通道的重要程度。由原本全为白色的C个通道的特征,得到带有不同深浅程度的颜色的特征向量,也就是不同的重要程度。

具体操作:输出的50×512×1×1特征图,经过两个全连接层,最后用一 个类似于循环神经网络中门控机制,通过参数来为每个特征通道生成权重,参数被学习用来显式地建模特征通道间的相关性(论文中使用的是sigmoid)。50×512×1×1变成50×512 / 16×1×1,最后再还原回来:50×512×1×1


Reweight:将Excitation输出的权重看做每个特征通道的重要性,也就是对于U每个位置上的所有H×W上的值都乘上对应通道的权值,完成对原始特征的重校准。

具体操作:50×512×1×1通过expand_as得到50×512×7×7, 完成在通道维度上对原始特征的重标定,并作为下一级的输入数据。


三、PyTorch代码实现

(1)SEblock搭建

全局平均池化+1*1卷积核+ReLu+1*1卷积核+Sigmoid

'''-------------一、SE模块-----------------------------'''
#全局平均池化+1*1卷积核+ReLu+1*1卷积核+Sigmoid
class SE_Block(nn.Module):
    def __init__(self, inchannel, ratio=16):
        super(SE_Block, self).__init__()
        # 全局平均池化(Fsq操作)
        self.gap = nn.AdaptiveAvgPool2d((1, 1))
        # 两个全连接层(Fex操作)
        self.fc = nn.Sequential(
            nn.Linear(inchannel, inchannel // ratio, bias=False),  # 从 c -> c/r
            nn.ReLU(),
            nn.Linear(inchannel // ratio, inchannel, bias=False),  # 从 c/r -> c
            nn.Sigmoid()
        )

    def forward(self, x):
            # 读取批数据图片数量及通道数
            b, c, h, w = x.size()
            # Fsq操作:经池化后输出b*c的矩阵
            y = self.gap(x).view(b, c)
            # Fex操作:经全连接层输出(b,c,1,1)矩阵
            y = self.fc(y).view(b, c, 1, 1)
            # Fscale操作:将得到的权重乘以原来的特征图x
            return x * y.expand_as(x)

(2)将SEblock嵌入残差模块

SEblock可以灵活的加入到resnet等相关完整模型中,通常加在残差之前。【因为激活是sigmoid原因,存在梯度弥散问题,所以尽量不放到主信号通道去,即使本个残差模块有弥散问题,以不至于影响整个网络模型】

 这里我们将SE模块分别嵌入ResNet的BasicBlock和Bottleneck中,得到 SEBasicBlock和SEBottleneck(具体解释可以看我之前写的ResNet代码复现+超详细注释(PyTorch)

BasicBlock模块

'''-------------二、BasicBlock模块-----------------------------'''
# 左侧的 residual block 结构(18-layer、34-layer)
class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inchannel, outchannel, stride=1):
        super(BasicBlock, self).__init__()
        self.conv1 = nn.Conv2d(inchannel, outchannel, kernel_size=3,
                               stride=stride, padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(outchannel)
        self.conv2 = nn.Conv2d(outchannel, outchannel, kernel_size=3,
                               stride=1, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(outchannel)
        # SE_Block放在BN之后,shortcut之前
        self.SE = SE_Block(outchannel)

        self.shortcut = nn.Sequential()
        if stride != 1 or inchannel != self.expansion*outchannel:
            self.shortcut = nn.Sequential(
                nn.Conv2d(inchannel, self.expansion*outchannel,
                          kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(self.expansion*outchannel)
            )

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = self.bn2(self.conv2(out))
        SE_out = self.SE(out)
        out = out * SE_out
        out += self.shortcut(x)
        out = F.relu(out)
        return out

Bottleneck模块 

'''-------------三、Bottleneck模块-----------------------------'''
# 右侧的 residual block 结构(50-layer、101-layer、152-layer)
class Bottleneck(nn.Module):
    expansion = 4

    def __init__(self, inchannel, outchannel, stride=1):
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv2d(inchannel, outchannel, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm2d(outchannel)
        self.conv2 = nn.Conv2d(outchannel, outchannel, kernel_size=3,
                               stride=stride, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(outchannel)
        self.conv3 = nn.Conv2d(outchannel, self.expansion*outchannel,
                               kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(self.expansion*outchannel)
        # SE_Block放在BN之后,shortcut之前
        self.SE = SE_Block(self.expansion*outchannel)

        self.shortcut = nn.Sequential()
        if stride != 1 or inchannel != self.expansion*outchannel:
            self.shortcut = nn.Sequential(
                nn.Conv2d(inchannel, self.expansion*outchannel,
                          kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(self.expansion*outchannel)
            )

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = F.relu(self.bn2(self.conv2(out)))
        out = self.bn3(self.conv3(out))
        SE_out = self.SE(out)
        out = out * SE_out
        out += self.shortcut(x)
        out = F.relu(out)
        return out

(3)搭建SE_ResNet结构

'''-------------四、搭建SE_ResNet结构-----------------------------'''
class SE_ResNet(nn.Module):
    def __init__(self, block, num_blocks, num_classes=10):
        super(SE_ResNet, self).__init__()
        self.in_planes = 64

        self.conv1 = nn.Conv2d(3, 64, kernel_size=3,
                               stride=1, padding=1, bias=False)                  # conv1
        self.bn1 = nn.BatchNorm2d(64)
        self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)       # conv2_x
        self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)      # conv3_x
        self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)      # conv4_x
        self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)      # conv5_x
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.linear = nn.Linear(512 * block.expansion, num_classes)

    def _make_layer(self, block, planes, num_blocks, stride):
        strides = [stride] + [1]*(num_blocks-1)
        layers = []
        for stride in strides:
            layers.append(block(self.in_planes, planes, stride))
            self.in_planes = planes * block.expansion
        return nn.Sequential(*layers)

    def forward(self, x):
        x = F.relu(self.bn1(self.conv1(x)))
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        out = self.linear(x)
        return out

(4)网络模型的创建和测试

网络模型创建打印 SE_ResNet50

# test()
if __name__ == '__main__':

    model = SE_ResNet50()
    print(model)

    input = torch.randn(1, 3, 224, 224)
    out = model(input)
    print(out.shape)

打印模型如下

SE_ResNet(
  (conv1): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
  (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
  (layer1): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=256, out_features=16, bias=False)
          (1): ReLU()
          (2): Linear(in_features=16, out_features=256, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential(
        (0): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=256, out_features=16, bias=False)
          (1): ReLU()
          (2): Linear(in_features=16, out_features=256, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential()
    )
    (2): Bottleneck(
      (conv1): Conv2d(256, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=256, out_features=16, bias=False)
          (1): ReLU()
          (2): Linear(in_features=16, out_features=256, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential()
    )
  )
  (layer2): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=512, out_features=32, bias=False)
          (1): ReLU()
          (2): Linear(in_features=32, out_features=512, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential(
        (0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=512, out_features=32, bias=False)
          (1): ReLU()
          (2): Linear(in_features=32, out_features=512, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential()
    )
    (2): Bottleneck(
      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=512, out_features=32, bias=False)
          (1): ReLU()
          (2): Linear(in_features=32, out_features=512, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential()
    )
    (3): Bottleneck(
      (conv1): Conv2d(512, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(128, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=512, out_features=32, bias=False)
          (1): ReLU()
          (2): Linear(in_features=32, out_features=512, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential()
    )
  )
  (layer3): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(512, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=1024, out_features=64, bias=False)
          (1): ReLU()
          (2): Linear(in_features=64, out_features=1024, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential(
        (0): Conv2d(512, 1024, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=1024, out_features=64, bias=False)
          (1): ReLU()
          (2): Linear(in_features=64, out_features=1024, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential()
    )
    (2): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=1024, out_features=64, bias=False)
          (1): ReLU()
          (2): Linear(in_features=64, out_features=1024, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential()
    )
    (3): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=1024, out_features=64, bias=False)
          (1): ReLU()
          (2): Linear(in_features=64, out_features=1024, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential()
    )
    (4): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=1024, out_features=64, bias=False)
          (1): ReLU()
          (2): Linear(in_features=64, out_features=1024, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential()
    )
    (5): Bottleneck(
      (conv1): Conv2d(1024, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(256, 1024, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=1024, out_features=64, bias=False)
          (1): ReLU()
          (2): Linear(in_features=64, out_features=1024, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential()
    )
  )
  (layer4): Sequential(
    (0): Bottleneck(
      (conv1): Conv2d(1024, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=2048, out_features=128, bias=False)
          (1): ReLU()
          (2): Linear(in_features=128, out_features=2048, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential(
        (0): Conv2d(1024, 2048, kernel_size=(1, 1), stride=(2, 2), bias=False)
        (1): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      )
    )
    (1): Bottleneck(
      (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=2048, out_features=128, bias=False)
          (1): ReLU()
          (2): Linear(in_features=128, out_features=2048, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential()
    )
    (2): Bottleneck(
      (conv1): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
      (bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (conv3): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)
      (bn3): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
      (SE): SE_Block(
        (gap): AdaptiveAvgPool2d(output_size=(1, 1))
        (fc): Sequential(
          (0): Linear(in_features=2048, out_features=128, bias=False)
          (1): ReLU()
          (2): Linear(in_features=128, out_features=2048, bias=False)
          (3): Sigmoid()
        )
      )
      (shortcut): Sequential()
    )
  )
  (avgpool): AdaptiveAvgPool2d(output_size=(1, 1))
  (linear): Linear(in_features=2048, out_features=10, bias=True)
)
torch.Size([1, 10])

 使用torchsummary打印每个网络模型的详细信息

if __name__ == '__main__':
    net = SE_ResNet50().cuda()
    summary(net, (3, 224, 224))

打印模型如下

----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1         [-1, 64, 224, 224]           1,728
       BatchNorm2d-2         [-1, 64, 224, 224]             128
            Conv2d-3         [-1, 64, 224, 224]           4,096
       BatchNorm2d-4         [-1, 64, 224, 224]             128
            Conv2d-5         [-1, 64, 224, 224]          36,864
       BatchNorm2d-6         [-1, 64, 224, 224]             128
            Conv2d-7        [-1, 256, 224, 224]          16,384
       BatchNorm2d-8        [-1, 256, 224, 224]             512
 AdaptiveAvgPool2d-9            [-1, 256, 1, 1]               0
           Linear-10                   [-1, 16]           4,096
             ReLU-11                   [-1, 16]               0
           Linear-12                  [-1, 256]           4,096
          Sigmoid-13                  [-1, 256]               0
         SE_Block-14        [-1, 256, 224, 224]               0
           Conv2d-15        [-1, 256, 224, 224]          16,384
      BatchNorm2d-16        [-1, 256, 224, 224]             512
       Bottleneck-17        [-1, 256, 224, 224]               0
           Conv2d-18         [-1, 64, 224, 224]          16,384
      BatchNorm2d-19         [-1, 64, 224, 224]             128
           Conv2d-20         [-1, 64, 224, 224]          36,864
      BatchNorm2d-21         [-1, 64, 224, 224]             128
           Conv2d-22        [-1, 256, 224, 224]          16,384
      BatchNorm2d-23        [-1, 256, 224, 224]             512
AdaptiveAvgPool2d-24            [-1, 256, 1, 1]               0
           Linear-25                   [-1, 16]           4,096
             ReLU-26                   [-1, 16]               0
           Linear-27                  [-1, 256]           4,096
          Sigmoid-28                  [-1, 256]               0
         SE_Block-29        [-1, 256, 224, 224]               0
       Bottleneck-30        [-1, 256, 224, 224]               0
           Conv2d-31         [-1, 64, 224, 224]          16,384
      BatchNorm2d-32         [-1, 64, 224, 224]             128
           Conv2d-33         [-1, 64, 224, 224]          36,864
      BatchNorm2d-34         [-1, 64, 224, 224]             128
           Conv2d-35        [-1, 256, 224, 224]          16,384
      BatchNorm2d-36        [-1, 256, 224, 224]             512
AdaptiveAvgPool2d-37            [-1, 256, 1, 1]               0
           Linear-38                   [-1, 16]           4,096
             ReLU-39                   [-1, 16]               0
           Linear-40                  [-1, 256]           4,096
          Sigmoid-41                  [-1, 256]               0
         SE_Block-42        [-1, 256, 224, 224]               0
       Bottleneck-43        [-1, 256, 224, 224]               0
           Conv2d-44        [-1, 128, 224, 224]          32,768
      BatchNorm2d-45        [-1, 128, 224, 224]             256
           Conv2d-46        [-1, 128, 112, 112]         147,456
      BatchNorm2d-47        [-1, 128, 112, 112]             256
           Conv2d-48        [-1, 512, 112, 112]          65,536
      BatchNorm2d-49        [-1, 512, 112, 112]           1,024
AdaptiveAvgPool2d-50            [-1, 512, 1, 1]               0
           Linear-51                   [-1, 32]          16,384
             ReLU-52                   [-1, 32]               0
           Linear-53                  [-1, 512]          16,384
          Sigmoid-54                  [-1, 512]               0
         SE_Block-55        [-1, 512, 112, 112]               0
           Conv2d-56        [-1, 512, 112, 112]         131,072
      BatchNorm2d-57        [-1, 512, 112, 112]           1,024
       Bottleneck-58        [-1, 512, 112, 112]               0
           Conv2d-59        [-1, 128, 112, 112]          65,536
      BatchNorm2d-60        [-1, 128, 112, 112]             256
           Conv2d-61        [-1, 128, 112, 112]         147,456
      BatchNorm2d-62        [-1, 128, 112, 112]             256
           Conv2d-63        [-1, 512, 112, 112]          65,536
      BatchNorm2d-64        [-1, 512, 112, 112]           1,024
AdaptiveAvgPool2d-65            [-1, 512, 1, 1]               0
           Linear-66                   [-1, 32]          16,384
             ReLU-67                   [-1, 32]               0
           Linear-68                  [-1, 512]          16,384
          Sigmoid-69                  [-1, 512]               0
         SE_Block-70        [-1, 512, 112, 112]               0
       Bottleneck-71        [-1, 512, 112, 112]               0
           Conv2d-72        [-1, 128, 112, 112]          65,536
      BatchNorm2d-73        [-1, 128, 112, 112]             256
           Conv2d-74        [-1, 128, 112, 112]         147,456
      BatchNorm2d-75        [-1, 128, 112, 112]             256
           Conv2d-76        [-1, 512, 112, 112]          65,536
      BatchNorm2d-77        [-1, 512, 112, 112]           1,024
AdaptiveAvgPool2d-78            [-1, 512, 1, 1]               0
           Linear-79                   [-1, 32]          16,384
             ReLU-80                   [-1, 32]               0
           Linear-81                  [-1, 512]          16,384
          Sigmoid-82                  [-1, 512]               0
         SE_Block-83        [-1, 512, 112, 112]               0
       Bottleneck-84        [-1, 512, 112, 112]               0
           Conv2d-85        [-1, 128, 112, 112]          65,536
      BatchNorm2d-86        [-1, 128, 112, 112]             256
           Conv2d-87        [-1, 128, 112, 112]         147,456
      BatchNorm2d-88        [-1, 128, 112, 112]             256
           Conv2d-89        [-1, 512, 112, 112]          65,536
      BatchNorm2d-90        [-1, 512, 112, 112]           1,024
AdaptiveAvgPool2d-91            [-1, 512, 1, 1]               0
           Linear-92                   [-1, 32]          16,384
             ReLU-93                   [-1, 32]               0
           Linear-94                  [-1, 512]          16,384
          Sigmoid-95                  [-1, 512]               0
         SE_Block-96        [-1, 512, 112, 112]               0
       Bottleneck-97        [-1, 512, 112, 112]               0
           Conv2d-98        [-1, 256, 112, 112]         131,072
      BatchNorm2d-99        [-1, 256, 112, 112]             512
          Conv2d-100          [-1, 256, 56, 56]         589,824
     BatchNorm2d-101          [-1, 256, 56, 56]             512
          Conv2d-102         [-1, 1024, 56, 56]         262,144
     BatchNorm2d-103         [-1, 1024, 56, 56]           2,048
AdaptiveAvgPool2d-104           [-1, 1024, 1, 1]               0
          Linear-105                   [-1, 64]          65,536
            ReLU-106                   [-1, 64]               0
          Linear-107                 [-1, 1024]          65,536
         Sigmoid-108                 [-1, 1024]               0
        SE_Block-109         [-1, 1024, 56, 56]               0
          Conv2d-110         [-1, 1024, 56, 56]         524,288
     BatchNorm2d-111         [-1, 1024, 56, 56]           2,048
      Bottleneck-112         [-1, 1024, 56, 56]               0
          Conv2d-113          [-1, 256, 56, 56]         262,144
     BatchNorm2d-114          [-1, 256, 56, 56]             512
          Conv2d-115          [-1, 256, 56, 56]         589,824
     BatchNorm2d-116          [-1, 256, 56, 56]             512
          Conv2d-117         [-1, 1024, 56, 56]         262,144
     BatchNorm2d-118         [-1, 1024, 56, 56]           2,048
AdaptiveAvgPool2d-119           [-1, 1024, 1, 1]               0
          Linear-120                   [-1, 64]          65,536
            ReLU-121                   [-1, 64]               0
          Linear-122                 [-1, 1024]          65,536
         Sigmoid-123                 [-1, 1024]               0
        SE_Block-124         [-1, 1024, 56, 56]               0
      Bottleneck-125         [-1, 1024, 56, 56]               0
          Conv2d-126          [-1, 256, 56, 56]         262,144
     BatchNorm2d-127          [-1, 256, 56, 56]             512
          Conv2d-128          [-1, 256, 56, 56]         589,824
     BatchNorm2d-129          [-1, 256, 56, 56]             512
          Conv2d-130         [-1, 1024, 56, 56]         262,144
     BatchNorm2d-131         [-1, 1024, 56, 56]           2,048
AdaptiveAvgPool2d-132           [-1, 1024, 1, 1]               0
          Linear-133                   [-1, 64]          65,536
            ReLU-134                   [-1, 64]               0
          Linear-135                 [-1, 1024]          65,536
         Sigmoid-136                 [-1, 1024]               0
        SE_Block-137         [-1, 1024, 56, 56]               0
      Bottleneck-138         [-1, 1024, 56, 56]               0
          Conv2d-139          [-1, 256, 56, 56]         262,144
     BatchNorm2d-140          [-1, 256, 56, 56]             512
          Conv2d-141          [-1, 256, 56, 56]         589,824
     BatchNorm2d-142          [-1, 256, 56, 56]             512
          Conv2d-143         [-1, 1024, 56, 56]         262,144
     BatchNorm2d-144         [-1, 1024, 56, 56]           2,048
AdaptiveAvgPool2d-145           [-1, 1024, 1, 1]               0
          Linear-146                   [-1, 64]          65,536
            ReLU-147                   [-1, 64]               0
          Linear-148                 [-1, 1024]          65,536
         Sigmoid-149                 [-1, 1024]               0
        SE_Block-150         [-1, 1024, 56, 56]               0
      Bottleneck-151         [-1, 1024, 56, 56]               0
          Conv2d-152          [-1, 256, 56, 56]         262,144
     BatchNorm2d-153          [-1, 256, 56, 56]             512
          Conv2d-154          [-1, 256, 56, 56]         589,824
     BatchNorm2d-155          [-1, 256, 56, 56]             512
          Conv2d-156         [-1, 1024, 56, 56]         262,144
     BatchNorm2d-157         [-1, 1024, 56, 56]           2,048
AdaptiveAvgPool2d-158           [-1, 1024, 1, 1]               0
          Linear-159                   [-1, 64]          65,536
            ReLU-160                   [-1, 64]               0
          Linear-161                 [-1, 1024]          65,536
         Sigmoid-162                 [-1, 1024]               0
        SE_Block-163         [-1, 1024, 56, 56]               0
      Bottleneck-164         [-1, 1024, 56, 56]               0
          Conv2d-165          [-1, 256, 56, 56]         262,144
     BatchNorm2d-166          [-1, 256, 56, 56]             512
          Conv2d-167          [-1, 256, 56, 56]         589,824
     BatchNorm2d-168          [-1, 256, 56, 56]             512
          Conv2d-169         [-1, 1024, 56, 56]         262,144
     BatchNorm2d-170         [-1, 1024, 56, 56]           2,048
AdaptiveAvgPool2d-171           [-1, 1024, 1, 1]               0
          Linear-172                   [-1, 64]          65,536
            ReLU-173                   [-1, 64]               0
          Linear-174                 [-1, 1024]          65,536
         Sigmoid-175                 [-1, 1024]               0
        SE_Block-176         [-1, 1024, 56, 56]               0
      Bottleneck-177         [-1, 1024, 56, 56]               0
          Conv2d-178          [-1, 512, 56, 56]         524,288
     BatchNorm2d-179          [-1, 512, 56, 56]           1,024
          Conv2d-180          [-1, 512, 28, 28]       2,359,296
     BatchNorm2d-181          [-1, 512, 28, 28]           1,024
          Conv2d-182         [-1, 2048, 28, 28]       1,048,576
     BatchNorm2d-183         [-1, 2048, 28, 28]           4,096
AdaptiveAvgPool2d-184           [-1, 2048, 1, 1]               0
          Linear-185                  [-1, 128]         262,144
            ReLU-186                  [-1, 128]               0
          Linear-187                 [-1, 2048]         262,144
         Sigmoid-188                 [-1, 2048]               0
        SE_Block-189         [-1, 2048, 28, 28]               0
          Conv2d-190         [-1, 2048, 28, 28]       2,097,152
     BatchNorm2d-191         [-1, 2048, 28, 28]           4,096
      Bottleneck-192         [-1, 2048, 28, 28]               0
          Conv2d-193          [-1, 512, 28, 28]       1,048,576
     BatchNorm2d-194          [-1, 512, 28, 28]           1,024
          Conv2d-195          [-1, 512, 28, 28]       2,359,296
     BatchNorm2d-196          [-1, 512, 28, 28]           1,024
          Conv2d-197         [-1, 2048, 28, 28]       1,048,576
     BatchNorm2d-198         [-1, 2048, 28, 28]           4,096
AdaptiveAvgPool2d-199           [-1, 2048, 1, 1]               0
          Linear-200                  [-1, 128]         262,144
            ReLU-201                  [-1, 128]               0
          Linear-202                 [-1, 2048]         262,144
         Sigmoid-203                 [-1, 2048]               0
        SE_Block-204         [-1, 2048, 28, 28]               0
      Bottleneck-205         [-1, 2048, 28, 28]               0
          Conv2d-206          [-1, 512, 28, 28]       1,048,576
     BatchNorm2d-207          [-1, 512, 28, 28]           1,024
          Conv2d-208          [-1, 512, 28, 28]       2,359,296
     BatchNorm2d-209          [-1, 512, 28, 28]           1,024
          Conv2d-210         [-1, 2048, 28, 28]       1,048,576
     BatchNorm2d-211         [-1, 2048, 28, 28]           4,096
AdaptiveAvgPool2d-212           [-1, 2048, 1, 1]               0
          Linear-213                  [-1, 128]         262,144
            ReLU-214                  [-1, 128]               0
          Linear-215                 [-1, 2048]         262,144
         Sigmoid-216                 [-1, 2048]               0
        SE_Block-217         [-1, 2048, 28, 28]               0
      Bottleneck-218         [-1, 2048, 28, 28]               0
AdaptiveAvgPool2d-219           [-1, 2048, 1, 1]               0
          Linear-220                   [-1, 10]          20,490
================================================================
Total params: 26,035,786
Trainable params: 26,035,786
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.57
Forward/backward pass size (MB): 3914.25
Params size (MB): 99.32
Estimated Total Size (MB): 4014.14
----------------------------------------------------------------

Process finished with exit code 0

(5)完整代码

import torch
import torch.nn as nn
import torch.nn.functional as F
from torchsummary import summary

'''-------------一、SE模块-----------------------------'''
#全局平均池化+1*1卷积核+ReLu+1*1卷积核+Sigmoid
class SE_Block(nn.Module):
    def __init__(self, inchannel, ratio=16):
        super(SE_Block, self).__init__()
        # 全局平均池化(Fsq操作)
        self.gap = nn.AdaptiveAvgPool2d((1, 1))
        # 两个全连接层(Fex操作)
        self.fc = nn.Sequential(
            nn.Linear(inchannel, inchannel // ratio, bias=False),  # 从 c -> c/r
            nn.ReLU(),
            nn.Linear(inchannel // ratio, inchannel, bias=False),  # 从 c/r -> c
            nn.Sigmoid()
        )

    def forward(self, x):
            # 读取批数据图片数量及通道数
            b, c, h, w = x.size()
            # Fsq操作:经池化后输出b*c的矩阵
            y = self.gap(x).view(b, c)
            # Fex操作:经全连接层输出(b,c,1,1)矩阵
            y = self.fc(y).view(b, c, 1, 1)
            # Fscale操作:将得到的权重乘以原来的特征图x
            return x * y.expand_as(x)

'''-------------二、BasicBlock模块-----------------------------'''
# 左侧的 residual block 结构(18-layer、34-layer)
class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inchannel, outchannel, stride=1):
        super(BasicBlock, self).__init__()
        self.conv1 = nn.Conv2d(inchannel, outchannel, kernel_size=3,
                               stride=stride, padding=1, bias=False)
        self.bn1 = nn.BatchNorm2d(outchannel)
        self.conv2 = nn.Conv2d(outchannel, outchannel, kernel_size=3,
                               stride=1, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(outchannel)
        # SE_Block放在BN之后,shortcut之前
        self.SE = SE_Block(outchannel)

        self.shortcut = nn.Sequential()
        if stride != 1 or inchannel != self.expansion*outchannel:
            self.shortcut = nn.Sequential(
                nn.Conv2d(inchannel, self.expansion*outchannel,
                          kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(self.expansion*outchannel)
            )

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = self.bn2(self.conv2(out))
        SE_out = self.SE(out)
        out = out * SE_out
        out += self.shortcut(x)
        out = F.relu(out)
        return out

'''-------------三、Bottleneck模块-----------------------------'''
# 右侧的 residual block 结构(50-layer、101-layer、152-layer)
class Bottleneck(nn.Module):
    expansion = 4

    def __init__(self, inchannel, outchannel, stride=1):
        super(Bottleneck, self).__init__()
        self.conv1 = nn.Conv2d(inchannel, outchannel, kernel_size=1, bias=False)
        self.bn1 = nn.BatchNorm2d(outchannel)
        self.conv2 = nn.Conv2d(outchannel, outchannel, kernel_size=3,
                               stride=stride, padding=1, bias=False)
        self.bn2 = nn.BatchNorm2d(outchannel)
        self.conv3 = nn.Conv2d(outchannel, self.expansion*outchannel,
                               kernel_size=1, bias=False)
        self.bn3 = nn.BatchNorm2d(self.expansion*outchannel)
        # SE_Block放在BN之后,shortcut之前
        self.SE = SE_Block(self.expansion*outchannel)

        self.shortcut = nn.Sequential()
        if stride != 1 or inchannel != self.expansion*outchannel:
            self.shortcut = nn.Sequential(
                nn.Conv2d(inchannel, self.expansion*outchannel,
                          kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(self.expansion*outchannel)
            )

    def forward(self, x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = F.relu(self.bn2(self.conv2(out)))
        out = self.bn3(self.conv3(out))
        SE_out = self.SE(out)
        out = out * SE_out
        out += self.shortcut(x)
        out = F.relu(out)
        return out

'''-------------四、搭建SE_ResNet结构-----------------------------'''
class SE_ResNet(nn.Module):
    def __init__(self, block, num_blocks, num_classes=10):
        super(SE_ResNet, self).__init__()
        self.in_planes = 64

        self.conv1 = nn.Conv2d(3, 64, kernel_size=3,
                               stride=1, padding=1, bias=False)                  # conv1
        self.bn1 = nn.BatchNorm2d(64)
        self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1)       # conv2_x
        self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2)      # conv3_x
        self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2)      # conv4_x
        self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2)      # conv5_x
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.linear = nn.Linear(512 * block.expansion, num_classes)

    def _make_layer(self, block, planes, num_blocks, stride):
        strides = [stride] + [1]*(num_blocks-1)
        layers = []
        for stride in strides:
            layers.append(block(self.in_planes, planes, stride))
            self.in_planes = planes * block.expansion
        return nn.Sequential(*layers)

    def forward(self, x):
        x = F.relu(self.bn1(self.conv1(x)))
        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        out = self.linear(x)
        return out


def SE_ResNet18():
    return SE_ResNet(BasicBlock, [2, 2, 2, 2])


def SE_ResNet34():
    return SE_ResNet(BasicBlock, [3, 4, 6, 3])


def SE_ResNet50():
    return SE_ResNet(Bottleneck, [3, 4, 6, 3])


def SE_ResNet101():
    return SE_ResNet(Bottleneck, [3, 4, 23, 3])


def SE_ResNet152():
    return SE_ResNet(Bottleneck, [3, 8, 36, 3])


'''
if __name__ == '__main__':

    model = SE_ResNet50()
    print(model)

    input = torch.randn(1, 3, 224, 224)
    out = model(input)
    print(out.shape)
# test()
'''
if __name__ == '__main__':
    net = SE_ResNet50().cuda()
    summary(net, (3, 224, 224))

本篇就结束了,欢迎大家留言讨论呀!

有关SENet代码复现+超详细注释(PyTorch)的更多相关文章

  1. ruby - 如何在 buildr 项目中使用 Ruby 代码? - 2

    如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby​​

  2. ruby-on-rails - Rails 源代码 : initialize hash in a weird way? - 2

    在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has

  3. ruby-on-rails - 浏览 Ruby 源代码 - 2

    我的主要目标是能够完全理解我正在使用的库/gem。我尝试在Github上从头到尾阅读源代码,但这真的很难。我认为更有趣、更温和的踏脚石就是在使用时阅读每个库/gem方法的源代码。例如,我想知道RubyonRails中的redirect_to方法是如何工作的:如何查找redirect_to方法的源代码?我知道在pry中我可以执行类似show-methodmethod的操作,但我如何才能对Rails框架中的方法执行此操作?您对我如何更好地理解Gem及其API有什么建议吗?仅仅阅读源代码似乎真的很难,尤其是对于框架。谢谢! 最佳答案 Ru

  4. ruby - 模块嵌套代码风格偏好 - 2

    我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的

  5. ruby - 寻找通过阅读代码确定编程语言的ruby gem? - 2

    几个月前,我读了一篇关于ruby​​gem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题:

  6. ruby - Net::HTTP 获取源代码和状态 - 2

    我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur

  7. 程序员如何提高代码能力? - 2

    前言作为一名程序员,自己的本质工作就是做程序开发,那么程序开发的时候最直接的体现就是代码,检验一个程序员技术水平的一个核心环节就是开发时候的代码能力。众所周知,程序开发的水平提升是一个循序渐进的过程,每一位程序员都是从“菜鸟”变成“大神”的,所以程序员在程序开发过程中的代码能力也是根据平时开发中的业务实践来积累和提升的。提高代码能力核心要素程序员要想提高自身代码能力,尤其是新晋程序员的代码能力有很大的提升空间的时候,需要针对性的去提高自己的代码能力。提高代码能力其实有几个比较关键的点,只要把握住这些方面,就能很好的、快速的提高自己的一部分代码能力。1、多去阅读开源项目,如有机会可以亲自参与开源

  8. 7个大一C语言必学的程序 / C语言经典代码大全 - 2

    嗨~大家好,这里是可莉!今天给大家带来的是7个C语言的经典基础代码~那一起往下看下去把【程序一】打印100到200之间的素数#includeintmain(){ inti; for(i=100;i 【程序二】输出乘法口诀表#includeintmain(){inti;for(i=1;i 【程序三】判断1000年---2000年之间的闰年#includeintmain(){intyear;for(year=1000;year 【程序四】给定两个整形变量的值,将两个值的内容进行交换。这里提供两种方法来进行交换,第一种为创建临时变量来进行交换,第二种是不创建临时变量而直接进行交换。1.创建临时变量来

  9. 在VMware16虚拟机安装Ubuntu详细教程 - 2

    在VMware16.2.4安装Ubuntu一、安装VMware1.打开VMwareWorkstationPro官网,点击即可进入。2.进入后向下滑动找到Workstation16ProforWindows,点击立即下载。3.下载完成,文件大小615MB,如下图:4.鼠标右击,以管理员身份运行。5.点击下一步6.勾选条款,点击下一步7.先勾选,再点击下一步8.去掉勾选,点击下一步9.点击下一步10.点击安装11.点击许可证12.在百度上搜索VM16许可证,复制填入,然后点击输入即可,亲测有效。13.点击完成14.重启系统,点击是15.双击VMwareWorkstationPro图标,进入虚拟机主

  10. git使用常见问题(提交代码,合并冲突) - 2

    文章目录git常用命令(简介,详细参数往下看)Git提交代码步骤gitpullgitstatusgitaddgitcommitgitpushgit代码冲突合并问题方法一:放弃本地代码方法二:合并代码常用命令以及详细参数gitadd将文件添加到仓库:gitdiff比较文件异同gitlog查看历史记录gitreset代码回滚版本库相关操作远程仓库相关操作分支相关操作创建分支查看分支:gitbranch合并分支:gitmerge删除分支:gitbranch-ddev查看分支合并图:gitlog–graph–pretty=oneline–abbrev-commit撤消某次提交git用户名密码相关配置g

随机推荐