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

Test presets with Keras saving fixes #2255

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
02d9dc8
Adapt KerasCV for Kaggle integration with new preset format (#2200)
nkovela1 Nov 29, 2023
fada027
Add Kaggle preset conversion upload script (#2205)
nkovela1 Nov 30, 2023
2aea34e
Switch all available presets to Kaggle format (#2206)
nkovela1 Dec 5, 2023
7d24e7b
Update conversion script to fix model naming (#2221)
nkovela1 Dec 7, 2023
e4b6d7f
Change CenterPillar name (#2219)
nkovela1 Dec 7, 2023
0d83971
Add clearer errors for preset methods (#2220)
nkovela1 Dec 7, 2023
b62cec9
Update segformer_presets with backbone import (fix merge) (#2225)
nkovela1 Dec 8, 2023
fdd7e35
Switch presets to final Kaggle location (#2216)
nkovela1 Dec 11, 2023
9161841
Update version to 0.8.0.dev0
sampathweb Dec 11, 2023
d04627b
Remove print statement in test
sampathweb Dec 11, 2023
4939f78
Update SegFormer preset tests to remove input_shape arg (#2234)
nkovela1 Dec 11, 2023
e0b3883
Update `load_weights` for Keras 2 compatibility (#2235)
nkovela1 Dec 11, 2023
16b88d0
Modify layer checkpoint dependencies to empty list (#2247)
nkovela1 Dec 15, 2023
209dc36
Change layer checkpoint dependencies to empty dict (#2249)
nkovela1 Dec 15, 2023
d87370b
Add file modification fix for weights file structure
nkovela1 Dec 15, 2023
21f0cde
Add fix for Keras autonaming
nkovela1 Dec 15, 2023
5757d82
Fix yolo v8 bug
nkovela1 Dec 15, 2023
34a67e8
Add renaming logic for safe mile modification
nkovela1 Dec 16, 2023
127ad62
Add specific fixes for RetinaNet and SAM
nkovela1 Dec 18, 2023
9fad0a5
Fix conflict between code fixes
nkovela1 Dec 18, 2023
19de0d6
Fix SAM
nkovela1 Dec 18, 2023
7d48df4
Adapt KerasCV for Kaggle integration with new preset format (#2200)
nkovela1 Nov 29, 2023
5b7536d
Add Kaggle preset conversion upload script (#2205)
nkovela1 Nov 30, 2023
a881bcb
Switch all available presets to Kaggle format (#2206)
nkovela1 Dec 5, 2023
1f15277
Update conversion script to fix model naming (#2221)
nkovela1 Dec 7, 2023
8555a3d
Change CenterPillar name (#2219)
nkovela1 Dec 7, 2023
af4093a
Add clearer errors for preset methods (#2220)
nkovela1 Dec 7, 2023
b1e55d6
Update segformer_presets with backbone import (fix merge) (#2225)
nkovela1 Dec 8, 2023
a19b26a
Switch presets to final Kaggle location (#2216)
nkovela1 Dec 11, 2023
61b4439
Update version to 0.8.0.dev0
sampathweb Dec 11, 2023
9e0f012
Remove print statement in test
sampathweb Dec 11, 2023
c569f2c
Update SegFormer preset tests to remove input_shape arg (#2234)
nkovela1 Dec 11, 2023
7eadfa6
Update `load_weights` for Keras 2 compatibility (#2235)
nkovela1 Dec 11, 2023
b8dabd6
Modify layer checkpoint dependencies to empty list (#2247)
nkovela1 Dec 15, 2023
c5fda25
Change layer checkpoint dependencies to empty dict (#2249)
nkovela1 Dec 15, 2023
892d796
Test saving fixes on broken presets
nkovela1 Dec 19, 2023
03f9092
Merge branch 'kaggle' into fix-presets
nkovela1 Dec 19, 2023
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
7 changes: 7 additions & 0 deletions .cloudbuild/cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ steps:
'--ext-str', 'backend=$_BACKEND',
'-o', 'output.yaml',
]
secretEnv: ['KAGGLE_USERNAME', 'KAGGLE_KEY']
- name: 'gcr.io/cloud-builders/gcloud'
id: create-job
waitFor:
Expand All @@ -75,3 +76,9 @@ options:
volumes:
- name: go-modules
path: /go
availableSecrets:
secretManager:
- versionName: projects/$PROJECT_ID/secrets/KAGGLE_USERNAME/versions/latest
env: 'KAGGLE_USERNAME'
- versionName: projects/$PROJECT_ID/secrets/KAGGLE_KEY/versions/latest
env: 'KAGGLE_KEY'
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
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"
49 changes: 12 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 Down Expand Up @@ -95,43 +95,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/1",
},
"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/1",
},
"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/1",
},
"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/1",
},
"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/1",
},
}

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/1", # 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/1",
},
}

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/1",
},
"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/1",
},
"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/1",
},
}

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/1",
},
"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/1",
},
"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/1",
},
}

Expand Down
Loading
Loading