Skip to content

Commit

Permalink
Add Kaggle preset conversion upload script (keras-team#2205)
Browse files Browse the repository at this point in the history
* Add preset conversion upload script

* Fix formatting
  • Loading branch information
nkovela1 committed Jan 4, 2024
1 parent e71dd9c commit 62e3039
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
107 changes: 107 additions & 0 deletions keras_cv/tools/convert_presets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Copyright 2023 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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 os
import shutil

import keras_cv # noqa: E402
from keras_cv.src.utils.preset_utils import save_to_preset # noqa: E402

BUCKET = "keras-cv-kaggle"

# Save and upload Backbone presets

backbone_models = [
keras_cv.models.ResNetBackbone,
keras_cv.models.ResNet18Backbone,
keras_cv.models.ResNet34Backbone,
keras_cv.models.ResNet50Backbone,
keras_cv.models.ResNet101Backbone,
keras_cv.models.ResNet152Backbone,
keras_cv.models.ResNetV2Backbone,
keras_cv.models.ResNet18V2Backbone,
keras_cv.models.ResNet34V2Backbone,
keras_cv.models.ResNet50V2Backbone,
keras_cv.models.ResNet101V2Backbone,
keras_cv.models.ResNet152V2Backbone,
keras_cv.models.YOLOV8Backbone,
keras_cv.models.MobileNetV3Backbone,
keras_cv.models.MobileNetV3SmallBackbone,
keras_cv.models.MobileNetV3LargeBackbone,
keras_cv.models.EfficientNetV2Backbone,
keras_cv.models.EfficientNetV2B0Backbone,
keras_cv.models.EfficientNetV2B1Backbone,
keras_cv.models.EfficientNetV2B2Backbone,
keras_cv.models.EfficientNetV2B3Backbone,
keras_cv.models.EfficientNetV2SBackbone,
keras_cv.models.EfficientNetV2MBackbone,
keras_cv.models.EfficientNetV2LBackbone,
]
for backbone_cls in backbone_models:
for preset in backbone_cls.presets:
backbone = backbone_cls.from_preset(preset)
save_to_preset(
backbone,
preset,
config_filename="config.json",
)
# Delete first to clean up any exising version.
os.system(f"gsutil rm -rf gs://{BUCKET}/{preset}")
os.system(f"gsutil cp -r {preset} gs://{BUCKET}/{preset}")
for root, _, files in os.walk(preset):
for file in files:
path = os.path.join(BUCKET, root, file)
os.system(
f"gcloud storage objects update gs://{path} "
"--add-acl-grant=entity=AllUsers,role=READER"
)


# Save and upload task presets

task_models = [
keras_cv.models.RetinaNet,
keras_cv.models.YOLOV8Detector,
keras_cv.models.ImageClassifier,
keras_cv.models.DeepLabV3Plus,
]
for task_cls in task_models:
# Remove backbone-specific keys
task_preset_keys = set(task_cls.presets) ^ set(task_cls.backbone_presets)
for preset in task_preset_keys:
preset_metadata = task_cls.presets[preset]
kwargs = {}
if task_cls in [
keras_cv.models.RetinaNet,
keras_cv.models.YOLOV8Detector,
]:
kwargs.update({"bounding_box_format": "xywh"})
task = task_cls.from_preset(preset, **kwargs)
else:
task = task_cls.from_preset(preset)
save_to_preset(
task,
preset,
config_filename="config.json",
)
# Delete first to clean up any exising version.
os.system(f"gsutil rm -rf gs://{BUCKET}/{preset}")
os.system(f"gsutil cp -r {preset} gs://{BUCKET}/{preset}")
for root, _, files in os.walk(preset):
for file in files:
path = os.path.join(BUCKET, root, file)
os.system(
f"gcloud storage objects update gs://{path} "
"--add-acl-grant=entity=AllUsers,role=READER"
)
2 changes: 1 addition & 1 deletion keras_cv/utils/preset_utils_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2023 The KerasNLP Authors
# Copyright 2023 The KerasCV Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 62e3039

Please sign in to comment.