Skip to content

deeplabcut.pose_estimation_tensorflow.backbones.mobilenet_v2

Implementation of Mobilenet V2.

Architecture: https://arxiv.org/abs/1801.04381

The base model gives 72.2% accuracy on ImageNet, with 300MMadds, 3.4 M parameters.

Functions:

Name Description
mobilenet

Creates mobilenet V2 network.

mobilenet_base

Creates base of the mobilenet (no pooling and no logits) .

training_scope

Defines MobilenetV2 training scope.

mobilenet

mobilenet(
    input_tensor,
    num_classes=1001,
    depth_multiplier=1.0,
    scope="MobilenetV2",
    conv_defs=None,
    finegrain_classification_mode=False,
    min_depth=None,
    divisible_by=None,
    activation_fn=None,
    **kwargs
)

Creates mobilenet V2 network.

Inference mode is created by default. To create training use training_scope below.

with tf.contrib.slim.arg_scope(mobilenet_v2.training_scope()): logits, endpoints = mobilenet_v2.mobilenet(input_tensor)

Parameters:

Name Type Description Default

input_tensor

The input tensor

required

num_classes

number of classes

1001

depth_multiplier

The multiplier applied to scale number of

1.0

scope

Scope of the operator

'MobilenetV2'

conv_defs

Allows to override default conv def.

None

finegrain_classification_mode

When set to True, the model

False

https

//arxiv.org/abs/1801.04381

required

min_depth

If provided, will ensure that all layers will have that

None

divisible_by

If provided will ensure that all layers # channels

None

activation_fn

Activation function to use, defaults to tf.nn.relu6 if not specified.

None

**kwargs

passed directly to mobilenet.mobilenet: prediction_fn- what prediction function to use. reuse-: whether to reuse variables (if reuse set to true, scope must be given).

{}

Returns: logits/endpoints pair

Raises:

Type Description
ValueError

On invalid arguments

Source code in deeplabcut/pose_estimation_tensorflow/backbones/mobilenet_v2.py
@slim.add_arg_scope
def mobilenet(
    input_tensor,
    num_classes=1001,
    depth_multiplier=1.0,
    scope="MobilenetV2",
    conv_defs=None,
    finegrain_classification_mode=False,
    min_depth=None,
    divisible_by=None,
    activation_fn=None,
    **kwargs,
):
    """Creates mobilenet V2 network.

    Inference mode is created by default. To create training use training_scope
    below.

    with tf.contrib.slim.arg_scope(mobilenet_v2.training_scope()):
       logits, endpoints = mobilenet_v2.mobilenet(input_tensor)

    Args:
      input_tensor: The input tensor
      num_classes: number of classes
      depth_multiplier: The multiplier applied to scale number of
      channels in each layer.
      scope: Scope of the operator
      conv_defs: Allows to override default conv def.
      finegrain_classification_mode: When set to True, the model
      will keep the last layer large even for small multipliers. Following
      https://arxiv.org/abs/1801.04381
      suggests that it improves performance for ImageNet-type of problems.
        *Note* ignored if final_endpoint makes the builder exit earlier.
      min_depth: If provided, will ensure that all layers will have that
      many channels after application of depth multiplier.
      divisible_by: If provided will ensure that all layers # channels
      will be divisible by this number.
      activation_fn: Activation function to use, defaults to tf.nn.relu6 if not
        specified.
      **kwargs: passed directly to mobilenet.mobilenet:
        prediction_fn- what prediction function to use.
        reuse-: whether to reuse variables (if reuse set to true, scope
        must be given).
    Returns:
      logits/endpoints pair

    Raises:
      ValueError: On invalid arguments
    """
    if conv_defs is None:
        conv_defs = V2_DEF
    if "multiplier" in kwargs:
        raise ValueError('mobilenetv2 doesn\'t support generic multiplier parameter use "depth_multiplier" instead.')
    if finegrain_classification_mode:
        conv_defs = copy.deepcopy(conv_defs)
        if depth_multiplier < 1:
            conv_defs["spec"][-1].params["num_outputs"] /= depth_multiplier
    if activation_fn:
        conv_defs = copy.deepcopy(conv_defs)
        defaults = conv_defs["defaults"]
        conv_defaults = defaults[(slim.conv2d, slim.fully_connected, slim.separable_conv2d)]
        conv_defaults["activation_fn"] = activation_fn

    depth_args = {}
    # NB: do not set depth_args unless they are provided to avoid overriding
    # whatever default depth_multiplier might have thanks to arg_scope.
    if min_depth is not None:
        depth_args["min_depth"] = min_depth
    if divisible_by is not None:
        depth_args["divisible_by"] = divisible_by

    with slim.arg_scope((lib.depth_multiplier,), **depth_args):
        return lib.mobilenet(
            input_tensor,
            num_classes=num_classes,
            conv_defs=conv_defs,
            scope=scope,
            multiplier=depth_multiplier,
            **kwargs,
        )

mobilenet_base

mobilenet_base(input_tensor, depth_multiplier=1.0, **kwargs)

Creates base of the mobilenet (no pooling and no logits) .

Source code in deeplabcut/pose_estimation_tensorflow/backbones/mobilenet_v2.py
@slim.add_arg_scope
def mobilenet_base(input_tensor, depth_multiplier=1.0, **kwargs):
    """Creates base of the mobilenet (no pooling and no logits) ."""
    return mobilenet(input_tensor, depth_multiplier=depth_multiplier, base_only=True, **kwargs)

training_scope

training_scope(**kwargs)

Defines MobilenetV2 training scope.

Usage

with tf.contrib.slim.arg_scope(mobilenet_v2.training_scope()): logits, endpoints = mobilenet_v2.mobilenet(input_tensor)

with slim.

Parameters:

Name Type Description Default

**kwargs

Passed to mobilenet.training_scope. The following parameters

{}

are supported

weight_decay- The weight decay to use for regularizing the model. stddev- Standard deviation for initialization, if negative uses xavier. dropout_keep_prob- dropout keep probability bn_decay- decay for the batch norm moving averages.

required

Returns:

Type Description

An arg_scope to use for the mobilenet v2 model.

Source code in deeplabcut/pose_estimation_tensorflow/backbones/mobilenet_v2.py
def training_scope(**kwargs):
    """Defines MobilenetV2 training scope.

    Usage:
       with tf.contrib.slim.arg_scope(mobilenet_v2.training_scope()):
         logits, endpoints = mobilenet_v2.mobilenet(input_tensor)

    with slim.

    Args:
      **kwargs: Passed to mobilenet.training_scope. The following parameters
      are supported:
        weight_decay- The weight decay to use for regularizing the model.
        stddev-  Standard deviation for initialization, if negative uses xavier.
        dropout_keep_prob- dropout keep probability
        bn_decay- decay for the batch norm moving averages.

    Returns:
      An `arg_scope` to use for the mobilenet v2 model.
    """
    return lib.training_scope(**kwargs)