神经网络学习小记录38——MobileNetV3(large)模型的复现详解

学习前言

为了防止某位我的粉丝寒假没有办法正常工作,我赶紧看了看MobilenetV3。
在这里插入图片描述

什么是MobileNetV3

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

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

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

它综合了以下四个特点:
1、MobileNetV1的深度可分离卷积(depthwise separable convolutions)。
2、MobileNetV2的具有线性瓶颈的逆残差结构(the inverted residual with linear bottleneck)。
3、轻量级的注意力模型。
4、利用h-swish代替swish函数。

代码下载

https://github.com/bubbliiiing/classic-convolution-network

MobileNetV3(large)的网络结构

1、MobileNetV3(large)的整体结构

在这里插入图片描述
如何看懂这个表呢?我们从每一列出发:
第一列Input代表mobilenetV3每个特征层的shape变化;
第二列Operator代表每次特征层即将经历的block结构,我们可以看到在MobileNetV3中,特征提取经过了许多的bneck结构;
第三、四列分别代表了bneck内逆残差结构上升后的通道数、输入到bneck时特征层的通道数。
第五列SE代表了是否在这一层引入注意力机制。
第六列NL代表了激活函数的种类,HS代表h-swish,RE代表RELU。
第七列s代表了每一次block结构所用的步长。

2、MobileNetV3特有的bneck结构

bneck结构如下图所示:
在这里插入图片描述
它综合了以下四个特点:
a、MobileNetV2的具有线性瓶颈的逆残差结构(the inverted residual with linear bottleneck)。
在这里插入图片描述
即先利用1x1卷积进行升维度,再进行下面的操作,并具有残差边。

b、MobileNetV1的深度可分离卷积(depthwise separable convolutions)。
在这里插入图片描述
在输入1x1卷积进行升维度后,进行3x3深度可分离卷积。

c、轻量级的注意力模型。
在这里插入图片描述
这个注意力机制的作用方式是调整每个通道的权重。

d、利用h-swish代替swish函数。
在结构中使用了h-swishj激活函数,代替swish函数,减少运算量,提高性能。
在这里插入图片描述

网络实现代码

由于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_large(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')
    x = bottleneck(x, 16, (3, 3), up_dim=16, stride=1, sq=False, nl='RE')
    # 112,112,16 -> 56,56,24
    x = bottleneck(x, 24, (3, 3), up_dim=64, stride=2, sq=False, nl='RE')
    x = bottleneck(x, 24, (3, 3), up_dim=72, stride=1, sq=False, nl='RE')
    
    # 56,56,24 -> 28,28,40
    x = bottleneck(x, 40, (5, 5), up_dim=72, stride=2, sq=True, nl='RE')
    x = bottleneck(x, 40, (5, 5), up_dim=120, stride=1, sq=True, nl='RE')
    x = bottleneck(x, 40, (5, 5), up_dim=120, stride=1, sq=True, nl='RE')

    # 28,28,40 -> 14,14,80
    x = bottleneck(x, 80, (3, 3), up_dim=240, stride=2, sq=False, nl='HS')
    x = bottleneck(x, 80, (3, 3), up_dim=200, stride=1, sq=False, nl='HS')
    x = bottleneck(x, 80, (3, 3), up_dim=184, stride=1, sq=False, nl='HS')
    x = bottleneck(x, 80, (3, 3), up_dim=184, stride=1, sq=False, nl='HS')

    # 14,14,80 -> 14,14,112
    x = bottleneck(x, 112, (3, 3), up_dim=480, stride=1, sq=True, nl='HS')
    x = bottleneck(x, 112, (3, 3), up_dim=672, stride=1, sq=True, nl='HS')

    # 14,14,112 -> 7,7,160
    x = bottleneck(x, 160, (5, 5), up_dim=672, stride=2, sq=True, nl='HS')
    x = bottleneck(x, 160, (5, 5), up_dim=960, stride=1, sq=True, nl='HS')
    x = bottleneck(x, 160, (5, 5), up_dim=960, stride=1, sq=True, nl='HS')
    
    # 7,7,160 -> 7,7,960
    x = conv_block(x, 960, (1, 1), strides=(1, 1), nl='HS')
    x = GlobalAveragePooling2D()(x)
    x = Reshape((1, 1, 960))(x)

    x = Conv2D(1280, (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_large()
    model.summary()
<think>嗯,用户想要查找MobileNetV3的管道网络结构图,也就是架构可视化。首先,我需要回忆一下MobileNetV3的相关信息。MobileNetV3是由Google在2019年提出的,应该是分为Large和Small两个版本,主要针对移动设备优化的轻量级网络。用户提到的“管道网络结构”可能是指整个模型的结构图,包括各个模块的组成和连接方式。 接下来,我应该考虑如何获取结构图。通常,论文中会有结构图,尤其是原论文。所以建议用户查阅原论文,比如Howard等人在2019年的论文。不过用户可能需要更直观的可视化工具,比如Keras或TensorFlow的模型摘要和绘图功能。比如使用model.summary()keras.utils.plot_model函数。不过要注意的是,MobileNetV3可能在不同框架中的实现情况不同,比如Keras是否有内置的MobileNetV3模型。我记得Keras Applications模块中有MobileNetV3,所以需要确认用户使用的TensorFlow版本是否支持。 另外,用户提到的引用中有提到MobileNetV2的使用,比如引用[2]和[3],可能用户之前接触过MobileNetV2的代码,现在想迁移到V3。需要指出代码中的差异,比如在导入模型时使用applications.MobileNetV3而不是V2,并注意参数是否变化,比如include_top、pooling等。 然后,可视化部分可能需要使用graphviz库,如果用户没有安装的话,会遇到问题,所以需要提醒安装。此外,使用Netron工具也是一个好建议,因为用户可能更喜欢图形界面查看结构,特别是如果模型保存为.h5或.keras文件后,用Netron打开会更直观。 可能用户的问题还包括如何调整输入形状或输出层,比如迁移学习时需要修改顶层,这时候结构图可以帮助理解哪些层需要冻结,哪些需要微调。引用[2]中提到了使用预训练的MobileNetV2,所以类似的方法适用于MobileNetV3,但需要正确导入模型。 还要考虑用户是否有更深层的需求,比如结构图中的特定模块,如SE模块(Squeeze-and-Excitation)或h-swish激活函数,这些是MobileNetV3的特点,可能在结构图中需要特别标注。不过标准的结构图工具可能不会显示这些细节,所以建议结合论文中的描述。 最后,生成相关问题时要围绕结构可视化、代码实现、模块组成、迁移学习应用等方面,帮助用户进一步探索MobileNetV3的结构和应用。</think>### MobileNetV3 结构可视化方法 需要获取 MobileNetV3 的结构图,可通过以下方法实现: 1. **官方论文与开源实现** MobileNetV3 的原论文《Searching for MobileNetV3》提供了详细的模块级结构描述[^1]。其核心创新在于: - 引入 **h-swish 激活函数** - 使用 **Squeeze-and-Excitation (SE) 模块** - 采用 **NAS(神经架构搜索)** 优化网络结构 2. **代码级可视化(Keras/TensorFlow)** 若已安装 TensorFlow 2.x,可通过以下代码生成模型结构: ```python from tensorflow.keras.applications import MobileNetV3Small model = MobileNetV3Small( input_shape=(224, 224, 3), include_top=True, weights=&#39;imagenet&#39;, pooling=None ) model.summary() # 输出层级结构 ``` 执行后会显示类似如下的模块序列: ``` Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) [(None, 224, 224, 3)] 0 ... expanded_conv_2 (Sequential) (None, 112, 112, 16) 1456 ... ``` (完整输出约包含 100+ 层) 3. **图形化结构图生成** 使用 `keras.utils.plot_model` 生成矢量图: ```python from tensorflow.keras.utils import plot_model plot_model(model, to_file=&#39;mobilenetv3.png&#39;, show_shapes=True) ``` 需提前安装 `graphviz` 和 `pydot` 库: ```bash pip install graphviz pydot ``` 4. **Netron 工具查看** 将模型保存为 `.h5` 或 `.keras` 格式后,用 [Netron](https://github.com/lutzroeder/netron) 打开可交互查看每层参数: ```python model.save(&#39;mobilenetv3.keras&#39;) ```
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bubbliiiing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值