Skip to content

Commit 28e98c9

Browse files
authored
[api-polish] Renaming api's (#1267)
* API polish * Update * Update * Update * Typo fix * Updated * Update global_random_scaling_test.py * Updated * Update * Update
1 parent 660ae0f commit 28e98c9

File tree

7 files changed

+111
-113
lines changed

7 files changed

+111
-113
lines changed

keras_cv/layers/preprocessing_3d/frustum_random_dropping_points.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,16 @@ def __init__(self, r_distance, theta_width, phi_width, drop_rate=None, **kwargs)
5454
super().__init__(**kwargs)
5555

5656
if r_distance < 0:
57-
raise ValueError("r_distance must be >=0.")
57+
raise ValueError(f"r_distance must be >=0, but got r_distance={r_distance}")
5858
if theta_width < 0:
59-
raise ValueError("theta_width must be >=0.")
59+
raise ValueError(
60+
f"theta_width must be >=0, but got theta_width={theta_width}"
61+
)
6062
if phi_width < 0:
61-
raise ValueError("phi_width must be >=0.")
63+
raise ValueError(f"phi_width must be >=0, but got phi_width={phi_width}")
6264
drop_rate = drop_rate if drop_rate else 0.0
6365
if drop_rate > 1:
64-
raise ValueError("drop_rate must be <=1.")
66+
raise ValueError(f"drop_rate must be <=1, but got drop_rate={drop_rate}")
6567

6668
self._r_distance = r_distance
6769
self._theta_width = theta_width

keras_cv/layers/preprocessing_3d/global_random_scaling.py

Lines changed: 68 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -44,116 +44,110 @@ class GlobalRandomScaling(base_augmentation_layer_3d.BaseAugmentationLayer3D):
4444
A dictionary of Tensors with the same shape as input Tensors.
4545
4646
Arguments:
47-
scaling_factor_x: A tuple of float scalars or a float scalar sets the minimum and maximum scaling factors for the X axis.
48-
scaling_factor_y: A tuple of float scalars or a float scalar sets the minimum and maximum scaling factors for the Y axis.
49-
scaling_factor_z: A tuple of float scalars or a float scalar sets the minimum and maximum scaling factors for the Z axis.
47+
x_factor: A tuple of float scalars or a float scalar sets the minimum and maximum scaling factors for the X axis.
48+
y_factor: A tuple of float scalars or a float scalar sets the minimum and maximum scaling factors for the Y axis.
49+
z_factor: A tuple of float scalars or a float scalar sets the minimum and maximum scaling factors for the Z axis.
5050
"""
5151

5252
def __init__(
5353
self,
54-
scaling_factor_x=None,
55-
scaling_factor_y=None,
56-
scaling_factor_z=None,
57-
same_scaling_xyz=False,
54+
x_factor=None,
55+
y_factor=None,
56+
z_factor=None,
57+
preserve_aspect_ratio=False,
5858
**kwargs
5959
):
6060
super().__init__(**kwargs)
61-
if not scaling_factor_x:
62-
min_scaling_factor_x = 1.0
63-
max_scaling_factor_x = 1.0
64-
elif type(scaling_factor_x) is float:
65-
min_scaling_factor_x = scaling_factor_x
66-
max_scaling_factor_x = scaling_factor_x
61+
if not x_factor:
62+
min_x_factor = 1.0
63+
max_x_factor = 1.0
64+
elif type(x_factor) is float:
65+
min_x_factor = x_factor
66+
max_x_factor = x_factor
6767
else:
68-
min_scaling_factor_x = scaling_factor_x[0]
69-
max_scaling_factor_x = scaling_factor_x[1]
70-
if not scaling_factor_y:
71-
min_scaling_factor_y = 1.0
72-
max_scaling_factor_y = 1.0
73-
elif type(scaling_factor_y) is float:
74-
min_scaling_factor_y = scaling_factor_y
75-
max_scaling_factor_y = scaling_factor_y
68+
min_x_factor = x_factor[0]
69+
max_x_factor = x_factor[1]
70+
if not y_factor:
71+
min_y_factor = 1.0
72+
max_y_factor = 1.0
73+
elif type(y_factor) is float:
74+
min_y_factor = y_factor
75+
max_y_factor = y_factor
7676
else:
77-
min_scaling_factor_y = scaling_factor_y[0]
78-
max_scaling_factor_y = scaling_factor_y[1]
79-
if not scaling_factor_z:
80-
min_scaling_factor_z = 1.0
81-
max_scaling_factor_z = 1.0
82-
elif type(scaling_factor_z) is float:
83-
min_scaling_factor_z = scaling_factor_z
84-
max_scaling_factor_z = scaling_factor_z
77+
min_y_factor = y_factor[0]
78+
max_y_factor = y_factor[1]
79+
if not z_factor:
80+
min_z_factor = 1.0
81+
max_z_factor = 1.0
82+
elif type(z_factor) is float:
83+
min_z_factor = z_factor
84+
max_z_factor = z_factor
8585
else:
86-
min_scaling_factor_z = scaling_factor_z[0]
87-
max_scaling_factor_z = scaling_factor_z[1]
86+
min_z_factor = z_factor[0]
87+
max_z_factor = z_factor[1]
8888

8989
if (
90-
min_scaling_factor_x < 0
91-
or max_scaling_factor_x < 0
92-
or min_scaling_factor_y < 0
93-
or max_scaling_factor_y < 0
94-
or min_scaling_factor_z < 0
95-
or max_scaling_factor_z < 0
90+
min_x_factor < 0
91+
or max_x_factor < 0
92+
or min_y_factor < 0
93+
or max_y_factor < 0
94+
or min_z_factor < 0
95+
or max_z_factor < 0
9696
):
97-
raise ValueError("min_scaling_factor and max_scaling_factor must be >=0.")
97+
raise ValueError("min_factor and max_factor must be >=0.")
9898
if (
99-
min_scaling_factor_x > max_scaling_factor_x
100-
or min_scaling_factor_y > max_scaling_factor_y
101-
or min_scaling_factor_z > max_scaling_factor_z
99+
min_x_factor > max_x_factor
100+
or min_y_factor > max_y_factor
101+
or min_z_factor > max_z_factor
102102
):
103-
raise ValueError("min_scaling_factor must be less than max_scaling_factor.")
104-
if same_scaling_xyz:
105-
if (
106-
min_scaling_factor_x != min_scaling_factor_y
107-
or min_scaling_factor_y != min_scaling_factor_z
108-
):
103+
raise ValueError("min_factor must be less than max_factor.")
104+
if preserve_aspect_ratio:
105+
if min_x_factor != min_y_factor or min_y_factor != min_z_factor:
109106
raise ValueError(
110-
"min_scaling_factor must be the same when same_scaling_xyz is true."
107+
"min_factor must be the same when preserve_aspect_ratio is true."
111108
)
112-
if (
113-
max_scaling_factor_x != max_scaling_factor_y
114-
or max_scaling_factor_y != max_scaling_factor_z
115-
):
109+
if max_x_factor != max_y_factor or max_y_factor != max_z_factor:
116110
raise ValueError(
117-
"max_scaling_factor must be the same when same_scaling_xyz is true."
111+
"max_factor must be the same when preserve_aspect_ratio is true."
118112
)
119113

120-
self._min_scaling_factor_x = min_scaling_factor_x
121-
self._max_scaling_factor_x = max_scaling_factor_x
122-
self._min_scaling_factor_y = min_scaling_factor_y
123-
self._max_scaling_factor_y = max_scaling_factor_y
124-
self._min_scaling_factor_z = min_scaling_factor_z
125-
self._max_scaling_factor_z = max_scaling_factor_z
126-
self._same_scaling_xyz = same_scaling_xyz
114+
self._min_x_factor = min_x_factor
115+
self._max_x_factor = max_x_factor
116+
self._min_y_factor = min_y_factor
117+
self._max_y_factor = max_y_factor
118+
self._min_z_factor = min_z_factor
119+
self._max_z_factor = max_z_factor
120+
self._preserve_aspect_ratio = preserve_aspect_ratio
127121

128122
def get_config(self):
129123
return {
130-
"scaling_factor_x": (
131-
self._min_scaling_factor_x,
132-
self._max_scaling_factor_x,
124+
"x_factor": (
125+
self._min_x_factor,
126+
self._max_x_factor,
133127
),
134-
"scaling_factor_y": (
135-
self._min_scaling_factor_y,
136-
self._max_scaling_factor_y,
128+
"y_factor": (
129+
self._min_y_factor,
130+
self._max_y_factor,
137131
),
138-
"scaling_factor_z": (
139-
self._min_scaling_factor_z,
140-
self._max_scaling_factor_z,
132+
"z_factor": (
133+
self._min_z_factor,
134+
self._max_z_factor,
141135
),
142-
"same_scaling_xyz": self._same_scaling_xyz,
136+
"preserve_aspect_ratio": self._preserve_aspect_ratio,
143137
}
144138

145139
def get_random_transformation(self, **kwargs):
146140

147141
random_scaling_x = self._random_generator.random_uniform(
148-
(), minval=self._min_scaling_factor_x, maxval=self._max_scaling_factor_x
142+
(), minval=self._min_x_factor, maxval=self._max_x_factor
149143
)
150144
random_scaling_y = self._random_generator.random_uniform(
151-
(), minval=self._min_scaling_factor_y, maxval=self._max_scaling_factor_y
145+
(), minval=self._min_y_factor, maxval=self._max_y_factor
152146
)
153147
random_scaling_z = self._random_generator.random_uniform(
154-
(), minval=self._min_scaling_factor_z, maxval=self._max_scaling_factor_z
148+
(), minval=self._min_z_factor, maxval=self._max_z_factor
155149
)
156-
if not self._same_scaling_xyz:
150+
if not self._preserve_aspect_ratio:
157151
return {
158152
"scale": tf.stack(
159153
[random_scaling_x, random_scaling_y, random_scaling_z]

keras_cv/layers/preprocessing_3d/global_random_scaling_test.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
class GlobalScalingTest(tf.test.TestCase):
2525
def test_augment_point_clouds_and_bounding_boxes(self):
2626
add_layer = GlobalRandomScaling(
27-
scaling_factor_x=(0.5, 1.5),
28-
scaling_factor_y=(0.5, 1.5),
29-
scaling_factor_z=(0.5, 1.5),
27+
x_factor=(0.5, 1.5),
28+
y_factor=(0.5, 1.5),
29+
z_factor=(0.5, 1.5),
3030
)
3131
point_clouds = np.random.random(size=(2, 50, 10)).astype("float32")
3232
bounding_boxes = np.random.random(size=(2, 10, 7)).astype("float32")
@@ -36,10 +36,10 @@ def test_augment_point_clouds_and_bounding_boxes(self):
3636

3737
def test_augment_point_clouds_and_bounding_boxes_with_same_scaling(self):
3838
add_layer = GlobalRandomScaling(
39-
scaling_factor_x=(0.5, 1.5),
40-
scaling_factor_y=(0.5, 1.5),
41-
scaling_factor_z=(0.5, 1.5),
42-
same_scaling_xyz=True,
39+
x_factor=(0.5, 1.5),
40+
y_factor=(0.5, 1.5),
41+
z_factor=(0.5, 1.5),
42+
preserve_aspect_ratio=True,
4343
)
4444
point_clouds = np.random.random(size=(2, 50, 10)).astype("float32")
4545
bounding_boxes = np.random.random(size=(2, 10, 7)).astype("float32")
@@ -49,9 +49,9 @@ def test_augment_point_clouds_and_bounding_boxes_with_same_scaling(self):
4949

5050
def test_not_augment_point_clouds_and_bounding_boxes(self):
5151
add_layer = GlobalRandomScaling(
52-
scaling_factor_x=(1.0, 1.0),
53-
scaling_factor_y=(1.0, 1.0),
54-
scaling_factor_z=(1.0, 1.0),
52+
x_factor=(1.0, 1.0),
53+
y_factor=(1.0, 1.0),
54+
z_factor=(1.0, 1.0),
5555
)
5656
point_clouds = np.random.random(size=(2, 50, 10)).astype("float32")
5757
bounding_boxes = np.random.random(size=(2, 10, 7)).astype("float32")
@@ -61,9 +61,9 @@ def test_not_augment_point_clouds_and_bounding_boxes(self):
6161

6262
def test_2x_scaling_point_clouds_and_bounding_boxes(self):
6363
add_layer = GlobalRandomScaling(
64-
scaling_factor_x=(2.0, 2.0),
65-
scaling_factor_y=(2.0, 2.0),
66-
scaling_factor_z=(2.0, 2.0),
64+
x_factor=(2.0, 2.0),
65+
y_factor=(2.0, 2.0),
66+
z_factor=(2.0, 2.0),
6767
)
6868
point_clouds = np.array([[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]] * 2] * 2).astype(
6969
"float32"
@@ -82,9 +82,9 @@ def test_2x_scaling_point_clouds_and_bounding_boxes(self):
8282

8383
def test_augment_batch_point_clouds_and_bounding_boxes(self):
8484
add_layer = GlobalRandomScaling(
85-
scaling_factor_x=(0.5, 1.5),
86-
scaling_factor_y=(0.5, 1.5),
87-
scaling_factor_z=(0.5, 1.5),
85+
x_factor=(0.5, 1.5),
86+
y_factor=(0.5, 1.5),
87+
z_factor=(0.5, 1.5),
8888
)
8989
point_clouds = np.random.random(size=(3, 2, 50, 10)).astype("float32")
9090
bounding_boxes = np.random.random(size=(3, 2, 10, 7)).astype("float32")
@@ -94,9 +94,9 @@ def test_augment_batch_point_clouds_and_bounding_boxes(self):
9494

9595
def test_not_augment_batch_point_clouds_and_bounding_boxes(self):
9696
add_layer = GlobalRandomScaling(
97-
scaling_factor_x=(1.0, 1.0),
98-
scaling_factor_y=(1.0, 1.0),
99-
scaling_factor_z=(1.0, 1.0),
97+
x_factor=(1.0, 1.0),
98+
y_factor=(1.0, 1.0),
99+
z_factor=(1.0, 1.0),
100100
)
101101
point_clouds = np.random.random(size=(3, 2, 50, 10)).astype("float32")
102102
bounding_boxes = np.random.random(size=(3, 2, 10, 7)).astype("float32")

keras_cv/layers/serialization_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,10 @@ class SerializationTest(tf.test.TestCase, parameterized.TestCase):
361361
"GlobalRandomScaling",
362362
cv_layers.GlobalRandomScaling,
363363
{
364-
"scaling_factor_x": (0.2, 1.0),
365-
"scaling_factor_y": (0.3, 1.1),
366-
"scaling_factor_z": (0.4, 1.3),
367-
"same_scaling_xyz": False,
364+
"x_factor": (0.2, 1.0),
365+
"y_factor": (0.3, 1.1),
366+
"z_factor": (0.4, 1.3),
367+
"preserve_aspect_ratio": False,
368368
},
369369
),
370370
(

keras_cv/losses/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
from keras_cv.losses.focal import FocalLoss
1616
from keras_cv.losses.giou_loss import GIoULoss
1717
from keras_cv.losses.iou_loss import IoULoss
18-
from keras_cv.losses.penalty_reduced_focal_loss import BinaryPenaltyReducedFocalXent
18+
from keras_cv.losses.penalty_reduced_focal_loss import (
19+
BinaryPenaltyReducedFocalCrossEntropy,
20+
)
1921
from keras_cv.losses.simclr_loss import SimCLRLoss
2022
from keras_cv.losses.smooth_l1 import SmoothL1Loss

keras_cv/losses/penalty_reduced_focal_loss.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
# TODO(tanzhenyu): consider inherit from LossFunctionWrapper to
1919
# get the dimension squeeze.
2020
@tf.keras.utils.register_keras_serializable(package="keras_cv")
21-
class BinaryPenaltyReducedFocalXent(tf.keras.losses.Loss):
21+
class BinaryPenaltyReducedFocalCrossEntropy(tf.keras.losses.Loss):
2222
"""Implements CenterNet modified Focal loss.
2323
2424
Compared with `keras.losses.BinaryFocalCrossentropy`, this loss discounts for negative
@@ -56,7 +56,7 @@ def __init__(
5656
positive_weight=1.0,
5757
negative_weight=1.0,
5858
reduction=tf.keras.losses.Reduction.AUTO,
59-
name="binary_penalty_reduced_focal_xent",
59+
name="binary_penalty_reduced_focal_cross_entropy",
6060
):
6161
super().__init__(reduction=reduction, name=name)
6262
self.alpha = alpha

0 commit comments

Comments
 (0)