Fix cspnet presets 3010 - #3027
Draft
pandeyshub-byte wants to merge 6 commits into
Draft
Conversation
github-actions
Bot
requested review from
divyashreepathihalli and
laxmareddyp
September 2, 2026 14:35
Contributor
There was a problem hiding this comment.
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}) |
Contributor
There was a problem hiding this comment.
This from_config method has two issues:
- Indentation: It is defined at the module level, not as part of the
CSPNetBackboneclass. This will cause aNameErrorat runtime. It needs to be indented to be a class method. expand_ratiodefault: The default value forexpand_ratiois set to0.5, which contradicts the PR description and the__init__default of1.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 |
Contributor
pandeyshub-byte
marked this pull request as draft
September 4, 2026 07:48
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
pandeyshub-byte
force-pushed
the
fix-cspnet-presets-3010
branch
from
September 8, 2026 09:20
e20bcc5 to
7f2ee56
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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