torch.chunk与nn.Conv2d groups

torch.chunk

Segmentation
If the size of feature x is: 32x64x224x224 (BxCxHxW)
q = torch.chunk(x, 8, dim=1)
x is the feature to be segmented, 8 is to be divided into several pieces, dim is the dimension of the specified segmentation, It is equal to 1 here, that is, it is divided according to the channel
. It will be divided into 8 pieces according to the channel, then each piece is 32x8x224x224. The
returned q is a tuple, and these eight pieces are placed in the tuple.

nn.Conv2d groups

The picture comes from: https://www.bilibili.com/video/BV1SL411V7dc?p=2&vd_source=20756f1667908eb0bfec8057bec3fb85
The default value of groups is 1, which means they are divided into groups, which is equivalent to no grouping.
For example: the input feature size is 1x6x5x5, and the convolution kernel size If it is 9x6x4x4, the output feature is 1x9x2x2. At this time, if groups=1,
insert image description here
if you set groups to 3
, the output channel and input channel will be divided into three groups. The
input feature is three groups of 1x2x5x5, and the output feature is three groups of 1x3x2x2, corresponding to each other. Each group has a 3x2x4x4 convolution kernel.
These three convolution kernels are put together to be 9x2x4x4 (as you can see, the number of parameters is reduced).
insert image description here
When setting groups, pay attention that it must be the common divisor of in_channels and out_channels.

Guess you like

Origin blog.csdn.net/holly_Z_P_F/article/details/128370943