pip install -U -q keras-hub
pip install -U -q keras| Preset | Parameters | Description |
|---|---|---|
| hgnetv2_b4_ssld_stage2_ft_in1k | 13.6M | HGNetV2-B4 model with 2-stage SSLD training, fine-tuned on ImageNet-1K. |
| hgnetv2_b5_ssld_stage1_in22k_in1k | 33.4M | 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.4M | HGNetV2-B5 model with 2-stage SSLD training, fine-tuned on ImageNet-1K. |
| hgnetv2_b6_ssld_stage1_in22k_in1k | 69.2M | 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.2M | HGNetV2-B6 model with 2-stage SSLD training, fine-tuned on ImageNet-1K. |
1# Make necessary imports.
2import keras
3import keras_hub
4import numpy as np
5
6# Load backbone for feature extraction.
7backbone = keras_hub.models.HGNetV2Backbone.from_preset(
8 "hgnetv2_b5_ssld_stage2_ft_in1k",
9)
10
11# Load pre-trained classifier.
12classifier = keras_hub.models.HGNetV2ImageClassifier.from_preset(
13 "hgnetv2_b5_ssld_stage2_ft_in1k",
14)
15
16# Setup parameters.
17num_samples = 100
18num_classes = 10
19image_size = 224
20
21# Use for inference.
22
23# Generate random images - make sure batch size matches.
24images = np.random.randint(0, 256, size=(num_samples, image_size, image_size, 3), dtype=np.uint8)
25
26# Generate random labels - same batch size as images.
27labels = np.random.randint(0, num_classes, size=(num_samples,), dtype=np.int32)
28features = backbone(images) # Multi-scale features
29print(f"Features shape: {features.shape}")
30predictions = classifier.predict(images) # Classification output
31print(f"Predictions shape: {predictions.shape}")
32
33# Create custom HGNetV2 backbone.
34backbone = keras_hub.models.HGNetV2Backbone(
35 depths=[1, 2, 4],
36 embedding_size=32,
37 hidden_sizes=[64, 128, 256],
38 stem_channels=[3, 16, 32],
39 hidden_act="relu",
40 use_learnable_affine_block=False,
41 stackwise_stage_filters=[
42 # Stage 0: (in_channels=32, mid_channels=16, out_channels=64, num_blocks=1, num_layers=1, kernel_size=3).
43 (32, 16, 64, 1, 1, 3),
44 # Stage 1: (in_channels=64, mid_channels=32, out_channels=128, num_blocks=2, num_layers=1, kernel_size=3).
45 (64, 32, 128, 2, 1, 3),
46 # Stage 2: (in_channels=128, mid_channels=64, out_channels=256, num_blocks=4, num_layers=1, kernel_size=3).
47 (128, 64, 256, 4, 1, 3),
48 ],
49 apply_downsample=[False, True, True],
50 use_lightweight_conv_block=[False, False, False],
51 image_shape=(224, 224, 3),
52)
53
54# Freeze backbone for transfer learning.
55classifier.backbone.trainable = False
56print("Backbone frozen for transfer learning.")
57
58# Compile for training.
59classifier.compile(
60 loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
61 optimizer=keras.optimizers.Adam(5e-5),
62 metrics=["accuracy"]
63)
64print("Classifier compiled.")
65
66# Train on custom dataset.
67history = classifier.fit(x=images, y=labels, batch_size=32)
68print(f"Training completed. Training history: {history.history}")1# Make necessary imports.
2import keras
3import keras_hub
4import numpy as np
5
6# Load backbone for feature extraction.
7backbone = keras_hub.models.HGNetV2Backbone.from_preset(
8 "hf://keras/hgnetv2_b5_ssld_stage2_ft_in1k",
9)
10
11# Load pre-trained classifier.
12classifier = keras_hub.models.HGNetV2ImageClassifier.from_preset(
13 "hf://keras/hgnetv2_b5_ssld_stage2_ft_in1k",
14)
15
16# Setup parameters.
17num_samples = 100
18num_classes = 10
19image_size = 224
20
21# Use for inference.
22
23# Generate random images - make sure batch size matches.
24images = np.random.randint(0, 256, size=(num_samples, image_size, image_size, 3), dtype=np.uint8)
25
26# Generate random labels - same batch size as images.
27labels = np.random.randint(0, num_classes, size=(num_samples,), dtype=np.int32)
28features = backbone(images) # Multi-scale features
29print(f"Features shape: {features.shape}")
30predictions = classifier.predict(images) # Classification output
31print(f"Predictions shape: {predictions.shape}")
32
33# Create custom HGNetV2 backbone.
34backbone = keras_hub.models.HGNetV2Backbone(
35 depths=[1, 2, 4],
36 embedding_size=32,
37 hidden_sizes=[64, 128, 256],
38 stem_channels=[3, 16, 32],
39 hidden_act="relu",
40 use_learnable_affine_block=False,
41 stackwise_stage_filters=[
42 # Stage 0: (in_channels=32, mid_channels=16, out_channels=64, num_blocks=1, num_layers=1, kernel_size=3).
43 (32, 16, 64, 1, 1, 3),
44 # Stage 1: (in_channels=64, mid_channels=32, out_channels=128, num_blocks=2, num_layers=1, kernel_size=3).
45 (64, 32, 128, 2, 1, 3),
46 # Stage 2: (in_channels=128, mid_channels=64, out_channels=256, num_blocks=4, num_layers=1, kernel_size=3).
47 (128, 64, 256, 4, 1, 3),
48 ],
49 apply_downsample=[False, True, True],
50 use_lightweight_conv_block=[False, False, False],
51 image_shape=(224, 224, 3),
52)
53
54# Freeze backbone for transfer learning.
55classifier.backbone.trainable = False
56print("Backbone frozen for transfer learning.")
57
58# Compile for training.
59classifier.compile(
60 loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True),
61 optimizer=keras.optimizers.Adam(5e-5),
62 metrics=["accuracy"]
63)
64print("Classifier compiled.")
65
66# Train on custom dataset.
67history = classifier.fit(x=images, y=labels, batch_size=32)
68print(f"Training completed. Training history: {history.history}")