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

Update keras cv docs to the newest release #920

Merged
merged 20 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 19 additions & 13 deletions guides/ipynb/keras_cv/coco_metrics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,26 @@
"source": [
"## Input format\n",
"\n",
"KerasCV COCO metrics require a specific input format.\n",
"All KerasCV components that process bounding boxes, including COCO metrics, require a\n",
"`bounding_box_format` parameter. This parameter is used to tell the components what\n",
"format your bounding boxes are in. While this guide uses the `xyxy` format, a full\n",
"list of supported formats is available in\n",
"[the bounding_box API documentation](/api/keras_cv/bounding_box/formats).\n",
"\n",
"The metrics expect `y_true` and be a `float` Tensor with the shape `[batch,\n",
"num_images, num_boxes, 5]`. The final axis stores the locational and class\n",
"information for each specific bounding box. The dimensions in order are: `[left,\n",
"top, right, bottom, class]`.\n",
"\n",
"The metrics expect `y_pred` and be a `float` Tensor with the shape `[batch,\n",
"num_images, num_boxes, 56]`. The final axis stores the locational and class\n",
"information for each specific bounding box. The dimensions in order are: `[left,\n",
"top, right, bottom, class, confidence]`.\n",
"num_images, num_boxes, 5]`, with the ordering of last set of axes determined by the\n",
"provided format. The same is true of `y_pred`, except that an additional `confidence`\n",
"axis must be provided.\n",
"\n",
"Due to the fact that each image may have a different number of bounding boxes,\n",
"the `num_boxes` dimension may actually have a mismatching shape between images.\n",
"KerasCV works around this by allowing you to either pass a `RaggedTensor` as an\n",
"input to the KerasCV COCO metrics, or padding unused bounding boxes with `-1`.\n",
"\n",
"Utility functions to manipulate bounding boxes, transform between formats, and\n",
"pad bounding box Tensors with `-1s` are available at\n",
"[`keras_cv.bounding_box`](https://github.com/keras-team/keras-cv/blob/master/keras_cv/bounding_box)."
"pad bounding box Tensors with `-1s` are available from the\n",
"[`keras_cv.bounding_box`](https://github.com/keras-team/keras-cv/blob/master/keras_cv/bounding_box)\n",
"package."
]
},
{
Expand Down Expand Up @@ -102,7 +102,9 @@
"from tensorflow import keras\n",
"\n",
"# only consider boxes with areas less than a 32x32 square.\n",
"metric = keras_cv.metrics.COCORecall(class_ids=[1, 2, 3], area_range=(0, 32**2))"
"metric = keras_cv.metrics.COCORecall(\n",
" bounding_box_format=\"xyxy\", class_ids=[1, 2, 3], area_range=(0, 32**2)\n",
")"
]
},
{
Expand Down Expand Up @@ -254,7 +256,11 @@
"outputs": [],
"source": [
"recall = keras_cv.metrics.COCORecall(\n",
" max_detections=100, class_ids=[1], area_range=(0, 64**2), name=\"coco_recall\"\n",
" bounding_box_format=\"xyxy\",\n",
" max_detections=100,\n",
" class_ids=[1],\n",
" area_range=(0, 64**2),\n",
" name=\"coco_recall\",\n",
")\n",
"model.compile(metrics=[recall])"
]
Expand Down
5 changes: 5 additions & 0 deletions guides/ipynb/keras_cv/custom_image_augmentations.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,11 @@
"colab_type": "text"
},
"source": [
"Additionally, be sure to accept `**kwargs` to your `augment_*` methods to ensure\n",
"forwards compatibility. KerasCV will add additional label types in the future, and\n",
"if you do not include a `**kwargs` argument your augmentation layers will not be\n",
"forward compatible.\n",
"\n",
"## Conclusion and next steps\n",
"\n",
"KerasCV offers a standard set of APIs to streamline the process of implementing your\n",
Expand Down
47 changes: 19 additions & 28 deletions guides/ipynb/keras_cv/cut_mix_mix_up_and_rand_augment.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"pipelines for image classification and object detection tasks. KerasCV offers a wide\n",
"suite of preprocessing layers implementing common data augmentation techniques.\n",
"\n",
"Perhaps three of the most useful layers are `CutMix`, `MixUp`, and `RandAugment`. These\n",
"Perhaps three of the most useful layers are `keras_cv.layers.CutMix`,\n",
"`keras_cv.layers.MixUp`, and `keras_cv.layers.RandAugment`. These\n",
"layers are used in nearly all state-of-the-art image classification pipelines.\n",
"\n",
"This guide will show you how to compose these layers into your own data\n",
Expand Down Expand Up @@ -124,7 +125,7 @@
"num_classes = dataset_info.features[\"label\"].num_classes\n",
"\n",
"\n",
"def prepare(image, label):\n",
"def to_dict(image, label):\n",
" image = tf.image.resize(image, IMAGE_SIZE)\n",
" image = tf.cast(image, tf.float32)\n",
" label = tf.one_hot(label, num_classes)\n",
Expand All @@ -135,11 +136,11 @@
" if split == \"train\":\n",
" return (\n",
" dataset.shuffle(10 * BATCH_SIZE)\n",
" .map(prepare, num_parallel_calls=AUTOTUNE)\n",
" .map(to_dict, num_parallel_calls=AUTOTUNE)\n",
" .batch(BATCH_SIZE)\n",
" )\n",
" if split == \"test\":\n",
" return dataset.map(prepare, num_parallel_calls=AUTOTUNE).batch(BATCH_SIZE)\n",
" return dataset.map(to_dict, num_parallel_calls=AUTOTUNE).batch(BATCH_SIZE)\n",
"\n",
"\n",
"def load_dataset(split=\"train\"):\n",
Expand Down Expand Up @@ -305,11 +306,7 @@
"\n",
"In this example, we will use `CutMix` and `MixUp` independently in a manually created\n",
"preprocessing pipeline. In most state of the art pipelines images are randomly\n",
"augmented by either `CutMix`, `MixUp`, or neither. The function below implements this\n",
"in an equal 1/3 split.\n",
"\n",
"Note that our `cut_mix_and_mix_up` function is annotated with a `tf.function` to ensure\n",
"optimal performance."
"augmented by either `CutMix`, `MixUp`, or neither. The function below implements both."
]
},
{
Expand Down Expand Up @@ -354,7 +351,8 @@
"## Customizing your augmentation pipeline\n",
"\n",
"Perhaps you want to exclude an augmentation from `RandAugment`, or perhaps you want to\n",
"include the `GridMask()` as an option alongside the default `RandAugment` augmentations.\n",
"include the `keras_cv.layers.GridMask` as an option alongside the default `RandAugment`\n",
"augmentations.\n",
"\n",
"KerasCV allows you to construct production grade custom data augmentation pipelines using\n",
"the `keras_cv.layers.RandomAugmentationPipeline` layer. This class operates similarly to\n",
Expand Down Expand Up @@ -419,7 +417,7 @@
"colab_type": "text"
},
"source": [
"Next, let's add `GridMask` to our layers:"
"Next, let's add `keras_cv.layers.GridMask` to our layers:"
]
},
{
Expand Down Expand Up @@ -452,7 +450,13 @@
"source": [
"pipeline = keras_cv.layers.RandomAugmentationPipeline(\n",
" layers=layers, augmentations_per_image=3\n",
")"
")\n",
"\n",
"\n",
"def apply_pipeline(inputs):\n",
" inputs[\"images\"] = pipeline(inputs[\"images\"])\n",
" return inputs\n",
""
]
},
{
Expand All @@ -472,12 +476,6 @@
},
"outputs": [],
"source": [
"\n",
"def apply_pipeline(inputs):\n",
" inputs[\"images\"] = pipeline(inputs[\"images\"])\n",
" return inputs\n",
"\n",
"\n",
"train_dataset = load_dataset().map(apply_pipeline, num_parallel_calls=AUTOTUNE)\n",
"visualize_dataset(train_dataset, title=\"After custom pipeline\")"
]
Expand Down Expand Up @@ -523,11 +521,6 @@
},
"outputs": [],
"source": [
"\n",
"def apply_pipeline(inputs):\n",
" inputs[\"images\"] = pipeline(inputs[\"images\"])\n",
" return inputs\n",
"\n",
"\n",
"train_dataset = load_dataset().map(apply_pipeline, num_parallel_calls=AUTOTUNE)\n",
"visualize_dataset(train_dataset, title=\"After custom pipeline\")"
Expand Down Expand Up @@ -612,11 +605,9 @@
"\n",
"\n",
"def get_model():\n",
" inputs = keras.layers.Input(input_shape)\n",
" x = applications.ResNet50V2(\n",
" input_shape=input_shape, classes=num_classes, weights=None\n",
" )(inputs)\n",
" model = keras.Model(inputs, x)\n",
" model = keras_cv.models.DenseNet121(\n",
" include_rescaling=True, include_top=True, classes=num_classes\n",
" )\n",
" model.compile(\n",
" loss=losses.CategoricalCrossentropy(label_smoothing=0.1),\n",
" optimizer=optimizers.SGD(momentum=0.9),\n",
Expand Down
32 changes: 19 additions & 13 deletions guides/keras_cv/coco_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@
"""
## Input format

KerasCV COCO metrics require a specific input format.
All KerasCV components that process bounding boxes, including COCO metrics, require a
`bounding_box_format` parameter. This parameter is used to tell the components what
format your bounding boxes are in. While this guide uses the `xyxy` format, a full
list of supported formats is available in
[the bounding_box API documentation](/api/keras_cv/bounding_box/formats).

The metrics expect `y_true` and be a `float` Tensor with the shape `[batch,
num_images, num_boxes, 5]`. The final axis stores the locational and class
information for each specific bounding box. The dimensions in order are: `[left,
top, right, bottom, class]`.

The metrics expect `y_pred` and be a `float` Tensor with the shape `[batch,
num_images, num_boxes, 56]`. The final axis stores the locational and class
information for each specific bounding box. The dimensions in order are: `[left,
top, right, bottom, class, confidence]`.
num_images, num_boxes, 5]`, with the ordering of last set of axes determined by the
provided format. The same is true of `y_pred`, except that an additional `confidence`
axis must be provided.

Due to the fact that each image may have a different number of bounding boxes,
the `num_boxes` dimension may actually have a mismatching shape between images.
KerasCV works around this by allowing you to either pass a `RaggedTensor` as an
input to the KerasCV COCO metrics, or padding unused bounding boxes with `-1`.

Utility functions to manipulate bounding boxes, transform between formats, and
pad bounding box Tensors with `-1s` are available at
[`keras_cv.bounding_box`](https://github.com/keras-team/keras-cv/blob/master/keras_cv/bounding_box).
pad bounding box Tensors with `-1s` are available from the
[`keras_cv.bounding_box`](https://github.com/keras-team/keras-cv/blob/master/keras_cv/bounding_box)
package.

"""

Expand All @@ -67,7 +67,9 @@
from tensorflow import keras

# only consider boxes with areas less than a 32x32 square.
metric = keras_cv.metrics.COCORecall(class_ids=[1, 2, 3], area_range=(0, 32**2))
metric = keras_cv.metrics.COCORecall(
bounding_box_format="xyxy", class_ids=[1, 2, 3], area_range=(0, 32**2)
)

"""
2.) Create Some Bounding Boxes:
Expand Down Expand Up @@ -130,7 +132,11 @@
"""

recall = keras_cv.metrics.COCORecall(
max_detections=100, class_ids=[1], area_range=(0, 64**2), name="coco_recall"
bounding_box_format="xyxy",
max_detections=100,
class_ids=[1],
area_range=(0, 64**2),
name="coco_recall",
)
model.compile(metrics=[recall])

Expand Down
6 changes: 6 additions & 0 deletions guides/keras_cv/custom_image_augmentations.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,12 @@ def __init__(self, **kwargs):


"""

Additionally, be sure to accept `**kwargs` to your `augment_*` methods to ensure
forwards compatibility. KerasCV will add additional label types in the future, and
if you do not include a `**kwargs` argument your augmentation layers will not be
forward compatible.

## Conclusion and next steps

KerasCV offers a standard set of APIs to streamline the process of implementing your
Expand Down
35 changes: 15 additions & 20 deletions guides/keras_cv/cut_mix_mix_up_and_rand_augment.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
pipelines for image classification and object detection tasks. KerasCV offers a wide
suite of preprocessing layers implementing common data augmentation techniques.

Perhaps three of the most useful layers are `CutMix`, `MixUp`, and `RandAugment`. These
Perhaps three of the most useful layers are `keras_cv.layers.CutMix`,
`keras_cv.layers.MixUp`, and `keras_cv.layers.RandAugment`. These
layers are used in nearly all state-of-the-art image classification pipelines.

This guide will show you how to compose these layers into your own data
Expand Down Expand Up @@ -70,7 +71,7 @@
num_classes = dataset_info.features["label"].num_classes


def prepare(image, label):
def to_dict(image, label):
image = tf.image.resize(image, IMAGE_SIZE)
image = tf.cast(image, tf.float32)
label = tf.one_hot(label, num_classes)
Expand All @@ -81,11 +82,11 @@ def prepare_dataset(dataset, split):
if split == "train":
return (
dataset.shuffle(10 * BATCH_SIZE)
.map(prepare, num_parallel_calls=AUTOTUNE)
.map(to_dict, num_parallel_calls=AUTOTUNE)
.batch(BATCH_SIZE)
)
if split == "test":
return dataset.map(prepare, num_parallel_calls=AUTOTUNE).batch(BATCH_SIZE)
return dataset.map(to_dict, num_parallel_calls=AUTOTUNE).batch(BATCH_SIZE)


def load_dataset(split="train"):
Expand Down Expand Up @@ -214,7 +215,8 @@ def cut_mix_and_mix_up(samples):
## Customizing your augmentation pipeline

Perhaps you want to exclude an augmentation from `RandAugment`, or perhaps you want to
include the `GridMask()` as an option alongside the default `RandAugment` augmentations.
include the `keras_cv.layers.GridMask` as an option alongside the default `RandAugment`
augmentations.

KerasCV allows you to construct production grade custom data augmentation pipelines using
the `keras_cv.layers.RandomAugmentationPipeline` layer. This class operates similarly to
Expand Down Expand Up @@ -246,7 +248,7 @@ def cut_mix_and_mix_up(samples):
]

"""
Next, let's add `GridMask` to our layers:
Next, let's add `keras_cv.layers.GridMask` to our layers:
"""

layers = layers + [keras_cv.layers.GridMask()]
Expand All @@ -259,16 +261,16 @@ def cut_mix_and_mix_up(samples):
layers=layers, augmentations_per_image=3
)

"""
Let's check out the results!
"""


def apply_pipeline(inputs):
inputs["images"] = pipeline(inputs["images"])
return inputs


"""
Let's check out the results!
"""

train_dataset = load_dataset().map(apply_pipeline, num_parallel_calls=AUTOTUNE)
visualize_dataset(train_dataset, title="After custom pipeline")

Expand All @@ -287,11 +289,6 @@ def apply_pipeline(inputs):
"""


def apply_pipeline(inputs):
inputs["images"] = pipeline(inputs["images"])
return inputs


train_dataset = load_dataset().map(apply_pipeline, num_parallel_calls=AUTOTUNE)
visualize_dataset(train_dataset, title="After custom pipeline")

Expand Down Expand Up @@ -343,11 +340,9 @@ def preprocess_for_model(inputs):


def get_model():
inputs = keras.layers.Input(input_shape)
x = applications.ResNet50V2(
input_shape=input_shape, classes=num_classes, weights=None
)(inputs)
model = keras.Model(inputs, x)
model = keras_cv.models.DenseNet121(
include_rescaling=True, include_top=True, classes=num_classes
)
model.compile(
loss=losses.CategoricalCrossentropy(label_smoothing=0.1),
optimizer=optimizers.SGD(momentum=0.9),
Expand Down