Skip to content

Commit

Permalink
Switch all available presets to Kaggle format (#2206)
Browse files Browse the repository at this point in the history
* Change preset configs to Kaggle hangles

* Remove legacy from_preset

* Convert all remaining model presets to Kaggle handle

* Convert last remaining presets to Kaggle handle

* Add kaggle handle checking for task class

* Add  flag checking

* Add alias subclass checking

* Add input shape config override

* Fix formatting

* Update config for CenterPillarBackbone

* Ensure that preset with weights defaults to loading weights

* Fix formatting issues

* Comment out preset conversion script

* Convert preset script to functions

* Address preset checking comments, update utils_test.py

* Ensure subclass and alias checking

* Change weights check

* Update presets script

* Change load_weights check

* Add more models to preset conversion

* Remaining models to preset conversion script

* Add comments on load_weights behavior

* Remove useless comment
  • Loading branch information
nkovela1 authored Dec 5, 2023
1 parent be7dd8a commit 96ac0c5
Show file tree
Hide file tree
Showing 27 changed files with 266 additions and 1,554 deletions.
2 changes: 1 addition & 1 deletion cloudbuild/unit_test_jobs.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ local unittest = base.BaseTest {
)
],
command: [
'pytest --run_large --durations 0',
'pytest --run_extra_large --durations 0',
'keras_cv/bounding_box',
'keras_cv/callbacks',
'keras_cv/losses',
Expand Down
31 changes: 3 additions & 28 deletions keras_cv/models/backbones/backbone.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
# limitations under the License.
"""Base class for Backbone models."""

import os

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
Expand Down Expand Up @@ -67,30 +65,6 @@ def presets_without_weights(cls):
for preset in set(cls.presets) - set(cls.presets_with_weights)
}

@classmethod
def _legacy_from_preset(
cls,
preset,
load_weights=None,
**kwargs,
):
metadata = cls.presets[preset]
config = metadata["config"]
model = cls.from_config({**config, **kwargs})

if preset not in cls.presets_with_weights or load_weights is False:
return model

weights = keras.utils.get_file(
"model.h5",
metadata["weights_url"],
cache_subdir=os.path.join("models", preset),
file_hash=metadata["weights_hash"],
)

model.load_weights(weights)
return model

@classmethod
def from_preset(
cls,
Expand Down Expand Up @@ -121,9 +95,10 @@ def from_preset(
load_weights=False,
```
"""
# TODO: delete me!
# We support short IDs for official presets, e.g. `"bert_base_en"`.
# Map these to a Kaggle Models handle.
if preset in cls.presets:
return cls._legacy_from_preset(preset, **kwargs)
preset = cls.presets[preset]["kaggle_handle"]

check_preset_class(preset, cls)
return load_from_preset(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,7 @@
"official_name": "CSPDarkNet",
"path": "csp_darknet",
},
"class_name": "keras_cv>CSPDarkNetBackbone",
"config": {
"stackwise_channels": [48, 96, 192, 384],
"stackwise_depth": [1, 3, 3, 1],
"include_rescaling": True,
"use_depthwise": False,
"input_shape": (None, None, 3),
"input_tensor": None,
},
"kaggle_handle": "gs://keras-cv-kaggle/csp_darknet_tiny",
},
"csp_darknet_s": {
"metadata": {
Expand All @@ -47,15 +39,7 @@
"official_name": "CSPDarkNet",
"path": "csp_darknet",
},
"class_name": "keras_cv>CSPDarkNetBackbone",
"config": {
"stackwise_channels": [64, 128, 256, 512],
"stackwise_depth": [1, 3, 3, 1],
"include_rescaling": True,
"use_depthwise": False,
"input_shape": (None, None, 3),
"input_tensor": None,
},
"kaggle_handle": "gs://keras-cv-kaggle/csp_darknet_s",
},
"csp_darknet_m": {
"metadata": {
Expand All @@ -68,15 +52,7 @@
"official_name": "CSPDarkNet",
"path": "csp_darknet",
},
"class_name": "keras_cv>CSPDarkNetBackbone",
"config": {
"stackwise_channels": [96, 192, 384, 768],
"stackwise_depth": [2, 6, 6, 2],
"include_rescaling": True,
"use_depthwise": False,
"input_shape": (None, None, 3),
"input_tensor": None,
},
"kaggle_handle": "gs://keras-cv-kaggle/csp_darknet_m",
},
"csp_darknet_l": {
"metadata": {
Expand All @@ -89,15 +65,7 @@
"official_name": "CSPDarkNet",
"path": "csp_darknet",
},
"class_name": "keras_cv>CSPDarkNetBackbone",
"config": {
"stackwise_channels": [128, 256, 512, 1024],
"stackwise_depth": [3, 9, 9, 3],
"include_rescaling": True,
"use_depthwise": False,
"input_shape": (None, None, 3),
"input_tensor": None,
},
"kaggle_handle": "gs://keras-cv-kaggle/csp_darknet_l",
},
"csp_darknet_xl": {
"metadata": {
Expand All @@ -110,15 +78,7 @@
"official_name": "CSPDarkNet",
"path": "csp_darknet",
},
"class_name": "keras_cv>CSPDarkNetBackbone",
"config": {
"stackwise_channels": [170, 340, 680, 1360],
"stackwise_depth": [4, 12, 12, 4],
"include_rescaling": True,
"use_depthwise": False,
"input_shape": (None, None, 3),
"input_tensor": None,
},
"kaggle_handle": "gs://keras-cv-kaggle/csp_darknet_xl",
},
}

Expand All @@ -135,10 +95,7 @@
"official_name": "CSPDarkNet",
"path": "csp_darknet",
},
"class_name": "keras_cv>CSPDarkNetBackbone",
"config": backbone_presets_no_weights["csp_darknet_tiny"]["config"],
"weights_url": "https://storage.googleapis.com/keras-cv/models/cspdarknettiny/imagenet/classification-v0-notop.h5", # noqa: E501
"weights_hash": "0007ae82c95be4d4aef06368a7c38e006381324d77e5df029b04890e18a8ad19", # noqa: E501
"kaggle_handle": "gs://keras-cv-kaggle/csp_darknet_tiny_imagenet",
},
"csp_darknet_l_imagenet": {
"metadata": {
Expand All @@ -152,10 +109,7 @@
"official_name": "CSPDarkNet",
"path": "csp_darknet",
},
"class_name": "keras_cv>CSPDarkNetBackbone",
"config": backbone_presets_no_weights["csp_darknet_l"]["config"],
"weights_url": "https://storage.googleapis.com/keras-cv/models/cspdarknetl/imagenet/classification-v0-notop.h5", # noqa: E501
"weights_hash": "9303aabfadffbff8447171fce1e941f96d230d8f3cef30d3f05a9c85097f8f1e", # noqa: E501
"kaggle_handle": "gs://keras-cv-kaggle/csp_darknet_l_imagenet",
},
}

Expand Down
45 changes: 6 additions & 39 deletions keras_cv/models/backbones/densenet/densenet_backbone_presets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,19 @@
"metadata": {
"description": "DenseNet model with 121 layers.",
},
"class_name": "keras_cv>DenseNetBackbone",
"config": {
"stackwise_num_repeats": [6, 12, 24, 16],
"include_rescaling": True,
"input_shape": (None, None, 3),
"input_tensor": None,
"compression_ratio": 0.5,
"growth_rate": 32,
},
"kaggle_handle": "gs://keras-cv-kaggle/densenet121",
},
"densenet169": {
"metadata": {
"description": "DenseNet model with 169 layers.",
},
"class_name": "keras_cv>DenseNetBackbone",
"config": {
"stackwise_num_repeats": [6, 12, 32, 32],
"include_rescaling": True,
"input_shape": (None, None, 3),
"input_tensor": None,
"compression_ratio": 0.5,
"growth_rate": 32,
},
"kaggle_handle": "gs://keras-cv-kaggle/densenet169",
},
"densenet201": {
"metadata": {
"description": "DenseNet model with 201 layers.",
},
"class_name": "keras_cv>DenseNetBackbone",
"config": {
"stackwise_num_repeats": [6, 12, 48, 32],
"include_rescaling": True,
"input_shape": (None, None, 3),
"input_tensor": None,
"compression_ratio": 0.5,
"growth_rate": 32,
},
"kaggle_handle": "gs://keras-cv-kaggle/densenet201",
},
}

Expand All @@ -66,10 +42,7 @@
"classification task."
),
},
"class_name": "keras_cv>DenseNetBackbone",
"config": backbone_presets_no_weights["densenet121"]["config"],
"weights_url": "https://storage.googleapis.com/keras-cv/models/densenet121/imagenet/classification-v0-notop.h5", # noqa: E501
"weights_hash": "709afe0321d9f2b2562e562ff9d0dc44cca10ed09e0e2cfba08d783ff4dab6bf", # noqa: E501
"kaggle_handle": "gs://keras-cv-kaggle/densenet121_imagenet",
},
"densenet169_imagenet": {
"metadata": {
Expand All @@ -78,10 +51,7 @@
"classification task."
),
},
"class_name": "keras_cv>DenseNetBackbone",
"config": backbone_presets_no_weights["densenet169"]["config"],
"weights_url": "https://storage.googleapis.com/keras-cv/models/densenet169/imagenet/classification-v0-notop.h5", # noqa: E501
"weights_hash": "a99d1bb2cbe1a59a1cdd1f435fb265453a97c2a7b723d26f4ebee96e5fb49d62", # noqa: E501
"kaggle_handle": "gs://keras-cv-kaggle/densenet169_imagenet",
},
"densenet201_imagenet": {
"metadata": {
Expand All @@ -90,10 +60,7 @@
"classification task."
),
},
"class_name": "keras_cv>DenseNetBackbone",
"config": backbone_presets_no_weights["densenet201"]["config"],
"weights_url": "https://storage.googleapis.com/keras-cv/models/densenet201/imagenet/classification-v0-notop.h5", # noqa: E501
"weights_hash": "c1189a934f12c1a676a9cf52238e5994401af925e2adfc0365bad8133c052060", # noqa: E501
"kaggle_handle": "gs://keras-cv-kaggle/densenet201_imagenet",
},
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,7 @@
"official_name": "EfficientNetLite",
"path": "EfficientNetLite",
},
"class_name": "keras_cv.models>EfficientNetLiteBackbone",
"config": {
"width_coefficient": 1.0,
"depth_coefficient": 1.0,
"dropout_rate": 0.2,
"drop_connect_rate": 0.2,
"depth_divisor": 8,
"stackwise_kernel_sizes": [3, 3, 5, 3, 5, 5, 3],
"stackwise_num_repeats": [1, 2, 2, 3, 3, 4, 1],
"stackwise_input_filters": [32, 16, 24, 40, 80, 112, 192],
"stackwise_output_filters": [16, 24, 40, 80, 112, 192, 320],
"stackwise_expansion_ratios": [1, 6, 6, 6, 6, 6, 6],
"stackwise_strides": [1, 2, 2, 2, 1, 2, 1],
"include_rescaling": True,
"input_shape": (None, None, 3),
"input_tensor": None,
"activation": "relu6",
},
"kaggle_handle": "gs://keras-cv-kaggle/efficientnetlite_b0",
},
"efficientnetlite_b1": {
"metadata": {
Expand All @@ -56,24 +39,7 @@
"official_name": "EfficientNetLite",
"path": "EfficientNetLite",
},
"class_name": "keras_cv.models>EfficientNetLiteBackbone",
"config": {
"width_coefficient": 1.0,
"depth_coefficient": 1.1,
"dropout_rate": 0.2,
"drop_connect_rate": 0.2,
"depth_divisor": 8,
"stackwise_kernel_sizes": [3, 3, 5, 3, 5, 5, 3],
"stackwise_num_repeats": [1, 2, 2, 3, 3, 4, 1],
"stackwise_input_filters": [32, 16, 24, 40, 80, 112, 192],
"stackwise_output_filters": [16, 24, 40, 80, 112, 192, 320],
"stackwise_expansion_ratios": [1, 6, 6, 6, 6, 6, 6],
"stackwise_strides": [1, 2, 2, 2, 1, 2, 1],
"include_rescaling": True,
"input_shape": (None, None, 3),
"input_tensor": None,
"activation": "relu6",
},
"kaggle_handle": "gs://keras-cv-kaggle/efficientnetlite_b1",
},
"efficientnetlite_b2": {
"metadata": {
Expand All @@ -86,24 +52,7 @@
"official_name": "EfficientNetLite",
"path": "EfficientNetLite",
},
"class_name": "keras_cv.models>EfficientNetLiteBackbone",
"config": {
"width_coefficient": 1.1,
"depth_coefficient": 1.2,
"dropout_rate": 0.3,
"drop_connect_rate": 0.2,
"depth_divisor": 8,
"stackwise_kernel_sizes": [3, 3, 5, 3, 5, 5, 3],
"stackwise_num_repeats": [1, 2, 2, 3, 3, 4, 1],
"stackwise_input_filters": [32, 16, 24, 40, 80, 112, 192],
"stackwise_output_filters": [16, 24, 40, 80, 112, 192, 320],
"stackwise_expansion_ratios": [1, 6, 6, 6, 6, 6, 6],
"stackwise_strides": [1, 2, 2, 2, 1, 2, 1],
"include_rescaling": True,
"input_shape": (None, None, 3),
"input_tensor": None,
"activation": "relu6",
},
"kaggle_handle": "gs://keras-cv-kaggle/efficientnetlite_b2",
},
"efficientnetlite_b3": {
"metadata": {
Expand All @@ -116,24 +65,7 @@
"official_name": "EfficientNetLite",
"path": "EfficientNetLite",
},
"class_name": "keras_cv.models>EfficientNetLiteBackbone",
"config": {
"width_coefficient": 1.2,
"depth_coefficient": 1.4,
"dropout_rate": 0.3,
"drop_connect_rate": 0.2,
"depth_divisor": 8,
"stackwise_kernel_sizes": [3, 3, 5, 3, 5, 5, 3],
"stackwise_num_repeats": [1, 2, 2, 3, 3, 4, 1],
"stackwise_input_filters": [32, 16, 24, 40, 80, 112, 192],
"stackwise_output_filters": [16, 24, 40, 80, 112, 192, 320],
"stackwise_expansion_ratios": [1, 6, 6, 6, 6, 6, 6],
"stackwise_strides": [1, 2, 2, 2, 1, 2, 1],
"include_rescaling": True,
"input_shape": (None, None, 3),
"input_tensor": None,
"activation": "relu6",
},
"kaggle_handle": "gs://keras-cv-kaggle/efficientnetlite_b3",
},
"efficientnetlite_b4": {
"metadata": {
Expand All @@ -146,24 +78,7 @@
"official_name": "EfficientNetLite",
"path": "EfficientNetLite",
},
"class_name": "keras_cv.models>EfficientNetLiteBackbone",
"config": {
"width_coefficient": 1.4,
"depth_coefficient": 1.8,
"dropout_rate": 0.3,
"drop_connect_rate": 0.2,
"depth_divisor": 8,
"stackwise_kernel_sizes": [3, 3, 5, 3, 5, 5, 3],
"stackwise_num_repeats": [1, 2, 2, 3, 3, 4, 1],
"stackwise_input_filters": [32, 16, 24, 40, 80, 112, 192],
"stackwise_output_filters": [16, 24, 40, 80, 112, 192, 320],
"stackwise_expansion_ratios": [1, 6, 6, 6, 6, 6, 6],
"stackwise_strides": [1, 2, 2, 2, 1, 2, 1],
"include_rescaling": True,
"input_shape": (None, None, 3),
"input_tensor": None,
"activation": "relu6",
},
"kaggle_handle": "gs://keras-cv-kaggle/efficientnetlite_b4",
},
}

Expand Down
Loading

0 comments on commit 96ac0c5

Please sign in to comment.