Skip to content

Commit 1f667e6

Browse files
authored
Use Namex for API generation (#1995)
* Structure for Namex export * Switch to programmatic API generation with Namex * Fix missing import * Fix preset class names
1 parent d14c16d commit 1f667e6

File tree

146 files changed

+554
-194
lines changed

Some content is hidden

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

146 files changed

+554
-194
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ jobs:
102102
pip install tensorflow==2.13.0
103103
python -m pip install --upgrade pip setuptools wheel twine
104104
export BUILD_WITH_CUSTOM_OPS=false
105-
python setup.py sdist bdist_wheel
105+
python pip_build.py
106106
- name: Upload wheels
107107
env:
108108
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}

keras_cv/api_export.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright 2023 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+
import types
16+
17+
from keras_cv.backend import keras
18+
19+
try:
20+
import namex
21+
except ImportError:
22+
namex = None
23+
24+
25+
def maybe_register_serializable(symbol):
26+
if isinstance(symbol, types.FunctionType) or hasattr(symbol, "get_config"):
27+
keras.saving.register_keras_serializable(package="keras_cv")(symbol)
28+
29+
30+
if namex:
31+
32+
class keras_cv_export(namex.export):
33+
def __init__(self, path):
34+
super().__init__(package="keras_cv", path=path)
35+
36+
def __call__(self, symbol):
37+
maybe_register_serializable(symbol)
38+
return super().__call__(symbol)
39+
40+
else:
41+
42+
class keras_cv_export:
43+
def __init__(self, path):
44+
pass
45+
46+
def __call__(self, symbol):
47+
maybe_register_serializable(symbol)
48+
return symbol

keras_cv/bounding_box/converters.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import tensorflow as tf
1818

19+
from keras_cv.api_export import keras_cv_export
1920
from keras_cv.backend import keras
2021
from keras_cv.backend import ops
2122
from keras_cv.backend.scope import tf_data
@@ -298,6 +299,7 @@ def _xyxy_to_rel_yxyx(boxes, images=None, image_shape=None):
298299
}
299300

300301

302+
@keras_cv_export("keras_cv.bounding_box.convert_format")
301303
@tf_data
302304
def convert_format(
303305
boxes, source, target, images=None, image_shape=None, dtype="float32"

keras_cv/bounding_box/ensure_tensor.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from keras_cv.api_export import keras_cv_export
1516
from keras_cv.utils import preprocessing
1617

1718

19+
@keras_cv_export("keras_cv.bounding_box.ensure_tensor")
1820
def ensure_tensor(boxes, dtype=None):
1921
boxes = boxes.copy()
2022
for key in ["boxes", "classes", "confidence"]:

keras_cv/bounding_box/formats.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
formats.py contains axis information for each supported format.
1616
"""
1717

18+
from keras_cv.api_export import keras_cv_export
1819

20+
21+
@keras_cv_export("keras_cv.bounding_box.XYXY")
1922
class XYXY:
2023
"""XYXY contains axis indices for the XYXY format.
2124
@@ -35,6 +38,7 @@ class XYXY:
3538
BOTTOM = 3
3639

3740

41+
@keras_cv_export("keras_cv.bounding_box.REL_XYXY")
3842
class REL_XYXY:
3943
"""REL_XYXY contains axis indices for the REL_XYXY format.
4044
@@ -56,6 +60,7 @@ class REL_XYXY:
5660
BOTTOM = 3
5761

5862

63+
@keras_cv_export("keras_cv.bounding_box.CENTER_XYWH")
5964
class CENTER_XYWH:
6065
"""CENTER_XYWH contains axis indices for the CENTER_XYWH format.
6166
@@ -75,6 +80,7 @@ class CENTER_XYWH:
7580
HEIGHT = 3
7681

7782

83+
@keras_cv_export("keras_cv.bounding_box.XYWH")
7884
class XYWH:
7985
"""XYWH contains axis indices for the XYWH format.
8086
@@ -94,6 +100,7 @@ class XYWH:
94100
HEIGHT = 3
95101

96102

103+
@keras_cv_export("keras_cv.bounding_box.REL_XYWH")
97104
class REL_XYWH:
98105
"""REL_XYWH contains axis indices for the XYWH format.
99106
@@ -113,6 +120,7 @@ class REL_XYWH:
113120
HEIGHT = 3
114121

115122

123+
@keras_cv_export("keras_cv.bounding_box.YXYX")
116124
class YXYX:
117125
"""YXYX contains axis indices for the YXYX format.
118126
@@ -132,6 +140,7 @@ class YXYX:
132140
RIGHT = 3
133141

134142

143+
@keras_cv_export("keras_cv.bounding_box.REL_YXYX")
135144
class REL_YXYX:
136145
"""REL_YXYX contains axis indices for the REL_YXYX format.
137146

keras_cv/bounding_box/iou.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import math
1616

1717
from keras_cv import bounding_box
18+
from keras_cv.api_export import keras_cv_export
1819
from keras_cv.backend import keras
1920
from keras_cv.backend import ops
2021

@@ -60,6 +61,7 @@ def _compute_intersection(boxes1, boxes2):
6061
return intersect_height * intersect_width
6162

6263

64+
@keras_cv_export("keras_cv.bounding_box.compute_iou")
6365
def compute_iou(
6466
boxes1,
6567
boxes2,
@@ -170,6 +172,7 @@ def compute_iou(
170172
return iou_lookup_table
171173

172174

175+
@keras_cv_export("keras_cv.bounding_box.compute_ciou")
173176
def compute_ciou(box1, box2, bounding_box_format, eps=1e-7):
174177
"""
175178
Computes the Complete IoU (CIoU) between two bounding boxes or between

keras_cv/bounding_box/mask_invalid_detections.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
# limitations under the License.
1414

1515
from keras_cv import backend
16+
from keras_cv.api_export import keras_cv_export
1617
from keras_cv.backend import ops
1718
from keras_cv.bounding_box.to_ragged import to_ragged
1819
from keras_cv.bounding_box.validate_format import validate_format
1920

2021

22+
@keras_cv_export("keras_cv.bounding_box.mask_invalid_detections")
2123
def mask_invalid_detections(bounding_boxes, output_ragged=False):
2224
"""masks out invalid detections with -1s.
2325

keras_cv/bounding_box/to_dense.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import tensorflow as tf
1515

1616
import keras_cv.bounding_box.validate_format as validate_format
17+
from keras_cv.api_export import keras_cv_export
1718
from keras_cv.backend.scope import tf_data
1819

1920

@@ -36,6 +37,7 @@ def _classes_shape(batched, classes_shape, max_boxes):
3637
return [max_boxes] + classes_shape[2:]
3738

3839

40+
@keras_cv_export("keras_cv.bounding_box.to_dense")
3941
@tf_data
4042
def to_dense(bounding_boxes, max_boxes=None, default_value=-1):
4143
"""to_dense converts bounding boxes to Dense tensors

keras_cv/bounding_box/to_ragged.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515

1616
import keras_cv.bounding_box.validate_format as validate_format
1717
from keras_cv import backend
18+
from keras_cv.api_export import keras_cv_export
1819
from keras_cv.backend import keras
1920

2021

22+
@keras_cv_export("keras_cv.bounding_box.to_ragged")
2123
def to_ragged(bounding_boxes, sentinel=-1, dtype=tf.float32):
2224
"""converts a Dense padded bounding box `tf.Tensor` to a `tf.RaggedTensor`.
2325

keras_cv/bounding_box/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
"""Utility functions for working with bounding boxes."""
1515

1616
from keras_cv import bounding_box
17+
from keras_cv.api_export import keras_cv_export
1718
from keras_cv.backend import ops
1819
from keras_cv.bounding_box.formats import XYWH
1920

2021

22+
@keras_cv_export("keras_cv.bounding_box.is_relative")
2123
def is_relative(bounding_box_format):
2224
"""A util to check if a bounding box format uses relative coordinates"""
2325
if (
@@ -34,6 +36,7 @@ def is_relative(bounding_box_format):
3436
return bounding_box_format.startswith("rel")
3537

3638

39+
@keras_cv_export("keras_cv.bounding_box.as_relative")
3740
def as_relative(bounding_box_format):
3841
"""A util to get the relative equivalent of a provided bounding box format.
3942
@@ -61,8 +64,7 @@ def _relative_area(boxes, bounding_box_format):
6164
)
6265

6366

64-
# bounding_boxes is a dictionary with shape:
65-
# {"boxes": [None, None, 4], "mask": [None, None]}
67+
@keras_cv_export("keras_cv.bounding_box.clip_to_image")
6668
def clip_to_image(
6769
bounding_boxes, bounding_box_format, images=None, image_shape=None
6870
):

0 commit comments

Comments
 (0)