Skip to content

DarkNetV4

The DarkNetV4 model is based on the "CSPNet: A New Backbone that can Enhance Learning Capability of CNN" paper.

Architecture overview

This paper makes a more powerful version than its predecedors by increasing depth and using ResNet tricks.

The key takeaways from the paper are the following:

  • add cross-path connections to its predecessors
  • explores newer non-linearities

Model builders

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

DarknetV4

DarknetV4(layout: list[tuple[int, int]], num_classes: int = 10, in_channels: int = 3, stem_channels: int = 32, num_features: int = 1, act_layer: Module | None = None, norm_layer: Callable[[int], Module] | None = None, drop_layer: Callable[..., Module] | None = None, conv_layer: Callable[..., Module] | None = None)

Bases: Sequential

Source code in holocron/models/classification/darknetv4.py
def __init__(
    self,
    layout: list[tuple[int, int]],
    num_classes: int = 10,
    in_channels: int = 3,
    stem_channels: int = 32,
    num_features: int = 1,
    act_layer: nn.Module | None = None,
    norm_layer: Callable[[int], nn.Module] | None = None,
    drop_layer: Callable[..., nn.Module] | None = None,
    conv_layer: Callable[..., nn.Module] | None = None,
) -> None:
    super().__init__(
        OrderedDict([
            (
                "features",
                DarknetBodyV4(
                    layout,
                    in_channels,
                    stem_channels,
                    num_features,
                    act_layer,
                    norm_layer,
                    drop_layer,
                    conv_layer,
                ),
            ),
            ("pool", GlobalAvgPool2d(flatten=True)),
            ("classifier", nn.Linear(layout[-1][0], num_classes)),
        ])
    )

    init_module(self, "leaky_relu")

cspdarknet53

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

CSP-Darknet-53 from "CSPNet: A New Backbone that can Enhance Learning Capability of CNN"

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 DarknetV4

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
DarknetV4

classification model

CSPDarknet53_Checkpoint

Bases: Enum

IMAGENETTE class-attribute instance-attribute
IMAGENETTE = _checkpoint(arch='cspdarknet53', url='https://github.com/frgfm/Holocron/releases/download/v0.2.1/cspdarknet53_224-7a69463a.pth', acc1=0.945, acc5=0.9964, sha256='7a69463a4bd445beb6691dfd6ef7378efcf941f75d07d60034106ebedfcb82f8', size=106732575, num_params=26627434, commit='6e32c5b578711a2ef3731a8f8c61760ed9f03e58', train_args='./imagenette2-320/ --arch cspdarknet53 --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/darknetv4.py
def cspdarknet53(
    pretrained: bool = False,
    checkpoint: Checkpoint | None = None,
    progress: bool = True,
    **kwargs: Any,
) -> DarknetV4:
    """CSP-Darknet-53 from
    ["CSPNet: A New Backbone that can Enhance Learning Capability of CNN"](https://arxiv.org/pdf/1911.11929.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 [`DarknetV4`][holocron.models.classification.darknetv4.DarknetV4]

    Returns:
        classification model

    ::: holocron.models.CSPDarknet53_Checkpoint
        options:
            heading_level: 4
            show_if_no_docstring: true
    """
    checkpoint = _handle_legacy_pretrained(
        pretrained,
        checkpoint,
        CSPDarknet53_Checkpoint.DEFAULT.value,
    )
    return _darknet(checkpoint, progress, [(64, 1), (128, 2), (256, 8), (512, 8), (1024, 4)], **kwargs)

cspdarknet53_mish

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

Modified version of CSP-Darknet-53 from "CSPNet: A New Backbone that can Enhance Learning Capability of CNN" with Mish as activation layer and DropBlock as regularization layer.

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 DarknetV4

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
DarknetV4

classification model

CSPDarknet53_Mish_Checkpoint

Bases: Enum

IMAGENETTE class-attribute instance-attribute
IMAGENETTE = _checkpoint(arch='cspdarknet53_mish', url='https://github.com/frgfm/Holocron/releases/download/v0.2.1/cspdarknet53_mish_224-1b660b3c.pth', acc1=0.9465, acc5=0.9969, sha256='1b660b3cb144195100c99ee3b9b863c37a5b5a59619c8de8c588b3d2af954b15', size=106737530, num_params=26627434, commit='6e32c5b578711a2ef3731a8f8c61760ed9f03e58', train_args='./imagenette2-320/ --arch cspdarknet53_mish --batch-size 32 --grad-acc 2 --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/darknetv4.py
def cspdarknet53_mish(
    pretrained: bool = False,
    checkpoint: Checkpoint | None = None,
    progress: bool = True,
    **kwargs: Any,
) -> DarknetV4:
    """Modified version of CSP-Darknet-53 from
    ["CSPNet: A New Backbone that can Enhance Learning Capability of CNN"](https://arxiv.org/pdf/1911.11929.pdf)
    with Mish as activation layer and DropBlock as regularization layer.

    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 [`DarknetV4`][holocron.models.classification.darknetv4.DarknetV4]

    Returns:
        classification model

    ::: holocron.models.CSPDarknet53_Mish_Checkpoint
        options:
            heading_level: 4
            show_if_no_docstring: true
    """
    kwargs["act_layer"] = nn.Mish(inplace=True)
    kwargs["drop_layer"] = DropBlock2d

    checkpoint = _handle_legacy_pretrained(
        pretrained,
        checkpoint,
        CSPDarknet53_Mish_Checkpoint.DEFAULT.value,
    )
    return _darknet(checkpoint, progress, [(64, 1), (128, 2), (256, 8), (512, 8), (1024, 4)], **kwargs)