> 文章列表 > conv2d.中 groups 参数

conv2d.中 groups 参数

conv2d.中 groups 参数

1. groups 改变的是每个filter中通道数;

使用groups前后,并不改变卷积核的个数,
改变的是每个卷积核中通道数;

即普通的卷积核中, 每个卷积核中通道数 = 输入特征的通道数;

使用groups,  每个卷积核中通道数变成 =  输入的通道数/ groups;
卷积核的个数保持原来不变。

所以, groups,是对输入的通道数进行分组。

输入的通道数被分成grops 组, 每组中的通道数便是 input_channel / groups, 此时,卷积核中的通道数也从原始的输入通道数 减少为 每组中的通道数

1.1 举个例子

假设输入的特征的通道数目是8,

import torch.nn  as nn
conv1 =  nn.Conv2d(in_channels=8, out_channels=6, kernel_size=1, groups=1)
conv1.weight.shape
Out[4]: torch.Size([6, 8, 1, 1])
conv2 =  nn.Conv2d(in_channels=8, out_channels=6, kernel_size=1, groups=2)
conv2.weight.shape
Out[6]: torch.Size([6, 4, 1, 1])
x = torch.rand(12,8, 22,22)

那么没有使用groups时, 每个卷积核中通道数目 = 8;

使用了groups,后, 每个卷积核中通道数变为8/ groups;

1.2 depth wise 深度可分离卷积

那么便有一种极端情况, 便是groups组数 = 输入通道数,

此时,每个卷积核中的通道数便是1,
那么, 
即原始带有通道的3维卷积核 filter —> 便退化成二维平面的 kernel 了。

1.3 优缺点

这样做的好处,是减少了卷积参数的数量,
但是,缺点如图所示, 减少了各个通道之间信息的交互, 注意,下图并没有画出卷积核中通道数的改变。

conv2d.中 groups 参数

2. groups 参数不影响输出特征图的通道数

import torch
import torch
x = torch.rand(12,8, 22,22)
y1 = conv1(x)
y1.shape
Out[12]: torch.Size([12, 6, 22, 22])
y2 = conv2(x)
y2.shape
Out[14]: torch.Size([12, 6, 22, 22])