目录
  • 什么是MobileNetV3
  • large与small的区别
  • MobileNetV3(small)的网络结构
    • 1、MobileNetV3(small)的整体结构
    • 2、MobileNetV3特有的bneck结构
  • 网络实现代码

    什么是MobileNetV3

    不知道咋地,就是突然想把small也一起写了。

    最新的MobileNetV3的被写在了论文《Searching for MobileNetV3》中。

    它是mobilnet的最新版,据说效果还是很好的。

    作为一种轻量级网络,它的参数量还是一如既往的小。

    它综合了以下四个特点:

    1、MobileNetV1的深度可分离卷积(depthwise separable convolutions)。

    2、MobileNetV2的具有线性瓶颈的逆残差结构(the inverted residual with linear bottleneck)。

    3、轻量级的注意力模型。

    4、利用h-swish代替swish函数。

    代码下载

    large与small的区别

    其实MobileNetV3中的large与small模型没有特别大的区别,主要的区别是通道数的变化与bneck的次数。

    MobileNetV3(small)的网络结构

    1、MobileNetV3(small)的整体结构

    python神经网络MobileNetV3 small模型的复现详解

    如上为MobileNetV3(small)的表,与MobileNetV3(large)相比,bneck的次数与通道数都有一定的下降。

    如何看懂这个表呢?我们从每一列出发:

    第一列Input代表mobilenetV3每个特征层的shape变化;

    第二列Operator代表每次特征层即将经历的block结构,我们可以看到在MobileNetV3中,特征提取经过了许多的bneck结构;

    第三、四列分别代表了bneck内逆残差结构上升后的通道数、输入到bneck时特征层的通道数。

    第五列SE代表了是否在这一层引入注意力机制。

    第六列NL代表了激活函数的种类,HS代表h-swish,RE代表RELU。

    第七列s代表了每一次block结构所用的步长。

    2、MobileNetV3特有的bneck结构

    bneck结构如下图所示:

    python神经网络MobileNetV3 small模型的复现详解

    它综合了以下四个特点:

    a、MobileNetV2的具有线性瓶颈的逆残差结构(the inverted residual with linear bottleneck)。

    python神经网络MobileNetV3 small模型的复现详解

    即先利用1×1卷积进行升维度,再进行下面的操作,并具有残差边。

    b、MobileNetV1的深度可分离卷积(depthwise separable convolutions)。

    python神经网络MobileNetV3 small模型的复现详解

    在输入1×1卷积进行升维度后,进行3×3深度可分离卷积。

    c、轻量级的注意力模型。

    python神经网络MobileNetV3 small模型的复现详解

    这个注意力机制的作用方式是调整每个通道的权重。

    d、利用h-swish代替swish函数。

    在结构中使用了h-swishj激活函数,代替swish函数,减少运算量,提高性能。

    python神经网络MobileNetV3 small模型的复现详解

    网络实现代码

    由于keras代码没有预训练权重,所以只是把网络结构po出来。

    from keras.layers import Conv2D, DepthwiseConv2D, Dense, GlobalAveragePooling2D,Input
    from keras.layers import Activation, BatchNormalization, Add, Multiply, Reshape
    from keras.models import Model
    from keras import backend as K
    alpha = 1
    def relu6(x):
        # relu函数
        return K.relu(x, max_value=6.0)
    def hard_swish(x):
        # 利用relu函数乘上x模拟sigmoid
        return x * K.relu(x + 3.0, max_value=6.0) / 6.0
    def return_activation(x, nl):
        # 用于判断使用哪个激活函数
        if nl == 'HS':
            x = Activation(hard_swish)(x)
        if nl == 'RE':
            x = Activation(relu6)(x)
        return x
    def conv_block(inputs, filters, kernel, strides, nl):
        # 一个卷积单元,也就是conv2d + batchnormalization + activation
        channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
        x = Conv2D(filters, kernel, padding='same', strides=strides)(inputs)
        x = BatchNormalization(axis=channel_axis)(x)
        return return_activation(x, nl)
    def squeeze(inputs):
        # 注意力机制单元
        input_channels = int(inputs.shape[-1])
        x = GlobalAveragePooling2D()(inputs)
        x = Dense(int(input_channels/4))(x)
        x = Activation(relu6)(x)
        x = Dense(input_channels)(x)
        x = Activation(hard_swish)(x)
        x = Reshape((1, 1, input_channels))(x)
        x = Multiply()([inputs, x])
        return x
    def bottleneck(inputs, filters, kernel, up_dim, stride, sq, nl):
        channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
        input_shape = K.int_shape(inputs)
        tchannel = int(up_dim)
        cchannel = int(alpha * filters)
        r = stride == 1 and input_shape[3] == filters
        # 1x1卷积调整通道数,通道数上升
        x = conv_block(inputs, tchannel, (1, 1), (1, 1), nl)
        # 进行3x3深度可分离卷积
        x = DepthwiseConv2D(kernel, strides=(stride, stride), depth_multiplier=1, padding='same')(x)
        x = BatchNormalization(axis=channel_axis)(x)
        x = return_activation(x, nl)
        # 引入注意力机制
        if sq:
            x = squeeze(x)
        # 下降通道数
        x = Conv2D(cchannel, (1, 1), strides=(1, 1), padding='same')(x)
        x = BatchNormalization(axis=channel_axis)(x)
        if r:
            x = Add()([x, inputs])
        return x
    def MobileNetv3_small(shape = (224,224,3),n_class = 1000):
        inputs = Input(shape)
        # 224,224,3 -> 112,112,16
        x = conv_block(inputs, 16, (3, 3), strides=(2, 2), nl='HS')
        # 112,112,16 -> 56,56,16
        x = bottleneck(x, 16, (3, 3), up_dim=16, stride=2, sq=True, nl='RE')
        # 56,56,16 -> 28,28,24
        x = bottleneck(x, 24, (3, 3), up_dim=72, stride=2, sq=False, nl='RE')
        x = bottleneck(x, 24, (3, 3), up_dim=88, stride=1, sq=False, nl='RE')
        # 28,28,24 -> 14,14,40
        x = bottleneck(x, 40, (5, 5), up_dim=96, stride=2, sq=True, nl='HS')
        x = bottleneck(x, 40, (5, 5), up_dim=240, stride=1, sq=True, nl='HS')
        x = bottleneck(x, 40, (5, 5), up_dim=240, stride=1, sq=True, nl='HS')
        # 14,14,40 -> 14,14,48
        x = bottleneck(x, 48, (5, 5), up_dim=120, stride=1, sq=True, nl='HS')
        x = bottleneck(x, 48, (5, 5), up_dim=144, stride=1, sq=True, nl='HS')
        # 14,14,48 -> 7,7,96
        x = bottleneck(x, 96, (5, 5), up_dim=288, stride=2, sq=True, nl='HS')
        x = bottleneck(x, 96, (5, 5), up_dim=576, stride=1, sq=True, nl='HS')
        x = bottleneck(x, 96, (5, 5), up_dim=576, stride=1, sq=True, nl='HS')
        x = conv_block(x, 576, (1, 1), strides=(1, 1), nl='HS')
        x = GlobalAveragePooling2D()(x)
        x = Reshape((1, 1, 576))(x)
        x = Conv2D(1024, (1, 1), padding='same')(x)
        x = return_activation(x, 'HS')
        x = Conv2D(n_class, (1, 1), padding='same', activation='softmax')(x)
        x = Reshape((n_class,))(x)
        model = Model(inputs, x)
        return model
    if __name__ == "__main__":
        model = MobileNetv3_small()
        model.summary()
    

    以上就是python神经网络MobileNetV3 small模型的复现详解的详细内容,更多关于MobileNetV3 small模型复现的资料请关注其它相关文章!

    声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。