HGNetV2Backbone model

[source]

HGNetV2Backbone class

keras_hub.models.HGNetV2Backbone(
    depths,
    embedding_size,
    hidden_sizes,
    stem_channels,
    hidden_act,
    use_learnable_affine_block,
    stackwise_stage_filters,
    apply_downsample,
    use_lightweight_conv_block,
    image_shape=(None, None, 3),
    data_format=None,
    out_features=None,
    dtype=None,
    **kwargs
)

This class represents a Keras Backbone of the HGNetV2 model.

This class implements an HGNetV2 backbone architecture, a convolutional neural network (CNN) optimized for GPU efficiency. HGNetV2 is frequently used as a lightweight CNN backbone in object detection pipelines like RT-DETR and YOLO variants, delivering strong performance on classification and detection tasks, with speed-ups and accuracy gains compared to larger CNN backbones.

Arguments

  • depths: list of ints, the number of blocks in each stage.
  • embedding_size: int, the size of the embedding layer.
  • hidden_sizes: list of ints, the sizes of the hidden layers.
  • stem_channels: list of ints, the channels for the stem part.
  • hidden_act: str, the activation function for hidden layers.
  • use_learnable_affine_block: bool, whether to use learnable affine transformations.
  • stackwise_stage_filters: list of tuples, where each tuple contains configuration for a stage: (stage_in_channels, stage_mid_channels, stage_out_channels, stage_num_blocks, stage_num_of_layers, stage_kernel_size).
    • stage_in_channels: int, input channels for the stage
    • stage_mid_channels: int, middle channels for the stage
    • stage_out_channels: int, output channels for the stage
    • stage_num_blocks: int, number of blocks in the stage
    • stage_num_of_layers: int, number of layers in each block
    • stage_kernel_size: int, kernel size for the stage
  • apply_downsample: list of bools, whether to downsample in each stage.
  • use_lightweight_conv_block: list of bools, whether to use HGNetV2 lightweight convolutional blocks in each stage.
  • image_shape: tuple, the shape of the input image without the batch size. Defaults to (None, None, 3).
  • data_format: None or str, the data format ('channels_last' or 'channels_first'). If not specified, defaults to the image_data_format value in your Keras config.
  • out_features: list of str or None, the names of the output features to return. If None, returns all available features from all stages. Defaults to None.
  • dtype: None or str or keras.mixed_precision.DTypePolicy, the data type for computations and weights.

Examples

import numpy as np
from keras_hub.src.models.hgnetv2.hgnetv2_backbone import HGNetV2Backbone
input_data = np.ones(shape=(8, 224, 224, 3))

# Pretrained backbone.
model = keras_hub.models.HGNetV2Backbone.from_preset(
    "hgnetv2_b5_ssld_stage2_ft_in1k"
)
model(input_data)

# Randomly initialized backbone with a custom config.
model = HGNetV2Backbone(
    depths=[1, 2, 4],
    embedding_size=32,
    hidden_sizes=[64, 128, 256],
    stem_channels=[3, 16, 32],
    hidden_act="relu",
    use_learnable_affine_block=False,
    stackwise_stage_filters=[
        (32, 16, 64, 1, 1, 3),     # Stage 0
        (64, 32, 128, 2, 1, 3),    # Stage 1
        (128, 64, 256, 4, 1, 3),   # Stage 2
    ],
    apply_downsample=[False, True, True],
    use_lightweight_conv_block=[False, False, False],
    image_shape=(224, 224, 3),
)
model(input_data)

[source]

from_preset method

HGNetV2Backbone.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
hgnetv2_b4_ssld_stage2_ft_in1k 13.60M HGNetV2 B4 model with 2-stage SSLD training, fine-tuned on ImageNet-1K.
hgnetv2_b5_ssld_stage1_in22k_in1k 33.42M HGNetV2 B5 model with 1-stage SSLD training, pre-trained on ImageNet-22K and fine-tuned on ImageNet-1K.
hgnetv2_b5_ssld_stage2_ft_in1k 33.42M HGNetV2 B5 model with 2-stage SSLD training, fine-tuned on ImageNet-1K.
hgnetv2_b6_ssld_stage1_in22k_in1k 69.18M HGNetV2 B6 model with 1-stage SSLD training, pre-trained on ImageNet-22K and fine-tuned on ImageNet-1K.
hgnetv2_b6_ssld_stage2_ft_in1k 69.18M HGNetV2 B6 model with 2-stage SSLD training, fine-tuned on ImageNet-1K.