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

Generated API #2425

Merged
merged 6 commits into from
Apr 29, 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
35 changes: 22 additions & 13 deletions .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
${{ runner.os }}-pip-
- name: Install dependencies
run: |
pip install tensorflow~=2.14
pip install tensorflow~=2.15.0
pip install torch>=2.0.1+cpu
pip install "jax[cpu]"
pip install keras-core
Expand Down Expand Up @@ -86,17 +86,16 @@ jobs:
KAGGLE_KEY: ${{ secrets.KAGGLE_KEY}}
JAX_ENABLE_X64: true
run: |
pytest keras_cv/bounding_box \
keras_cv/callbacks \
keras_cv/losses \
keras_cv/layers/object_detection \
keras_cv/layers/preprocessing \
keras_cv/models/backbones \
keras_cv/models/classification \
keras_cv/models/object_detection/retinanet \
keras_cv/models/object_detection/yolo_v8 \
keras_cv/models/object_detection_3d \
keras_cv/models/segmentation \
pytest keras_cv/src/bounding_box \
keras_cv/src/callbacks \
keras_cv/src/losses \
keras_cv/src/layers/object_detection \
keras_cv/src/layers/preprocessing \
keras_cv/src/models/classification \
keras_cv/src/models/object_detection/retinanet \
keras_cv/src/models/object_detection/yolo_v8 \
keras_cv/src/models/object_detection_3d \
keras_cv/src/models/segmentation \
--durations 0
format:
name: Check the code format
Expand All @@ -121,10 +120,20 @@ jobs:
${{ runner.os }}-pip-
- name: Install dependencies
run: |
pip install tensorflow~=2.14
pip install -r requirements.txt
pip install -e ".[tests]" --progress-bar off --upgrade
pip install keras-nlp-nightly
- name: Lint
run: bash shell/lint.sh
- name: Check for API changes
run: |
bash shell/api_gen.sh
git status
clean=$(git status | grep "nothing to commit")
if [ -z "$clean" ]; then
echo "Please run shell/api_gen.sh to generate API."
exit 1
fi
- name: Clang
uses: DoozyX/clang-format-lint-action@v0.17
with:
Expand Down
56 changes: 28 additions & 28 deletions .kokoro/github/ubuntu/gpu/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ if [ "${KERAS2:-0}" == "1" ]
then
echo "Keras2 detected."
pip install -r requirements-common.txt --progress-bar off --timeout 1000
pip install tensorflow~=2.14
pip install tensorflow~=2.15.0
pip install --extra-index-url https://download.pytorch.org/whl/cpu torch==2.1.0+cpu
pip install torchvision~=0.16.0
pip install "jax[cpu]"
pip install keras-nlp-nightly --no-deps
pip install tensorflow-text==2.15
pip install tensorflow-text==2.15.0

elif [ "$KERAS_BACKEND" == "tensorflow" ]
then
Expand Down Expand Up @@ -58,32 +58,32 @@ pip install --no-deps -e "." --progress-bar off
if [ "${RUN_XLARGE:-0}" == "1" ]
then
pytest --cache-clear --check_gpu --run_large --run_extra_large --durations 0 \
keras_cv/bounding_box \
keras_cv/callbacks \
keras_cv/losses \
keras_cv/layers/object_detection \
keras_cv/layers/preprocessing \
keras_cv/models/backbones \
keras_cv/models/classification \
keras_cv/models/object_detection/retinanet \
keras_cv/models/object_detection/yolo_v8 \
keras_cv/models/object_detection_3d \
keras_cv/models/segmentation \
keras_cv/models/feature_extractor/clip \
keras_cv/models/stable_diffusion
keras_cv/src/bounding_box \
keras_cv/src/callbacks \
keras_cv/src/losses \
keras_cv/src/layers/object_detection \
keras_cv/src/layers/preprocessing \
keras_cv/src/models/backbones \
keras_cv/src/models/classification \
keras_cv/src/models/object_detection/retinanet \
keras_cv/src/models/object_detection/yolo_v8 \
keras_cv/src/models/object_detection_3d \
keras_cv/src/models/segmentation \
keras_cv/src/models/feature_extractor/clip \
keras_cv/src/models/stable_diffusion
else
pytest --cache-clear --check_gpu --run_large --durations 0 \
keras_cv/bounding_box \
keras_cv/callbacks \
keras_cv/losses \
keras_cv/layers/object_detection \
keras_cv/layers/preprocessing \
keras_cv/models/backbones \
keras_cv/models/classification \
keras_cv/models/object_detection/retinanet \
keras_cv/models/object_detection/yolo_v8 \
keras_cv/models/object_detection_3d \
keras_cv/models/segmentation \
keras_cv/models/feature_extractor/clip \
keras_cv/models/stable_diffusion
keras_cv/src/bounding_box \
keras_cv/src/callbacks \
keras_cv/src/losses \
keras_cv/src/layers/object_detection \
keras_cv/src/layers/preprocessing \
keras_cv/src/models/backbones \
keras_cv/src/models/classification \
keras_cv/src/models/object_detection/retinanet \
keras_cv/src/models/object_detection/yolo_v8 \
keras_cv/src/models/object_detection_3d \
keras_cv/src/models/segmentation \
keras_cv/src/models/feature_extractor/clip \
keras_cv/src/models/stable_diffusion
fi
6 changes: 6 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ pytest keras_cv/layers/preprocessing/cut_mix_test.py -k="label"

### Run all tests

Run API generation script when creating PRs that update `keras_export` public APIs:

```
./shell/api_gen.sh
```

You can run the unit tests for KerasCV by running:

```
Expand Down
111 changes: 111 additions & 0 deletions api_gen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"""Script to generate keras public API in `keras_cv/api` directory.

Usage:

Run via `./shell/api_gen.sh`.
It generates API and formats user and generated APIs.
"""

import os
import shutil

import namex

package = "keras_cv"


def ignore_files(_, filenames):
return [f for f in filenames if f.endswith("_test.py")]


def copy_source_to_build_directory(root_path):
# Copy sources (`keras_cv/` directory and setup files) to build dir
build_dir = os.path.join(root_path, "tmp_build_dir")
if os.path.exists(build_dir):
shutil.rmtree(build_dir)
os.mkdir(build_dir)
shutil.copytree(
package, os.path.join(build_dir, package), ignore=ignore_files
)
return build_dir


def export_version_string(api_init_fname):
with open(api_init_fname) as f:
contents = f.read()
with open(api_init_fname, "w") as f:
contents += "from keras_cv.src.version_utils import __version__\n"
f.write(contents)


def update_package_init(init_fname):
contents = """
# Import everything from /api/ into keras.
from keras_cv.api import * # noqa: F403
from keras_cv.api import __version__ # Import * ignores names start with "_".

import os

# Add everything in /api/ to the module search path.
__path__.append(os.path.join(os.path.dirname(__file__), "api")) # noqa: F405

# Don't pollute namespace.
del os

# Never autocomplete `.src` or `.api` on an imported keras object.
def __dir__():
keys = dict.fromkeys((globals().keys()))
keys.pop("src")
keys.pop("api")
return list(keys)


# Don't import `.src` or `.api` during `from keras import *`.
__all__ = [
name
for name in globals().keys()
if not (name.startswith("_") or name in ("src", "api"))
]"""
with open(init_fname) as f:
init_contents = f.read()
with open(init_fname, "w") as f:
f.write(init_contents.replace("\nfrom keras_cv import api", contents))


def build():
# Backup the `keras_cv/__init__.py` and restore it on error in api gen.
root_path = os.path.dirname(os.path.abspath(__file__))
code_api_dir = os.path.join(root_path, package, "api")
code_init_fname = os.path.join(root_path, package, "__init__.py")
# Create temp build dir
build_dir = copy_source_to_build_directory(root_path)
build_api_dir = os.path.join(build_dir, package, "api")
build_init_fname = os.path.join(build_dir, package, "__init__.py")
build_api_init_fname = os.path.join(build_api_dir, "__init__.py")
try:
os.chdir(build_dir)
# Generates `keras_cv/api` directory.
if os.path.exists(build_api_dir):
shutil.rmtree(build_api_dir)
if os.path.exists(build_init_fname):
os.remove(build_init_fname)
os.makedirs(build_api_dir)
namex.generate_api_files(
"keras_cv", code_directory="src", target_directory="api"
)
# Creates `keras_cv/__init__.py` importing from `keras_cv/api`
update_package_init(build_init_fname)
# Add __version__ to keras package
export_version_string(build_api_init_fname)
# Copy back the keras_cv/api and keras_cv/__init__.py from build dir
if os.path.exists(code_api_dir):
shutil.rmtree(code_api_dir)
shutil.copytree(build_api_dir, code_api_dir)
shutil.copy(build_init_fname, code_init_fname)
finally:
# Clean up: remove the build directory (no longer needed)
shutil.rmtree(build_dir)


if __name__ == "__main__":
build()
4 changes: 2 additions & 2 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import tensorflow as tf
from packaging import version

from keras_cv.backend import config as backend_config
from keras_cv.backend.config import keras_3
from keras_cv.src.backend import config as backend_config
from keras_cv.src.backend.config import keras_3


def pytest_addoption(parser):
Expand Down
78 changes: 33 additions & 45 deletions keras_cv/__init__.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
# Copyright 2022 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.

try:
# When using torch and tensorflow, torch needs to be imported first,
# otherwise it will segfault upon import.
import torch

del torch
except ImportError:
pass

# isort:off
from keras_cv import version_check

version_check.check_tf_version()
# isort:on

from keras_cv import bounding_box
from keras_cv import callbacks
from keras_cv import datasets
from keras_cv import layers
from keras_cv import losses
from keras_cv import metrics
from keras_cv import models
from keras_cv import training
from keras_cv import utils
from keras_cv import visualization
from keras_cv.core import ConstantFactorSampler
from keras_cv.core import FactorSampler
from keras_cv.core import NormalFactorSampler
from keras_cv.core import UniformFactorSampler
from keras_cv.version_utils import __version__
from keras_cv.version_utils import version
"""DO NOT EDIT.

This file was autogenerated. Do not edit it by hand,
since your modifications would be overwritten.
"""

import os

# Import everything from /api/ into keras.
from keras_cv.api import * # noqa: F403
from keras_cv.api import __version__ # Import * ignores names start with "_".

# Add everything in /api/ to the module search path.
__path__.append(os.path.join(os.path.dirname(__file__), "api")) # noqa: F405

# Don't pollute namespace.
del os


# Never autocomplete `.src` or `.api` on an imported keras object.
def __dir__():
keys = dict.fromkeys((globals().keys()))
keys.pop("src")
keys.pop("api")
return list(keys)


# Don't import `.src` or `.api` during `from keras import *`.
__all__ = [
name
for name in globals().keys()
if not (name.startswith("_") or name in ("src", "api"))
]
18 changes: 18 additions & 0 deletions keras_cv/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""DO NOT EDIT.

This file was autogenerated. Do not edit it by hand,
since your modifications would be overwritten.
"""

from keras_cv.api import bounding_box
from keras_cv.api import callbacks
from keras_cv.api import core
from keras_cv.api import datasets
from keras_cv.api import keypoint
from keras_cv.api import layers
from keras_cv.api import losses
from keras_cv.api import metrics
from keras_cv.api import models
from keras_cv.api import visualization
from keras_cv.src.version_utils import __version__
from keras_cv.src.version_utils import version
26 changes: 26 additions & 0 deletions keras_cv/api/bounding_box/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""DO NOT EDIT.

This file was autogenerated. Do not edit it by hand,
since your modifications would be overwritten.
"""

from keras_cv.src.bounding_box.converters import convert_format
from keras_cv.src.bounding_box.ensure_tensor import ensure_tensor
from keras_cv.src.bounding_box.formats import CENTER_XYWH
from keras_cv.src.bounding_box.formats import REL_XYWH
from keras_cv.src.bounding_box.formats import REL_XYXY
from keras_cv.src.bounding_box.formats import REL_YXYX
from keras_cv.src.bounding_box.formats import XYWH
from keras_cv.src.bounding_box.formats import XYXY
from keras_cv.src.bounding_box.formats import YXYX
from keras_cv.src.bounding_box.iou import compute_ciou
from keras_cv.src.bounding_box.iou import compute_iou
from keras_cv.src.bounding_box.mask_invalid_detections import (
mask_invalid_detections,
)
from keras_cv.src.bounding_box.to_dense import to_dense
from keras_cv.src.bounding_box.to_ragged import to_ragged
from keras_cv.src.bounding_box.utils import as_relative
from keras_cv.src.bounding_box.utils import clip_to_image
from keras_cv.src.bounding_box.utils import is_relative
from keras_cv.src.bounding_box.validate_format import validate_format
Loading
Loading