Skip to content

Commit eaff91b

Browse files
sineelimattdangerw
authored andcommitted
Add remaining bbox utils (#1804)
* - Add formats, iou, utils for bounding box * - Add `AnchorGenerator`, `BoxMatcher` and `NonMaxSupression` layers * - Remove scope_name not required. * use default keras name scope * - Correct format error * - Remove layers as of now and keep them at model level till keras core supports them * - Correct api_gen
1 parent 7aba8e4 commit eaff91b

File tree

7 files changed

+1006
-0
lines changed

7 files changed

+1006
-0
lines changed

keras_nlp/api/bounding_box/__init__.py

+13
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@
1818
"""
1919

2020
from keras_nlp.src.bounding_box.converters import convert_format
21+
from keras_nlp.src.bounding_box.formats import CENTER_XYWH
22+
from keras_nlp.src.bounding_box.formats import REL_XYWH
23+
from keras_nlp.src.bounding_box.formats import REL_XYXY
24+
from keras_nlp.src.bounding_box.formats import REL_YXYX
25+
from keras_nlp.src.bounding_box.formats import XYWH
26+
from keras_nlp.src.bounding_box.formats import XYXY
27+
from keras_nlp.src.bounding_box.formats import YXYX
28+
from keras_nlp.src.bounding_box.iou import compute_ciou
29+
from keras_nlp.src.bounding_box.iou import compute_iou
2130
from keras_nlp.src.bounding_box.to_dense import to_dense
2231
from keras_nlp.src.bounding_box.to_ragged import to_ragged
32+
from keras_nlp.src.bounding_box.utils import as_relative
33+
from keras_nlp.src.bounding_box.utils import clip_boxes
34+
from keras_nlp.src.bounding_box.utils import clip_to_image
35+
from keras_nlp.src.bounding_box.utils import is_relative
2336
from keras_nlp.src.bounding_box.validate_format import validate_format

keras_nlp/src/bounding_box/formats.py

+162
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Copyright 2024 The KerasNLP 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+
formats.py contains axis information for each supported format.
16+
"""
17+
18+
from keras_nlp.src.api_export import keras_nlp_export
19+
20+
21+
@keras_nlp_export("keras_nlp.bounding_box.XYXY")
22+
class XYXY:
23+
"""XYXY contains axis indices for the XYXY format.
24+
25+
All values in the XYXY format should be absolute pixel values.
26+
27+
The XYXY format consists of the following required indices:
28+
29+
- LEFT: left of the bounding box
30+
- TOP: top of the bounding box
31+
- RIGHT: right of the bounding box
32+
- BOTTOM: bottom of the bounding box
33+
"""
34+
35+
LEFT = 0
36+
TOP = 1
37+
RIGHT = 2
38+
BOTTOM = 3
39+
40+
41+
@keras_nlp_export("keras_nlp.bounding_box.REL_XYXY")
42+
class REL_XYXY:
43+
"""REL_XYXY contains axis indices for the REL_XYXY format.
44+
45+
REL_XYXY is like XYXY, but each value is relative to the width and height of
46+
the origin image. Values are percentages of the origin images' width and
47+
height respectively.
48+
49+
The REL_XYXY format consists of the following required indices:
50+
51+
- LEFT: left of the bounding box
52+
- TOP: top of the bounding box
53+
- RIGHT: right of the bounding box
54+
- BOTTOM: bottom of the bounding box
55+
"""
56+
57+
LEFT = 0
58+
TOP = 1
59+
RIGHT = 2
60+
BOTTOM = 3
61+
62+
63+
@keras_nlp_export("keras_nlp.bounding_box.CENTER_XYWH")
64+
class CENTER_XYWH:
65+
"""CENTER_XYWH contains axis indices for the CENTER_XYWH format.
66+
67+
All values in the CENTER_XYWH format should be absolute pixel values.
68+
69+
The CENTER_XYWH format consists of the following required indices:
70+
71+
- X: X coordinate of the center of the bounding box
72+
- Y: Y coordinate of the center of the bounding box
73+
- WIDTH: width of the bounding box
74+
- HEIGHT: height of the bounding box
75+
"""
76+
77+
X = 0
78+
Y = 1
79+
WIDTH = 2
80+
HEIGHT = 3
81+
82+
83+
@keras_nlp_export("keras_nlp.bounding_box.XYWH")
84+
class XYWH:
85+
"""XYWH contains axis indices for the XYWH format.
86+
87+
All values in the XYWH format should be absolute pixel values.
88+
89+
The XYWH format consists of the following required indices:
90+
91+
- X: X coordinate of the left of the bounding box
92+
- Y: Y coordinate of the top of the bounding box
93+
- WIDTH: width of the bounding box
94+
- HEIGHT: height of the bounding box
95+
"""
96+
97+
X = 0
98+
Y = 1
99+
WIDTH = 2
100+
HEIGHT = 3
101+
102+
103+
@keras_nlp_export("keras_nlp.bounding_box.REL_XYWH")
104+
class REL_XYWH:
105+
"""REL_XYWH contains axis indices for the XYWH format.
106+
107+
REL_XYXY is like XYWH, but each value is relative to the width and height of
108+
the origin image. Values are percentages of the origin images' width and
109+
height respectively.
110+
111+
- X: X coordinate of the left of the bounding box
112+
- Y: Y coordinate of the top of the bounding box
113+
- WIDTH: width of the bounding box
114+
- HEIGHT: height of the bounding box
115+
"""
116+
117+
X = 0
118+
Y = 1
119+
WIDTH = 2
120+
HEIGHT = 3
121+
122+
123+
@keras_nlp_export("keras_nlp.bounding_box.YXYX")
124+
class YXYX:
125+
"""YXYX contains axis indices for the YXYX format.
126+
127+
All values in the YXYX format should be absolute pixel values.
128+
129+
The YXYX format consists of the following required indices:
130+
131+
- TOP: top of the bounding box
132+
- LEFT: left of the bounding box
133+
- BOTTOM: bottom of the bounding box
134+
- RIGHT: right of the bounding box
135+
"""
136+
137+
TOP = 0
138+
LEFT = 1
139+
BOTTOM = 2
140+
RIGHT = 3
141+
142+
143+
@keras_nlp_export("keras_nlp.bounding_box.REL_YXYX")
144+
class REL_YXYX:
145+
"""REL_YXYX contains axis indices for the REL_YXYX format.
146+
147+
REL_YXYX is like YXYX, but each value is relative to the width and height of
148+
the origin image. Values are percentages of the origin images' width and
149+
height respectively.
150+
151+
The REL_YXYX format consists of the following required indices:
152+
153+
- TOP: top of the bounding box
154+
- LEFT: left of the bounding box
155+
- BOTTOM: bottom of the bounding box
156+
- RIGHT: right of the bounding box
157+
"""
158+
159+
TOP = 0
160+
LEFT = 1
161+
BOTTOM = 2
162+
RIGHT = 3

0 commit comments

Comments
 (0)