KerasHub:预训练模型 / API 文档 / 模型架构 / MobileNet / MobileNetBackbone 模型

MobileNetBackbone 模型

[来源]

MobileNetBackbone

keras_hub.models.MobileNetBackbone(
    stackwise_expansion,
    stackwise_num_blocks,
    stackwise_num_filters,
    stackwise_kernel_size,
    stackwise_num_strides,
    stackwise_se_ratio,
    stackwise_activation,
    stackwise_padding,
    output_num_filters,
    depthwise_filters,
    depthwise_stride,
    depthwise_residual,
    last_layer_filter,
    squeeze_and_excite=None,
    image_shape=(None, None, 3),
    input_activation="hard_swish",
    output_activation="hard_swish",
    input_num_filters=16,
    dtype=None,
    **kwargs
)

实例化 MobileNet 架构。

MobileNet 是一种轻量级卷积神经网络 (CNN),针对移动和边缘设备进行了优化,在准确性和效率之间取得了平衡。通过采用深度可分离卷积和 Squeeze-and-Excitation (SE) 块等技术,MobileNet 模型非常适用于资源受限设备上的实时应用。

参考文献

参数

  • stackwise_expansion:int 列表的列表,模型中每个块的每个倒残差块的扩展过滤器。
  • stackwise_num_blocks:int 列表,每个块中的倒残差块数量
  • stackwise_num_filters:int 列表的列表,模型中每个倒残差块的过滤器数量。
  • stackwise_kernel_size:int 列表的列表,模型中每个倒残差块的核大小。
  • stackwise_num_strides:int 列表的列表,模型中每个倒残差块的步长。
  • stackwise_se_ratio:模型中每个倒残差块的 SE 比率。如果不想添加 Squeeze and Excite 层,则为 0。
  • stackwise_activation:激活函数列表的列表,用于模型中的每个倒残差块。
  • stackwise_padding:int 列表的列表,提供模型中每个倒残差块的填充值。
  • output_num_filters:指定是否在末尾添加 conv 和 batch_norm 层,如果设置为 None,则不会在末尾添加这些层。MobileNetV1 为 'None'。
  • depthwise_filters:int,深度可分离卷积层中的过滤器数量,
  • last_layer_filter:int,头部 ConvBnAct 块的通道数/过滤器数
  • squeeze_and_excite:float,深度层中的 squeeze and excite 比率,如果不进行 squeeze and excite,则为 None。
  • image_shape:可选的 shape 元组,默认为 (224, 224, 3)。
  • input_activation:输入层使用的激活函数,MobileNetV3 为 'hard_swish',MobileNetV1 和 MobileNetV2 为 'relu6'。
  • output_activation:输出层使用的激活函数,MobileNetV3 为 'hard_swish',MobileNetV1 和 MobileNetV2 为 'relu6'。
  • input_num_filters:int,在 stem input_conv 之前的输入的通道数/过滤器数
  • dtypeNone 或 str 或 keras.mixed_precision.DTypePolicy。用于模型计算和权重的 dtype。

示例

input_data = tf.ones(shape=(8, 224, 224, 3))

# Randomly initialized backbone with a custom config
model = MobileNetBackbone(
    stackwise_expansion=[
            [40, 56],
            [64, 144, 144],
            [72, 72],
            [144, 288, 288],
        ],
        stackwise_num_blocks=[2, 3, 2, 3],
        stackwise_num_filters=[
            [16, 16],
            [24, 24, 24],
            [24, 24],
            [48, 48, 48],
        ],
        stackwise_kernel_size=[[3, 3], [5, 5, 5], [5, 5], [5, 5, 5]],
        stackwise_num_strides=[[2, 1], [2, 1, 1], [1, 1], [2, 1, 1]],
        stackwise_se_ratio=[
            [None, None],
            [0.25, 0.25, 0.25],
            [0.3, 0.3],
            [0.3, 0.25, 0.25],
        ],
        stackwise_activation=[
            ["relu", "relu"],
            ["hard_swish", "hard_swish", "hard_swish"],
            ["hard_swish", "hard_swish"],
            ["hard_swish", "hard_swish", "hard_swish"],
        ],
        output_num_filters=288,
        input_activation="hard_swish",
        output_activation="hard_swish",
        input_num_filters=16,
        image_shape=(224, 224, 3),
        depthwise_filters=8,
        squeeze_and_excite=0.5,

)
output = model(input_data)

[来源]

from_preset 方法

MobileNetBackbone.from_preset(preset, load_weights=True, **kwargs)

从模型预设实例化 keras_hub.models.Backbone

预设是用于保存和加载预训练模型的配置、权重和其他文件资产的目录。preset 可以作为以下之一传递:

  1. 内置预设标识符,例如 'bert_base_en'
  2. Kaggle Models 句柄,例如 'kaggle://user/bert/keras/bert_base_en'
  3. Hugging Face 句柄,例如 'hf://user/bert_base_en'
  4. 本地预设目录的路径,例如 './bert_base_en'

这个构造函数可以通过两种方式调用。要么从基类调用,例如 keras_hub.models.Backbone.from_preset(),要么从模型类调用,例如 keras_hub.models.GemmaBackbone.from_preset()。如果从基类调用,返回对象的子类将从预设目录中的配置推断出来。

对于任何 Backbone 子类,您可以运行 cls.presets.keys() 列出该类可用的所有内置预设。

参数

  • preset:string。内置预设标识符、Kaggle Models 句柄、Hugging Face 句柄或本地目录的路径。
  • load_weights:bool。如果为 True,权重将被加载到模型架构中。如果为 False,权重将被随机初始化。

示例

# Load a Gemma backbone with pre-trained weights.
model = keras_hub.models.Backbone.from_preset(
    "gemma_2b_en",
)

# Load a Bert backbone with a pre-trained config and random weights.
model = keras_hub.models.Backbone.from_preset(
    "bert_base_en",
    load_weights=False,
)
预设 参数 描述
mobilenet_v3_small_050_imagenet 278.78K 在 224x224 分辨率下,于 ImageNet 1k 数据集上预训练的小型 Mobilenet V3 模型。通道乘数减半。
mobilenet_v3_small_100_imagenet 939.12K 在 224x224 分辨率下,于 ImageNet 1k 数据集上预训练的小型 Mobilenet V3 模型。具有基线通道乘数。
mobilenet_v3_large_100_imagenet 3.00M 在 224x224 分辨率下,于 ImageNet 1k 数据集上预训练的大型 Mobilenet V3 模型。具有基线通道乘数。
mobilenet_v3_large_100_imagenet_21k 3.00M 在 224x224 分辨率下,于 ImageNet 21k 数据集上预训练的大型 Mobilenet V3 模型。具有基线通道乘数。