通道随机混合操作(Channel Shuffle Operation)可以看成“重塑-转置-重塑”(“reshape-transpose-
reshape”)操作。这里假设把4个Feature Maps级联后,共1024个Channels。现在把这1024个Channels随机打乱混合。首先把Channels重塑为(g, c),其中 g 表示分组数目,c=1024/g。然后把它转置一下为(c, g)。然后把它重塑为1024个通道。
def channel_shuffle(x, groups):
batchsize, num_channels, height, width = x.data.size()
channels_per_group = num_channels // groups
# reshape
x = x.view(batchsize, groups,
channels_per_group, height, width)
# transpose
# - contiguous() required if transpose() is used before view().
# See https://github.com/pytorch/pytorch/issues/764
x = torch.transpose(x, 1, 2).contiguous()
# flatten