diff --git a/keras_cv/models/backbones/backbone.py b/keras_cv/models/backbones/backbone.py index 6130945308..83f03dc847 100644 --- a/keras_cv/models/backbones/backbone.py +++ b/keras_cv/models/backbones/backbone.py @@ -15,10 +15,11 @@ from keras_cv.api_export import keras_cv_export from keras_cv.backend import keras -from keras_cv.utils.preset_utils import check_preset_class +from keras_cv.utils.preset_utils import check_config_class +from keras_cv.utils.preset_utils import list_presets +from keras_cv.utils.preset_utils import list_subclasses from keras_cv.utils.preset_utils import load_from_preset from keras_cv.utils.python_utils import classproperty -from keras_cv.utils.python_utils import format_docstring @keras_cv_export("keras_cv.models.Backbone") @@ -64,12 +65,18 @@ def from_config(cls, config): @classproperty def presets(cls): """Dictionary of preset names and configs.""" - return {} + presets = list_presets(cls) + for subclass in list_subclasses(cls): + presets.update(subclass.presets) + return presets @classproperty def presets_with_weights(cls): """Dictionary of preset names and configs that include weights.""" - return {} + presets = list_presets(cls, with_weights=True) + for subclass in list_subclasses(cls): + presets.update(subclass.presets) + return presets @classproperty def presets_without_weights(cls): @@ -109,47 +116,19 @@ def from_preset( load_weights=False, ``` """ - # We support short IDs for official presets, e.g. `"bert_base_en"`. - # Map these to a Kaggle Models handle. - if preset in cls.presets: - preset = cls.presets[preset]["kaggle_handle"] - - check_preset_class(preset, cls) + preset_cls = check_config_class(preset) + if not issubclass(preset_cls, cls): + raise ValueError( + f"Preset has type `{preset_cls.__name__}` which is not a " + f"a subclass of calling class `{cls.__name__}`. Call " + f"`from_preset` directly on `{preset_cls.__name__}` instead." + ) return load_from_preset( preset, load_weights=load_weights, config_overrides=kwargs, ) - def __init_subclass__(cls, **kwargs): - # Use __init_subclass__ to set up a correct docstring for from_preset. - super().__init_subclass__(**kwargs) - - # If the subclass does not define from_preset, assign a wrapper so that - # each class can have a distinct docstring. - if "from_preset" not in cls.__dict__: - - def from_preset(calling_cls, *args, **kwargs): - return super(cls, calling_cls).from_preset(*args, **kwargs) - - cls.from_preset = classmethod(from_preset) - - if not cls.presets: - cls.from_preset.__func__.__doc__ = """Not implemented. - - No presets available for this class. - """ - - # Format and assign the docstring unless the subclass has overridden it. - if cls.from_preset.__doc__ is None: - cls.from_preset.__func__.__doc__ = Backbone.from_preset.__doc__ - format_docstring( - model_name=cls.__name__, - example_preset_name=next(iter(cls.presets_with_weights), ""), - preset_names='", "'.join(cls.presets), - preset_with_weights_names='", "'.join(cls.presets_with_weights), - )(cls.from_preset.__func__) - @property def pyramid_level_inputs(self): """Intermediate model outputs for feature extraction. diff --git a/keras_cv/models/backbones/backbone_presets.py b/keras_cv/models/backbones/backbone_presets.py index b77163aa8f..4bdb5bf9e9 100644 --- a/keras_cv/models/backbones/backbone_presets.py +++ b/keras_cv/models/backbones/backbone_presets.py @@ -31,8 +31,10 @@ from keras_cv.models.backbones.video_swin import video_swin_backbone_presets from keras_cv.models.backbones.vit_det import vit_det_backbone_presets from keras_cv.models.object_detection.yolo_v8 import yolo_v8_backbone_presets +from keras_cv.models.object_detection_3d import center_pillar_backbone_presets backbone_presets_no_weights = { + **center_pillar_backbone_presets.backbone_presets_no_weights, **resnet_v1_backbone_presets.backbone_presets_no_weights, **resnet_v2_backbone_presets.backbone_presets_no_weights, **mobilenet_v3_backbone_presets.backbone_presets_no_weights, @@ -47,6 +49,7 @@ } backbone_presets_with_weights = { + **center_pillar_backbone_presets.backbone_presets_with_weights, **resnet_v1_backbone_presets.backbone_presets_with_weights, **resnet_v2_backbone_presets.backbone_presets_with_weights, **mobilenet_v3_backbone_presets.backbone_presets_with_weights, diff --git a/keras_cv/models/backbones/csp_darknet/__init__.py b/keras_cv/models/backbones/csp_darknet/__init__.py index 3992ffb59a..327ebcf3d3 100644 --- a/keras_cv/models/backbones/csp_darknet/__init__.py +++ b/keras_cv/models/backbones/csp_darknet/__init__.py @@ -11,3 +11,43 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + +from keras_cv.models.backbones.csp_darknet.csp_darknet_aliases import ( + CSPDarkNetLBackbone, +) +from keras_cv.models.backbones.csp_darknet.csp_darknet_aliases import ( + CSPDarkNetTinyBackbone, +) +from keras_cv.models.backbones.csp_darknet.csp_darknet_backbone import ( + CSPDarkNetBackbone, +) +from keras_cv.models.backbones.csp_darknet.csp_darknet_backbone_presets import ( + backbone_presets_no_weights, +) +from keras_cv.models.backbones.csp_darknet.csp_darknet_backbone_presets import ( + backbone_presets_with_weights, +) +from keras_cv.utils.preset_utils import register_preset +from keras_cv.utils.preset_utils import register_presets + +register_presets( + backbone_presets_no_weights, (CSPDarkNetBackbone,), with_weights=False +) +register_presets( + backbone_presets_with_weights, (CSPDarkNetBackbone,), with_weights=True +) +register_presets( + backbone_presets_with_weights, (CSPDarkNetBackbone,), with_weights=True +) +register_preset( + "csp_darknet_tiny_imagenet", + backbone_presets_with_weights["csp_darknet_tiny_imagenet"], + (CSPDarkNetTinyBackbone,), + with_weights=True, +) +register_preset( + "csp_darknet_l_imagenet", + backbone_presets_with_weights["csp_darknet_l_imagenet"], + (CSPDarkNetLBackbone,), + with_weights=True, +) diff --git a/keras_cv/models/backbones/csp_darknet/csp_darknet_aliases.py b/keras_cv/models/backbones/csp_darknet/csp_darknet_aliases.py index 24070abf3b..e028961a47 100644 --- a/keras_cv/models/backbones/csp_darknet/csp_darknet_aliases.py +++ b/keras_cv/models/backbones/csp_darknet/csp_darknet_aliases.py @@ -12,16 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -import copy from keras_cv.api_export import keras_cv_export from keras_cv.models.backbones.csp_darknet.csp_darknet_backbone import ( CSPDarkNetBackbone, ) -from keras_cv.models.backbones.csp_darknet.csp_darknet_backbone_presets import ( - backbone_presets, -) -from keras_cv.utils.python_utils import classproperty ALIAS_DOCSTRING = """CSPDarkNetBackbone model with {stackwise_channels} channels and {stackwise_depth} depths. @@ -71,21 +66,6 @@ def __new__( ) return CSPDarkNetBackbone.from_preset("csp_darknet_tiny", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "csp_darknet_tiny_imagenet": copy.deepcopy( - backbone_presets["csp_darknet_tiny_imagenet"] - ) - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return cls.presets - @keras_cv_export("keras_cv.models.CSPDarkNetSBackbone") class CSPDarkNetSBackbone(CSPDarkNetBackbone): @@ -106,17 +86,6 @@ def __new__( ) return CSPDarkNetBackbone.from_preset("csp_darknet_s", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.CSPDarkNetMBackbone") class CSPDarkNetMBackbone(CSPDarkNetBackbone): @@ -137,17 +106,6 @@ def __new__( ) return CSPDarkNetBackbone.from_preset("csp_darknet_m", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.CSPDarkNetLBackbone") class CSPDarkNetLBackbone(CSPDarkNetBackbone): @@ -168,21 +126,6 @@ def __new__( ) return CSPDarkNetBackbone.from_preset("csp_darknet_l", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "csp_darknet_l_imagenet": copy.deepcopy( - backbone_presets["csp_darknet_l_imagenet"] - ) - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return cls.presets - @keras_cv_export("keras_cv.models.CSPDarkNetXLBackbone") class CSPDarkNetXLBackbone(CSPDarkNetBackbone): @@ -203,17 +146,6 @@ def __new__( ) return CSPDarkNetBackbone.from_preset("csp_darknet_xl", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - setattr( CSPDarkNetTinyBackbone, diff --git a/keras_cv/models/backbones/csp_darknet/csp_darknet_backbone.py b/keras_cv/models/backbones/csp_darknet/csp_darknet_backbone.py index 70fb629eca..a90e7c5a36 100644 --- a/keras_cv/models/backbones/csp_darknet/csp_darknet_backbone.py +++ b/keras_cv/models/backbones/csp_darknet/csp_darknet_backbone.py @@ -13,18 +13,11 @@ # limitations under the License. """CSPDarkNet backbone model. """ -import copy from keras_cv.api_export import keras_cv_export from keras_cv.backend import keras from keras_cv.models import utils from keras_cv.models.backbones.backbone import Backbone -from keras_cv.models.backbones.csp_darknet.csp_darknet_backbone_presets import ( - backbone_presets, -) -from keras_cv.models.backbones.csp_darknet.csp_darknet_backbone_presets import ( - backbone_presets_with_weights, -) from keras_cv.models.backbones.csp_darknet.csp_darknet_utils import ( CrossStagePartial, ) @@ -38,7 +31,6 @@ from keras_cv.models.backbones.csp_darknet.csp_darknet_utils import ( SpatialPyramidPoolingBottleneck, ) -from keras_cv.utils.python_utils import classproperty @keras_cv_export("keras_cv.models.CSPDarkNetBackbone") @@ -169,14 +161,3 @@ def get_config(self): } ) return config - - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return copy.deepcopy(backbone_presets) - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return copy.deepcopy(backbone_presets_with_weights) diff --git a/keras_cv/models/backbones/densenet/__init__.py b/keras_cv/models/backbones/densenet/__init__.py index 3992ffb59a..0ef0be1d11 100644 --- a/keras_cv/models/backbones/densenet/__init__.py +++ b/keras_cv/models/backbones/densenet/__init__.py @@ -11,3 +11,49 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + +from keras_cv.models.backbones.densenet.densenet_aliases import ( + DenseNet121Backbone, +) +from keras_cv.models.backbones.densenet.densenet_aliases import ( + DenseNet169Backbone, +) +from keras_cv.models.backbones.densenet.densenet_aliases import ( + DenseNet201Backbone, +) +from keras_cv.models.backbones.densenet.densenet_backbone import ( + DenseNetBackbone, +) +from keras_cv.models.backbones.densenet.densenet_backbone_presets import ( + backbone_presets_no_weights, +) +from keras_cv.models.backbones.densenet.densenet_backbone_presets import ( + backbone_presets_with_weights, +) +from keras_cv.utils.preset_utils import register_preset +from keras_cv.utils.preset_utils import register_presets + +register_presets( + backbone_presets_no_weights, (DenseNetBackbone,), with_weights=False +) +register_presets( + backbone_presets_with_weights, (DenseNetBackbone,), with_weights=True +) +register_preset( + "densenet121_imagenet", + backbone_presets_with_weights["densenet121_imagenet"], + (DenseNet121Backbone,), + with_weights=True, +) +register_preset( + "densenet169_imagenet", + backbone_presets_with_weights["densenet169_imagenet"], + (DenseNet169Backbone,), + with_weights=True, +) +register_preset( + "densenet201_imagenet", + backbone_presets_with_weights["densenet201_imagenet"], + (DenseNet201Backbone,), + with_weights=True, +) diff --git a/keras_cv/models/backbones/densenet/densenet_aliases.py b/keras_cv/models/backbones/densenet/densenet_aliases.py index 6593b100f6..ec0d8a0f11 100644 --- a/keras_cv/models/backbones/densenet/densenet_aliases.py +++ b/keras_cv/models/backbones/densenet/densenet_aliases.py @@ -12,16 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import copy - from keras_cv.api_export import keras_cv_export from keras_cv.models.backbones.densenet.densenet_backbone import ( DenseNetBackbone, ) -from keras_cv.models.backbones.densenet.densenet_backbone_presets import ( - backbone_presets, -) -from keras_cv.utils.python_utils import classproperty ALIAS_DOCSTRING = """DenseNetBackbone model with {num_layers} layers. @@ -69,20 +63,6 @@ def __new__( ) return DenseNetBackbone.from_preset("densenet121", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "densenet121_imagenet": copy.deepcopy( - backbone_presets["densenet121_imagenet"] - ), - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include weights.""" # noqa: E501 - return cls.presets - @keras_cv_export("keras_cv.models.DenseNet169Backbone") class DenseNet169Backbone(DenseNetBackbone): @@ -103,20 +83,6 @@ def __new__( ) return DenseNetBackbone.from_preset("densenet169", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "densenet169_imagenet": copy.deepcopy( - backbone_presets["densenet169_imagenet"] - ), - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include weights.""" # noqa: E501 - return cls.presets - @keras_cv_export("keras_cv.models.DenseNet201Backbone") class DenseNet201Backbone(DenseNetBackbone): @@ -137,20 +103,6 @@ def __new__( ) return DenseNetBackbone.from_preset("densenet201", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "densenet201_imagenet": copy.deepcopy( - backbone_presets["densenet201_imagenet"] - ), - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include weights.""" # noqa: E501 - return cls.presets - setattr(DenseNet121Backbone, "__doc__", ALIAS_DOCSTRING.format(num_layers=121)) setattr(DenseNet169Backbone, "__doc__", ALIAS_DOCSTRING.format(num_layers=169)) diff --git a/keras_cv/models/backbones/densenet/densenet_backbone.py b/keras_cv/models/backbones/densenet/densenet_backbone.py index 251f3601ec..f382e3f589 100644 --- a/keras_cv/models/backbones/densenet/densenet_backbone.py +++ b/keras_cv/models/backbones/densenet/densenet_backbone.py @@ -19,19 +19,10 @@ - [Based on the Original keras.applications DenseNet](https://github.com/keras-team/keras/blob/master/keras/applications/densenet.py) """ # noqa: E501 -import copy - from keras_cv.api_export import keras_cv_export from keras_cv.backend import keras from keras_cv.models import utils from keras_cv.models.backbones.backbone import Backbone -from keras_cv.models.backbones.densenet.densenet_backbone_presets import ( - backbone_presets, -) -from keras_cv.models.backbones.densenet.densenet_backbone_presets import ( - backbone_presets_with_weights, -) -from keras_cv.utils.python_utils import classproperty BN_AXIS = 3 BN_EPSILON = 1.001e-5 @@ -153,16 +144,6 @@ def get_config(self): ) return config - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return copy.deepcopy(backbone_presets) - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include weights.""" # noqa: E501 - return copy.deepcopy(backbone_presets_with_weights) - def apply_dense_block(x, num_repeats, growth_rate, name=None): """A dense block. diff --git a/keras_cv/models/backbones/efficientnet_lite/__init__.py b/keras_cv/models/backbones/efficientnet_lite/__init__.py index 3992ffb59a..c0c48953d9 100644 --- a/keras_cv/models/backbones/efficientnet_lite/__init__.py +++ b/keras_cv/models/backbones/efficientnet_lite/__init__.py @@ -11,3 +11,23 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + +from keras_cv.models.backbones.efficientnet_lite.efficientnet_lite_backbone import ( # noqa: E501 + EfficientNetLiteBackbone, +) +from keras_cv.models.backbones.efficientnet_lite.efficientnet_lite_backbone_presets import ( # noqa: E501 + backbone_presets_no_weights, +) +from keras_cv.models.backbones.efficientnet_lite.efficientnet_lite_backbone_presets import ( # noqa: E501 + backbone_presets_with_weights, +) +from keras_cv.utils.preset_utils import register_presets + +register_presets( + backbone_presets_no_weights, (EfficientNetLiteBackbone,), with_weights=False +) +register_presets( + backbone_presets_with_weights, + (EfficientNetLiteBackbone,), + with_weights=True, +) diff --git a/keras_cv/models/backbones/efficientnet_lite/efficientnet_lite_aliases.py b/keras_cv/models/backbones/efficientnet_lite/efficientnet_lite_aliases.py index ef654a7a44..3f2fb87a4f 100644 --- a/keras_cv/models/backbones/efficientnet_lite/efficientnet_lite_aliases.py +++ b/keras_cv/models/backbones/efficientnet_lite/efficientnet_lite_aliases.py @@ -16,7 +16,6 @@ from keras_cv.models.backbones.efficientnet_lite.efficientnet_lite_backbone import ( # noqa: E501 EfficientNetLiteBackbone, ) -from keras_cv.utils.python_utils import classproperty ALIAS_DOCSTRING = """Instantiates the {name} architecture. @@ -63,17 +62,6 @@ def __new__( "efficientnetlite_b0", **kwargs ) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.EfficientNetLiteB1Backbone") class EfficientNetLiteB1Backbone(EfficientNetLiteBackbone): @@ -96,17 +84,6 @@ def __new__( "efficientnetlite_b1", **kwargs ) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.EfficientNetLiteB2Backbone") class EfficientNetLiteB2Backbone(EfficientNetLiteBackbone): @@ -129,17 +106,6 @@ def __new__( "efficientnetlite_b2", **kwargs ) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.EfficientNetLiteB3Backbone") class EfficientNetLiteB3Backbone(EfficientNetLiteBackbone): @@ -162,17 +128,6 @@ def __new__( "efficientnetlite_b3", **kwargs ) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.EfficientNetLiteB4Backbone") class EfficientNetLiteB4Backbone(EfficientNetLiteBackbone): @@ -195,17 +150,6 @@ def __new__( "efficientnetlite_b4", **kwargs ) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - setattr( EfficientNetLiteB0Backbone, diff --git a/keras_cv/models/backbones/efficientnet_lite/efficientnet_lite_backbone.py b/keras_cv/models/backbones/efficientnet_lite/efficientnet_lite_backbone.py index b86e57ce8e..4265ad0157 100644 --- a/keras_cv/models/backbones/efficientnet_lite/efficientnet_lite_backbone.py +++ b/keras_cv/models/backbones/efficientnet_lite/efficientnet_lite_backbone.py @@ -21,17 +21,12 @@ - [Based on the original EfficientNet Lite's](https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet/lite) """ # noqa: E501 -import copy import math from keras_cv.api_export import keras_cv_export from keras_cv.backend import keras from keras_cv.models import utils from keras_cv.models.backbones.backbone import Backbone -from keras_cv.models.backbones.efficientnet_lite.efficientnet_lite_backbone_presets import ( # noqa: E501 - backbone_presets, -) -from keras_cv.utils.python_utils import classproperty BN_AXIS = 3 @@ -247,11 +242,6 @@ def get_config(self): ) return config - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return copy.deepcopy(backbone_presets) - def conv_kernel_initializer(scale=2.0): return keras.initializers.VarianceScaling( diff --git a/keras_cv/models/backbones/efficientnet_v1/__init__.py b/keras_cv/models/backbones/efficientnet_v1/__init__.py index 3992ffb59a..725aa6096e 100644 --- a/keras_cv/models/backbones/efficientnet_v1/__init__.py +++ b/keras_cv/models/backbones/efficientnet_v1/__init__.py @@ -11,3 +11,21 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + +from keras_cv.models.backbones.efficientnet_v1.efficientnet_v1_backbone import ( + EfficientNetV1Backbone, +) +from keras_cv.models.backbones.efficientnet_v1.efficientnet_v1_backbone_presets import ( # noqa: E501 + backbone_presets_no_weights, +) +from keras_cv.models.backbones.efficientnet_v1.efficientnet_v1_backbone_presets import ( # noqa: E501 + backbone_presets_with_weights, +) +from keras_cv.utils.preset_utils import register_presets + +register_presets( + backbone_presets_no_weights, (EfficientNetV1Backbone,), with_weights=False +) +register_presets( + backbone_presets_with_weights, (EfficientNetV1Backbone,), with_weights=True +) diff --git a/keras_cv/models/backbones/efficientnet_v1/efficientnet_v1_aliases.py b/keras_cv/models/backbones/efficientnet_v1/efficientnet_v1_aliases.py index 6fe6e230c4..6752b656c1 100644 --- a/keras_cv/models/backbones/efficientnet_v1/efficientnet_v1_aliases.py +++ b/keras_cv/models/backbones/efficientnet_v1/efficientnet_v1_aliases.py @@ -16,7 +16,6 @@ from keras_cv.models.backbones.efficientnet_v1.efficientnet_v1_backbone import ( EfficientNetV1Backbone, ) -from keras_cv.utils.python_utils import classproperty ALIAS_DOCSTRING = """Instantiates the {name} architecture. @@ -53,17 +52,6 @@ def __new__( ) return EfficientNetV1Backbone.from_preset("efficientnetv1_b0", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.EfficientNetV1B1Backbone") class EfficientNetV1B1Backbone(EfficientNetV1Backbone): @@ -84,17 +72,6 @@ def __new__( ) return EfficientNetV1Backbone.from_preset("efficientnetv1_b1", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.EfficientNetV1B2Backbone") class EfficientNetV1B2Backbone(EfficientNetV1Backbone): @@ -115,17 +92,6 @@ def __new__( ) return EfficientNetV1Backbone.from_preset("efficientnetv1_b2", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.EfficientNetV1B3Backbone") class EfficientNetV1B3Backbone(EfficientNetV1Backbone): @@ -146,17 +112,6 @@ def __new__( ) return EfficientNetV1Backbone.from_preset("efficientnetv1_b3", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.EfficientNetV1B4Backbone") class EfficientNetV1B4Backbone(EfficientNetV1Backbone): @@ -177,17 +132,6 @@ def __new__( ) return EfficientNetV1Backbone.from_preset("efficientnetv1_b4", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.EfficientNetV1B5Backbone") class EfficientNetV1B5Backbone(EfficientNetV1Backbone): @@ -208,17 +152,6 @@ def __new__( ) return EfficientNetV1Backbone.from_preset("efficientnetv1_b5", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.EfficientNetV1B6Backbone") class EfficientNetV1B6Backbone(EfficientNetV1Backbone): @@ -239,17 +172,6 @@ def __new__( ) return EfficientNetV1Backbone.from_preset("efficientnetv1_b6", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.EfficientNetV1B7Backbone") class EfficientNetV1B7Backbone(EfficientNetV1Backbone): @@ -270,17 +192,6 @@ def __new__( ) return EfficientNetV1Backbone.from_preset("efficientnetv1_b7", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - setattr( EfficientNetV1B0Backbone, diff --git a/keras_cv/models/backbones/efficientnet_v1/efficientnet_v1_backbone.py b/keras_cv/models/backbones/efficientnet_v1/efficientnet_v1_backbone.py index 2ff9e66ac7..25a2108c05 100644 --- a/keras_cv/models/backbones/efficientnet_v1/efficientnet_v1_backbone.py +++ b/keras_cv/models/backbones/efficientnet_v1/efficientnet_v1_backbone.py @@ -11,17 +11,12 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import copy import math from keras_cv.api_export import keras_cv_export from keras_cv.backend import keras from keras_cv.models import utils from keras_cv.models.backbones.backbone import Backbone -from keras_cv.models.backbones.efficientnet_v1.efficientnet_v1_backbone_presets import ( # noqa: E501 - backbone_presets, -) -from keras_cv.utils.python_utils import classproperty @keras_cv_export("keras_cv.models.EfficientNetV1Backbone") @@ -284,11 +279,6 @@ def get_config(self): ) return config - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return copy.deepcopy(backbone_presets) - def conv_kernel_initializer(scale=2.0): return keras.initializers.VarianceScaling( diff --git a/keras_cv/models/backbones/efficientnet_v2/__init__.py b/keras_cv/models/backbones/efficientnet_v2/__init__.py index 3992ffb59a..66792b1503 100644 --- a/keras_cv/models/backbones/efficientnet_v2/__init__.py +++ b/keras_cv/models/backbones/efficientnet_v2/__init__.py @@ -11,3 +11,58 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + +from keras_cv.models.backbones.efficientnet_v2.efficientnet_v2_aliases import ( + EfficientNetV2B0Backbone, +) +from keras_cv.models.backbones.efficientnet_v2.efficientnet_v2_aliases import ( + EfficientNetV2B1Backbone, +) +from keras_cv.models.backbones.efficientnet_v2.efficientnet_v2_aliases import ( + EfficientNetV2B2Backbone, +) +from keras_cv.models.backbones.efficientnet_v2.efficientnet_v2_aliases import ( + EfficientNetV2SBackbone, +) +from keras_cv.models.backbones.efficientnet_v2.efficientnet_v2_backbone import ( + EfficientNetV2Backbone, +) +from keras_cv.models.backbones.efficientnet_v2.efficientnet_v2_backbone_presets import ( # noqa: E501 + backbone_presets_no_weights, +) +from keras_cv.models.backbones.efficientnet_v2.efficientnet_v2_backbone_presets import ( # noqa: E501 + backbone_presets_with_weights, +) +from keras_cv.utils.preset_utils import register_preset +from keras_cv.utils.preset_utils import register_presets + +register_presets( + backbone_presets_no_weights, (EfficientNetV2Backbone,), with_weights=False +) +register_presets( + backbone_presets_with_weights, (EfficientNetV2Backbone,), with_weights=True +) +register_preset( + "efficientnetv2_s_imagenet", + backbone_presets_with_weights["efficientnetv2_s_imagenet"], + (EfficientNetV2SBackbone,), + with_weights=True, +) +register_preset( + "efficientnetv2_b0_imagenet", + backbone_presets_with_weights["efficientnetv2_b0_imagenet"], + (EfficientNetV2B0Backbone,), + with_weights=True, +) +register_preset( + "efficientnetv2_b1_imagenet", + backbone_presets_with_weights["efficientnetv2_b1_imagenet"], + (EfficientNetV2B1Backbone,), + with_weights=True, +) +register_preset( + "efficientnetv2_b2_imagenet", + backbone_presets_with_weights["efficientnetv2_b2_imagenet"], + (EfficientNetV2B2Backbone,), + with_weights=True, +) diff --git a/keras_cv/models/backbones/efficientnet_v2/efficientnet_v2_aliases.py b/keras_cv/models/backbones/efficientnet_v2/efficientnet_v2_aliases.py index f338874982..4e246676e1 100644 --- a/keras_cv/models/backbones/efficientnet_v2/efficientnet_v2_aliases.py +++ b/keras_cv/models/backbones/efficientnet_v2/efficientnet_v2_aliases.py @@ -11,16 +11,10 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import copy - from keras_cv.api_export import keras_cv_export from keras_cv.models.backbones.efficientnet_v2.efficientnet_v2_backbone import ( EfficientNetV2Backbone, ) -from keras_cv.models.backbones.efficientnet_v2.efficientnet_v2_backbone_presets import ( # noqa: E501 - backbone_presets, -) -from keras_cv.utils.python_utils import classproperty ALIAS_DOCSTRING = """Instantiates the {name} architecture. @@ -57,21 +51,6 @@ def __new__( ) return EfficientNetV2Backbone.from_preset("efficientnetv2_s", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "efficientnetv2_s_imagenet": copy.deepcopy( - backbone_presets["efficientnetv2_s_imagenet"] - ), - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return cls.presets - @keras_cv_export("keras_cv.models.EfficientNetV2MBackbone") class EfficientNetV2MBackbone(EfficientNetV2Backbone): @@ -92,17 +71,6 @@ def __new__( ) return EfficientNetV2Backbone.from_preset("efficientnetv2_m", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.EfficientNetV2LBackbone") class EfficientNetV2LBackbone(EfficientNetV2Backbone): @@ -123,17 +91,6 @@ def __new__( ) return EfficientNetV2Backbone.from_preset("efficientnetv2_l", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.EfficientNetV2B0Backbone") class EfficientNetV2B0Backbone(EfficientNetV2Backbone): @@ -154,21 +111,6 @@ def __new__( ) return EfficientNetV2Backbone.from_preset("efficientnetv2_b0", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "efficientnetv2_b0_imagenet": copy.deepcopy( - backbone_presets["efficientnetv2_b0_imagenet"] - ), - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return cls.presets - @keras_cv_export("keras_cv.models.EfficientNetV2B1Backbone") class EfficientNetV2B1Backbone(EfficientNetV2Backbone): @@ -189,21 +131,6 @@ def __new__( ) return EfficientNetV2Backbone.from_preset("efficientnetv2_b1", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "efficientnetv2_b1_imagenet": copy.deepcopy( - backbone_presets["efficientnetv2_b1_imagenet"] - ), - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return cls.presets - @keras_cv_export("keras_cv.models.EfficientNetV2B2Backbone") class EfficientNetV2B2Backbone(EfficientNetV2Backbone): @@ -224,21 +151,6 @@ def __new__( ) return EfficientNetV2Backbone.from_preset("efficientnetv2_b2", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "efficientnetv2_b2_imagenet": copy.deepcopy( - backbone_presets["efficientnetv2_b2_imagenet"] - ), - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return cls.presets - @keras_cv_export("keras_cv.models.EfficientNetV2B3Backbone") class EfficientNetV2B3Backbone(EfficientNetV2Backbone): @@ -259,17 +171,6 @@ def __new__( ) return EfficientNetV2Backbone.from_preset("efficientnetv2_b3", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - setattr( EfficientNetV2SBackbone, diff --git a/keras_cv/models/backbones/efficientnet_v2/efficientnet_v2_backbone.py b/keras_cv/models/backbones/efficientnet_v2/efficientnet_v2_backbone.py index 1160184789..8d1c0cc5a6 100644 --- a/keras_cv/models/backbones/efficientnet_v2/efficientnet_v2_backbone.py +++ b/keras_cv/models/backbones/efficientnet_v2/efficientnet_v2_backbone.py @@ -11,7 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import copy import math from keras_cv.api_export import keras_cv_export @@ -20,13 +19,6 @@ from keras_cv.layers import MBConvBlock from keras_cv.models import utils from keras_cv.models.backbones.backbone import Backbone -from keras_cv.models.backbones.efficientnet_v2.efficientnet_v2_backbone_presets import ( # noqa: E501 - backbone_presets, -) -from keras_cv.models.backbones.efficientnet_v2.efficientnet_v2_backbone_presets import ( # noqa: E501 - backbone_presets_with_weights, -) -from keras_cv.utils.python_utils import classproperty @keras_cv_export("keras_cv.models.EfficientNetV2Backbone") @@ -296,17 +288,6 @@ def get_config(self): ) return config - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return copy.deepcopy(backbone_presets) - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return copy.deepcopy(backbone_presets_with_weights) - def conv_kernel_initializer(scale=2.0): return keras.initializers.VarianceScaling( diff --git a/keras_cv/models/backbones/mix_transformer/__init__.py b/keras_cv/models/backbones/mix_transformer/__init__.py index 3992ffb59a..5fc2016898 100644 --- a/keras_cv/models/backbones/mix_transformer/__init__.py +++ b/keras_cv/models/backbones/mix_transformer/__init__.py @@ -11,3 +11,31 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + +from keras_cv.models.backbones.mix_transformer.mix_transformer_aliases import ( + MiTB0Backbone, +) +from keras_cv.models.backbones.mix_transformer.mix_transformer_backbone import ( + MiTBackbone, +) +from keras_cv.models.backbones.mix_transformer.mix_transformer_backbone_presets import ( # noqa: E501 + backbone_presets_no_weights, +) +from keras_cv.models.backbones.mix_transformer.mix_transformer_backbone_presets import ( # noqa: E501 + backbone_presets_with_weights, +) +from keras_cv.utils.preset_utils import register_preset +from keras_cv.utils.preset_utils import register_presets + +register_presets( + backbone_presets_no_weights, (MiTBackbone,), with_weights=False +) +register_presets( + backbone_presets_with_weights, (MiTBackbone,), with_weights=True +) +register_preset( + "mit_b0_imagenet", + backbone_presets_with_weights["mit_b0_imagenet"], + (MiTB0Backbone,), + with_weights=True, +) diff --git a/keras_cv/models/backbones/mix_transformer/mix_transformer_aliases.py b/keras_cv/models/backbones/mix_transformer/mix_transformer_aliases.py index 65b88e7e1e..e704903635 100644 --- a/keras_cv/models/backbones/mix_transformer/mix_transformer_aliases.py +++ b/keras_cv/models/backbones/mix_transformer/mix_transformer_aliases.py @@ -12,16 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import copy - from keras_cv.api_export import keras_cv_export from keras_cv.models.backbones.mix_transformer.mix_transformer_backbone import ( MiTBackbone, ) -from keras_cv.models.backbones.mix_transformer.mix_transformer_backbone_presets import ( # noqa: E501 - backbone_presets, -) -from keras_cv.utils.python_utils import classproperty ALIAS_DOCSTRING = """MiT model. @@ -66,21 +60,6 @@ def __new__( ) return MiTBackbone.from_preset("mit_b0", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "mit_b0_imagenet": copy.deepcopy( - backbone_presets["mit_b0_imagenet"] - ), - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return cls.presets - @keras_cv_export("keras_cv.models.MiTB1Backbone") class MiTB1Backbone(MiTBackbone): @@ -101,16 +80,6 @@ def __new__( ) return MiTBackbone.from_preset("mit_b1", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations.""" - return {} - @keras_cv_export("keras_cv.models.MiTB2Backbone") class MiTB2Backbone(MiTBackbone): @@ -131,16 +100,6 @@ def __new__( ) return MiTBackbone.from_preset("mit_b2", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations.""" - return {} - @keras_cv_export("keras_cv.models.MiTB3Backbone") class MiTB3Backbone(MiTBackbone): @@ -161,16 +120,6 @@ def __new__( ) return MiTBackbone.from_preset("mit_b3", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations.""" - return {} - @keras_cv_export("keras_cv.models.MiTB4Backbone") class MiTB4Backbone(MiTBackbone): @@ -191,16 +140,6 @@ def __new__( ) return MiTBackbone.from_preset("mit_b4", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations.""" - return {} - @keras_cv_export("keras_cv.models.MiTB5Backbone") class MiTB5Backbone(MiTBackbone): @@ -221,16 +160,6 @@ def __new__( ) return MiTBackbone.from_preset("mit_b5", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations.""" - return {} - setattr( MiTB0Backbone, diff --git a/keras_cv/models/backbones/mix_transformer/mix_transformer_backbone.py b/keras_cv/models/backbones/mix_transformer/mix_transformer_backbone.py index 28efc48fc5..bb1ff9e43e 100644 --- a/keras_cv/models/backbones/mix_transformer/mix_transformer_backbone.py +++ b/keras_cv/models/backbones/mix_transformer/mix_transformer_backbone.py @@ -21,8 +21,6 @@ - [Inspired by @sithu31296's reimplementation](https://github.com/sithu31296/semantic-segmentation/blob/main/semseg/models/backbones/mit.py) """ # noqa: E501 -import copy - import numpy as np from keras_cv import layers as cv_layers @@ -31,13 +29,6 @@ from keras_cv.backend import ops from keras_cv.models import utils from keras_cv.models.backbones.backbone import Backbone -from keras_cv.models.backbones.mix_transformer.mix_transformer_backbone_presets import ( # noqa: E501 - backbone_presets, -) -from keras_cv.models.backbones.mix_transformer.mix_transformer_backbone_presets import ( # noqa: E501 - backbone_presets_with_weights, -) -from keras_cv.utils.python_utils import classproperty @keras_cv_export("keras_cv.models.MiTBackbone") @@ -175,14 +166,3 @@ def get_config(self): } ) return config - - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return copy.deepcopy(backbone_presets) - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return copy.deepcopy(backbone_presets_with_weights) diff --git a/keras_cv/models/backbones/mobilenet_v3/__init__.py b/keras_cv/models/backbones/mobilenet_v3/__init__.py index 3992ffb59a..63620c724e 100644 --- a/keras_cv/models/backbones/mobilenet_v3/__init__.py +++ b/keras_cv/models/backbones/mobilenet_v3/__init__.py @@ -11,3 +11,40 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + +from keras_cv.models.backbones.mobilenet_v3.mobilenet_v3_aliases import ( + MobileNetV3LargeBackbone, +) +from keras_cv.models.backbones.mobilenet_v3.mobilenet_v3_aliases import ( + MobileNetV3SmallBackbone, +) +from keras_cv.models.backbones.mobilenet_v3.mobilenet_v3_backbone import ( + MobileNetV3Backbone, +) +from keras_cv.models.backbones.mobilenet_v3.mobilenet_v3_backbone_presets import ( # noqa: E501 + backbone_presets_no_weights, +) +from keras_cv.models.backbones.mobilenet_v3.mobilenet_v3_backbone_presets import ( # noqa: E501 + backbone_presets_with_weights, +) +from keras_cv.utils.preset_utils import register_preset +from keras_cv.utils.preset_utils import register_presets + +register_presets( + backbone_presets_no_weights, (MobileNetV3Backbone,), with_weights=False +) +register_presets( + backbone_presets_with_weights, (MobileNetV3Backbone,), with_weights=True +) +register_preset( + "mobilenet_v3_small_imagenet", + backbone_presets_with_weights["mobilenet_v3_small_imagenet"], + (MobileNetV3SmallBackbone,), + with_weights=True, +) +register_preset( + "mobilenet_v3_large_imagenet", + backbone_presets_with_weights["mobilenet_v3_large_imagenet"], + (MobileNetV3LargeBackbone,), + with_weights=True, +) diff --git a/keras_cv/models/backbones/mobilenet_v3/mobilenet_v3_aliases.py b/keras_cv/models/backbones/mobilenet_v3/mobilenet_v3_aliases.py index 752c088528..319071e524 100644 --- a/keras_cv/models/backbones/mobilenet_v3/mobilenet_v3_aliases.py +++ b/keras_cv/models/backbones/mobilenet_v3/mobilenet_v3_aliases.py @@ -12,16 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import copy - from keras_cv.api_export import keras_cv_export from keras_cv.models.backbones.mobilenet_v3.mobilenet_v3_backbone import ( MobileNetV3Backbone, ) -from keras_cv.models.backbones.mobilenet_v3.mobilenet_v3_backbone_presets import ( # noqa: E501 - backbone_presets, -) -from keras_cv.utils.python_utils import classproperty ALIAS_DOCSTRING = """MobileNetV3Backbone model with {num_layers} layers. @@ -70,20 +64,6 @@ def __new__( ) return MobileNetV3Backbone.from_preset("mobilenet_v3_small", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "mobilenet_v3_small_imagenet": copy.deepcopy( - backbone_presets["mobilenet_v3_small_imagenet"] - ), - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations.""" - return cls.presets - @keras_cv_export("keras_cv.models.MobileNetV3LargeBackbone") class MobileNetV3LargeBackbone(MobileNetV3Backbone): @@ -104,21 +84,6 @@ def __new__( ) return MobileNetV3Backbone.from_preset("mobilenet_v3_large", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "mobilenet_v3_large_imagenet": copy.deepcopy( - backbone_presets["mobilenet_v3_large_imagenet"] - ), - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return cls.presets - setattr( MobileNetV3LargeBackbone, diff --git a/keras_cv/models/backbones/mobilenet_v3/mobilenet_v3_backbone.py b/keras_cv/models/backbones/mobilenet_v3/mobilenet_v3_backbone.py index 547fa57864..9c63ff844d 100644 --- a/keras_cv/models/backbones/mobilenet_v3/mobilenet_v3_backbone.py +++ b/keras_cv/models/backbones/mobilenet_v3/mobilenet_v3_backbone.py @@ -20,20 +20,11 @@ - [Based on the original keras.applications MobileNetv3](https://github.com/keras-team/keras/blob/master/keras/applications/mobilenet_v3.py) """ # noqa: E501 -import copy - from keras_cv import layers as cv_layers from keras_cv.api_export import keras_cv_export from keras_cv.backend import keras from keras_cv.models import utils from keras_cv.models.backbones.backbone import Backbone -from keras_cv.models.backbones.mobilenet_v3.mobilenet_v3_backbone_presets import ( # noqa: E501 - backbone_presets, -) -from keras_cv.models.backbones.mobilenet_v3.mobilenet_v3_backbone_presets import ( # noqa: E501 - backbone_presets_with_weights, -) -from keras_cv.utils.python_utils import classproperty CHANNEL_AXIS = -1 BN_EPSILON = 1e-3 @@ -198,17 +189,6 @@ def get_config(self): ) return config - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return copy.deepcopy(backbone_presets) - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return copy.deepcopy(backbone_presets_with_weights) - class HardSigmoidActivation(keras.layers.Layer): def __init__(self): diff --git a/keras_cv/models/backbones/resnet_v1/__init__.py b/keras_cv/models/backbones/resnet_v1/__init__.py index 3992ffb59a..80145a8ec3 100644 --- a/keras_cv/models/backbones/resnet_v1/__init__.py +++ b/keras_cv/models/backbones/resnet_v1/__init__.py @@ -11,3 +11,31 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + +from keras_cv.models.backbones.resnet_v1.resnet_v1_aliases import ( + ResNet50Backbone, +) +from keras_cv.models.backbones.resnet_v1.resnet_v1_backbone import ( + ResNetBackbone, +) +from keras_cv.models.backbones.resnet_v1.resnet_v1_backbone_presets import ( + backbone_presets_no_weights, +) +from keras_cv.models.backbones.resnet_v1.resnet_v1_backbone_presets import ( + backbone_presets_with_weights, +) +from keras_cv.utils.preset_utils import register_preset +from keras_cv.utils.preset_utils import register_presets + +register_presets( + backbone_presets_no_weights, (ResNetBackbone,), with_weights=False +) +register_presets( + backbone_presets_with_weights, (ResNetBackbone,), with_weights=True +) +register_preset( + "resnet50_imagenet", + backbone_presets_with_weights["resnet50_imagenet"], + (ResNet50Backbone,), + with_weights=True, +) diff --git a/keras_cv/models/backbones/resnet_v1/resnet_v1_aliases.py b/keras_cv/models/backbones/resnet_v1/resnet_v1_aliases.py index 052b399306..f1572af996 100644 --- a/keras_cv/models/backbones/resnet_v1/resnet_v1_aliases.py +++ b/keras_cv/models/backbones/resnet_v1/resnet_v1_aliases.py @@ -12,16 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import copy - from keras_cv.api_export import keras_cv_export from keras_cv.models.backbones.resnet_v1.resnet_v1_backbone import ( ResNetBackbone, ) -from keras_cv.models.backbones.resnet_v1.resnet_v1_backbone_presets import ( - backbone_presets, -) -from keras_cv.utils.python_utils import classproperty ALIAS_DOCSTRING = """ResNetBackbone (V1) model with {num_layers} layers. @@ -75,17 +69,6 @@ def __new__( ) return ResNetBackbone.from_preset("resnet18", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.ResNet34Backbone") class ResNet34Backbone(ResNetBackbone): @@ -106,17 +89,6 @@ def __new__( ) return ResNetBackbone.from_preset("resnet34", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.ResNet50Backbone") class ResNet50Backbone(ResNetBackbone): @@ -137,21 +109,6 @@ def __new__( ) return ResNetBackbone.from_preset("resnet50", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "resnet50_imagenet": copy.deepcopy( - backbone_presets["resnet50_imagenet"] - ), - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return cls.presets - @keras_cv_export("keras_cv.models.ResNet101Backbone") class ResNet101Backbone(ResNetBackbone): @@ -172,17 +129,6 @@ def __new__( ) return ResNetBackbone.from_preset("resnet101", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.ResNet152Backbone") class ResNet152Backbone(ResNetBackbone): @@ -203,17 +149,6 @@ def __new__( ) return ResNetBackbone.from_preset("resnet152", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - setattr(ResNet18Backbone, "__doc__", ALIAS_DOCSTRING.format(num_layers=18)) setattr(ResNet34Backbone, "__doc__", ALIAS_DOCSTRING.format(num_layers=34)) diff --git a/keras_cv/models/backbones/resnet_v1/resnet_v1_backbone.py b/keras_cv/models/backbones/resnet_v1/resnet_v1_backbone.py index 07c896613c..6b9d37310d 100644 --- a/keras_cv/models/backbones/resnet_v1/resnet_v1_backbone.py +++ b/keras_cv/models/backbones/resnet_v1/resnet_v1_backbone.py @@ -18,19 +18,10 @@ - [Based on the original keras.applications ResNet](https://github.com/keras-team/keras/blob/master/keras/applications/resnet.py) # noqa: E501 """ -import copy - from keras_cv.api_export import keras_cv_export from keras_cv.backend import keras from keras_cv.models import utils from keras_cv.models.backbones.backbone import Backbone -from keras_cv.models.backbones.resnet_v1.resnet_v1_backbone_presets import ( - backbone_presets, -) -from keras_cv.models.backbones.resnet_v1.resnet_v1_backbone_presets import ( - backbone_presets_with_weights, -) -from keras_cv.utils.python_utils import classproperty BN_AXIS = 3 BN_EPSILON = 1.001e-5 @@ -162,17 +153,6 @@ def get_config(self): ) return config - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return copy.deepcopy(backbone_presets) - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return copy.deepcopy(backbone_presets_with_weights) - def apply_basic_block( x, filters, kernel_size=3, stride=1, conv_shortcut=True, name=None diff --git a/keras_cv/models/backbones/resnet_v2/__init__.py b/keras_cv/models/backbones/resnet_v2/__init__.py index 3992ffb59a..1e81797931 100644 --- a/keras_cv/models/backbones/resnet_v2/__init__.py +++ b/keras_cv/models/backbones/resnet_v2/__init__.py @@ -11,3 +11,31 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + +from keras_cv.models.backbones.resnet_v2.resnet_v2_aliases import ( + ResNet50V2Backbone, +) +from keras_cv.models.backbones.resnet_v2.resnet_v2_backbone import ( + ResNetV2Backbone, +) +from keras_cv.models.backbones.resnet_v2.resnet_v2_backbone_presets import ( + backbone_presets_no_weights, +) +from keras_cv.models.backbones.resnet_v2.resnet_v2_backbone_presets import ( + backbone_presets_with_weights, +) +from keras_cv.utils.preset_utils import register_preset +from keras_cv.utils.preset_utils import register_presets + +register_presets( + backbone_presets_no_weights, (ResNetV2Backbone,), with_weights=False +) +register_presets( + backbone_presets_with_weights, (ResNetV2Backbone,), with_weights=True +) +register_preset( + "resnet50_v2_imagenet", + backbone_presets_with_weights["resnet50_v2_imagenet"], + (ResNet50V2Backbone,), + with_weights=True, +) diff --git a/keras_cv/models/backbones/resnet_v2/resnet_v2_aliases.py b/keras_cv/models/backbones/resnet_v2/resnet_v2_aliases.py index 3878fb2a24..e9fdd9774e 100644 --- a/keras_cv/models/backbones/resnet_v2/resnet_v2_aliases.py +++ b/keras_cv/models/backbones/resnet_v2/resnet_v2_aliases.py @@ -12,16 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -import copy - from keras_cv.api_export import keras_cv_export from keras_cv.models.backbones.resnet_v2.resnet_v2_backbone import ( ResNetV2Backbone, ) -from keras_cv.models.backbones.resnet_v2.resnet_v2_backbone_presets import ( # noqa: E501 - backbone_presets, -) -from keras_cv.utils.python_utils import classproperty ALIAS_DOCSTRING = """ResNetV2Backbone model with {num_layers} layers. @@ -75,17 +69,6 @@ def __new__( ) return ResNetV2Backbone.from_preset("resnet18_v2", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.ResNet34V2Backbone") class ResNet34V2Backbone(ResNetV2Backbone): @@ -106,17 +89,6 @@ def __new__( ) return ResNetV2Backbone.from_preset("resnet34_v2", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.ResNet50V2Backbone") class ResNet50V2Backbone(ResNetV2Backbone): @@ -137,21 +109,6 @@ def __new__( ) return ResNetV2Backbone.from_preset("resnet50_v2", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "resnet50_v2_imagenet": copy.deepcopy( - backbone_presets["resnet50_v2_imagenet"] - ), - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return cls.presets - @keras_cv_export("keras_cv.models.ResNet101V2Backbone") class ResNet101V2Backbone(ResNetV2Backbone): @@ -172,17 +129,6 @@ def __new__( ) return ResNetV2Backbone.from_preset("resnet101_v2", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - @keras_cv_export("keras_cv.models.ResNet152V2Backbone") class ResNet152V2Backbone(ResNetV2Backbone): @@ -203,17 +149,6 @@ def __new__( ) return ResNetV2Backbone.from_preset("resnet152_v2", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return {} - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return {} - setattr( ResNet18V2Backbone, diff --git a/keras_cv/models/backbones/resnet_v2/resnet_v2_backbone.py b/keras_cv/models/backbones/resnet_v2/resnet_v2_backbone.py index 6a0cc74740..23ac77ff07 100644 --- a/keras_cv/models/backbones/resnet_v2/resnet_v2_backbone.py +++ b/keras_cv/models/backbones/resnet_v2/resnet_v2_backbone.py @@ -17,19 +17,10 @@ - [Based on the original keras.applications ResNet](https://github.com/keras-team/keras/blob/master/keras/applications/resnet_v2.py) """ # noqa: E501 -import copy - from keras_cv.api_export import keras_cv_export from keras_cv.backend import keras from keras_cv.models import utils from keras_cv.models.backbones.backbone import Backbone -from keras_cv.models.backbones.resnet_v2.resnet_v2_backbone_presets import ( - backbone_presets, -) -from keras_cv.models.backbones.resnet_v2.resnet_v2_backbone_presets import ( - backbone_presets_with_weights, -) -from keras_cv.utils.python_utils import classproperty BN_AXIS = 3 BN_EPSILON = 1.001e-5 @@ -175,17 +166,6 @@ def get_config(self): ) return config - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return copy.deepcopy(backbone_presets) - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return copy.deepcopy(backbone_presets_with_weights) - def apply_basic_block( x, diff --git a/keras_cv/models/backbones/video_swin/__init__.py b/keras_cv/models/backbones/video_swin/__init__.py index 0e9cbb5ac9..472a1bdbd8 100644 --- a/keras_cv/models/backbones/video_swin/__init__.py +++ b/keras_cv/models/backbones/video_swin/__init__.py @@ -11,3 +11,49 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + +from keras_cv.models.backbones.video_swin.video_swin_aliases import ( + VideoSwinBBackbone, +) +from keras_cv.models.backbones.video_swin.video_swin_aliases import ( + VideoSwinSBackbone, +) +from keras_cv.models.backbones.video_swin.video_swin_aliases import ( + VideoSwinTBackbone, +) +from keras_cv.models.backbones.video_swin.video_swin_backbone import ( + VideoSwinBackbone, +) +from keras_cv.models.backbones.video_swin.video_swin_backbone_presets import ( + backbone_presets_no_weights, +) +from keras_cv.models.backbones.video_swin.video_swin_backbone_presets import ( + backbone_presets_with_weights, +) +from keras_cv.utils.preset_utils import register_preset +from keras_cv.utils.preset_utils import register_presets + +register_presets( + backbone_presets_no_weights, (VideoSwinBackbone,), with_weights=False +) +register_presets( + backbone_presets_with_weights, (VideoSwinBackbone,), with_weights=True +) +register_preset( + "videoswin_tiny_kinetics400", + backbone_presets_with_weights["videoswin_tiny_kinetics400"], + (VideoSwinTBackbone,), + with_weights=True, +) +register_preset( + "videoswin_small_kinetics400", + backbone_presets_with_weights["videoswin_small_kinetics400"], + (VideoSwinSBackbone,), + with_weights=True, +) +register_preset( + "videoswin_base_kinetics400", + backbone_presets_with_weights["videoswin_base_kinetics400"], + (VideoSwinBBackbone,), + with_weights=True, +) diff --git a/keras_cv/models/backbones/video_swin/video_swin_aliases.py b/keras_cv/models/backbones/video_swin/video_swin_aliases.py index 161ba0bbb4..3a4b192f76 100644 --- a/keras_cv/models/backbones/video_swin/video_swin_aliases.py +++ b/keras_cv/models/backbones/video_swin/video_swin_aliases.py @@ -12,15 +12,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import copy - from keras_cv.models.backbones.video_swin.video_swin_backbone import ( VideoSwinBackbone, ) -from keras_cv.models.backbones.video_swin.video_swin_backbone_presets import ( - backbone_presets, -) -from keras_cv.utils.python_utils import classproperty ALIAS_DOCSTRING = """VideoSwin{size}Backbone model. @@ -63,21 +57,6 @@ def __new__( ) return VideoSwinBackbone.from_preset("videoswin_tiny", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "videoswin_tiny_kinetics400": copy.deepcopy( - backbone_presets["videoswin_tiny_kinetics400"] - ), - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return cls.presets - class VideoSwinSBackbone(VideoSwinBackbone): def __new__( @@ -100,21 +79,6 @@ def __new__( ) return VideoSwinBackbone.from_preset("videoswin_small", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "videoswin_small_kinetics400": copy.deepcopy( - backbone_presets["videoswin_small_kinetics400"] - ), - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return cls.presets - class VideoSwinBBackbone(VideoSwinBackbone): def __new__( @@ -137,21 +101,6 @@ def __new__( ) return VideoSwinBackbone.from_preset("videoswin_base", **kwargs) - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return { - "videoswin_base_kinetics400": copy.deepcopy( - backbone_presets["videoswin_base_kinetics400"] - ), - } - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return cls.presets - setattr(VideoSwinTBackbone, "__doc__", ALIAS_DOCSTRING.format(size="T")) setattr(VideoSwinSBackbone, "__doc__", ALIAS_DOCSTRING.format(size="S")) diff --git a/keras_cv/models/backbones/video_swin/video_swin_backbone.py b/keras_cv/models/backbones/video_swin/video_swin_backbone.py index 9bb62eb385..26872dc549 100644 --- a/keras_cv/models/backbones/video_swin/video_swin_backbone.py +++ b/keras_cv/models/backbones/video_swin/video_swin_backbone.py @@ -11,7 +11,6 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import copy from functools import partial import numpy as np @@ -21,12 +20,6 @@ from keras_cv.backend import keras from keras_cv.models import utils from keras_cv.models.backbones.backbone import Backbone -from keras_cv.models.backbones.video_swin.video_swin_backbone_presets import ( # noqa: E501 - backbone_presets, -) -from keras_cv.models.backbones.video_swin.video_swin_backbone_presets import ( # noqa: E501 - backbone_presets_with_weights, -) from keras_cv.models.backbones.video_swin.video_swin_layers import ( VideoSwinBasicLayer, ) @@ -36,7 +29,6 @@ from keras_cv.models.backbones.video_swin.video_swin_layers import ( VideoSwinPatchMerging, ) -from keras_cv.utils.python_utils import classproperty @keras_cv_export("keras_cv.models.VideoSwinBackbone", package="keras_cv.models") @@ -222,17 +214,6 @@ def get_config(self): ) return config - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return copy.deepcopy(backbone_presets) - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return copy.deepcopy(backbone_presets_with_weights) - @property def pyramid_level_inputs(self): raise NotImplementedError( diff --git a/keras_cv/models/backbones/vit_det/__init__.py b/keras_cv/models/backbones/vit_det/__init__.py index 3992ffb59a..e003cbf8de 100644 --- a/keras_cv/models/backbones/vit_det/__init__.py +++ b/keras_cv/models/backbones/vit_det/__init__.py @@ -11,3 +11,41 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. + +from keras_cv.models.backbones.vit_det.vit_det_aliases import ViTDetBBackbone +from keras_cv.models.backbones.vit_det.vit_det_aliases import ViTDetHBackbone +from keras_cv.models.backbones.vit_det.vit_det_aliases import ViTDetLBackbone +from keras_cv.models.backbones.vit_det.vit_det_backbone import ViTDetBackbone +from keras_cv.models.backbones.vit_det.vit_det_backbone_presets import ( + backbone_presets_no_weights, +) +from keras_cv.models.backbones.vit_det.vit_det_backbone_presets import ( + backbone_presets_with_weights, +) +from keras_cv.utils.preset_utils import register_preset +from keras_cv.utils.preset_utils import register_presets + +register_presets( + backbone_presets_no_weights, (ViTDetBackbone,), with_weights=False +) +register_presets( + backbone_presets_with_weights, (ViTDetBackbone,), with_weights=True +) +register_preset( + "vitdet_base_sa1b", + backbone_presets_with_weights["vitdet_base_sa1b"], + (ViTDetBBackbone,), + with_weights=True, +) +register_preset( + "vitdet_large_sa1b", + backbone_presets_with_weights["vitdet_large_sa1b"], + (ViTDetLBackbone,), + with_weights=True, +) +register_preset( + "vitdet_huge_sa1b", + backbone_presets_with_weights["vitdet_huge_sa1b"], + (ViTDetHBackbone,), + with_weights=True, +) diff --git a/keras_cv/models/object_detection/retinanet/retinanet_test.py b/keras_cv/models/object_detection/retinanet/retinanet_test.py index bbede75943..b45b91028d 100644 --- a/keras_cv/models/object_detection/retinanet/retinanet_test.py +++ b/keras_cv/models/object_detection/retinanet/retinanet_test.py @@ -280,7 +280,7 @@ def data_generator(): bounding_box_format="xyxy", backbone=keras_cv.models.ResNet50Backbone.from_preset( "resnet50_imagenet", - load_weights=False, + # load_weights=False, ), ) diff --git a/keras_cv/models/object_detection/yolo_v8/__init__.py b/keras_cv/models/object_detection/yolo_v8/__init__.py index 8ec6a842ed..587b660acf 100644 --- a/keras_cv/models/object_detection/yolo_v8/__init__.py +++ b/keras_cv/models/object_detection/yolo_v8/__init__.py @@ -11,6 +11,23 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +from keras_cv.models.object_detection.yolo_v8.yolo_v8_backbone import ( + YOLOV8Backbone, +) +from keras_cv.models.object_detection.yolo_v8.yolo_v8_backbone_presets import ( + backbone_presets_no_weights, +) +from keras_cv.models.object_detection.yolo_v8.yolo_v8_backbone_presets import ( + backbone_presets_with_weights, +) from keras_cv.models.object_detection.yolo_v8.yolo_v8_label_encoder import ( YOLOV8LabelEncoder, ) +from keras_cv.utils.preset_utils import register_presets + +register_presets( + backbone_presets_no_weights, (YOLOV8Backbone,), with_weights=False +) +register_presets( + backbone_presets_with_weights, (YOLOV8Backbone,), with_weights=True +) diff --git a/keras_cv/models/object_detection/yolo_v8/yolo_v8_backbone.py b/keras_cv/models/object_detection/yolo_v8/yolo_v8_backbone.py index a2bf4bdd3b..2c9ee7c974 100644 --- a/keras_cv/models/object_detection/yolo_v8/yolo_v8_backbone.py +++ b/keras_cv/models/object_detection/yolo_v8/yolo_v8_backbone.py @@ -11,26 +11,18 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import copy from keras_cv.api_export import keras_cv_export from keras_cv.backend import keras from keras_cv.backend import ops from keras_cv.models import utils from keras_cv.models.backbones.backbone import Backbone -from keras_cv.models.object_detection.yolo_v8.yolo_v8_backbone_presets import ( - backbone_presets, -) -from keras_cv.models.object_detection.yolo_v8.yolo_v8_backbone_presets import ( - backbone_presets_with_weights, -) from keras_cv.models.object_detection.yolo_v8.yolo_v8_layers import ( apply_conv_bn, ) from keras_cv.models.object_detection.yolo_v8.yolo_v8_layers import ( apply_csp_block, ) -from keras_cv.utils.python_utils import classproperty def apply_spatial_pyramid_pooling_fast( @@ -202,14 +194,3 @@ def get_config(self): } ) return config - - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return copy.deepcopy(backbone_presets) - - @classproperty - def presets_with_weights(cls): - """Dictionary of preset names and configurations that include - weights.""" - return copy.deepcopy(backbone_presets_with_weights) diff --git a/keras_cv/models/object_detection_3d/__init__.py b/keras_cv/models/object_detection_3d/__init__.py index 50ed959e68..508f629870 100644 --- a/keras_cv/models/object_detection_3d/__init__.py +++ b/keras_cv/models/object_detection_3d/__init__.py @@ -17,3 +17,17 @@ from keras_cv.models.object_detection_3d.center_pillar_backbone import ( CenterPillarBackbone, ) +from keras_cv.models.object_detection_3d.center_pillar_backbone_presets import ( + backbone_presets_no_weights, +) +from keras_cv.models.object_detection_3d.center_pillar_backbone_presets import ( + backbone_presets_with_weights, +) +from keras_cv.utils.preset_utils import register_presets + +register_presets( + backbone_presets_no_weights, (CenterPillarBackbone,), with_weights=False +) +register_presets( + backbone_presets_with_weights, (CenterPillarBackbone,), with_weights=True +) diff --git a/keras_cv/models/object_detection_3d/center_pillar_backbone.py b/keras_cv/models/object_detection_3d/center_pillar_backbone.py index 01554f260b..1e669745b8 100644 --- a/keras_cv/models/object_detection_3d/center_pillar_backbone.py +++ b/keras_cv/models/object_detection_3d/center_pillar_backbone.py @@ -11,15 +11,10 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import copy from keras_cv.api_export import keras_cv_export from keras_cv.backend import keras from keras_cv.models.backbones.backbone import Backbone -from keras_cv.models.object_detection_3d.center_pillar_backbone_presets import ( - backbone_presets, -) -from keras_cv.utils.python_utils import classproperty @keras_cv_export("keras_cv.models.CenterPillarBackbone") @@ -103,11 +98,6 @@ def get_config(self): ) return config - @classproperty - def presets(cls): - """Dictionary of preset names and configurations.""" - return copy.deepcopy(backbone_presets) - def Block(filters, downsample): """A default block which serves as an example of the block interface. diff --git a/keras_cv/models/object_detection_3d/center_pillar_backbone_presets.py b/keras_cv/models/object_detection_3d/center_pillar_backbone_presets.py index 8f875e638a..d50d151a81 100644 --- a/keras_cv/models/object_detection_3d/center_pillar_backbone_presets.py +++ b/keras_cv/models/object_detection_3d/center_pillar_backbone_presets.py @@ -14,7 +14,7 @@ """YOLOv8 Backbone presets.""" -backbone_presets = { +backbone_presets_no_weights = { "center_pillar_waymo_open_dataset": { "metadata": { "description": "An example CenterPillar backbone for WOD.", @@ -24,3 +24,10 @@ "kaggle_handle": "gs://keras-cv-kaggle/center_pillar_waymo_open_dataset", # noqa: E501 }, } + +backbone_presets_with_weights = {} + +backbone_presets = { + **backbone_presets_no_weights, + **backbone_presets_with_weights, +} diff --git a/keras_cv/utils/preset_utils.py b/keras_cv/utils/preset_utils.py index bb0b6b0a0b..d3ace14009 100644 --- a/keras_cv/utils/preset_utils.py +++ b/keras_cv/utils/preset_utils.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import collections import datetime import inspect import json @@ -27,6 +28,11 @@ KAGGLE_PREFIX = "kaggle://" GS_PREFIX = "gs://" +# Global state for preset registry. +BUILTIN_PRESETS = {} +BUILTIN_PRESETS_FOR_CLASS = collections.defaultdict(dict) +BUILTIN_PRESETS_FOR_CLASS_WITH_WEIGHT = collections.defaultdict(dict) + def get_file(preset, path): """Download a preset file in necessary and return the local path.""" @@ -34,6 +40,10 @@ def get_file(preset, path): raise ValueError( f"A preset identifier must be a string. Received: preset={preset}" ) + + if preset in BUILTIN_PRESETS: + preset = BUILTIN_PRESETS[preset]["kaggle_handle"] + if preset.startswith(KAGGLE_PREFIX): if kagglehub is None: raise ImportError( @@ -170,6 +180,24 @@ def load_from_preset( return layer +def check_config_class( + preset, + config_file="config.json", +): + """Validate a preset is being loaded on the correct class.""" + config_path = get_file(preset, config_file) + try: + with open(config_path) as config_file: + config = json.load(config_file) + except: + raise ValueError( + f"The specified preset `{preset}` is unknown. " + "Please check documentation to ensure the correct preset " + "handle is being used." + ) + return keras.saving.get_registered_object(config["registered_name"]) + + def check_preset_class( preset, classes, @@ -216,3 +244,33 @@ def legacy_load_weights(layer, weights_path): functional_cls._layer_checkpoint_dependencies = {} layer.load_weights(weights_path) functional_cls._layer_checkpoint_dependencies = property + + +def register_preset(preset_name, preset, classes, with_weights=False): + BUILTIN_PRESETS[preset_name] = preset + for cls in classes: + BUILTIN_PRESETS_FOR_CLASS[cls][preset_name] = preset + if with_weights: + BUILTIN_PRESETS_FOR_CLASS_WITH_WEIGHT[cls][preset_name] = preset + + +def register_presets(presets, classes, with_weights=False): + for preset_name, preset in presets.items(): + register_preset(preset_name, preset, classes, with_weights) + + +def list_presets(cls, with_weights=False): + """Find all registered builtin presets for a class.""" + if with_weights: + return dict(BUILTIN_PRESETS_FOR_CLASS_WITH_WEIGHT[cls]) + return dict(BUILTIN_PRESETS_FOR_CLASS[cls]) + + +def list_subclasses(cls): + """Find all registered subclasses of a class.""" + custom_objects = keras.saving.get_custom_objects().values() + subclasses = [] + for x in custom_objects: + if inspect.isclass(x) and x != cls and issubclass(x, cls): + subclasses.append(x) + return subclasses