基于paddlepaddle实现Mobilenet_v2复现

Posted 扬志九洲

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于paddlepaddle实现Mobilenet_v2复现相关的知识,希望对你有一定的参考价值。

基于paddlepaddle实现Mobilenet_v1复现

基于paddlepaddle实现Mobilenet_v3复现

文章目录

一、介绍

Mobilenet_v2继承了Mobilenet_v1的深度可分离结构,引入了Inverted_residual_block结构,相比于Residual_block结构,该结构对特征矩阵先增加层数再减少层数。

Residual_block结构和Inverted_residual_block结构比较

二、Bottleneck residual block结构

三、整体网络架构


此处t为在Bottleneck residual block结构中间特征矩阵的深度的扩增倍数,input为输入特征矩阵的结构,c为输出网络的层数,n为该种网络的重复次数,s为stride。

四、论文复现

1、导入相关库

import paddle
from paddle.nn import Conv2D, BatchNorm2D, ReLU6, AdaptiveAvgPool2D, Dropout

2、建立基础的代码运行块

class ConvBNLayer(paddle.nn.Layer):
    def __init__(self, inchannels, outchannels, stride, kernelsize = 3, groups = 1):
        super(ConvBNLayer, self).__init__()
        padding = (kernelsize - 1) // 2
        self.conv = Conv2D(
            in_channels=inchannels,
            out_channels=outchannels,
            kernel_size=kernelsize,
            stride=stride,
            padding=padding,
            groups=groups
            )
        self.bn = BatchNorm2D(outchannels)
        self.relu = ReLU6()

    def forward(self, inputs):
        x = self.conv(inputs)
        x = self.bn(x)
        x = self.relu(x)

        return x

3、建立Bottleneck_residual_block

此处注意stride=1和stride=2时Bottleneck_residual_block为两种结构:

class Bottleneck_Block(paddle.nn.Layer):
    def __init__(self, inchannel, outchannel, stride, t):
        super(Bottleneck_Block, self).__init__()
        self.stride = stride
        self.layer0 = ConvBNLayer(
            inchannels=inchannel,
            outchannels=t*inchannel,
            kernelsize=1,
            stride=1
            )
        self.layer1 = ConvBNLayer(
            inchannels=t*inchannel,
            outchannels=t*inchannel,
            kernelsize=3,
            stride=stride,
            groups=t*inchannel
            )
        self.conv0 = Conv2D(
            in_channels=t*inchannel,
            out_channels=outchannel,
            kernel_size=1
            )
        self.bn0 = BatchNorm2D(outchannel)
        self.conv1 = Conv2D(
            in_channels=inchannel,
            out_channels=outchannel,
            kernel_size=1
            )
        self.bn1 = BatchNorm2D(outchannel)

    def forward(self, inputs):
        y = inputs
        x = self.layer0(inputs)
        x = self.layer1(x)
        x = self.conv0(x)
        x = self.bn0(x)
        if self.stride == 2:
            return x          
        if self.stride == 1:
            y = self.conv1(inputs)
            y = self.bn1(y)
            return  x+y

4、搭建整体的网络结构

class Mobilenet_v2(paddle.nn.Layer):
    def __init__(self, numchannel = 1000):
        super(Mobilenet_v2, self).__init__()
        Inverted_residual_block_setting=[
            [1, 16, 1, 1],
            [6, 24, 2, 2],
            [6, 32, 3, 2],
            [6, 64, 4, 2],
            [6, 96, 3, 1],
            [6, 160, 3, 2],
            [6, 320, 1, 1]
        ]
        self.feature=[
            ConvBNLayer(
            inchannels=3,
            outchannels=32,
            kernelsize=3,
            stride=2
            )
        ]
        inchannel = 32
        for t, c, n, s in Inverted_residual_block_setting:
            for i in range(n):
                stride = s if i == 0 else 1
                self.feature.append(
                    Bottleneck_Block(
                        inchannel=inchannel,
                        outchannel=c,
                        stride=stride,
                        t=t
                        )
                    )
                inchannel = c
        self.feature.append(
            ConvBNLayer(
                inchannels=320,
                outchannels=1280,
                stride=1
            )
        )
        self.pool = AdaptiveAvgPool2D(output_size=(1, 1))
        self.conv = Conv2D(in_channels=1280, out_channels=numchannel, kernel_size=1)
        self.dropout = Dropout(0.2)

    def forward(self, x):
        for layer in self.feature:
            x = layer(x)
        x = self.pool(x)
        x =self.conv(x)
        x = self.dropout(x)
        x = paddle.squeeze(x)
        
        return x    
             

5、查看网络结构

model = Mobilenet_v2()
paddle.summary(model, (1, 3, 224, 224))

以上是关于基于paddlepaddle实现Mobilenet_v2复现的主要内容,如果未能解决你的问题,请参考以下文章

基于paddlepaddle实现Mobilenet_v3复现

基于paddlepaddle实现Mobilenet_v3复现

基于paddlepaddle实现Mobilenet_v3复现

基于paddlepaddle实现Mobilenet_v3复现

基于paddlepaddle实现Mobilenet_v2复现

基于paddlepaddle实现MobileNets_v1复现