Skip to content

Commit c4a6991

Browse files
author
Ian Johnson
authored
Rename num_classes to classes across entire repo (#657)
* Rename num_classes to classes * Fix find and replace mistake * Fix accidental fstring removal * Rename num_classes to classes for non-model uses * More find-and-replace fixes * One more f+r fix * Fix non_max_supression * Rename targets to class_predictions in non_max_supression
1 parent 0758c89 commit c4a6991

File tree

23 files changed

+139
-139
lines changed

23 files changed

+139
-139
lines changed

benchmarks/metrics/coco/mean_average_precision_bucket_performance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from keras_cv.metrics import coco
1313

1414

15-
def produce_random_data(include_confidence=False, num_images=128, num_classes=20):
15+
def produce_random_data(include_confidence=False, num_images=128, classes=20):
1616
"""Generates a fake list of bounding boxes for use in this test.
1717
1818
Returns:
@@ -23,9 +23,9 @@ def produce_random_data(include_confidence=False, num_images=128, num_classes=20
2323
images = []
2424
for _ in range(num_images):
2525
num_boxes = math.floor(25 * random.uniform(0, 1))
26-
classes = np.floor(np.random.rand(num_boxes, 1) * num_classes)
26+
classes_in_image = np.floor(np.random.rand(num_boxes, 1) * classes)
2727
bboxes = np.random.rand(num_boxes, 4)
28-
boxes = np.concatenate([bboxes, classes], axis=-1)
28+
boxes = np.concatenate([bboxes, classes_in_image], axis=-1)
2929
if include_confidence:
3030
confidence = np.random.rand(num_boxes, 1)
3131
boxes = np.concatenate([boxes, confidence], axis=-1)

benchmarks/metrics/coco/mean_average_precision_performance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from keras_cv.metrics import coco
1313

1414

15-
def produce_random_data(include_confidence=False, num_images=128, num_classes=20):
15+
def produce_random_data(include_confidence=False, num_images=128, classes=20):
1616
"""Generates a fake list of bounding boxes for use in this test.
1717
1818
Returns:
@@ -23,9 +23,9 @@ def produce_random_data(include_confidence=False, num_images=128, num_classes=20
2323
images = []
2424
for _ in range(num_images):
2525
num_boxes = math.floor(25 * random.uniform(0, 1))
26-
classes = np.floor(np.random.rand(num_boxes, 1) * num_classes)
26+
classes_in_image = np.floor(np.random.rand(num_boxes, 1) * classes)
2727
bboxes = np.random.rand(num_boxes, 4)
28-
boxes = np.concatenate([bboxes, classes], axis=-1)
28+
boxes = np.concatenate([bboxes, classes_in_image], axis=-1)
2929
if include_confidence:
3030
confidence = np.random.rand(num_boxes, 1)
3131
boxes = np.concatenate([boxes, confidence], axis=-1)

benchmarks/metrics/coco/recall_performance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from keras_cv.metrics import coco
1313

1414

15-
def produce_random_data(include_confidence=False, num_images=128, num_classes=20):
15+
def produce_random_data(include_confidence=False, num_images=128, classes=20):
1616
"""Generates a fake list of bounding boxes for use in this test.
1717
1818
Returns:
@@ -23,9 +23,9 @@ def produce_random_data(include_confidence=False, num_images=128, num_classes=20
2323
images = []
2424
for _ in range(num_images):
2525
num_boxes = math.floor(25 * random.uniform(0, 1))
26-
classes = np.floor(np.random.rand(num_boxes, 1) * num_classes)
26+
classes_in_image = np.floor(np.random.rand(num_boxes, 1) * classes)
2727
bboxes = np.random.rand(num_boxes, 4)
28-
boxes = np.concatenate([bboxes, classes], axis=-1)
28+
boxes = np.concatenate([bboxes, classes_in_image], axis=-1)
2929
if include_confidence:
3030
confidence = np.random.rand(num_boxes, 1)
3131
boxes = np.concatenate([boxes, confidence], axis=-1)

examples/layers/preprocessing/classification/demo_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
import tensorflow_datasets as tfds
1818

1919

20-
def resize(image, label, img_size=(224, 224), num_classes=10):
20+
def resize(image, label, img_size=(224, 224), classes=10):
2121
image = tf.image.resize(image, img_size)
22-
label = tf.one_hot(label, num_classes)
22+
label = tf.one_hot(label, classes)
2323
return {"images": image, "labels": label}
2424

2525

@@ -32,11 +32,11 @@ def load_oxford_dataset(
3232
# Load dataset.
3333
data, ds_info = tfds.load(name, as_supervised=as_supervised, with_info=True)
3434
train_ds = data["train"]
35-
num_classes = ds_info.features["label"].num_classes
35+
classes = ds_info.features["label"].num_classes
3636

3737
# Get tf dataset.
3838
train_ds = train_ds.map(
39-
lambda x, y: resize(x, y, img_size=img_size, num_classes=num_classes)
39+
lambda x, y: resize(x, y, img_size=img_size, classes=classes)
4040
).batch(batch_size)
4141
return train_ds
4242

examples/training/classification/densenet/imagenet/basic_training.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
FLAGS = flags.FLAGS
6262
FLAGS(sys.argv)
6363

64-
NUM_CLASSES = 1000
64+
CLASSES = 1000
6565
BATCH_SIZE = 256
6666
IMAGE_SIZE = (224, 224)
6767
EPOCHS = 250
@@ -94,7 +94,7 @@ def parse_imagenet_example(example):
9494

9595
# Decode label
9696
label = tf.cast(tf.reshape(parsed[label_key], shape=()), dtype=tf.int32) - 1
97-
label = tf.one_hot(label, NUM_CLASSES)
97+
label = tf.one_hot(label, CLASSES)
9898
return image, label
9999

100100

@@ -164,7 +164,7 @@ def augment(img, label):
164164
model = DenseNet121(
165165
include_rescaling=True,
166166
include_top=True,
167-
num_classes=NUM_CLASSES,
167+
classes=CLASSES,
168168
input_shape=IMAGE_SIZE + (3,),
169169
)
170170

keras_cv/layers/object_detection/non_max_suppression.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class NonMaxSuppression(tf.keras.layers.Layer):
2929
- [Yolo paper](https://arxiv.org/pdf/1506.02640)
3030
3131
Args:
32-
num_classes: an integer representing the number of classes that a bounding
32+
classes: an integer representing the number of classes that a bounding
3333
box can belong to.
3434
bounding_box_format: a case-insensitive string which is one of `"xyxy"`,
3535
`"rel_xyxy"`, `"xyWH"`, `"center_xyWH"`, `"yxyx"`, `"rel_yxyx"`. The
@@ -67,7 +67,7 @@ class NonMaxSuppression(tf.keras.layers.Layer):
6767
], dtype = np.float32)
6868
6969
nms = NonMaxSuppression(
70-
num_classes=8,
70+
classes=8,
7171
bounding_box_format="center_xyWH",
7272
iou_threshold=0.1
7373
)
@@ -78,7 +78,7 @@ class NonMaxSuppression(tf.keras.layers.Layer):
7878

7979
def __init__(
8080
self,
81-
num_classes,
81+
classes,
8282
bounding_box_format,
8383
confidence_threshold=0.05,
8484
iou_threshold=0.5,
@@ -87,7 +87,7 @@ def __init__(
8787
**kwargs,
8888
):
8989
super().__init__(**kwargs)
90-
self.num_classes = num_classes
90+
self.classes = classes
9191
self.bounding_box_format = bounding_box_format
9292
self.confidence_threshold = confidence_threshold
9393
self.iou_threshold = iou_threshold
@@ -112,11 +112,11 @@ def call(self, predictions, images=None):
112112

113113
# preparing the predictions for TF NMS op
114114
boxes = tf.expand_dims(predictions[..., :4], axis=2)
115-
classes = tf.cast(predictions[..., 4], tf.int32)
115+
class_predictions = tf.cast(predictions[..., 4], tf.int32)
116116
scores = predictions[..., 5]
117117

118-
classes = tf.one_hot(classes, self.num_classes)
119-
scores = tf.expand_dims(scores, axis=-1) * classes
118+
class_predictions = tf.one_hot(class_predictions, self.classes)
119+
scores = tf.expand_dims(scores, axis=-1) * class_predictions
120120

121121
# applying the NMS operation
122122
nmsed_boxes = tf.image.combined_non_max_suppression(
@@ -181,7 +181,7 @@ def _encode_to_ragged(self, boxes, valid_detections):
181181

182182
def get_config(self):
183183
config = {
184-
"num_classes": self.num_classes,
184+
"classes": self.classes,
185185
"bounding_box_format": self.bounding_box_format,
186186
"confidence_threshold": self.confidence_threshold,
187187
"iou_threshold": self.iou_threshold,

keras_cv/layers/object_detection/non_max_suppression_test.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
class NonMaxSuppressionTest(tf.test.TestCase):
2121
def test_return_shapes(self):
22-
layer = NonMaxSuppression(num_classes=4, bounding_box_format="xyWH")
22+
layer = NonMaxSuppression(classes=4, bounding_box_format="xyWH")
2323
images = tf.ones((3, 480, 480, 3))
2424

2525
boxes = tf.cast(tf.random.uniform((3, 5, 4), 0, 480, tf.int32), tf.float32)
@@ -32,7 +32,7 @@ def test_return_shapes(self):
3232
self.assertEqual(boxes.shape, [3, None, 6])
3333

3434
def test_non_square_images(self):
35-
layer = NonMaxSuppression(num_classes=4, bounding_box_format="xyxy")
35+
layer = NonMaxSuppression(classes=4, bounding_box_format="xyxy")
3636

3737
boxes = tf.cast(tf.random.uniform((2, 5, 4), 0, 480, tf.int32), tf.float32)
3838
classes = tf.cast(tf.random.uniform((2, 5, 1), 0, 4, tf.int32), tf.float32)
@@ -51,7 +51,7 @@ def test_non_square_images(self):
5151
self.assertEqual(boxes.shape, [2, None, 6])
5252

5353
def test_different_channels(self):
54-
layer = NonMaxSuppression(num_classes=4, bounding_box_format="xyWH")
54+
layer = NonMaxSuppression(classes=4, bounding_box_format="xyWH")
5555
images = tf.ones((3, 480, 480, 5))
5656

5757
boxes = tf.cast(tf.random.uniform((3, 5, 4), 0, 480, tf.int32), tf.float32)
@@ -66,7 +66,7 @@ def test_different_channels(self):
6666
def test_in_a_model(self):
6767
input1 = tf.keras.layers.Input([5, 6])
6868
input2 = tf.keras.layers.Input([480, 480, 3])
69-
layer = NonMaxSuppression(num_classes=4, bounding_box_format="xyWH")
69+
layer = NonMaxSuppression(classes=4, bounding_box_format="xyWH")
7070
outputs = layer(input1, input2)
7171

7272
model = tf.keras.models.Model(inputs=[input1, input2], outputs=outputs)
@@ -83,7 +83,7 @@ def test_in_a_model(self):
8383
self.assertEqual(boxes.shape, [3, None, 6])
8484

8585
def test_without_images(self):
86-
layer = NonMaxSuppression(num_classes=4, bounding_box_format="xyWH")
86+
layer = NonMaxSuppression(classes=4, bounding_box_format="xyWH")
8787

8888
boxes = tf.cast(tf.random.uniform((3, 5, 4), 0, 480, tf.int32), tf.float32)
8989
classes = tf.cast(tf.random.uniform((3, 5, 1), 0, 4, tf.int32), tf.float32)

keras_cv/layers/preprocessing/cut_mix_test.py

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

1616
from keras_cv.layers.preprocessing.cut_mix import CutMix
1717

18-
NUM_CLASSES = 10
18+
classes = 10
1919

2020

2121
class CutMixTest(tf.test.TestCase):
@@ -24,7 +24,7 @@ def test_return_shapes(self):
2424
# randomly sample labels
2525
ys = tf.random.categorical(tf.math.log([[0.5, 0.5]]), 2)
2626
ys = tf.squeeze(ys)
27-
ys = tf.one_hot(ys, NUM_CLASSES)
27+
ys = tf.one_hot(ys, classes)
2828

2929
layer = CutMix(seed=1)
3030
outputs = layer({"images": xs, "labels": ys})

keras_cv/layers/preprocessing/fourier_mix_test.py

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

1616
from keras_cv.layers.preprocessing.fourier_mix import FourierMix
1717

18-
NUM_CLASSES = 10
18+
classes = 10
1919

2020

2121
class FourierMixTest(tf.test.TestCase):
@@ -24,7 +24,7 @@ def test_return_shapes(self):
2424
# randomly sample labels
2525
ys = tf.random.categorical(tf.math.log([[0.5, 0.5]]), 2)
2626
ys = tf.squeeze(ys)
27-
ys = tf.one_hot(ys, NUM_CLASSES)
27+
ys = tf.one_hot(ys, classes)
2828

2929
layer = FourierMix()
3030
outputs = layer({"images": xs, "labels": ys})

keras_cv/layers/preprocessing/mix_up_test.py

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

1616
from keras_cv.layers.preprocessing.mix_up import MixUp
1717

18-
NUM_CLASSES = 10
18+
classes = 10
1919

2020

2121
class MixUpTest(tf.test.TestCase):
@@ -24,7 +24,7 @@ def test_return_shapes(self):
2424
# randomly sample labels
2525
ys_labels = tf.random.categorical(tf.math.log([[0.5, 0.5]]), 2)
2626
ys_labels = tf.squeeze(ys_labels)
27-
ys_labels = tf.one_hot(ys_labels, NUM_CLASSES)
27+
ys_labels = tf.one_hot(ys_labels, classes)
2828

2929
# randomly sample bounding boxes
3030
ys_bounding_boxes = tf.random.uniform((2, 3, 5), 0, 1)

0 commit comments

Comments
 (0)