Skip to content

Fix cspnet presets 3010 - #3027

Draft
pandeyshub-byte wants to merge 6 commits into
keras-team:masterfrom
pandeyshub-byte:fix-cspnet-presets-3010
Draft

Fix cspnet presets 3010#3027
pandeyshub-byte wants to merge 6 commits into
keras-team:masterfrom
pandeyshub-byte:fix-cspnet-presets-3010

Conversation

@pandeyshub-byte

Copy link
Copy Markdown

Description of the change

This PR adds 7 missing CSPNet presets to the preset map that were available on Kaggle but not accessible via from_preset().
While adding these, I found that the legacy weights (originally from KerasCV) use a configuration format that doesn't align with the current CSPNetBackbone constructor. To resolve this, I've:
Updated CSPNetBackbone with a from_config method to map legacy keys (like stackwise_channels and use_depthwise) to the current implementation.
Set appropriate architectural defaults (stage_type="cs3", expand_ratio=1.0) to match the Kaggle checkpoints.
Registered keras_cv>CSPDarkNetBackbone as a serializable alias so the loader recognizes the package name used in the original saved models.
I verified the fix by successfully loading all backbones and classifiers locally using the new preset strings.

Reference

Fixes #3010.
Kaggle models: https://www.kaggle.com/models/keras/cspnet

Colab Notebook

N/A (Preset addition and internal bug fix).

Checklist

  • I have added all the necessary unit tests for my change.
  • I have verified that my change does not break existing code and works with all backends (TensorFlow, JAX, and PyTorch).
  • My PR is based on the latest changes of the main branch (if unsure, rebase the code).
  • I have followed the Keras Hub Model contribution guidelines in making these changes.
  • I have followed the Keras Hub API design guidelines in making these changes.
  • I have signed the Contributor License Agreement.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds several new preset configurations for CSP-DarkNet models to cspnet_presets.py and updates cspnet_backbone.py to support them. Key feedback includes correcting the indentation of the newly added from_config class method (which is currently defined at the module level), fixing an incorrect default value for expand_ratio in from_config, removing an unused import of backbone_presets, and correcting an implicit string concatenation bug in the docstring example for from_preset.

Comment on lines +258 to +303
@classmethod
def from_config(cls, config):
if "config" in config and isinstance(config["config"], dict):
config = config["config"]

config = dict(config)

if "stackwise_channels" in config:
config["stackwise_num_filters"] = config.pop("stackwise_channels")

depths = config.get("stackwise_depth", [])
num_stages = len(depths)

if "stackwise_num_filters" not in config:
config["stackwise_num_filters"] = [64, 128, 256, 512]

if "stackwise_strides" not in config:
config["stackwise_strides"] = [2] * num_stages

if "stem_filters" not in config:
filters = config.get("stackwise_num_filters")
config["stem_filters"] = filters[0] // 2 if filters else 32

config.setdefault("stem_kernel_size", 3)
config.setdefault("stem_strides", 2)
config.setdefault("block_type", "dark_block")
config.setdefault("stage_type", "cs3")
config.setdefault("expand_ratio", 0.5)
config.setdefault("bottle_ratio", 0.5)

valid_keys = [
"stackwise_num_filters",
"stackwise_depth",
"stackwise_strides",
"stem_filters",
"stem_kernel_size",
"stem_strides",
"block_type",
"stage_type",
"expand_ratio",
"bottle_ratio",
"name",
"trainable",
]

return cls(**{k: v for k, v in config.items() if k in valid_keys})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

This from_config method has two issues:

  1. Indentation: It is defined at the module level, not as part of the CSPNetBackbone class. This will cause a NameError at runtime. It needs to be indented to be a class method.
  2. expand_ratio default: The default value for expand_ratio is set to 0.5, which contradicts the PR description and the __init__ default of 1.0.

I've provided a suggestion below that fixes both the indentation and the default value for expand_ratio.

    @classmethod
    def from_config(cls, config):
        if "config" in config and isinstance(config["config"], dict):
            config = config["config"]

        config = dict(config)

        if "stackwise_channels" in config:
            config["stackwise_num_filters"] = config.pop("stackwise_channels")

        depths = config.get("stackwise_depth", [])
        num_stages = len(depths)

        if "stackwise_num_filters" not in config:
            config["stackwise_num_filters"] = [64, 128, 256, 512]

        if "stackwise_strides" not in config:
            config["stackwise_strides"] = [2] * num_stages

        if "stem_filters" not in config:
            filters = config.get("stackwise_num_filters")
            config["stem_filters"] = filters[0] // 2 if filters else 32

        config.setdefault("stem_kernel_size", 3)
        config.setdefault("stem_strides", 2)
        config.setdefault("block_type", "dark_block")
        config.setdefault("stage_type", "cs3")
        config.setdefault("expand_ratio", 1.0)
        config.setdefault("bottle_ratio", 0.5)

        valid_keys = [
            "stackwise_num_filters",
            "stackwise_depth",
            "stackwise_strides",
            "stem_filters",
            "stem_kernel_size",
            "stem_strides",
            "block_type",
            "stage_type",
            "expand_ratio",
            "bottle_ratio",
            "name",
            "trainable",
        ]

        return cls(**{k: v for k, v in config.items() if k in valid_keys})

from keras_hub.src.api_export import keras_hub_export
from keras_hub.src.models.feature_pyramid_backbone import FeaturePyramidBackbone
from keras_hub.src.utils.keras_utils import standardize_data_format
from keras_hub.src.models.cspnet.cspnet_presets import backbone_presets

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This import is unnecessary. The Backbone superclass automatically loads presets from the corresponding _presets.py file via its __init_subclass__ method. Please remove this unused import.

Comment thread keras_hub/src/models/cspnet/cspnet_backbone.py Outdated
@pandeyshub-byte
pandeyshub-byte marked this pull request as draft September 4, 2026 07:48
pandeyshub-byte and others added 2 commits September 7, 2026 11:49
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CSPNet: Add Kaggle presets to preset map

1 participant