ShuffleNetv1、v2网络详解、使用pytorch搭建模型ShuffleNetv2并基于迁移学习训练

1.ShuffleNetv1网络详解

另一种轻量级网络
网络创新点:
(1)提出了channel shuffle(通道重排)的思想
(2)在ShuffleNet Unit中全是GConv和DWConv

(1)channel shuffle的思想

在这里插入图片描述
在很多网络中都有1x1逐点卷积,这会造成有限的通道之间充满约束,造成精度损失;

可以应用通道稀疏连接,例如组卷积(group convolutions),通过确保每个卷积操作仅在对应的输入通道组上,组卷积可以显著的降低计算损失;

如果多个组卷积堆叠在一起,会有一个副作用:GConv虽然能够减少参数与计算量,但GConv中不同组之间信息没有交流,如图中(a)所示,降低了信息表示能力。

原论文作者提出channel shuffle的思想:首先将输入特征矩阵通过GConv卷积,假设在GConv1中有3个组,那么就对经过GConv1卷积得到的特征矩阵再划分成3份,将每个组中的第一份组合在一起、第二份组合在一起、第三份组合在一起(如b),这样就得到通过channel shuffle的特征矩阵(如c)。

(2)ShuffleNet unit

在这里插入图片描述
作者在原论文中在ResNeXt网络中1x1的卷积(图a)占据了93.4%的计算量,因此ShuffleNetv1中将其都替换成组卷积(图b、c):

1、当Stride==1的时候,采用左边的模块,为了适配和恒等映射做通道融合,配合BN层和ReLU激活函数构成基本单元。

2、当Stride==2的时候,采用右边的模块,在辅分支加入步长为2的3×3平均池化,原本做元素相加的操作转为了通道级联,这扩大了通道维度,增加的计算成本却很少。

2.ShuffleNetv1网络参数

在这里插入图片描述
在原论文中使用最多的是g=3版本(即groups=3),其中Stage为将ShuffleNet unit中的block进行堆叠。

每个Stage中的第一个block,采用的是步长为2的block;第二个block采用的是步长为1的block。对于下一个stage,输出特征矩阵channel会进行翻倍

Stage2中,其第一个block的第一个1x1卷积层不使用组卷积,而使用1x1普通卷积,因为Stage2的输入特征矩阵为24,比较小。

3.ShuffleNetv2网络详解

目前大部分的轻量级模型在对比模型速度时用的指标是FLOPs(指浮点运算数,可以理解为计算量),这个指标可以用来衡量算法或模型的复杂度。

在ShuffleNetv2中作者发现计算复杂度不能直接看FLOPs,并提出了4条设计高效网络准则
1、卷积层的输入和输出特征通道数相等时MAC最小,此时模型速度最快;
2、过量使用组卷积会增加MAC(memory access cost);
3、网络碎片化会降低并行度;
4、不能忽略元素级操作,比如ReLU和Add,虽然它们的FLOPs较小,但是却需要较大的MAC。
基于这四条准则提出了新的block设计

(1)新的block设计

在这里插入图片描述
图中(a)(b)为ShuffleNetv1的两种block,图中©(d)为ShuffleNetv2的两种block。(步长为1和2时)

在©中将每个输入特征矩阵channel划分成两个branch部分(c-c’,c’(c’=c/2)),对应Channel Split

根据准则3要减少模型的碎片化程度,所以在捷径分支没有操作主分支由三个卷积层组成(用于相同的输入和输出channel,满足准则1),两个1x1卷积不使用组卷积(满足准则2);

两个分支通过Concat进行拼接(满足准则1),并且不对最终的输出进行ReLU激活(满足准则4)。(add是数值相加,concat是维度相加)

在(b)中去掉了Channel Split。

1、当Stride==1的时候,采用左边的模块,由于残差边没有卷积,因此宽高不变,主要用于加深网络层数

2、当Stride==2的时候,采用右边的模块,由于残差边有卷积,因此宽高可变,主要用于压缩特征层的宽高,进行下采样

4.ShuffleNetv2网络参数

在这里插入图片描述
与ShuffleNetv2大概框架相同,不同之处为多了Conv5

5.使用pytorch搭建模型ShuffleNetv2

1.model.py

channel_shuffle的实现

在这里插入图片描述

def channel_shuffle(x: Tensor, groups: int) -> Tensor:               #channel_shuffle的实现

    batch_size, num_channels, height, width = x.size()               #获取传入的特征矩阵的size
    channels_per_group = num_channels // groups                      #将channel划分为groups组,channels_per_group对应每个组中channel的个数

    # reshape
    # [batch_size, num_channels, height, width] -> [batch_size, groups, channels_per_group, height, width]
    x = x.view(batch_size, groups, channels_per_group, height, width)   #改变通道排列顺序

    x = torch.transpose(x, 1, 2).contiguous()                           #通过transpose将维度1(groups)和维度2(channels_per_group)的信息调换,相当于转置

    # flatten
    x = x.view(batch_size, -1, height, width)                           #通过view方法将其还原成batch_size、channel、height、width

    return x

block的实现

class InvertedResidual(nn.Module):                                      #shufflenet中的block
    def __init__(self, input_c: int, output_c: int, stride: int):
        super(InvertedResidual, self).__init__()

        if stride not in [1, 2]:                                        #倒残差结构中步长只可能等1和2
            raise ValueError("illegal stride value.")
        self.stride = stride

        assert output_c % 2 == 0                                        #判断输出特征矩阵channel是否是2的整数倍
        branch_features = output_c // 2                                 #因为concat左右两分支channel相同,如果相同则channel一定是2的整数倍
        # 当stride为1时,input_channel应该是branch_features的两倍
        # python中 '<<' 是位运算,可理解为计算×2的快速方法
        assert (self.stride != 1) or (input_c == branch_features << 1)  #当s=2或input_channel是branch_features的两倍

        if self.stride == 2:                                            #当s=2,对应图d的block
            self.branch1 = nn.Sequential(                               #图d中左边分支
                self.depthwise_conv(input_c, input_c, kernel_s=3, stride=self.stride, padding=1),     #左边分支中的dw卷积
                nn.BatchNorm2d(input_c),
                nn.Conv2d(input_c, branch_features, kernel_size=1, stride=1, padding=0, bias=False),  #左边分支中的1x1卷积
                nn.BatchNorm2d(branch_features),
                nn.ReLU(inplace=True)
            )
        else:
            self.branch1 = nn.Sequential()                               #当s=1,对应图c的左边分支

        self.branch2 = nn.Sequential(                                    #无论s=1或2,右边分支结构一样,但步长不一样
            nn.Conv2d(input_c if self.stride > 1 else branch_features, branch_features, kernel_size=1,   #右边分支的第一个1x1卷积
                      stride=1, padding=0, bias=False),                  #当s=2时输入特征矩阵channel为input_c;当s=1时channel为branch_features
            nn.BatchNorm2d(branch_features),
            nn.ReLU(inplace=True),
            self.depthwise_conv(branch_features, branch_features, kernel_s=3, stride=self.stride, padding=1),   #右边分支中的dw卷积
            nn.BatchNorm2d(branch_features),
            nn.Conv2d(branch_features, branch_features, kernel_size=1, stride=1, padding=0, bias=False),     #右边分支的第二个1x1卷积
            nn.BatchNorm2d(branch_features),
            nn.ReLU(inplace=True)
        )

    @staticmethod
    def depthwise_conv(input_c: int,              #实现图d中的dw卷积
                       output_c: int,
                       kernel_s: int,
                       stride: int = 1,
                       padding: int = 0,
                       bias: bool = False) -> nn.Conv2d:
        return nn.Conv2d(in_channels=input_c, out_channels=output_c, kernel_size=kernel_s,
                         stride=stride, padding=padding, bias=bias, groups=input_c)

    def forward(self, x: Tensor) -> Tensor:                               #定义正向传播
        if self.stride == 1:                                              #s=1时,将输入特征矩阵channel进行均分处理
            x1, x2 = x.chunk(2, dim=1)                                    #通过chunk()进行均分成x1和x2,2为2等分,dim=1为通道排序中的维度
            out = torch.cat((x1, self.branch2(x2)), dim=1)                #对x1不做处理,将x2传入branch2得到其输出,然后在channel维度将其进行concat拼接
        else:                                                             #s=2时
            out = torch.cat((self.branch1(x), self.branch2(x)), dim=1)

        out = channel_shuffle(out, 2)                                     #将concat拼接得到的输出进行channel_shuffle处理

        return out

ShuffleNetv2网络搭建

class ShuffleNetV2(nn.Module):                         #shufflenetv2网络搭建
    def __init__(self,
                 stages_repeats: List[int],            #stage2~4中block重复的次数
                 stages_out_channels: List[int],       #conv1、stage2~4、conv5对应的输出特征矩阵channel
                 num_classes: int = 1000,              #类别个数
                 inverted_residual: Callable[..., nn.Module] = InvertedResidual):     #搭建的block
        super(ShuffleNetV2, self).__init__()

        if len(stages_repeats) != 3:                   #判断stages_repeats是否有三个参数(stage2~4)
            raise ValueError("expected stages_repeats as list of 3 positive ints")
        if len(stages_out_channels) != 5:              #判断stages_out_channels是否有五个参数(conv1、stage2~4、conv5)
            raise ValueError("expected stages_out_channels as list of 5 positive ints")
        self._stage_out_channels = stages_out_channels

        # input RGB image
        input_channels = 3
        output_channels = self._stage_out_channels[0]  #conv1的输出特征矩阵channel

        self.conv1 = nn.Sequential(
            nn.Conv2d(input_channels, output_channels, kernel_size=3, stride=2, padding=1, bias=False),
            nn.BatchNorm2d(output_channels),
            nn.ReLU(inplace=True)
        )
        input_channels = output_channels          #将当前的输出channel赋值给下一层的输入channel

        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)

        # Static annotations for mypy     声明三个stage都是通过nn.Sequential实现的
        self.stage2: nn.Sequential
        self.stage3: nn.Sequential
        self.stage4: nn.Sequential

        stage_names = ["stage{}".format(i) for i in [2, 3, 4]]                       #构建stage_name
        for name, repeats, output_channels in zip(stage_names, stages_repeats,       #通过for循环来搭建三个stage中的block
                                                  self._stage_out_channels[1:]):
            seq = [inverted_residual(input_channels, output_channels, 2)]            #每个stage中的第一个block步长都是2,在Sequential中添加步长为2的block
            for i in range(repeats - 1):                                             #遍历剩下的block(步长都是1)
                seq.append(inverted_residual(output_channels, output_channels, 1))   #在Sequential中添加步长为1的block
            setattr(self, name, nn.Sequential(*seq))                                 #通过setattr给self设置一个变量,名字为name,值为nn.Sequential(*seq)
            input_channels = output_channels                                         #将当前的输出channel赋值给下一层的输入channel

        output_channels = self._stage_out_channels[-1]                               #将conv5输出特征矩阵channel赋值给当前的输出channel
        self.conv5 = nn.Sequential(
            nn.Conv2d(input_channels, output_channels, kernel_size=1, stride=1, padding=0, bias=False),
            nn.BatchNorm2d(output_channels),
            nn.ReLU(inplace=True)
        )

        self.fc = nn.Linear(output_channels, num_classes)          #全连接层

    def _forward_impl(self, x: Tensor) -> Tensor:                  #定义正向传播过程
        # See note [TorchScript super()]
        x = self.conv1(x)
        x = self.maxpool(x)
        x = self.stage2(x)
        x = self.stage3(x)
        x = self.stage4(x)
        x = self.conv5(x)
        x = x.mean([2, 3])  # global pool     
        x = self.fc(x)
        return x

    def forward(self, x: Tensor) -> Tensor:
        return self._forward_impl(x)

2.train.py

训练部分代码同Pytorch中自定义Dataset读取数据

预训练权重下载

shufflenet_v2_x1_0
weight: https://download.pytorch.org/models/shufflenetv2_x1-5666bf0f80.pth
shufflenet_v2_x0_5
weight: https://download.pytorch.org/models/shufflenetv2_x0.5-f707e7126e.pth

下载完成后放在项目文件夹中

使用不同版本的shufflenet

from model import shufflenet_v2_x1_0(shufflenet_v2_x0_5)

预训练权重路径更改

    parser.add_argument('--weights', type=str, default='./shufflenetv2_x1.pth',
                        help='initial weights path')                  #将default改成下载的预训练权重名称

不适用预训练权重

    parser.add_argument('--weights', type=str, default='',
                        help='initial weights path')               #将预训练权重路径删除
    parser.add_argument('--freeze-layers', type=bool, default=True)         #为True是只会训练最后的全连接层,如果不使用预训练权重就改成False训练

数据集路径

    parser.add_argument('--data-path', type=str,
                        default="/data/flower_photos")      

3.训练结果

在训练过程中会弹出信息_IncompatibleKeys(missing_keys=['fc.weight', 'fc.bias'], unexpected_keys=[])

这是因为训练的是花分类数据集,只有5个类别,而载入的预训练权重有1000个类别,因此没有载入最后的全连接层的权重。
在这里插入图片描述
跑了30个epoch
在这里插入图片描述

4.predict.py

更改预测图片的路径

    img_path = "your_image_path"

更改训练后的模型权重的路径

    model_weight_path = "your_model_weight_path"
  • 3
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ShuffleNet V2是一种轻量级的深度神经网络,适用于移动设备和嵌入式设备上的图像分类任务。它采用了channel shuffle的思想,在保证模型精度的同时,大大减少了模型的计算量和参数量。 下面是使用PyTorch实现ShuffleNet V2进行图像分类模型代码和训练代码: ```python import torch import torch.nn as nn class ShuffleBlock(nn.Module): def __init__(self, groups=2): super(ShuffleBlock, self).__init__() self.groups = groups def forward(self, x): batch_size, channels, height, width = x.size() channels_per_group = channels // self.groups x = x.view(batch_size, self.groups, channels_per_group, height, width) x = x.transpose(1, 2).contiguous() x = x.view(batch_size, -1, height, width) return x class ShuffleNetV2(nn.Module): def __init__(self, num_classes=1000): super(ShuffleNetV2, self).__init__() self.conv1 = nn.Conv2d(3, 24, kernel_size=3, stride=2, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(24) self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) self.stage2 = self._make_stage(24, 116, 3) self.stage3 = self._make_stage(116, 232, 4) self.stage4 = self._make_stage(232, 464, 6) self.conv5 = nn.Conv2d(464, 1024, kernel_size=1, stride=1, padding=0, bias=False) self.bn5 = nn.BatchNorm2d(1024) self.avgpool = nn.AdaptiveAvgPool2d((1, 1)) self.fc = nn.Linear(1024, num_classes) def _make_stage(self, in_channels, out_channels, repeat): layers = [] layers.append(nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False)) layers.append(nn.BatchNorm2d(out_channels)) layers.append(nn.ReLU(inplace=True)) for i in range(repeat): layers.append(ShuffleBlock()) layers.append(nn.Conv2d(out_channels, out_channels, kernel_size=1, stride=1, padding=0, bias=False)) layers.append(nn.BatchNorm2d(out_channels)) layers.append(nn.ReLU(inplace=True)) return nn.Sequential(*layers) def forward(self, x): x = self.conv1(x) x = self.bn1(x) x = nn.ReLU(inplace=True)(x) x = self.maxpool(x) x = self.stage2(x) x = self.stage3(x) x = self.stage4(x) x = self.conv5(x) x = self.bn5(x) x = nn.ReLU(inplace=True)(x) x = self.avgpool(x) x = x.view(x.size(0), -1) x = self.fc(x) return x ``` 上面的代码实现了ShuffleNet V2的主体结构。通过_make_stage函数可以定义每个stage的结构,其中包含多个ShuffleBlock以及卷积、BN和ReLU激活函数等操作。在forward函数中,将主体结构按照顺序连接起来,最后通过全局平均池化和全连接层输出分类结果。 下面是使用PyTorch进行模型训练的代码: ```python import torch.optim as optim import torchvision.transforms as transforms import torchvision.datasets as datasets # 数据预处理 transform_train = transforms.Compose([ transforms.RandomResizedCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) transform_test = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # 加载训练集和测试集 trainset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train) trainloader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=True, num_workers=2) testset = datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test) testloader = torch.utils.data.DataLoader(testset, batch_size=128, shuffle=False, num_workers=2) # 定义模型 net = ShuffleNetV2(num_classes=10) # 定义损失函数和优化器 criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(net.parameters(), lr=0.1, momentum=0.9, weight_decay=0.0001) # 训练模型 for epoch in range(200): running_loss = 0.0 for i, data in enumerate(trainloader, 0): inputs, labels = data optimizer.zero_grad() outputs = net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step() running_loss += loss.item() if i % 100 == 99: print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 100)) running_loss = 0.0 # 测试模型 correct = 0 total = 0 with torch.no_grad(): for data in testloader: images, labels = data outputs = net(images) _, predicted = torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted == labels).sum().item() print('Accuracy of the network on the test images: %d %%' % (100 * correct / total)) ``` 上面的代码中,使用了CIFAR10数据集进行模型训练和测试。在训练过程中,定义了交叉熵损失函数和随机梯度下降优化器,并使用了数据增强技术。在每个epoch结束后,通过测试集计算模型的准确率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值