ShuffleNet v2网络结构复现(Pytorch版)

简介: ShuffleNet v2网络结构复现(Pytorch版)

ShuffleNet v2网络结构复现


3ebfc1253bd442b2a667fa04462651c8.png

from torch import nn
from torch.nn import functional
import torch
from torchsummary import summary
# ---------------------------- ShuffleBlock start -------------------------------
# 通道重排,跨group信息交流
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)
    x = torch.transpose(x, 1, 2).contiguous()
    # flatten
    x = x.view(batchsize, -1, height, width)
    return x
class CBRM(nn.Module):
    def __init__(self, c1, c2):  # ch_in, ch_out
        super(CBRM, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(c1, c2, kernel_size=3, stride=2, padding=1, bias=False),
            nn.BatchNorm2d(c2),
            nn.ReLU(inplace=True),
        )
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
    def forward(self, x):
        return self.maxpool(self.conv(x))
class Shuffle_Block(nn.Module):
    def __init__(self, inp, oup, stride):
        super(Shuffle_Block, self).__init__()
        if not (1 <= stride <= 3):
            raise ValueError('illegal stride value')
        self.stride = stride
        branch_features = oup // 2
        assert (self.stride != 1) or (inp == branch_features << 1)
        if self.stride > 1:
            self.branch1 = nn.Sequential(
                self.depthwise_conv(inp, inp, kernel_size=3, stride=self.stride, padding=1),
                nn.BatchNorm2d(inp),
                nn.Conv2d(inp, branch_features, kernel_size=1, stride=1, padding=0, bias=False),
                nn.BatchNorm2d(branch_features),
                nn.ReLU(inplace=True),
            )
        self.branch2 = nn.Sequential(
            nn.Conv2d(inp if (self.stride > 1) else branch_features,
                      branch_features, kernel_size=1, stride=1, padding=0, bias=False),
            nn.BatchNorm2d(branch_features),
            nn.ReLU(inplace=True),
            self.depthwise_conv(branch_features, branch_features, kernel_size=3, stride=self.stride, padding=1),
            nn.BatchNorm2d(branch_features),
            nn.Conv2d(branch_features, branch_features, kernel_size=1, stride=1, padding=0, bias=False),
            nn.BatchNorm2d(branch_features),
            nn.ReLU(inplace=True),
        )
    @staticmethod
    def depthwise_conv(i, o, kernel_size, stride=1, padding=0, bias=False):
        return nn.Conv2d(i, o, kernel_size, stride, padding, bias=bias, groups=i)
    def forward(self, x):
        if self.stride == 1:
            x1, x2 = x.chunk(2, dim=1)  # 按照维度1进行split
            out = torch.cat((x1, self.branch2(x2)), dim=1)
        else:
            out = torch.cat((self.branch1(x), self.branch2(x)), dim=1)
        out = channel_shuffle(out, 2)
        return out
class ShuffleNetV2(nn.Module):
    def __init__(self):
        super(ShuffleNetV2, self).__init__()
        self.MobileNet_01 = nn.Sequential(
            CBRM(3, 32),                 # 160x160
            Shuffle_Block(32, 128, 2),   # 80x80
            Shuffle_Block(128, 128, 1),  # 80x80
            Shuffle_Block(128, 256, 2),  # 40x40
            Shuffle_Block(256, 256, 1),  # 40x40
            Shuffle_Block(256, 512, 2),  # 20x20
            Shuffle_Block(512, 512, 1),  # 20x20
        )
    def forward(self, x):
        x = self.MobileNet_01(x)
        return x
if __name__ == '__main__':
    shufflenetv2 = ShuffleNetV2()
    device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
    inputs = shufflenetv2.to(device)
    summary(inputs, (3, 640, 640), batch_size=1, device="cuda")  # 分别是输入数据的三个维度
    #print(shufflenetv2)
----------------------------------------------------------------
        Layer (type)               Output Shape         Param #
================================================================
            Conv2d-1          [1, 32, 320, 320]             864
       BatchNorm2d-2          [1, 32, 320, 320]              64
              ReLU-3          [1, 32, 320, 320]               0
         MaxPool2d-4          [1, 32, 160, 160]               0
              CBRM-5          [1, 32, 160, 160]               0
            Conv2d-6            [1, 32, 80, 80]             288
       BatchNorm2d-7            [1, 32, 80, 80]              64
            Conv2d-8            [1, 64, 80, 80]           2,048
       BatchNorm2d-9            [1, 64, 80, 80]             128
             ReLU-10            [1, 64, 80, 80]               0
           Conv2d-11          [1, 64, 160, 160]           2,048
      BatchNorm2d-12          [1, 64, 160, 160]             128
             ReLU-13          [1, 64, 160, 160]               0
           Conv2d-14            [1, 64, 80, 80]             576
      BatchNorm2d-15            [1, 64, 80, 80]             128
           Conv2d-16            [1, 64, 80, 80]           4,096
      BatchNorm2d-17            [1, 64, 80, 80]             128
             ReLU-18            [1, 64, 80, 80]               0
    Shuffle_Block-19           [1, 128, 80, 80]               0
           Conv2d-20            [1, 64, 80, 80]           4,096
      BatchNorm2d-21            [1, 64, 80, 80]             128
             ReLU-22            [1, 64, 80, 80]               0
           Conv2d-23            [1, 64, 80, 80]             576
      BatchNorm2d-24            [1, 64, 80, 80]             128
           Conv2d-25            [1, 64, 80, 80]           4,096
      BatchNorm2d-26            [1, 64, 80, 80]             128
             ReLU-27            [1, 64, 80, 80]               0
    Shuffle_Block-28           [1, 128, 80, 80]               0
           Conv2d-29           [1, 128, 40, 40]           1,152
      BatchNorm2d-30           [1, 128, 40, 40]             256
           Conv2d-31           [1, 128, 40, 40]          16,384
      BatchNorm2d-32           [1, 128, 40, 40]             256
             ReLU-33           [1, 128, 40, 40]               0
           Conv2d-34           [1, 128, 80, 80]          16,384
      BatchNorm2d-35           [1, 128, 80, 80]             256
             ReLU-36           [1, 128, 80, 80]               0
           Conv2d-37           [1, 128, 40, 40]           1,152
      BatchNorm2d-38           [1, 128, 40, 40]             256
           Conv2d-39           [1, 128, 40, 40]          16,384
      BatchNorm2d-40           [1, 128, 40, 40]             256
             ReLU-41           [1, 128, 40, 40]               0
    Shuffle_Block-42           [1, 256, 40, 40]               0
           Conv2d-43           [1, 128, 40, 40]          16,384
      BatchNorm2d-44           [1, 128, 40, 40]             256
             ReLU-45           [1, 128, 40, 40]               0
           Conv2d-46           [1, 128, 40, 40]           1,152
      BatchNorm2d-47           [1, 128, 40, 40]             256
           Conv2d-48           [1, 128, 40, 40]          16,384
      BatchNorm2d-49           [1, 128, 40, 40]             256
             ReLU-50           [1, 128, 40, 40]               0
    Shuffle_Block-51           [1, 256, 40, 40]               0
           Conv2d-52           [1, 256, 20, 20]           2,304
      BatchNorm2d-53           [1, 256, 20, 20]             512
           Conv2d-54           [1, 256, 20, 20]          65,536
      BatchNorm2d-55           [1, 256, 20, 20]             512
             ReLU-56           [1, 256, 20, 20]               0
           Conv2d-57           [1, 256, 40, 40]          65,536
      BatchNorm2d-58           [1, 256, 40, 40]             512
             ReLU-59           [1, 256, 40, 40]               0
           Conv2d-60           [1, 256, 20, 20]           2,304
      BatchNorm2d-61           [1, 256, 20, 20]             512
           Conv2d-62           [1, 256, 20, 20]          65,536
      BatchNorm2d-63           [1, 256, 20, 20]             512
             ReLU-64           [1, 256, 20, 20]               0
    Shuffle_Block-65           [1, 512, 20, 20]               0
           Conv2d-66           [1, 256, 20, 20]          65,536
      BatchNorm2d-67           [1, 256, 20, 20]             512
             ReLU-68           [1, 256, 20, 20]               0
           Conv2d-69           [1, 256, 20, 20]           2,304
      BatchNorm2d-70           [1, 256, 20, 20]             512
           Conv2d-71           [1, 256, 20, 20]          65,536
      BatchNorm2d-72           [1, 256, 20, 20]             512
             ReLU-73           [1, 256, 20, 20]               0
    Shuffle_Block-74           [1, 512, 20, 20]               0
================================================================
Total params: 445,824
Trainable params: 445,824
Non-trainable params: 0
----------------------------------------------------------------
Input size (MB): 4.69
Forward/backward pass size (MB): 270.31
Params size (MB): 1.70
Estimated Total Size (MB): 276.70
----------------------------------------------------------------
ShuffleNetV2(
  (MobileNet_01): Sequential(
    (0): CBRM(
      (conv): Sequential(
        (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
        (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (2): ReLU(inplace=True)
      )
      (maxpool): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
    )
    (1): Shuffle_Block(
      (branch1): Sequential(
        (0): Conv2d(32, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=32, bias=False)
        (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (2): Conv2d(32, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (3): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (4): ReLU(inplace=True)
      )
      (branch2): Sequential(
        (0): Conv2d(32, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (2): ReLU(inplace=True)
        (3): Conv2d(64, 64, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=64, bias=False)
        (4): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (5): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (6): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (7): ReLU(inplace=True)
      )
    )
    (2): Shuffle_Block(
      (branch2): Sequential(
        (0): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (2): ReLU(inplace=True)
        (3): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=64, bias=False)
        (4): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (5): Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (6): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (7): ReLU(inplace=True)
      )
    )
    (3): Shuffle_Block(
      (branch1): Sequential(
        (0): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=128, bias=False)
        (1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (2): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (3): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (4): ReLU(inplace=True)
      )
      (branch2): Sequential(
        (0): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (2): ReLU(inplace=True)
        (3): Conv2d(128, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=128, bias=False)
        (4): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (5): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (6): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (7): ReLU(inplace=True)
      )
    )
    (4): Shuffle_Block(
      (branch2): Sequential(
        (0): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (2): ReLU(inplace=True)
        (3): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=128, bias=False)
        (4): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (5): Conv2d(128, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (6): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (7): ReLU(inplace=True)
      )
    )
    (5): Shuffle_Block(
      (branch1): Sequential(
        (0): Conv2d(256, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=256, bias=False)
        (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (2): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (3): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (4): ReLU(inplace=True)
      )
      (branch2): Sequential(
        (0): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (2): ReLU(inplace=True)
        (3): Conv2d(256, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=256, bias=False)
        (4): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (5): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (6): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (7): ReLU(inplace=True)
      )
    )
    (6): Shuffle_Block(
      (branch2): Sequential(
        (0): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (2): ReLU(inplace=True)
        (3): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=256, bias=False)
        (4): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (5): Conv2d(256, 256, kernel_size=(1, 1), stride=(1, 1), bias=False)
        (6): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        (7): ReLU(inplace=True)
      )
    )
  )
)
目录
打赏
0
0
0
0
11
分享
相关文章
PyTorch生态系统中的连续深度学习:使用Torchdyn实现连续时间神经网络
神经常微分方程(Neural ODEs)是深度学习领域的创新模型,将神经网络的离散变换扩展为连续时间动力系统。本文基于Torchdyn库介绍Neural ODE的实现与训练方法,涵盖数据集构建、模型构建、基于PyTorch Lightning的训练及实验结果可视化等内容。Torchdyn支持多种数值求解算法和高级特性,适用于生成模型、时间序列分析等领域。
157 77
PyTorch生态系统中的连续深度学习:使用Torchdyn实现连续时间神经网络
基于昇腾用PyTorch实现传统CTR模型WideDeep网络
本文介绍了如何在昇腾平台上使用PyTorch实现经典的WideDeep网络模型,以处理推荐系统中的点击率(CTR)预测问题。
186 66
YOLOv11改进策略【模型轻量化】| 替换轻量化骨干网络:ShuffleNet V1
YOLOv11改进策略【模型轻量化】| 替换轻量化骨干网络:ShuffleNet V1
55 11
YOLOv11改进策略【模型轻量化】| 替换轻量化骨干网络:ShuffleNet V1
RT-DETR改进策略【模型轻量化】| 替换轻量化骨干网络:ShuffleNet V1
RT-DETR改进策略【模型轻量化】| 替换轻量化骨干网络:ShuffleNet V1
21 0
RT-DETR改进策略【模型轻量化】| 替换轻量化骨干网络:ShuffleNet V1
深度强化学习中SAC算法:数学原理、网络架构及其PyTorch实现
软演员-评论家算法(Soft Actor-Critic, SAC)是深度强化学习领域的重要进展,基于最大熵框架优化策略,在探索与利用之间实现动态平衡。SAC通过双Q网络设计和自适应温度参数,提升了训练稳定性和样本效率。本文详细解析了SAC的数学原理、网络架构及PyTorch实现,涵盖演员网络的动作采样与对数概率计算、评论家网络的Q值估计及其损失函数,并介绍了完整的SAC智能体实现流程。SAC在连续动作空间中表现出色,具有高样本效率和稳定的训练过程,适合实际应用场景。
213 7
深度强化学习中SAC算法:数学原理、网络架构及其PyTorch实现
PyTorch 中的动态计算图:实现灵活的神经网络架构
【8月更文第27天】PyTorch 是一款流行的深度学习框架,它以其灵活性和易用性而闻名。与 TensorFlow 等其他框架相比,PyTorch 最大的特点之一是支持动态计算图。这意味着开发者可以在运行时定义网络结构,这为构建复杂的模型提供了极大的便利。本文将深入探讨 PyTorch 中动态计算图的工作原理,并通过一些示例代码展示如何利用这一特性来构建灵活的神经网络架构。
478 1
基于Pytorch Gemotric在昇腾上实现GraphSage图神经网络
本文详细介绍了如何在昇腾平台上使用PyTorch实现GraphSage算法,在CiteSeer数据集上进行图神经网络的分类训练。内容涵盖GraphSage的创新点、算法原理、网络架构及实战代码分析,通过采样和聚合方法高效处理大规模图数据。实验结果显示,模型在CiteSeer数据集上的分类准确率达到66.5%。
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等