Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global rename of classes to num_classes #1474

Merged
merged 18 commits into from
Mar 9, 2023
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ test_dataset = test_dataset.batch(16).map(
densenet = keras_cv.models.DenseNet121(
include_rescaling=True,
include_top=True,
classes=3
num_classes=3
)
densenet.compile(
loss='categorical_crossentropy',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
from keras_cv.metrics import coco


def produce_random_data(include_confidence=False, num_images=128, classes=20):
def produce_random_data(
include_confidence=False, num_images=128, num_classes=20
):
"""Generates a fake list of bounding boxes for use in this test.

Returns:
Expand All @@ -23,7 +25,7 @@ def produce_random_data(include_confidence=False, num_images=128, classes=20):
images = []
for _ in range(num_images):
num_boxes = math.floor(25 * random.uniform(0, 1))
classes_in_image = np.floor(np.random.rand(num_boxes, 1) * classes)
classes_in_image = np.floor(np.random.rand(num_boxes, 1) * num_classes)
bboxes = np.random.rand(num_boxes, 4)
boxes = np.concatenate([bboxes, classes_in_image], axis=-1)
if include_confidence:
Expand Down
6 changes: 4 additions & 2 deletions benchmarks/metrics/coco/mean_average_precision_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
from keras_cv.metrics import coco


def produce_random_data(include_confidence=False, num_images=128, classes=20):
def produce_random_data(
include_confidence=False, num_images=128, num_classes=20
):
"""Generates a fake list of bounding boxes for use in this test.

Returns:
Expand All @@ -23,7 +25,7 @@ def produce_random_data(include_confidence=False, num_images=128, classes=20):
images = []
for _ in range(num_images):
num_boxes = math.floor(25 * random.uniform(0, 1))
classes_in_image = np.floor(np.random.rand(num_boxes, 1) * classes)
classes_in_image = np.floor(np.random.rand(num_boxes, 1) * num_classes)
bboxes = np.random.rand(num_boxes, 4)
boxes = np.concatenate([bboxes, classes_in_image], axis=-1)
if include_confidence:
Expand Down
6 changes: 4 additions & 2 deletions benchmarks/metrics/coco/recall_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
from keras_cv.metrics import coco


def produce_random_data(include_confidence=False, num_images=128, classes=20):
def produce_random_data(
include_confidence=False, num_images=128, num_classes=20
):
"""Generates a fake list of bounding boxes for use in this test.

Returns:
Expand All @@ -23,7 +25,7 @@ def produce_random_data(include_confidence=False, num_images=128, classes=20):
images = []
for _ in range(num_images):
num_boxes = math.floor(25 * random.uniform(0, 1))
classes_in_image = np.floor(np.random.rand(num_boxes, 1) * classes)
classes_in_image = np.floor(np.random.rand(num_boxes, 1) * num_classes)
bboxes = np.random.rand(num_boxes, 4)
boxes = np.concatenate([bboxes, classes_in_image], axis=-1)
if include_confidence:
Expand Down
2 changes: 1 addition & 1 deletion examples/benchmarking/imagenet_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
model = model(
include_rescaling=FLAGS.include_rescaling,
include_top=True,
classes=1000,
num_classes=1000,
input_shape=(224, 224, 3),
weights=FLAGS.weights,
**eval(FLAGS.model_kwargs),
Expand Down
8 changes: 4 additions & 4 deletions examples/layers/preprocessing/classification/demo_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import tensorflow_datasets as tfds


def resize(image, label, img_size=(224, 224), classes=10):
def resize(image, label, img_size=(224, 224), num_classes=10):
image = tf.image.resize(image, img_size)
label = tf.one_hot(label, classes)
label = tf.one_hot(label, num_classes)
return {"images": image, "labels": label}


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

# Get tf dataset.
train_ds = train_ds.map(
lambda x, y: resize(x, y, img_size=img_size, classes=classes)
lambda x, y: resize(x, y, img_size=img_size, num_classes=num_classes)
).batch(batch_size)
return train_ds

Expand Down
4 changes: 2 additions & 2 deletions examples/training/classification/imagenet/basic_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
FLAGS = flags.FLAGS
FLAGS(sys.argv)

CLASSES = 1000
NUM_CLASSES = 1000
IMAGE_SIZE = (224, 224)
REDUCE_ON_PLATEAU = "ReduceOnPlateau"
COSINE_DECAY_WITH_WARMUP = "CosineDecayWithWarmup"
Expand Down Expand Up @@ -239,7 +239,7 @@ def augment(img, label):
model = model(
include_rescaling=True,
include_top=True,
classes=CLASSES,
num_classes=NUM_CLASSES,
input_shape=IMAGE_SIZE + (3,),
**eval(FLAGS.model_kwargs),
)
Expand Down
4 changes: 2 additions & 2 deletions examples/training/contrastive/imagenet/simclr_training.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
if FLAGS.model_name not in models.__dict__:
raise ValueError(f"Invalid model name: {FLAGS.model_name}")

CLASSES = 1000
NUM_CLASSES = 1000
IMAGE_SIZE = (224, 224)
EPOCHS = 250

Expand Down Expand Up @@ -102,7 +102,7 @@
augmenter=training.SimCLRAugmenter(
value_range=(0, 255), target_size=IMAGE_SIZE
),
probe=layers.Dense(CLASSES, name="linear_probe"),
probe=layers.Dense(NUM_CLASSES, name="linear_probe"),
)

optimizer = optimizers.SGD(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
# include_top=False, weights="imagenet", include_rescaling=False
# ).as_backbone()
model = keras_cv.models.FasterRCNN(
classes=20, bounding_box_format="yxyx", backbone=backbone
num_classes=20, bounding_box_format="yxyx", backbone=backbone
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ def pad_fn(images, bounding_boxes):
# ).as_backbone()
model = keras_cv.models.RetinaNet(
# number of classes to be used in box classification
classes=20,
num_classes=20,
# For more info on supported bounding box formats, visit
# https://keras.io/api/keras_cv/bounding_box/
bounding_box_format="xywh",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def proc_eval_fn(examples):
weights="imagenet",
)
model = models.__dict__[FLAGS.model_name]
model = model(classes=21, backbone=backbone, **eval(FLAGS.model_kwargs))
model = model(num_classes=21, backbone=backbone, **eval(FLAGS.model_kwargs))
optimizer = tf.keras.optimizers.SGD(
learning_rate=lr_decay, momentum=0.9, clipnorm=10.0
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _run_benchmark(self, app, strategy):

model = app(
include_top=True,
classes=self.num_classes,
num_classes=self.num_classes,
input_shape=(56, 56, 1),
include_rescaling=True,
)
Expand Down
4 changes: 2 additions & 2 deletions keras_cv/callbacks/pycoco_callback_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def cleanup_global_session(self):

def test_model_fit_retinanet(self):
model = keras_cv.models.RetinaNet(
classes=10,
num_classes=10,
bounding_box_format="xywh",
backbone=keras_cv.models.ResNet50V2Backbone().get_feature_extractor(),
)
Expand Down Expand Up @@ -61,7 +61,7 @@ def test_model_fit_retinanet(self):

def test_model_fit_rcnn(self):
model = keras_cv.models.FasterRCNN(
classes=10,
num_classes=10,
bounding_box_format="xywh",
backbone=keras_cv.models.ResNet50V2Backbone().get_feature_extractor(),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@


def decode_predictions_output_shapes():
classes = 10
predictions_shape = (8, 98208, 4 + classes)
num_classes = 10
predictions_shape = (8, 98208, 4 + num_classes)

predictions = tf.random.stateless_uniform(
shape=predictions_shape,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ class CenterNetLabelEncoder(tf.keras.layers.Layer):
min_radius: minimum Gasussian radius in each dimension in meters.
max_radius: maximum Gasussian radius in each dimension in meters.
spatial_size: the x, y, z boundary of voxels
classes: number of object classes.
num_classes: number of object classes.
top_k_heatmap: A sequence of integers, top k for each class. Can be None.
"""

Expand All @@ -341,7 +341,7 @@ def __init__(
min_radius: Sequence[float],
max_radius: Sequence[float],
spatial_size: Sequence[float],
classes: int,
num_classes: int,
top_k_heatmap: Sequence[int],
**kwargs,
):
Expand All @@ -350,7 +350,7 @@ def __init__(
self._min_radius = min_radius
self._max_radius = max_radius
self._spatial_size = spatial_size
self._classes = classes
self._num_classes = num_classes
self._top_k_heatmap = top_k_heatmap

def call(self, box_3d, box_classes, box_mask):
Expand Down Expand Up @@ -418,7 +418,7 @@ def call(self, box_3d, box_classes, box_mask):
heatmap_dict = {}
box_3d_dict = {}
top_k_heatmap_feature_idx_dict = {}
for i in range(self._classes):
for i in range(self._num_classes):
class_key = f"class_{i+1}"
# Object class is 1-indexed (0 is background).
dense_box_class_i = tf.cast(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def test_voxelization_output_shape_no_z(self):
min_radius=[0.8, 0.8, 0.0],
max_radius=[8.0, 8.0, 0.0],
spatial_size=[-20, 20, -20, 20, -20, 20],
classes=2,
num_classes=2,
top_k_heatmap=[10, 20],
)
box_3d = tf.random.uniform(
Expand All @@ -53,7 +53,7 @@ def test_voxelization_output_shape_with_z(self):
min_radius=[0.8, 0.8, 0.0],
max_radius=[8.0, 8.0, 0.0],
spatial_size=[-20, 20, -20, 20, -20, 20],
classes=2,
num_classes=2,
top_k_heatmap=[10, 20],
)
box_3d = tf.random.uniform(
Expand All @@ -79,7 +79,7 @@ def test_voxelization_output_shape_missing_topk(self):
min_radius=[0.8, 0.8, 0.0],
max_radius=[8.0, 8.0, 0.0],
spatial_size=[-20, 20, -20, 20, -20, 20],
classes=2,
num_classes=2,
top_k_heatmap=[10, 0],
)
box_3d = tf.random.uniform(
Expand Down
4 changes: 2 additions & 2 deletions keras_cv/layers/preprocessing/cut_mix_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from keras_cv.layers.preprocessing.cut_mix import CutMix

classes = 10
num_classes = 10


class CutMixTest(tf.test.TestCase):
Expand All @@ -24,7 +24,7 @@ def test_return_shapes(self):
# randomly sample labels
ys = tf.random.categorical(tf.math.log([[0.5, 0.5]]), 2)
ys = tf.squeeze(ys)
ys = tf.one_hot(ys, classes)
ys = tf.one_hot(ys, num_classes)

layer = CutMix(seed=1)
outputs = layer({"images": xs, "labels": ys})
Expand Down
4 changes: 2 additions & 2 deletions keras_cv/layers/preprocessing/fourier_mix_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from keras_cv.layers.preprocessing.fourier_mix import FourierMix

classes = 10
num_classes = 10


class FourierMixTest(tf.test.TestCase):
Expand All @@ -24,7 +24,7 @@ def test_return_shapes(self):
# randomly sample labels
ys = tf.random.categorical(tf.math.log([[0.5, 0.5]]), 2)
ys = tf.squeeze(ys)
ys = tf.one_hot(ys, classes)
ys = tf.one_hot(ys, num_classes)

layer = FourierMix()
outputs = layer({"images": xs, "labels": ys})
Expand Down
4 changes: 2 additions & 2 deletions keras_cv/layers/preprocessing/mix_up_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from keras_cv.layers.preprocessing.mix_up import MixUp

classes = 10
num_classes = 10


class MixUpTest(tf.test.TestCase):
Expand All @@ -24,7 +24,7 @@ def test_return_shapes(self):
# randomly sample labels
ys_labels = tf.random.categorical(tf.math.log([[0.5, 0.5]]), 2)
ys_labels = tf.squeeze(ys_labels)
ys_labels = tf.one_hot(ys_labels, classes)
ys_labels = tf.one_hot(ys_labels, num_classes)

# randomly sample bounding boxes
ys_bounding_boxes = {
Expand Down
4 changes: 2 additions & 2 deletions keras_cv/layers/preprocessing/mosaic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from keras_cv.layers.preprocessing.mosaic import Mosaic

classes = 10
num_classes = 10


class MosaicTest(tf.test.TestCase):
Expand All @@ -24,7 +24,7 @@ def test_return_shapes(self):
# randomly sample labels
ys_labels = tf.random.categorical(tf.math.log([[0.5, 0.5]]), 2)
ys_labels = tf.squeeze(ys_labels)
ys_labels = tf.one_hot(ys_labels, classes)
ys_labels = tf.one_hot(ys_labels, num_classes)

# randomly sample bounding boxes
ys_bounding_boxes = {
Expand Down
11 changes: 6 additions & 5 deletions keras_cv/layers/preprocessing/random_crop_and_resize_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ def test_crop_area_factor_errors(self, crop_area_factor):
)

def test_augment_sparse_segmentation_mask(self):
classes = 8
num_classes = 8

input_image_shape = (1, self.height, self.width, 3)
mask_shape = (1, self.height, self.width, 1)
image = tf.random.uniform(shape=input_image_shape, seed=self.seed)
mask = np.random.randint(2, size=mask_shape) * (classes - 1)
mask = np.random.randint(2, size=mask_shape) * (num_classes - 1)

inputs = {"images": image, "segmentation_masks": mask}

Expand Down Expand Up @@ -160,16 +160,17 @@ def test_augment_sparse_segmentation_mask(self):
self.assertAllInSet(output["segmentation_masks"], [0, 7])

def test_augment_one_hot_segmentation_mask(self):
classes = 8
num_classes = 8

input_image_shape = (1, self.height, self.width, 3)
mask_shape = (1, self.height, self.width, 1)
image = tf.random.uniform(shape=input_image_shape, seed=self.seed)
mask = tf.one_hot(
tf.squeeze(
np.random.randint(2, size=mask_shape) * (classes - 1), axis=-1
np.random.randint(2, size=mask_shape) * (num_classes - 1),
axis=-1,
),
classes,
num_classes,
)

inputs = {"images": image, "segmentation_masks": mask}
Expand Down
Loading