Views
No views yet
pip install -U -q keras-hub
pip install -U -q keras| Preset | Parameters | Description |
|---|---|---|
| dfine_nano_coco | 3.79M | D-FINE Nano model, the smallest variant in the family, pretrained on the COCO dataset. Ideal for applications where computational resources are limited. |
| dfine_small_coco | 10.33M | D-FINE Small model pretrained on the COCO dataset. Offers a balance between performance and computational efficiency. |
| dfine_medium_coco | 19.62M | D-FINE Medium model pretrained on the COCO dataset. A solid baseline with strong performance for general-purpose object detection. |
| dfine_large_coco | 31.34M | D-FINE Large model pretrained on the COCO dataset. Provides high accuracy and is suitable for more demanding tasks. |
| dfine_xlarge_coco | 62.83M | D-FINE X-Large model, the largest COCO-pretrained variant, designed for state-of-the-art performance where accuracy is the top priority. |
| dfine_small_obj365 | 10.62M | D-FINE Small model pretrained on the large-scale Objects365 dataset, enhancing its ability to recognize a wider variety of objects. |
| dfine_medium_obj365 | 19.99M | D-FINE Medium model pretrained on the Objects365 dataset. Benefits from a larger and more diverse pretraining corpus. |
| dfine_large_obj365 | 31.86M | D-FINE Large model pretrained on the Objects365 dataset for improved generalization and performance on diverse object categories. |
| dfine_xlarge_obj365 | 63.35M | D-FINE X-Large model pretrained on the Objects365 dataset, offering maximum performance by leveraging a vast number of object categories during pretraining. |
| dfine_small_obj2coco | 10.33M | D-FINE Small model first pretrained on Objects365 and then fine-tuned on COCO, combining broad feature learning with benchmark-specific adaptation. |
| dfine_medium_obj2coco | 19.62M | D-FINE Medium model using a two-stage training process: pretraining on Objects365 followed by fine-tuning on COCO. |
| dfine_large_obj2coco_e25 | 31.34M | D-FINE Large model pretrained on Objects365 and then fine-tuned on COCO for 25 epochs. A high-performance model with specialized tuning. |
| dfine_xlarge_obj2coco | 62.83M | D-FINE X-Large model, pretrained on Objects365 and fine-tuned on COCO, representing the most powerful model in this series for COCO-style tasks. |
1import keras
2import keras_hub
3import numpy as np
4from keras_hub.models import DFineBackbone
5from keras_hub.models import DFineObjectDetector
6from keras_hub.models import HGNetV2Backbonefrom_preset() to load a D-FINE model with pretrained weights.1object_detector = DFineObjectDetector.from_preset(
2 "dfine_small_obj2coco"
3)predict() on a batch of images. The images will be automatically preprocessed.1# Create a random image.
2image = np.random.uniform(size=(1, 256, 256, 3)).astype("float32")
3
4# Make predictions.
5predictions = object_detector.predict(image)
6
7# The output is a dictionary containing boxes, labels, confidence scores,
8# and the number of detections.
9print(predictions["boxes"].shape)
10print(predictions["labels"].shape)
11print(predictions["confidence"].shape)
12print(predictions["num_detections"])1# Load a pretrained backbone.
2backbone = DFineBackbone.from_preset(
3 "dfine_small_obj2coco"
4)
5
6# Create a new detector with a different number of classes for fine-tuning.
7finetuning_detector = DFineObjectDetector(
8 backbone=backbone,
9 num_classes=10 # Example: fine-tuning on a new dataset with 10 classes
10)
11
12# The `finetuning_detector` is now ready to be compiled and trained on a new dataset.HGNetV2Backbone.1# 1. Define a base backbone for feature extraction.
2hgnetv2_backbone = HGNetV2Backbone(
3 stem_channels=[3, 16, 16],
4 stackwise_stage_filters=[
5 [16, 16, 64, 1, 3, 3],
6 [64, 32, 256, 1, 3, 3],
7 [256, 64, 512, 2, 3, 5],
8 [512, 128, 1024, 1, 3, 5],
9 ],
10 apply_downsample=[False, True, True, True],
11 use_lightweight_conv_block=[False, False, True, True],
12 depths=[1, 1, 2, 1],
13 hidden_sizes=[64, 256, 512, 1024],
14 embedding_size=16,
15 image_shape=(256, 256, 3),
16 out_features=["stage3", "stage4"],
17)
18
19# 2. Create the D-FINE backbone, which includes the hybrid encoder and decoder.
20d_fine_backbone = DFineBackbone(
21 backbone=hgnetv2_backbone,
22 decoder_in_channels=[128, 128],
23 encoder_hidden_dim=128,
24 num_denoising=0, # Denoising is off
25 num_labels=80,
26 hidden_dim=128,
27 learn_initial_query=False,
28 num_queries=300,
29 anchor_image_size=(256, 256),
30 feat_strides=[16, 32],
31 num_feature_levels=2,
32 encoder_in_channels=[512, 1024],
33 encode_proj_layers=[1],
34 num_attention_heads=8,
35 encoder_ffn_dim=512,
36 num_encoder_layers=1,
37 hidden_expansion=0.34,
38 depth_multiplier=0.5,
39 eval_idx=-1,
40 num_decoder_layers=3,
41 decoder_attention_heads=8,
42 decoder_ffn_dim=512,
43 decoder_n_points=[6, 6],
44 lqe_hidden_dim=64,
45 num_lqe_layers=2,
46 image_shape=(256, 256, 3),
47)
48
49# 3. Create the final object detector model.
50object_detector_scratch = DFineObjectDetector(
51 backbone=d_fine_backbone,
52 num_classes=80,
53 bounding_box_format="yxyx",
54)fit() on a batch of images and ground truth bounding boxes. The compute_loss method from the detector handles the complex loss calculations.1# Prepare sample training data.
2images = np.random.uniform(
3 low=0, high=255, size=(2, 256, 256, 3)
4).astype("float32")
5bounding_boxes = {
6 "boxes": [
7 np.array([[0.1, 0.1, 0.3, 0.3], [0.5, 0.5, 0.8, 0.8]], dtype="float32"),
8 np.array([[0.2, 0.2, 0.4, 0.4]], dtype="float32"),
9 ],
10 "labels": [
11 np.array([1, 10], dtype="int32"),
12 np.array([20], dtype="int32"),
13 ],
14}
15
16# Compile the model with the built-in loss function.
17object_detector_scratch.compile(
18 optimizer="adam",
19 loss=object_detector_scratch.compute_loss,
20)
21
22# Train the model.
23object_detector_scratch.fit(x=images, y=bounding_boxes, epochs=1)labels when initializing the DFineBackbone.1# Sample ground truth labels for initializing the denoising generator.
2labels_for_denoising = [
3 {
4 "boxes": np.array([[0.5, 0.5, 0.2, 0.2]]), "labels": np.array([1])
5 },
6 {
7 "boxes": np.array([[0.6, 0.6, 0.3, 0.3]]), "labels": np.array([2])
8 },
9]
10
11# Create a D-FINE backbone with denoising enabled.
12d_fine_backbone_denoising = DFineBackbone(
13 backbone=hgnetv2_backbone, # Using the hgnetv2_backbone from before
14 decoder_in_channels=[128, 128],
15 encoder_hidden_dim=128,
16 num_denoising=100, # Number of denoising queries
17 label_noise_ratio=0.5,
18 box_noise_scale=1.0,
19 labels=labels_for_denoising, # Provide labels at initialization
20 num_labels=80,
21 hidden_dim=128,
22 learn_initial_query=False,
23 num_queries=300,
24 anchor_image_size=(256, 256),
25 feat_strides=[16, 32],
26 num_feature_levels=2,
27 encoder_in_channels=[512, 1024],
28 encode_proj_layers=[1],
29 num_attention_heads=8,
30 encoder_ffn_dim=512,
31 num_encoder_layers=1,
32 hidden_expansion=0.34,
33 depth_multiplier=0.5,
34 eval_idx=-1,
35 num_decoder_layers=3,
36 decoder_attention_heads=8,
37 decoder_ffn_dim=512,
38 decoder_n_points=[6, 6],
39 lqe_hidden_dim=64,
40 num_lqe_layers=2,
41 image_shape=(256, 256, 3),
42)
43
44# Create the final detector.
45object_detector_denoising = DFineObjectDetector(
46 backbone=d_fine_backbone_denoising,
47 num_classes=80
48)
49
50# This model can now be compiled and trained as shown in the previous example.1import keras
2import keras_hub
3import numpy as np
4from keras_hub.models import DFineBackbone
5from keras_hub.models import DFineObjectDetector
6from keras_hub.models import HGNetV2Backbonefrom_preset() to load a D-FINE model with pretrained weights.1object_detector = DFineObjectDetector.from_preset(
2 "hf://keras/dfine_small_obj2coco"
3)predict() on a batch of images. The images will be automatically preprocessed.1# Create a random image.
2image = np.random.uniform(size=(1, 256, 256, 3)).astype("float32")
3
4# Make predictions.
5predictions = object_detector.predict(image)
6
7# The output is a dictionary containing boxes, labels, confidence scores,
8# and the number of detections.
9print(predictions["boxes"].shape)
10print(predictions["labels"].shape)
11print(predictions["confidence"].shape)
12print(predictions["num_detections"])1# Load a pretrained backbone.
2backbone = DFineBackbone.from_preset(
3 "hf://keras/dfine_small_obj2coco"
4)
5
6# Create a new detector with a different number of classes for fine-tuning.
7finetuning_detector = DFineObjectDetector(
8 backbone=backbone,
9 num_classes=10 # Example: fine-tuning on a new dataset with 10 classes
10)
11
12# The `finetuning_detector` is now ready to be compiled and trained on a new dataset.HGNetV2Backbone.1# 1. Define a base backbone for feature extraction.
2hgnetv2_backbone = HGNetV2Backbone(
3 stem_channels=[3, 16, 16],
4 stackwise_stage_filters=[
5 [16, 16, 64, 1, 3, 3],
6 [64, 32, 256, 1, 3, 3],
7 [256, 64, 512, 2, 3, 5],
8 [512, 128, 1024, 1, 3, 5],
9 ],
10 apply_downsample=[False, True, True, True],
11 use_lightweight_conv_block=[False, False, True, True],
12 depths=[1, 1, 2, 1],
13 hidden_sizes=[64, 256, 512, 1024],
14 embedding_size=16,
15 image_shape=(256, 256, 3),
16 out_features=["stage3", "stage4"],
17)
18
19# 2. Create the D-FINE backbone, which includes the hybrid encoder and decoder.
20d_fine_backbone = DFineBackbone(
21 backbone=hgnetv2_backbone,
22 decoder_in_channels=[128, 128],
23 encoder_hidden_dim=128,
24 num_denoising=0, # Denoising is off
25 num_labels=80,
26 hidden_dim=128,
27 learn_initial_query=False,
28 num_queries=300,
29 anchor_image_size=(256, 256),
30 feat_strides=[16, 32],
31 num_feature_levels=2,
32 encoder_in_channels=[512, 1024],
33 encode_proj_layers=[1],
34 num_attention_heads=8,
35 encoder_ffn_dim=512,
36 num_encoder_layers=1,
37 hidden_expansion=0.34,
38 depth_multiplier=0.5,
39 eval_idx=-1,
40 num_decoder_layers=3,
41 decoder_attention_heads=8,
42 decoder_ffn_dim=512,
43 decoder_n_points=[6, 6],
44 lqe_hidden_dim=64,
45 num_lqe_layers=2,
46 image_shape=(256, 256, 3),
47)
48
49# 3. Create the final object detector model.
50object_detector_scratch = DFineObjectDetector(
51 backbone=d_fine_backbone,
52 num_classes=80,
53 bounding_box_format="yxyx",
54)fit() on a batch of images and ground truth bounding boxes. The compute_loss method from the detector handles the complex loss calculations.1# Prepare sample training data.
2images = np.random.uniform(
3 low=0, high=255, size=(2, 256, 256, 3)
4).astype("float32")
5bounding_boxes = {
6 "boxes": [
7 np.array([[0.1, 0.1, 0.3, 0.3], [0.5, 0.5, 0.8, 0.8]], dtype="float32"),
8 np.array([[0.2, 0.2, 0.4, 0.4]], dtype="float32"),
9 ],
10 "labels": [
11 np.array([1, 10], dtype="int32"),
12 np.array([20], dtype="int32"),
13 ],
14}
15
16# Compile the model with the built-in loss function.
17object_detector_scratch.compile(
18 optimizer="adam",
19 loss=object_detector_scratch.compute_loss,
20)
21
22# Train the model.
23object_detector_scratch.fit(x=images, y=bounding_boxes, epochs=1)labels when initializing the DFineBackbone.1# Sample ground truth labels for initializing the denoising generator.
2labels_for_denoising = [
3 {
4 "boxes": np.array([[0.5, 0.5, 0.2, 0.2]]), "labels": np.array([1])
5 },
6 {
7 "boxes": np.array([[0.6, 0.6, 0.3, 0.3]]), "labels": np.array([2])
8 },
9]
10
11# Create a D-FINE backbone with denoising enabled.
12d_fine_backbone_denoising = DFineBackbone(
13 backbone=hgnetv2_backbone, # Using the hgnetv2_backbone from before
14 decoder_in_channels=[128, 128],
15 encoder_hidden_dim=128,
16 num_denoising=100, # Number of denoising queries
17 label_noise_ratio=0.5,
18 box_noise_scale=1.0,
19 labels=labels_for_denoising, # Provide labels at initialization
20 num_labels=80,
21 hidden_dim=128,
22 learn_initial_query=False,
23 num_queries=300,
24 anchor_image_size=(256, 256),
25 feat_strides=[16, 32],
26 num_feature_levels=2,
27 encoder_in_channels=[512, 1024],
28 encode_proj_layers=[1],
29 num_attention_heads=8,
30 encoder_ffn_dim=512,
31 num_encoder_layers=1,
32 hidden_expansion=0.34,
33 depth_multiplier=0.5,
34 eval_idx=-1,
35 num_decoder_layers=3,
36 decoder_attention_heads=8,
37 decoder_ffn_dim=512,
38 decoder_n_points=[6, 6],
39 lqe_hidden_dim=64,
40 num_lqe_layers=2,
41 image_shape=(256, 256, 3),
42)
43
44# Create the final detector.
45object_detector_denoising = DFineObjectDetector(
46 backbone=d_fine_backbone_denoising,
47 num_classes=80
48)
49
50# This model can now be compiled and trained as shown in the previous example.