Skip to content

MobileOne

The ResNet model is based on the "An Improved One millisecond Mobile Backbone" paper.

Architecture overview

This architecture optimizes the model for inference speed at inference time on mobile device.

MobileOne architecture

The key takeaways from the paper are the following:

  • reuse the reparametrization concept of RepVGG while adding overparametrization in the block branches.
  • each block is composed of two consecutive reparametrizeable blocks (in a similar fashion than RepVGG): a depth-wise convolutional block, a point-wise convolutional block.

Model builders

The following model builders can be used to instantiate a MobileOne model, with or without pre-trained weights. All the model builders internally rely on the MobileOne base class.

MobileOne

MobileOne(num_blocks: list[int], width_multipliers: list[float], overparam_factor: int = 1, num_classes: int = 10, in_channels: int = 3, act_layer: Module | None = None, norm_layer: Callable[[int], Module] | None = None)

Bases: Sequential

Source code in holocron/models/classification/mobileone.py
def __init__(
    self,
    num_blocks: list[int],
    width_multipliers: list[float],
    overparam_factor: int = 1,
    num_classes: int = 10,
    in_channels: int = 3,
    act_layer: nn.Module | None = None,
    norm_layer: Callable[[int], nn.Module] | None = None,
) -> None:
    if norm_layer is None:
        norm_layer = nn.BatchNorm2d
    if act_layer is None:
        act_layer = nn.ReLU(inplace=True)

    base_planes = [64, 128, 256, 512]
    planes = [round(mult * chans) for mult, chans in zip(width_multipliers, base_planes, strict=True)]

    in_planes = min(64, planes[0])
    # Stem
    layers: list[nn.Module] = [MobileOneBlock(in_channels, in_planes, overparam_factor, 2, act_layer, norm_layer)]

    # Consecutive convolutional blocks
    for _num_blocks, _planes in zip(num_blocks, planes, strict=True):
        # Stride & channel changes
        stage = [MobileOneBlock(in_planes, _planes, overparam_factor, 2, act_layer, norm_layer)]
        # Depth
        stage.extend([
            MobileOneBlock(_planes, _planes, overparam_factor, 1, act_layer, norm_layer)
            for _ in range(_num_blocks - 1)
        ])
        in_planes = _planes

        layers.append(nn.Sequential(*stage))

    super().__init__(
        OrderedDict([
            ("features", nn.Sequential(*layers)),
            ("pool", GlobalAvgPool2d(flatten=True)),
            ("head", nn.Linear(in_planes, num_classes)),
        ])
    )

    # Init all layers
    init.init_module(self, nonlinearity="relu")

reparametrize

reparametrize() -> None

Reparametrize the block by fusing convolutions and BN in each branch, then fusing all branches

Source code in holocron/models/classification/mobileone.py
def reparametrize(self) -> None:
    """Reparametrize the block by fusing convolutions and BN in each branch, then fusing all branches"""
    self.features: nn.Sequential
    # Stem
    self.features[0].reparametrize()
    for stage in self.features[1:]:
        for block in stage:
            block.reparametrize()

mobileone_s0

mobileone_s0(pretrained: bool = False, checkpoint: Checkpoint | None = None, progress: bool = True, **kwargs: Any) -> MobileOne

MobileOne-S0 from "An Improved One millisecond Mobile Backbone"

PARAMETER DESCRIPTION
pretrained

If True, returns a model pre-trained on ImageNet

TYPE: bool DEFAULT: False

checkpoint

If specified, the model's parameters will be set to the checkpoint's values

TYPE: Checkpoint | None DEFAULT: None

progress

If True, displays a progress bar of the download to stderr

TYPE: bool DEFAULT: True

kwargs

keyword args of MobileOne

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
MobileOne

classification model

MobileOne_S0_Checkpoint

Bases: Enum

IMAGENETTE class-attribute instance-attribute
IMAGENETTE = _checkpoint(arch='mobileone_s0', url='https://github.com/frgfm/Holocron/releases/download/v0.2.1/mobileone_s0_224-9ddd1fe9.pth', acc1=0.8808, acc5=0.9883, sha256='9ddd1fe9d6c0a73d3c4d51d3c967a8a27ff5e545705afc557b4d4ac0f34395cb', size=17708169, num_params=4277991, commit='d4a59999179b42fc0d3058ac6b76cc41f49dd56e', train_args='./imagenette2-320/ --arch mobileone_s0 --batch-size 64 --mixup-alpha 0.2 --amp --device 0 --epochs 100 --lr 1e-3 --label-smoothing 0.1 --random-erase 0.1 --train-crop-size 176 --val-resize-size 232 --opt adamw --weight-decay 5e-2')
DEFAULT class-attribute instance-attribute
DEFAULT = IMAGENETTE
Source code in holocron/models/classification/mobileone.py
def mobileone_s0(
    pretrained: bool = False,
    checkpoint: Checkpoint | None = None,
    progress: bool = True,
    **kwargs: Any,
) -> MobileOne:
    """MobileOne-S0 from
    ["An Improved One millisecond Mobile Backbone"](https://arxiv.org/pdf/2206.04040.pdf)

    Args:
        pretrained: If True, returns a model pre-trained on ImageNet
        checkpoint: If specified, the model's parameters will be set to the checkpoint's values
        progress: If True, displays a progress bar of the download to stderr
        kwargs: keyword args of [`MobileOne`][holocron.models.MobileOne]

    Returns:
        classification model

    ::: holocron.models.MobileOne_S0_Checkpoint
        options:
            heading_level: 4
            show_if_no_docstring: true
    """
    checkpoint = _handle_legacy_pretrained(
        pretrained,
        checkpoint,
        MobileOne_S0_Checkpoint.DEFAULT,  # type: ignore[arg-type]
    )
    return _mobileone(checkpoint, progress, [0.75, 1.0, 1.0, 2.0], 4, **kwargs)

mobileone_s1

mobileone_s1(pretrained: bool = False, checkpoint: Checkpoint | None = None, progress: bool = True, **kwargs: Any) -> MobileOne

MobileOne-S1 from "An Improved One millisecond Mobile Backbone"

PARAMETER DESCRIPTION
pretrained

If True, returns a model pre-trained on ImageNet

TYPE: bool DEFAULT: False

checkpoint

If specified, the model's parameters will be set to the checkpoint's values

TYPE: Checkpoint | None DEFAULT: None

progress

If True, displays a progress bar of the download to stderr

TYPE: bool DEFAULT: True

kwargs

keyword args of MobileOne

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
MobileOne

classification model

MobileOne_S1_Checkpoint

Bases: Enum

IMAGENETTE class-attribute instance-attribute
IMAGENETTE = _checkpoint(arch='mobileone_s1', url='https://github.com/frgfm/Holocron/releases/download/v0.2.1/mobileone_s1_224-d4ec5433.pth', acc1=0.9126, acc5=0.9918, sha256='d4ec5433cff3d55d562b7a35fc0c95568ff8f4591bf822dd3e699535bdff90eb', size=14594817, num_params=3555188, commit='d4a59999179b42fc0d3058ac6b76cc41f49dd56e', train_args='./imagenette2-320/ --arch mobileone_s1 --batch-size 64 --mixup-alpha 0.2 --amp --device 0 --epochs 100 --lr 1e-3 --label-smoothing 0.1 --random-erase 0.1 --train-crop-size 176 --val-resize-size 232 --opt adamw --weight-decay 5e-2')
DEFAULT class-attribute instance-attribute
DEFAULT = IMAGENETTE
Source code in holocron/models/classification/mobileone.py
def mobileone_s1(
    pretrained: bool = False,
    checkpoint: Checkpoint | None = None,
    progress: bool = True,
    **kwargs: Any,
) -> MobileOne:
    """MobileOne-S1 from
    ["An Improved One millisecond Mobile Backbone"](https://arxiv.org/pdf/2206.04040.pdf)

    Args:
        pretrained: If True, returns a model pre-trained on ImageNet
        checkpoint: If specified, the model's parameters will be set to the checkpoint's values
        progress: If True, displays a progress bar of the download to stderr
        kwargs: keyword args of [`MobileOne`][holocron.models.MobileOne]

    Returns:
        classification model

    ::: holocron.models.MobileOne_S1_Checkpoint
        options:
            heading_level: 4
            show_if_no_docstring: true
    """
    checkpoint = _handle_legacy_pretrained(
        pretrained,
        checkpoint,
        MobileOne_S1_Checkpoint.DEFAULT,  # type: ignore[arg-type]
    )
    return _mobileone(checkpoint, progress, [1.5, 1.5, 2.0, 2.5], 1, **kwargs)

mobileone_s2

mobileone_s2(pretrained: bool = False, checkpoint: Checkpoint | None = None, progress: bool = True, **kwargs: Any) -> MobileOne

MobileOne-S2 from "An Improved One millisecond Mobile Backbone"

PARAMETER DESCRIPTION
pretrained

If True, returns a model pre-trained on ImageNet

TYPE: bool DEFAULT: False

checkpoint

If specified, the model's parameters will be set to the checkpoint's values

TYPE: Checkpoint | None DEFAULT: None

progress

If True, displays a progress bar of the download to stderr

TYPE: bool DEFAULT: True

kwargs

keyword args of MobileOne

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
MobileOne

classification model

MobileOne_S2_Checkpoint

Bases: Enum

IMAGENETTE class-attribute instance-attribute
IMAGENETTE = _checkpoint(arch='mobileone_s2', url='https://github.com/frgfm/Holocron/releases/download/v0.2.1/mobileone_s2_224-b748859c.pth', acc1=0.9131, acc5=0.9921, sha256='b748859c45a636ea22f0f68a3b7e75e5fb6ffb31178a5a3137931a21b4c41697', size=23866479, num_params=5854324, commit='d4a59999179b42fc0d3058ac6b76cc41f49dd56e', train_args='./imagenette2-320/ --arch mobileone_s2 --batch-size 64 --mixup-alpha 0.2 --amp --device 0 --epochs 100 --lr 1e-3 --label-smoothing 0.1 --random-erase 0.1 --train-crop-size 176 --val-resize-size 232 --opt adamw --weight-decay 5e-2')
DEFAULT class-attribute instance-attribute
DEFAULT = IMAGENETTE
Source code in holocron/models/classification/mobileone.py
def mobileone_s2(
    pretrained: bool = False,
    checkpoint: Checkpoint | None = None,
    progress: bool = True,
    **kwargs: Any,
) -> MobileOne:
    """MobileOne-S2 from
    ["An Improved One millisecond Mobile Backbone"](https://arxiv.org/pdf/2206.04040.pdf)

    Args:
        pretrained: If True, returns a model pre-trained on ImageNet
        checkpoint: If specified, the model's parameters will be set to the checkpoint's values
        progress: If True, displays a progress bar of the download to stderr
        kwargs: keyword args of [`MobileOne`][holocron.models.classification.mobileone.MobileOne]

    Returns:
        classification model

    ::: holocron.models.MobileOne_S2_Checkpoint
        options:
            heading_level: 4
            show_if_no_docstring: true
    """
    checkpoint = _handle_legacy_pretrained(
        pretrained,
        checkpoint,
        MobileOne_S2_Checkpoint.DEFAULT,  # type: ignore[arg-type]
    )
    return _mobileone(checkpoint, progress, [1.5, 2.0, 2.5, 4.0], 1, **kwargs)

mobileone_s3

mobileone_s3(pretrained: bool = False, checkpoint: Checkpoint | None = None, progress: bool = True, **kwargs: Any) -> MobileOne

MobileOne-S3 from "An Improved One millisecond Mobile Backbone"

PARAMETER DESCRIPTION
pretrained

If True, returns a model pre-trained on ImageNet

TYPE: bool DEFAULT: False

checkpoint

If specified, the model's parameters will be set to the checkpoint's values

TYPE: Checkpoint | None DEFAULT: None

progress

If True, displays a progress bar of the download to stderr

TYPE: bool DEFAULT: True

kwargs

keyword args of MobileOne

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
MobileOne

classification model

MobileOne_S3_Checkpoint

Bases: Enum

IMAGENETTE class-attribute instance-attribute
IMAGENETTE = _checkpoint(arch='mobileone_s3', url='https://github.com/frgfm/Holocron/releases/download/v0.2.1/mobileone_s3_224-7f357baf.pth', acc1=0.9106, acc5=0.9931, sha256='7f357baf0754136b4a02e7aec4129874db93ee462f43588b77def730db0b2bca', size=33080943, num_params=8140276, commit='d4a59999179b42fc0d3058ac6b76cc41f49dd56e', train_args='./imagenette2-320/ --arch mobileone_s3 --batch-size 64 --mixup-alpha 0.2 --amp --device 0 --epochs 100 --lr 1e-3 --label-smoothing 0.1 --random-erase 0.1 --train-crop-size 176 --val-resize-size 232 --opt adamw --weight-decay 5e-2')
DEFAULT class-attribute instance-attribute
DEFAULT = IMAGENETTE
Source code in holocron/models/classification/mobileone.py
def mobileone_s3(
    pretrained: bool = False,
    checkpoint: Checkpoint | None = None,
    progress: bool = True,
    **kwargs: Any,
) -> MobileOne:
    """MobileOne-S3 from
    ["An Improved One millisecond Mobile Backbone"](https://arxiv.org/pdf/2206.04040.pdf)

    Args:
        pretrained: If True, returns a model pre-trained on ImageNet
        checkpoint: If specified, the model's parameters will be set to the checkpoint's values
        progress: If True, displays a progress bar of the download to stderr
        kwargs: keyword args of [`MobileOne`][holocron.models.classification.mobileone.MobileOne]

    Returns:
        classification model

    ::: holocron.models.MobileOne_S3_Checkpoint
        options:
            heading_level: 4
            show_if_no_docstring: true
    """
    checkpoint = _handle_legacy_pretrained(
        pretrained,
        checkpoint,
        MobileOne_S3_Checkpoint.DEFAULT,  # type: ignore[arg-type]
    )
    return _mobileone(checkpoint, progress, [2.0, 2.5, 3.0, 4.0], 1, **kwargs)