赞
踩
分组卷积最早出现在AlexNet中,如下图所示。在CNN发展初期,GPU资源不足以满足训练任务的要求,因此,Hinton采用了多GPU训练的策略,每个GPU完成一部分卷积,最后把多个GPU的卷积结果进行融合。
这里提出一个小小的问题给大家思考:如上图所示,input Features 是12,将其分为3个组,每组4个Features map,那么output Feature maps 的数量可以是任意的吗,可以是1吗?
Pytorch是2017年推出的深度学习框架,不同于Tensorflow基于静态图的模型搭建方式,PyTorch是完全动态的框架,推出以来很快成为AI研究人员的热门选择并受到推崇。(介绍到此结束)
在PyTorch中,实现二维卷积是通过nn.Conv2d实现的,这个函数是非常强大的,其功能不仅仅是实现常规卷积,通过合理的参数选择就可以实现分组卷积、空洞卷积。API的官方介绍如下:
- CLASS torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
-
- - stride: controls the stride for the cross-correlation, a single number or a tuple.
- - padding: controls the amount of implicit zero-paddings on both sides for padding number of points for each dimension.
- - 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.
- - groups: controls the connections between inputs and outputs. in_channels and out_channels must both be divisible by groups. For example,
- At groups=1, all inputs are convolved to all outputs.
- 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.
- At groups= in_channels, each input channel is convolved with its own set of filters.
分组卷积只需要对nn.Conv2d
中的groups
参数进行设置即可,表示需要分的组数,groups
的默认值为1,即进行常规卷积。以下是实现分组卷积的代码:
- class CSDN_Tem(nn.Module):
- def __init__(self, in_ch, out_ch, groups):
- super(CSDN_Tem, 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
通过以下代码对该模型进行测试,设定输入特征图通道数为16,输出特征图通道数为64,分组数目为4:
- conv = CSDN_Tem(16, 64, 4)
- print(summary(conv, (16, 128, 128), batch_size=1))
控制台输出为:
- ----------------------------------------------------------------
- Layer (type) Output Shape Param #
- ================================================================
- Conv2d-1 [1, 64, 128, 128] 2,368
- ================================================================
- Total params: 2,368
- Trainable params: 2,368
- Non-trainable params: 0
- ----------------------------------------------------------------
- Input size (MB): 1.00
- Forward/backward pass size (MB): 8.00
- Params size (MB): 0.01
- Estimated Total Size (MB): 9.01
- ----------------------------------------------------------------
这一分组卷积过程所需参数为2368个,其中包含了偏置(Bias).
深度可分离卷积的PyTorch代码如下:
- class CSDN_Tem(nn.Module):
- def __init__(self, in_ch, out_ch):
- super(CSDN_Tem, 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 = depth_conv(16,64)
- print(summary(conv,(16,128,128),batch_size=1))
控制台输出结果为:
- ----------------------------------------------------------------
- Layer (type) Output Shape Param #
- ================================================================
- Conv2d-1 [1, 16, 128, 128] 160
- Conv2d-2 [1, 64, 128, 128] 1,088
- ================================================================
- Total params: 1,248
- Trainable params: 1,248
- Non-trainable params: 0
- ----------------------------------------------------------------
- Input size (MB): 1.00
- Forward/backward pass size (MB): 10.00
- Params size (MB): 0.00
- Estimated Total Size (MB): 11.00
深度可分离卷积实现相同的操作仅需1248个参数。
如有疑问,欢迎留言!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。