HGNetV2Backbone
classkeras_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
(None, None, 3)
.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.None
, the names of the output features to
return. If None
, returns all available features from all stages.
Defaults to None
.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)
from_preset
methodHGNetV2Backbone.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:
'bert_base_en'
'kaggle://user/bert/keras/bert_base_en'
'hf://user/bert_base_en'
'./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
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. |