instantiates the mobilenet architecture reference mobilenets efficient convolutional neural networks for mobile vision applications https arxiv orgabs1704 04861 this function returns a keras image classification model optionally loaded with weights pretrained on imagenet for image classification use cases see this page for detailed examples https keras ioapiapplicationsusageexamplesforimageclassificationmodels for transfer learning use cases make sure to read the guide to transfer learning finetuning https keras ioguidestransferlearning note each keras application expects a specific kind of input preprocessing for mobilenet call keras applications mobilenet preprocessinput on your inputs before passing them to the model mobilenet preprocessinput will scale input pixels between 1 and 1 args inputshape optional shape tuple only to be specified if includetop is false otherwise the input shape has to be 224 224 3 with channelslast data format or 3 224 224 with channelsfirst data format it should have exactly 3 inputs channels and width and height should be no smaller than 32 e g 200 200 3 would be one valid value defaults to none inputshape will be ignored if the inputtensor is provided alpha controls the width of the network this is known as the width multiplier in the mobilenet paper if alpha 1 0 proportionally decreases the number of filters in each layer if alpha 1 0 proportionally increases the number of filters in each layer if alpha 1 default number of filters from the paper are used at each layer defaults to 1 0 depthmultiplier depth multiplier for depthwise convolution this is called the resolution multiplier in the mobilenet paper defaults to 1 0 dropout dropout rate defaults to 0 001 includetop boolean whether to include the fullyconnected layer at the top of the network defaults to true weights one of none random initialization imagenet pretraining on imagenet or the path to the weights file to be loaded defaults to imagenet inputtensor optional keras tensor i e output of layers input to use as image input for the model inputtensor is useful for sharing inputs between multiple different networks defaults to none pooling optional pooling mode for feature extraction when includetop is false none default means that the output of the model will be the 4d tensor output of the last convolutional block avg means that global average pooling will be applied to the output of the last convolutional block and thus the output of the model will be a 2d tensor max means that global max pooling will be applied classes optional number of classes to classify images into only to be specified if includetop is true and if no weights argument is specified defaults to 1000 classifieractivation a str or callable the activation function to use on the top layer ignored unless includetoptrue set classifieractivationnone to return the logits of the top layer when loading pretrained weights classifieractivation can only be none or softmax returns a model instance determine proper input shape and default size ensure that the model takes into account any potential predecessors of inputtensor create model load weights adds an initial convolution layer with batch normalization and relu6 args inputs input tensor of shape rows cols 3 with channelslast data format or 3 rows cols with channelsfirst data format it should have exactly 3 inputs channels and width and height should be no smaller than 32 e g 224 224 3 would be one valid value filters integer the dimensionality of the output space i e the number of output filters in the convolution alpha controls the width of the network if alpha 1 0 proportionally decreases the number of filters in each layer if alpha 1 0 proportionally increases the number of filters in each layer if alpha 1 default number of filters from the paper are used at each layer kernel an integer or tuplelist of 2 integers specifying the width and height of the 2d convolution window can be a single integer to specify the same value for all spatial dimensions strides an integer or tuplelist of 2 integers specifying the strides of the convolution along the width and height can be a single integer to specify the same value for all spatial dimensions specifying any stride value 1 is incompatible with specifying any dilationrate value 1 input shape 4d tensor with shape samples channels rows cols if dataformat channelsfirst or 4d tensor with shape samples rows cols channels if dataformat channelslast output shape 4d tensor with shape samples filters newrows newcols if dataformat channelsfirst or 4d tensor with shape samples newrows newcols filters if dataformat channelslast rows and cols values might have changed due to stride returns output tensor of block adds a depthwise convolution block a depthwise convolution block consists of a depthwise conv batch normalization relu6 pointwise convolution batch normalization and relu6 activation args inputs input tensor of shape rows cols channels with channelslast data format or channels rows cols with channelsfirst data format pointwiseconvfilters integer the dimensionality of the output space i e the number of output filters in the pointwise convolution alpha controls the width of the network if alpha 1 0 proportionally decreases the number of filters in each layer if alpha 1 0 proportionally increases the number of filters in each layer if alpha 1 default number of filters from the paper are used at each layer depthmultiplier the number of depthwise convolution output channels for each input channel the total number of depthwise convolution output channels will be equal to filtersin depthmultiplier strides an integer or tuplelist of 2 integers specifying the strides of the convolution along the width and height can be a single integer to specify the same value for all spatial dimensions specifying any stride value 1 is incompatible with specifying any dilationrate value 1 blockid integer a unique identification designating the block number input shape 4d tensor with shape batch channels rows cols if dataformat channelsfirst or 4d tensor with shape batch rows cols channels if dataformat channelslast output shape 4d tensor with shape batch filters newrows newcols if dataformat channelsfirst or 4d tensor with shape batch newrows newcols filters if dataformat channelslast rows and cols values might have changed due to stride returns output tensor of block instantiates the mobilenet architecture reference mobilenets efficient convolutional neural networks for mobile vision applications https arxiv org abs 1704 04861 this function returns a keras image classification model optionally loaded with weights pre trained on imagenet for image classification use cases see this page for detailed examples https keras io api applications usage examples for image classification models for transfer learning use cases make sure to read the guide to transfer learning fine tuning https keras io guides transfer_learning note each keras application expects a specific kind of input preprocessing for mobilenet call keras applications mobilenet preprocess_input on your inputs before passing them to the model mobilenet preprocess_input will scale input pixels between 1 and 1 args input_shape optional shape tuple only to be specified if include_top is false otherwise the input shape has to be 224 224 3 with channels_last data format or 3 224 224 with channels_first data format it should have exactly 3 inputs channels and width and height should be no smaller than 32 e g 200 200 3 would be one valid value defaults to none input_shape will be ignored if the input_tensor is provided alpha controls the width of the network this is known as the width multiplier in the mobilenet paper if alpha 1 0 proportionally decreases the number of filters in each layer if alpha 1 0 proportionally increases the number of filters in each layer if alpha 1 default number of filters from the paper are used at each layer defaults to 1 0 depth_multiplier depth multiplier for depthwise convolution this is called the resolution multiplier in the mobilenet paper defaults to 1 0 dropout dropout rate defaults to 0 001 include_top boolean whether to include the fully connected layer at the top of the network defaults to true weights one of none random initialization imagenet pre training on imagenet or the path to the weights file to be loaded defaults to imagenet input_tensor optional keras tensor i e output of layers input to use as image input for the model input_tensor is useful for sharing inputs between multiple different networks defaults to none pooling optional pooling mode for feature extraction when include_top is false none default means that the output of the model will be the 4d tensor output of the last convolutional block avg means that global average pooling will be applied to the output of the last convolutional block and thus the output of the model will be a 2d tensor max means that global max pooling will be applied classes optional number of classes to classify images into only to be specified if include_top is true and if no weights argument is specified defaults to 1000 classifier_activation a str or callable the activation function to use on the top layer ignored unless include_top true set classifier_activation none to return the logits of the top layer when loading pretrained weights classifier_activation can only be none or softmax returns a model instance determine proper input shape and default size ensure that the model takes into account any potential predecessors of input_tensor create model load weights adds an initial convolution layer with batch normalization and relu6 args inputs input tensor of shape rows cols 3 with channels_last data format or 3 rows cols with channels_first data format it should have exactly 3 inputs channels and width and height should be no smaller than 32 e g 224 224 3 would be one valid value filters integer the dimensionality of the output space i e the number of output filters in the convolution alpha controls the width of the network if alpha 1 0 proportionally decreases the number of filters in each layer if alpha 1 0 proportionally increases the number of filters in each layer if alpha 1 default number of filters from the paper are used at each layer kernel an integer or tuple list of 2 integers specifying the width and height of the 2d convolution window can be a single integer to specify the same value for all spatial dimensions strides an integer or tuple list of 2 integers specifying the strides of the convolution along the width and height can be a single integer to specify the same value for all spatial dimensions specifying any stride value 1 is incompatible with specifying any dilation_rate value 1 input shape 4d tensor with shape samples channels rows cols if data_format channels_first or 4d tensor with shape samples rows cols channels if data_format channels_last output shape 4d tensor with shape samples filters new_rows new_cols if data_format channels_first or 4d tensor with shape samples new_rows new_cols filters if data_format channels_last rows and cols values might have changed due to stride returns output tensor of block adds a depthwise convolution block a depthwise convolution block consists of a depthwise conv batch normalization relu6 pointwise convolution batch normalization and relu6 activation args inputs input tensor of shape rows cols channels with channels_last data format or channels rows cols with channels_first data format pointwise_conv_filters integer the dimensionality of the output space i e the number of output filters in the pointwise convolution alpha controls the width of the network if alpha 1 0 proportionally decreases the number of filters in each layer if alpha 1 0 proportionally increases the number of filters in each layer if alpha 1 default number of filters from the paper are used at each layer depth_multiplier the number of depthwise convolution output channels for each input channel the total number of depthwise convolution output channels will be equal to filters_in depth_multiplier strides an integer or tuple list of 2 integers specifying the strides of the convolution along the width and height can be a single integer to specify the same value for all spatial dimensions specifying any stride value 1 is incompatible with specifying any dilation_rate value 1 block_id integer a unique identification designating the block number input shape 4d tensor with shape batch channels rows cols if data_format channels_first or 4d tensor with shape batch rows cols channels if data_format channels_last output shape 4d tensor with shape batch filters new_rows new_cols if data_format channels_first or 4d tensor with shape batch new_rows new_cols filters if data_format channels_last rows and cols values might have changed due to stride returns output tensor of block
import warnings

from keras import backend
from keras import layers
from keras.api_export import keras_export
from keras.applications import imagenet_utils
from keras.models import Functional
from keras.ops import operation_utils
from keras.utils import file_utils

BASE_WEIGHT_PATH = (
    "https://storage.googleapis.com/tensorflow/keras-applications/mobilenet/"
)


@keras_export(
    [
        "keras.applications.mobilenet.MobileNet",
        "keras.applications.MobileNet",
    ]
)
def MobileNet(
    input_shape=None,
    alpha=1.0,
    depth_multiplier=1,
    dropout=1e-3,
    include_top=True,
    weights="imagenet",
    input_tensor=None,
    pooling=None,
    classes=1000,
    classifier_activation="softmax",
):
    
    if not (weights in {"imagenet", None} or file_utils.exists(weights)):
        raise ValueError(
            "The `weights` argument should be either "
            "`None` (random initialization), 'imagenet' "
            "(pre-training on ImageNet), "
            "or the path to the weights file to be loaded. "
            f"Received weights={weights}"
        )

    if weights == "imagenet" and include_top and classes != 1000:
        raise ValueError(
            "If using `weights='imagenet'` with `include_top=True`, "
            "`classes` should be 1000.  "
            f"Received classes={classes}"
        )

    
    if input_shape is None:
        default_size = 224
    else:
        if backend.image_data_format() == "channels_first":
            rows = input_shape[1]
            cols = input_shape[2]
        else:
            rows = input_shape[0]
            cols = input_shape[1]

        if rows == cols and rows in [128, 160, 192, 224]:
            default_size = rows
        else:
            default_size = 224

    input_shape = imagenet_utils.obtain_input_shape(
        input_shape,
        default_size=default_size,
        min_size=32,
        data_format=backend.image_data_format(),
        require_flatten=include_top,
        weights=weights,
    )

    if backend.image_data_format() == "channels_last":
        row_axis, col_axis = (0, 1)
    else:
        row_axis, col_axis = (1, 2)
    rows = input_shape[row_axis]
    cols = input_shape[col_axis]

    if weights == "imagenet":
        if depth_multiplier != 1:
            raise ValueError(
                "If imagenet weights are being loaded, "
                "depth multiplier must be 1.  "
                f"Received depth_multiplier={depth_multiplier}"
            )

        if alpha not in [0.25, 0.50, 0.75, 1.0]:
            raise ValueError(
                "If imagenet weights are being loaded, "
                "alpha can be one of"
                "`0.25`, `0.50`, `0.75` or `1.0` only.  "
                f"Received alpha={alpha}"
            )

        if rows != cols or rows not in [128, 160, 192, 224]:
            rows = 224
            warnings.warn(
                "`input_shape` is undefined or non-square, "
                "or `rows` is not in [128, 160, 192, 224]. "
                "Weights for input shape (224, 224) will be "
                "loaded as the default.",
                stacklevel=2,
            )

    if input_tensor is None:
        img_input = layers.Input(shape=input_shape)
    else:
        if not backend.is_keras_tensor(input_tensor):
            img_input = layers.Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    x = _conv_block(img_input, 32, alpha, strides=(2, 2))
    x = _depthwise_conv_block(x, 64, alpha, depth_multiplier, block_id=1)

    x = _depthwise_conv_block(
        x, 128, alpha, depth_multiplier, strides=(2, 2), block_id=2
    )
    x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, block_id=3)

    x = _depthwise_conv_block(
        x, 256, alpha, depth_multiplier, strides=(2, 2), block_id=4
    )
    x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, block_id=5)

    x = _depthwise_conv_block(
        x, 512, alpha, depth_multiplier, strides=(2, 2), block_id=6
    )
    x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=7)
    x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=8)
    x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=9)
    x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=10)
    x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=11)

    x = _depthwise_conv_block(
        x, 1024, alpha, depth_multiplier, strides=(2, 2), block_id=12
    )
    x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, block_id=13)

    if include_top:
        x = layers.GlobalAveragePooling2D(keepdims=True)(x)
        x = layers.Dropout(dropout, name="dropout")(x)
        x = layers.Conv2D(classes, (1, 1), padding="same", name="conv_preds")(x)
        x = layers.Reshape((classes,), name="reshape_2")(x)
        imagenet_utils.validate_activation(classifier_activation, weights)
        x = layers.Activation(
            activation=classifier_activation, name="predictions"
        )(x)
    else:
        if pooling == "avg":
            x = layers.GlobalAveragePooling2D()(x)
        elif pooling == "max":
            x = layers.GlobalMaxPooling2D()(x)

    
    
    if input_tensor is not None:
        inputs = operation_utils.get_source_inputs(input_tensor)
    else:
        inputs = img_input

    
    model = Functional(inputs, x, name=f"mobilenet_{alpha:0.2f}_{rows}")

    
    if weights == "imagenet":
        if alpha == 1.0:
            alpha_text = "1_0"
        elif alpha == 0.75:
            alpha_text = "7_5"
        elif alpha == 0.50:
            alpha_text = "5_0"
        else:
            alpha_text = "2_5"

        if include_top:
            model_name = "mobilenet_%s_%d_tf.h5" % (alpha_text, rows)
            weight_path = BASE_WEIGHT_PATH + model_name
            weights_path = file_utils.get_file(
                model_name, weight_path, cache_subdir="models"
            )
        else:
            model_name = "mobilenet_%s_%d_tf_no_top.h5" % (alpha_text, rows)
            weight_path = BASE_WEIGHT_PATH + model_name
            weights_path = file_utils.get_file(
                model_name, weight_path, cache_subdir="models"
            )
        model.load_weights(weights_path)
    elif weights is not None:
        model.load_weights(weights)

    return model


def _conv_block(inputs, filters, alpha, kernel=(3, 3), strides=(1, 1)):
    
    channel_axis = 1 if backend.image_data_format() == "channels_first" else -1
    filters = int(filters * alpha)
    x = layers.Conv2D(
        filters,
        kernel,
        padding="same",
        use_bias=False,
        strides=strides,
        name="conv1",
    )(inputs)
    x = layers.BatchNormalization(axis=channel_axis, name="conv1_bn")(x)
    return layers.ReLU(6.0, name="conv1_relu")(x)


def _depthwise_conv_block(
    inputs,
    pointwise_conv_filters,
    alpha,
    depth_multiplier=1,
    strides=(1, 1),
    block_id=1,
):
    
    channel_axis = 1 if backend.image_data_format() == "channels_first" else -1
    pointwise_conv_filters = int(pointwise_conv_filters * alpha)

    if strides == (1, 1):
        x = inputs
    else:
        x = layers.ZeroPadding2D(
            ((0, 1), (0, 1)), name="conv_pad_%d" % block_id
        )(inputs)
    x = layers.DepthwiseConv2D(
        (3, 3),
        padding="same" if strides == (1, 1) else "valid",
        depth_multiplier=depth_multiplier,
        strides=strides,
        use_bias=False,
        name="conv_dw_%d" % block_id,
    )(x)
    x = layers.BatchNormalization(
        axis=channel_axis, name="conv_dw_%d_bn" % block_id
    )(x)
    x = layers.ReLU(6.0, name="conv_dw_%d_relu" % block_id)(x)

    x = layers.Conv2D(
        pointwise_conv_filters,
        (1, 1),
        padding="same",
        use_bias=False,
        strides=(1, 1),
        name="conv_pw_%d" % block_id,
    )(x)
    x = layers.BatchNormalization(
        axis=channel_axis, name="conv_pw_%d_bn" % block_id
    )(x)
    return layers.ReLU(6.0, name="conv_pw_%d_relu" % block_id)(x)


@keras_export("keras.applications.mobilenet.preprocess_input")
def preprocess_input(x, data_format=None):
    return imagenet_utils.preprocess_input(
        x, data_format=data_format, mode="tf"
    )


@keras_export("keras.applications.mobilenet.decode_predictions")
def decode_predictions(preds, top=5):
    return imagenet_utils.decode_predictions(preds, top=top)


preprocess_input.__doc__ = imagenet_utils.PREPROCESS_INPUT_DOC.format(
    mode="",
    ret=imagenet_utils.PREPROCESS_INPUT_RET_DOC_TF,
    error=imagenet_utils.PREPROCESS_INPUT_ERROR_DOC,
)
decode_predictions.__doc__ = imagenet_utils.decode_predictions.__doc__