0830组会分享——几种卷积辨析 && SELayer论文及代码实现

几种卷积类型辨析

1
2
3
4
conv = nn.Conv2d(in_channels=6, out_channels=6, kernel_size=1, groups=3)
conv.weight.data.size()

# output = torch.Size([6, 2, 1, 1])

一种分类方法:

几种卷积示意:(分组卷积 group_convolution;深度卷积 depthwise convolution; 全局深度卷积 global depthwise convolution

  1. groups 默认值为1, 对应的是常规卷积操作
  2. groups > 1, 且能够同时被in_channel / out_channel整除,对应group_convolution
  3. groups == input_channel == out_channel , 对应depthwise convolution,为条件2的特殊情况
  4. 在条件3的基础上,各卷积核的 H == input_height; W == input_width, 对应为 global depthwise convolution, 为条件3的特殊情况

另一种分类方法:主要分三类:正常卷积、分组卷积、深度分离卷积

  1. 正常卷积:

  2. 参数量 = cin * $K_h$ * $K_w$ * cout

  3. 常规卷积示意图

  4. img

  5. img

  6. 分组卷积示意图:

  7. 参数量: (cin * $K_h$ * $K_w$* cout ) / Groups

  8. 分组卷积示意图

  9. img

  10. img

  11. class GroupConv(nn.Module):
      def __init__(self, in_ch, out_ch, groups):
          super(GroupConv, self).__init__()
          self.conv = nn.Conv2d(
              in_channels=in_ch,
              out_channels=out_ch,
              kernel_size=3,
              stride=1,
              padding=1,
              groups=groups
          )
    
      def forward(self, input):
          out = self.conv(input)
          return out
    
    
# 测试
conv = CSDN_Tem(16, 32, 4)
print(summary(conv, (16, 64, 64), batch_size=1))

********************************
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1            [1, 32, 64, 64]           1,184
================================================================
Total params: 1,184
Trainable params: 1,184
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 0.25
Forward/backward pass size (MB): 1.00
Params size (MB): 0.00
Estimated Total Size (MB): 1.25
----------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

13.

14.

15. ## [深度可分离卷积(Depthwise Separable Convolution)][https://blog.csdn.net/weixin_30793735/article/details/88915612]

16. 参数量: $C_{in}$ * $K_h$ * $K_w$ + $C_{out}$ * 1 * 1

17. 理解上,可以看作是, 先做一次 cin == cout 的 分组卷积, 并且 groups == channels,即每个通道作为一组; 然后再对上述结果,做一次 **逐点卷积(Pointwise Convolution)** 实现通道数改变的,这个过程使用大小为 **Cin * 1 * 1 ** 的卷积核实现,数量为 **Cout** 个

18. ```python
class DepthSepConv(nn.Module):
def __init__(self, in_ch, out_ch):
super(DepthSepConv, self).__init__()
self.depth_conv = nn.Conv2d(
in_channels=in_ch,
out_channels=in_ch,
kernel_size=3,
stride=1,
padding=1,
groups=in_ch
)
self.point_conv = nn.Conv2d(
in_channels=in_ch,
out_channels=out_ch,
kernel_size=1,
stride=1,
padding=0,
groups=1
)

def forward(self, input):
out = self.depth_conv(input)
out = self.point_conv(out)
return out


# 测试
conv = DepthSepConv(16, 32)
print(summary(conv, (16, 64, 64), batch_size=1))

************************
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
Conv2d-1 [1, 16, 64, 64] 160
Conv2d-2 [1, 32, 64, 64] 544
================================================================
Total params: 704
Trainable params: 704
Non-trainable params: 0
----------------------------------------------------------------

SELayer

Squeeze-and-Excitation Networks

image-20200830114911949

image-20200830140039015

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
torch.Size([2, 512, 64, 64])
b = 2 c = 512
torch.Size([2, 512])
After LinearFC, tmp.shape = torch.Size([2, 32])
After RELU, tmp.shape = torch.Size([2, 32])
After LinearFC2, tmp.shape = torch.Size([2, 512])
After SIGMOID, tmp.shape = torch.Size([2, 512])
torch.Size([2, 512, 1, 1])
----------------------------------------------------------------
Layer (type) Output Shape Param #
================================================================
AdaptiveAvgPool2d-1 [-1, 512, 1, 1] 0
Linear-2 [-1, 32] 16,384
ReLU-3 [-1, 32] 0
Linear-4 [-1, 512] 16,384
Sigmoid-5 [-1, 512] 0
Linear-6 [-1, 32] 16,384
ReLU-7 [-1, 32] 0
Linear-8 [-1, 512] 16,384
Sigmoid-9 [-1, 512] 0
================================================================
Total params: 65,536
Trainable params: 65,536
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 8.00
Forward/backward pass size (MB): 0.02
Params size (MB): 0.25
Estimated Total Size (MB): 8.27
----------------------------------------------------------------

image-20200830153224613