Skip to content

Commit fb4fca4

Browse files
authored
Generated API (keras-team#2425)
* Add API Gen * Update API Gen Code * Generate API * Add API Gen * Lint API Gen Code * Disable Faster RCNN failing tests for Keras 2
1 parent 60ebdf6 commit fb4fca4

File tree

567 files changed

+3350
-2412
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

567 files changed

+3350
-2412
lines changed

.github/workflows/actions.yml

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
${{ runner.os }}-pip-
3535
- name: Install dependencies
3636
run: |
37-
pip install tensorflow~=2.14
37+
pip install tensorflow~=2.15.0
3838
pip install torch>=2.0.1+cpu
3939
pip install "jax[cpu]"
4040
pip install keras-core
@@ -86,17 +86,16 @@ jobs:
8686
KAGGLE_KEY: ${{ secrets.KAGGLE_KEY}}
8787
JAX_ENABLE_X64: true
8888
run: |
89-
pytest keras_cv/bounding_box \
90-
keras_cv/callbacks \
91-
keras_cv/losses \
92-
keras_cv/layers/object_detection \
93-
keras_cv/layers/preprocessing \
94-
keras_cv/models/backbones \
95-
keras_cv/models/classification \
96-
keras_cv/models/object_detection/retinanet \
97-
keras_cv/models/object_detection/yolo_v8 \
98-
keras_cv/models/object_detection_3d \
99-
keras_cv/models/segmentation \
89+
pytest keras_cv/src/bounding_box \
90+
keras_cv/src/callbacks \
91+
keras_cv/src/losses \
92+
keras_cv/src/layers/object_detection \
93+
keras_cv/src/layers/preprocessing \
94+
keras_cv/src/models/classification \
95+
keras_cv/src/models/object_detection/retinanet \
96+
keras_cv/src/models/object_detection/yolo_v8 \
97+
keras_cv/src/models/object_detection_3d \
98+
keras_cv/src/models/segmentation \
10099
--durations 0
101100
format:
102101
name: Check the code format
@@ -121,10 +120,20 @@ jobs:
121120
${{ runner.os }}-pip-
122121
- name: Install dependencies
123122
run: |
124-
pip install tensorflow~=2.14
123+
pip install -r requirements.txt
125124
pip install -e ".[tests]" --progress-bar off --upgrade
125+
pip install keras-nlp-nightly
126126
- name: Lint
127127
run: bash shell/lint.sh
128+
- name: Check for API changes
129+
run: |
130+
bash shell/api_gen.sh
131+
git status
132+
clean=$(git status | grep "nothing to commit")
133+
if [ -z "$clean" ]; then
134+
echo "Please run shell/api_gen.sh to generate API."
135+
exit 1
136+
fi
128137
- name: Clang
129138
uses: DoozyX/clang-format-lint-action@v0.17
130139
with:

.kokoro/github/ubuntu/gpu/build.sh

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ if [ "${KERAS2:-0}" == "1" ]
2323
then
2424
echo "Keras2 detected."
2525
pip install -r requirements-common.txt --progress-bar off --timeout 1000
26-
pip install tensorflow~=2.14
26+
pip install tensorflow~=2.15.0
2727
pip install --extra-index-url https://download.pytorch.org/whl/cpu torch==2.1.0+cpu
2828
pip install torchvision~=0.16.0
2929
pip install "jax[cpu]"
3030
pip install keras-nlp-nightly --no-deps
31-
pip install tensorflow-text==2.15
31+
pip install tensorflow-text==2.15.0
3232

3333
elif [ "$KERAS_BACKEND" == "tensorflow" ]
3434
then
@@ -58,32 +58,32 @@ pip install --no-deps -e "." --progress-bar off
5858
if [ "${RUN_XLARGE:-0}" == "1" ]
5959
then
6060
pytest --cache-clear --check_gpu --run_large --run_extra_large --durations 0 \
61-
keras_cv/bounding_box \
62-
keras_cv/callbacks \
63-
keras_cv/losses \
64-
keras_cv/layers/object_detection \
65-
keras_cv/layers/preprocessing \
66-
keras_cv/models/backbones \
67-
keras_cv/models/classification \
68-
keras_cv/models/object_detection/retinanet \
69-
keras_cv/models/object_detection/yolo_v8 \
70-
keras_cv/models/object_detection_3d \
71-
keras_cv/models/segmentation \
72-
keras_cv/models/feature_extractor/clip \
73-
keras_cv/models/stable_diffusion
61+
keras_cv/src/bounding_box \
62+
keras_cv/src/callbacks \
63+
keras_cv/src/losses \
64+
keras_cv/src/layers/object_detection \
65+
keras_cv/src/layers/preprocessing \
66+
keras_cv/src/models/backbones \
67+
keras_cv/src/models/classification \
68+
keras_cv/src/models/object_detection/retinanet \
69+
keras_cv/src/models/object_detection/yolo_v8 \
70+
keras_cv/src/models/object_detection_3d \
71+
keras_cv/src/models/segmentation \
72+
keras_cv/src/models/feature_extractor/clip \
73+
keras_cv/src/models/stable_diffusion
7474
else
7575
pytest --cache-clear --check_gpu --run_large --durations 0 \
76-
keras_cv/bounding_box \
77-
keras_cv/callbacks \
78-
keras_cv/losses \
79-
keras_cv/layers/object_detection \
80-
keras_cv/layers/preprocessing \
81-
keras_cv/models/backbones \
82-
keras_cv/models/classification \
83-
keras_cv/models/object_detection/retinanet \
84-
keras_cv/models/object_detection/yolo_v8 \
85-
keras_cv/models/object_detection_3d \
86-
keras_cv/models/segmentation \
87-
keras_cv/models/feature_extractor/clip \
88-
keras_cv/models/stable_diffusion
76+
keras_cv/src/bounding_box \
77+
keras_cv/src/callbacks \
78+
keras_cv/src/losses \
79+
keras_cv/src/layers/object_detection \
80+
keras_cv/src/layers/preprocessing \
81+
keras_cv/src/models/backbones \
82+
keras_cv/src/models/classification \
83+
keras_cv/src/models/object_detection/retinanet \
84+
keras_cv/src/models/object_detection/yolo_v8 \
85+
keras_cv/src/models/object_detection_3d \
86+
keras_cv/src/models/segmentation \
87+
keras_cv/src/models/feature_extractor/clip \
88+
keras_cv/src/models/stable_diffusion
8989
fi

CONTRIBUTING.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ pytest keras_cv/layers/preprocessing/cut_mix_test.py -k="label"
133133

134134
### Run all tests
135135

136+
Run API generation script when creating PRs that update `keras_export` public APIs:
137+
138+
```
139+
./shell/api_gen.sh
140+
```
141+
136142
You can run the unit tests for KerasCV by running:
137143

138144
```

api_gen.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
"""Script to generate keras public API in `keras_cv/api` directory.
2+
3+
Usage:
4+
5+
Run via `./shell/api_gen.sh`.
6+
It generates API and formats user and generated APIs.
7+
"""
8+
9+
import os
10+
import shutil
11+
12+
import namex
13+
14+
package = "keras_cv"
15+
16+
17+
def ignore_files(_, filenames):
18+
return [f for f in filenames if f.endswith("_test.py")]
19+
20+
21+
def copy_source_to_build_directory(root_path):
22+
# Copy sources (`keras_cv/` directory and setup files) to build dir
23+
build_dir = os.path.join(root_path, "tmp_build_dir")
24+
if os.path.exists(build_dir):
25+
shutil.rmtree(build_dir)
26+
os.mkdir(build_dir)
27+
shutil.copytree(
28+
package, os.path.join(build_dir, package), ignore=ignore_files
29+
)
30+
return build_dir
31+
32+
33+
def export_version_string(api_init_fname):
34+
with open(api_init_fname) as f:
35+
contents = f.read()
36+
with open(api_init_fname, "w") as f:
37+
contents += "from keras_cv.src.version_utils import __version__\n"
38+
f.write(contents)
39+
40+
41+
def update_package_init(init_fname):
42+
contents = """
43+
# Import everything from /api/ into keras.
44+
from keras_cv.api import * # noqa: F403
45+
from keras_cv.api import __version__ # Import * ignores names start with "_".
46+
47+
import os
48+
49+
# Add everything in /api/ to the module search path.
50+
__path__.append(os.path.join(os.path.dirname(__file__), "api")) # noqa: F405
51+
52+
# Don't pollute namespace.
53+
del os
54+
55+
# Never autocomplete `.src` or `.api` on an imported keras object.
56+
def __dir__():
57+
keys = dict.fromkeys((globals().keys()))
58+
keys.pop("src")
59+
keys.pop("api")
60+
return list(keys)
61+
62+
63+
# Don't import `.src` or `.api` during `from keras import *`.
64+
__all__ = [
65+
name
66+
for name in globals().keys()
67+
if not (name.startswith("_") or name in ("src", "api"))
68+
]"""
69+
with open(init_fname) as f:
70+
init_contents = f.read()
71+
with open(init_fname, "w") as f:
72+
f.write(init_contents.replace("\nfrom keras_cv import api", contents))
73+
74+
75+
def build():
76+
# Backup the `keras_cv/__init__.py` and restore it on error in api gen.
77+
root_path = os.path.dirname(os.path.abspath(__file__))
78+
code_api_dir = os.path.join(root_path, package, "api")
79+
code_init_fname = os.path.join(root_path, package, "__init__.py")
80+
# Create temp build dir
81+
build_dir = copy_source_to_build_directory(root_path)
82+
build_api_dir = os.path.join(build_dir, package, "api")
83+
build_init_fname = os.path.join(build_dir, package, "__init__.py")
84+
build_api_init_fname = os.path.join(build_api_dir, "__init__.py")
85+
try:
86+
os.chdir(build_dir)
87+
# Generates `keras_cv/api` directory.
88+
if os.path.exists(build_api_dir):
89+
shutil.rmtree(build_api_dir)
90+
if os.path.exists(build_init_fname):
91+
os.remove(build_init_fname)
92+
os.makedirs(build_api_dir)
93+
namex.generate_api_files(
94+
"keras_cv", code_directory="src", target_directory="api"
95+
)
96+
# Creates `keras_cv/__init__.py` importing from `keras_cv/api`
97+
update_package_init(build_init_fname)
98+
# Add __version__ to keras package
99+
export_version_string(build_api_init_fname)
100+
# Copy back the keras_cv/api and keras_cv/__init__.py from build dir
101+
if os.path.exists(code_api_dir):
102+
shutil.rmtree(code_api_dir)
103+
shutil.copytree(build_api_dir, code_api_dir)
104+
shutil.copy(build_init_fname, code_init_fname)
105+
finally:
106+
# Clean up: remove the build directory (no longer needed)
107+
shutil.rmtree(build_dir)
108+
109+
110+
if __name__ == "__main__":
111+
build()

conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import tensorflow as tf
1717
from packaging import version
1818

19-
from keras_cv.backend import config as backend_config
20-
from keras_cv.backend.config import keras_3
19+
from keras_cv.src.backend import config as backend_config
20+
from keras_cv.src.backend.config import keras_3
2121

2222

2323
def pytest_addoption(parser):

keras_cv/__init__.py

Lines changed: 33 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,33 @@
1-
# Copyright 2022 The KerasCV Authors
2-
#
3-
# Licensed under the Apache License, Version 2.0 (the "License");
4-
# you may not use this file except in compliance with the License.
5-
# You may obtain a copy of the License at
6-
#
7-
# https://www.apache.org/licenses/LICENSE-2.0
8-
#
9-
# Unless required by applicable law or agreed to in writing, software
10-
# distributed under the License is distributed on an "AS IS" BASIS,
11-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12-
# See the License for the specific language governing permissions and
13-
# limitations under the License.
14-
15-
try:
16-
# When using torch and tensorflow, torch needs to be imported first,
17-
# otherwise it will segfault upon import.
18-
import torch
19-
20-
del torch
21-
except ImportError:
22-
pass
23-
24-
# isort:off
25-
from keras_cv import version_check
26-
27-
version_check.check_tf_version()
28-
# isort:on
29-
30-
from keras_cv import bounding_box
31-
from keras_cv import callbacks
32-
from keras_cv import datasets
33-
from keras_cv import layers
34-
from keras_cv import losses
35-
from keras_cv import metrics
36-
from keras_cv import models
37-
from keras_cv import training
38-
from keras_cv import utils
39-
from keras_cv import visualization
40-
from keras_cv.core import ConstantFactorSampler
41-
from keras_cv.core import FactorSampler
42-
from keras_cv.core import NormalFactorSampler
43-
from keras_cv.core import UniformFactorSampler
44-
from keras_cv.version_utils import __version__
45-
from keras_cv.version_utils import version
1+
"""DO NOT EDIT.
2+
3+
This file was autogenerated. Do not edit it by hand,
4+
since your modifications would be overwritten.
5+
"""
6+
7+
import os
8+
9+
# Import everything from /api/ into keras.
10+
from keras_cv.api import * # noqa: F403
11+
from keras_cv.api import __version__ # Import * ignores names start with "_".
12+
13+
# Add everything in /api/ to the module search path.
14+
__path__.append(os.path.join(os.path.dirname(__file__), "api")) # noqa: F405
15+
16+
# Don't pollute namespace.
17+
del os
18+
19+
20+
# Never autocomplete `.src` or `.api` on an imported keras object.
21+
def __dir__():
22+
keys = dict.fromkeys((globals().keys()))
23+
keys.pop("src")
24+
keys.pop("api")
25+
return list(keys)
26+
27+
28+
# Don't import `.src` or `.api` during `from keras import *`.
29+
__all__ = [
30+
name
31+
for name in globals().keys()
32+
if not (name.startswith("_") or name in ("src", "api"))
33+
]

keras_cv/api/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""DO NOT EDIT.
2+
3+
This file was autogenerated. Do not edit it by hand,
4+
since your modifications would be overwritten.
5+
"""
6+
7+
from keras_cv.api import bounding_box
8+
from keras_cv.api import callbacks
9+
from keras_cv.api import core
10+
from keras_cv.api import datasets
11+
from keras_cv.api import keypoint
12+
from keras_cv.api import layers
13+
from keras_cv.api import losses
14+
from keras_cv.api import metrics
15+
from keras_cv.api import models
16+
from keras_cv.api import visualization
17+
from keras_cv.src.version_utils import __version__
18+
from keras_cv.src.version_utils import version
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""DO NOT EDIT.
2+
3+
This file was autogenerated. Do not edit it by hand,
4+
since your modifications would be overwritten.
5+
"""
6+
7+
from keras_cv.src.bounding_box.converters import convert_format
8+
from keras_cv.src.bounding_box.ensure_tensor import ensure_tensor
9+
from keras_cv.src.bounding_box.formats import CENTER_XYWH
10+
from keras_cv.src.bounding_box.formats import REL_XYWH
11+
from keras_cv.src.bounding_box.formats import REL_XYXY
12+
from keras_cv.src.bounding_box.formats import REL_YXYX
13+
from keras_cv.src.bounding_box.formats import XYWH
14+
from keras_cv.src.bounding_box.formats import XYXY
15+
from keras_cv.src.bounding_box.formats import YXYX
16+
from keras_cv.src.bounding_box.iou import compute_ciou
17+
from keras_cv.src.bounding_box.iou import compute_iou
18+
from keras_cv.src.bounding_box.mask_invalid_detections import (
19+
mask_invalid_detections,
20+
)
21+
from keras_cv.src.bounding_box.to_dense import to_dense
22+
from keras_cv.src.bounding_box.to_ragged import to_ragged
23+
from keras_cv.src.bounding_box.utils import as_relative
24+
from keras_cv.src.bounding_box.utils import clip_to_image
25+
from keras_cv.src.bounding_box.utils import is_relative
26+
from keras_cv.src.bounding_box.validate_format import validate_format

0 commit comments

Comments
 (0)