通道随机混合操作(Channel Shuffle Operation)可以看成“重塑-转置-重塑”(“reshape-transpose-
    reshape”)操作。这里假设把4个Feature Maps级联后,共1024个Channels。现在把这1024个Channels随机打乱混合。首先把Channels重塑为(g, c),其中 g 表示分组数目,c=1024/g。然后把它转置一下为(c, g)。然后把它重塑为1024个通道。

    1. def channel_shuffle(x, groups):
    2. batchsize, num_channels, height, width = x.data.size()
    3. channels_per_group = num_channels // groups
    4. # reshape
    5. x = x.view(batchsize, groups,
    6. channels_per_group, height, width)
    7. # transpose
    8. # - contiguous() required if transpose() is used before view().
    9. # See https://github.com/pytorch/pytorch/issues/764
    10. x = torch.transpose(x, 1, 2).contiguous()
    11. # flatten