通道随机混合操作(Channel Shuffle Operation)

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

具体代码如下:

import torch


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
    x = x.view(batchsize, -1, height, width)


    return x

如果您觉得我的文章对您有所帮助,欢迎扫码进行赞赏!

通道随机混合操作(Channel Shuffle Operation)_第1张图片

 

参考:

1. ShuffleNet

你可能感兴趣的:(PyTorch,ShuffleNet,Channel,Shuffle,通道随机混合)