Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Kaggle integration to CV master branch #2229

Merged
merged 15 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jobs:
pip install -e ".[tests]" --progress-bar off --upgrade
- name: Test with pytest
env:
KAGGLE_USERNAME: ${{ secrets.KAGGLE_USERNAME }}
KAGGLE_KEY: ${{ secrets.KAGGLE_KEY }}
TEST_CUSTOM_OPS: false
run: |
pytest keras_cv/ --ignore keras_cv/models/legacy/ --durations 0
Expand Down Expand Up @@ -77,6 +79,8 @@ jobs:
env:
TEST_CUSTOM_OPS: false # TODO(ianstenbit): test custom ops, or figure out what our story is here
KERAS_BACKEND: ${{ matrix.backend }}
KAGGLE_USERNAME: ${{ secrets.KAGGLE_USERNAME}}
KAGGLE_KEY: ${{ secrets.KAGGLE_KEY}}
JAX_ENABLE_X64: true
run: |
pytest keras_cv/bounding_box \
Expand Down
2 changes: 1 addition & 1 deletion keras_cv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@
from keras_cv.core import NormalFactorSampler
from keras_cv.core import UniformFactorSampler

__version__ = "0.8.0"
__version__ = "0.8.0.dev0"
63 changes: 26 additions & 37 deletions keras_cv/models/backbones/backbone.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
# 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
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

Expand All @@ -32,6 +32,20 @@ class Backbone(keras.Model):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._pyramid_level_inputs = {}
self._functional_layer_ids = set(
id(layer) for layer in self._flatten_layers()
)

def __dir__(self):
# Temporary fixes for weight saving. This mimics the following PR for
# older version of Keras: https://github.com/keras-team/keras/pull/18982
def filter_fn(attr):
try:
return id(getattr(self, attr)) not in self._functional_layer_ids
except:
return True

return filter(filter_fn, super().__dir__())

def get_config(self):
# Don't chain to super here. The default `get_config()` for functional
Expand Down Expand Up @@ -95,43 +109,18 @@ def from_preset(
load_weights=False,
```
"""

if not cls.presets:
raise NotImplementedError(
"No presets have been created for this class."
)

if preset not in cls.presets:
raise ValueError(
"`preset` must be one of "
f"""{", ".join(cls.presets)}. Received: {preset}."""
)

if load_weights and preset not in cls.presets_with_weights:
raise ValueError(
f"""Pretrained weights not available for preset "{preset}". """
"Set `load_weights=False` to use this preset or choose one of "
"the following presets with weights:"
f""" "{'", "'.join(cls.presets_with_weights)}"."""
)

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"],
# 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)
return load_from_preset(
preset,
load_weights=load_weights,
config_overrides=kwargs,
)

model.load_weights(weights)
return model

def __init_subclass__(cls, **kwargs):
# Use __init_subclass__ to set up a correct docstring for from_preset.
super().__init_subclass__(**kwargs)
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": "kaggle://keras/cspdarknet/csp_darknet_tiny/2",
},
"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": "kaggle://keras/cspdarknet/csp_darknet_s/2",
},
"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": "kaggle://keras/cspdarknet/csp_darknet_m/2",
},
"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": "kaggle://keras/cspdarknet/csp_darknet_l/2",
},
"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": "kaggle://keras/cspdarknet/csp_darknet_xl/2",
},
}

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": "kaggle://keras/cspdarknet/csp_darknet_tiny_imagenet/2", # noqa: E501
},
"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": "kaggle://keras/cspdarknet/csp_darknet_l_imagenet/2",
},
}

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": "kaggle://keras/densenet/densenet121/2",
},
"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": "kaggle://keras/densenet/densenet169/2",
},
"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": "kaggle://keras/densenet/densenet201/2",
},
}

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": "kaggle://keras/densenet/densenet121_imagenet/2",
},
"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": "kaggle://keras/densenet/densenet169_imagenet/2",
},
"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": "kaggle://keras/densenet/densenet201_imagenet/2",
},
}

Expand Down
Loading
Loading