MobileNetBackbone model

[source]

MobileNetBackbone class

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,
    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
)

Instantiates the MobileNet architecture.

MobileNet is a lightweight convolutional neural network (CNN) optimized for mobile and edge devices, striking a balance between accuracy and efficiency. By employing depthwise separable convolutions and techniques like Squeeze-and-Excitation (SE) blocks, MobileNet models are highly suitable for real-time applications on resource-constrained devices.

References

Arguments

  • stackwise_expansion: list of list of ints, the expanded filters for each inverted residual block for each block in the model.
  • stackwise_num_blocks: list of ints, number of inversted residual blocks per block
  • stackwise_num_filters: list of list of ints, number of filters for each inverted residual block in the model.
  • stackwise_kernel_size: list of list of ints, kernel size for each inverted residual block in the model.
  • stackwise_num_strides: list of list of ints, stride length for each inverted residual block in the model.
  • stackwise_se_ratio: se ratio for each inverted residual block in the model. 0 if dont want to add Squeeze and Excite layer.
  • stackwise_activation: list of list of activation functions, for each inverted residual block in the model.
  • stackwise_padding: list of list of int, to provide padding values for each inverted residual block in the model.
  • output_num_filters: specifies whether to add conv and batch_norm in the end, if set to None, it will not add these layers in the end. 'None' for MobileNetV1
  • depthwise_filters: int, number of filters in depthwise separable convolution layer,
  • last_layer_filter: int, channels/filters for the head ConvBnAct block
  • squeeze_and_excite: float, squeeze and excite ratio in the depthwise layer, None, if dont want to do squeeze and excite
  • image_shape: optional shape tuple, defaults to (224, 224, 3).
  • input_activation: activation function to be used in the input layer 'hard_swish' for MobileNetV3, 'relu6' for MobileNetV1 and MobileNetV2
  • output_activation: activation function to be used in the output layer 'hard_swish' for MobileNetV3, 'relu6' for MobileNetV1 and MobileNetV2
  • input_num_filters: int, channels/filters for the input before the stem input_conv
  • dtype: None or str or keras.mixed_precision.DTypePolicy. The dtype to use for the model's computations and weights.

Example

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)

[source]

from_preset method

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

Instantiate a keras_hub.models.Backbone from a model preset.

A preset is a directory of configs, weights and other file assets used to save and load a pre-trained model. The preset can be passed as a one of:

  1. a built-in preset identifier like 'bert_base_en'
  2. a Kaggle Models handle like 'kaggle://user/bert/keras/bert_base_en'
  3. a Hugging Face handle like 'hf://user/bert_base_en'
  4. a path to a local preset directory like './bert_base_en'

This constructor can be called in one of two ways. Either from the base class like keras_hub.models.Backbone.from_preset(), or from a model class like keras_hub.models.GemmaBackbone.from_preset(). If calling from the base class, the subclass of the returning object will be inferred from the config in the preset directory.

For any Backbone subclass, you can run cls.presets.keys() to list all built-in presets available on the class.

Arguments

  • preset: string. A built-in preset identifier, a Kaggle Models handle, a Hugging Face handle, or a path to a local directory.
  • load_weights: bool. If True, the weights will be loaded into the model architecture. If False, the weights will be randomly initialized.

Examples

# 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,
)
Preset Parameters Description
mobilenet_v3_small_050_imagenet 278.78K Small MobileNet V3 model pre-trained on the ImageNet 1k dataset at a 224x224 resolution.