当前位置:   article > 正文

深度可分离卷积(Depthwise Separable Convolution)和分组卷积(Group Convolution)的理解,相互关系及PyTorch实现_separable convolution pytorch实现

separable convolution pytorch实现

1. 分组卷积(Group Convolution)

分组卷积最早出现在AlexNet中,如下图所示。在CNN发展初期,GPU资源不足以满足训练任务的要求,因此,Hinton采用了多GPU训练的策略,每个GPU完成一部分卷积,最后把多个GPU的卷积结果进行融合。

                                       

                                         

这里提出一个小小的问题给大家思考:如上图所示,input Features 是12,将其分为3个组,每组4个Features map,那么output Feature maps 的数量可以是任意的吗,可以是1吗?

2. 深度可分离卷积(Depthwise Separable Convolution)

3. PyTorch实现

Pytorch是2017年推出的深度学习框架,不同于Tensorflow基于静态图的模型搭建方式,PyTorch是完全动态的框架,推出以来很快成为AI研究人员的热门选择并受到推崇。(介绍到此结束)

在PyTorch中,实现二维卷积是通过nn.Conv2d实现的,这个函数是非常强大的,其功能不仅仅是实现常规卷积,通过合理的参数选择就可以实现分组卷积、空洞卷积。API的官方介绍如下:

  1. CLASS torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
  2. - stride: controls the stride for the cross-correlation, a single number or a tuple.
  3. - padding: controls the amount of implicit zero-paddings on both sides for padding number of points for each dimension.
  4. - dilation: controls the spacing between the kernel points; also known as the à trous algorithm. It is harder to describe, but this link has a nice visualization of what dilation does.
  5. - groups: controls the connections between inputs and outputs. in_channels and out_channels must both be divisible by groups. For example,
  6. At groups=1, all inputs are convolved to all outputs.
  7. At groups=2, the operation becomes equivalent to having two conv layers side by side, each seeing half the input channels, and producing half the output channels, and both subsequently concatenated.
  8. At groups= in_channels, each input channel is convolved with its own set of filters.

3.1 分组卷积

分组卷积只需要对nn.Conv2d中的groups参数进行设置即可,表示需要分的组数,groups的默认值为1,即进行常规卷积。以下是实现分组卷积的代码:

  1. class CSDN_Tem(nn.Module):
  2. def __init__(self, in_ch, out_ch, groups):
  3. super(CSDN_Tem, self).__init__()
  4. self.conv = nn.Conv2d(
  5. in_channels=in_ch,
  6. out_channels=out_ch,
  7. kernel_size=3,
  8. stride=1,
  9. padding=1,
  10. groups=groups
  11. )
  12. def forward(self, input):
  13. out = self.conv(input)
  14. return out

通过以下代码对该模型进行测试,设定输入特征图通道数为16,输出特征图通道数为64,分组数目为4:

  1. conv = CSDN_Tem(16, 64, 4)
  2. print(summary(conv, (16, 128, 128), batch_size=1))

控制台输出为:

  1. ----------------------------------------------------------------
  2. Layer (type) Output Shape Param #
  3. ================================================================
  4. Conv2d-1 [1, 64, 128, 128] 2,368
  5. ================================================================
  6. Total params: 2,368
  7. Trainable params: 2,368
  8. Non-trainable params: 0
  9. ----------------------------------------------------------------
  10. Input size (MB): 1.00
  11. Forward/backward pass size (MB): 8.00
  12. Params size (MB): 0.01
  13. Estimated Total Size (MB): 9.01
  14. ----------------------------------------------------------------

这一分组卷积过程所需参数为2368个,其中包含了偏置(Bias).

3.2 深度可分离卷积

深度可分离卷积的PyTorch代码如下:

  1. class CSDN_Tem(nn.Module):
  2. def __init__(self, in_ch, out_ch):
  3. super(CSDN_Tem, self).__init__()
  4. self.depth_conv = nn.Conv2d(
  5. in_channels=in_ch,
  6. out_channels=in_ch,
  7. kernel_size=3,
  8. stride=1,
  9. padding=1,
  10. groups=in_ch
  11. )
  12. self.point_conv = nn.Conv2d(
  13. in_channels=in_ch,
  14. out_channels=out_ch,
  15. kernel_size=1,
  16. stride=1,
  17. padding=0,
  18. groups=1
  19. )
  20. def forward(self, input):
  21. out = self.depth_conv(input)
  22. out = self.point_conv(out)
  23. return out

采用和分组卷积相同的输入和输出通道数,测试代码如下:

  1. conv = depth_conv(16,64)
  2. print(summary(conv,(16,128,128),batch_size=1))

控制台输出结果为:

  1. ----------------------------------------------------------------
  2. Layer (type) Output Shape Param #
  3. ================================================================
  4. Conv2d-1 [1, 16, 128, 128] 160
  5. Conv2d-2 [1, 64, 128, 128] 1,088
  6. ================================================================
  7. Total params: 1,248
  8. Trainable params: 1,248
  9. Non-trainable params: 0
  10. ----------------------------------------------------------------
  11. Input size (MB): 1.00
  12. Forward/backward pass size (MB): 10.00
  13. Params size (MB): 0.00
  14. Estimated Total Size (MB): 11.00

深度可分离卷积实现相同的操作仅需1248个参数。

如有疑问,欢迎留言!

参考

  1. https://www.cnblogs.com/shine-lee/p/10243114.html
  2. https://blog.csdn.net/luoluonuoyasuolong/article/details/81750190
  3. https://blog.csdn.net/luoluonuoyasuolong/article/details/81750190
  4. https://pytorch.org/docs/stable/nn.html?highlight=conv2#torch.nn.Conv2d
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/371490
推荐阅读
相关标签
  

闽ICP备14008679号