Skip to content

Commit

Permalink
KerasCV touchup (keras-team#878)
Browse files Browse the repository at this point in the history
* KerasCV touchup

* Add KerasCV to navbar

* Update keras cv index

* Update index.md

* Update paths

* Update link

Co-authored-by: François Chollet <francois.chollet@gmail.com>
  • Loading branch information
LukeWood and fchollet authored May 18, 2022
1 parent 02c8525 commit da6a4bc
Show file tree
Hide file tree
Showing 22 changed files with 60 additions and 65 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion guides/ipynb/keras_cv/coco_metrics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"\n",
"Utility functions to manipulate bounding boxes, transform between formats, and\n",
"pad bounding box Tensors with `-1s` are available at\n",
"[`keras_cv.utils.bounding_box`](https://github.com/keras-team/keras-cv/blob/master/keras_cv/utils/bounding_box.py)."
"[`keras_cv.bounding_box`](https://github.com/keras-team/keras-cv/blob/master/keras_cv/bounding_box)."
]
},
{
Expand Down
21 changes: 9 additions & 12 deletions guides/ipynb/keras_cv/custom_image_augmentations.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@
"from tensorflow import keras\n",
"import keras_cv\n",
"from tensorflow.keras import layers\n",
"from keras_cv.utils import preprocessing as preprocessing_utils\n",
"from keras_cv import utils\n",
"from keras_cv.layers import BaseImageAugmentationLayer\n",
"import matplotlib.pyplot as plt\n",
""
"\n",
"tf.autograph.set_verbosity(0)"
]
},
{
Expand Down Expand Up @@ -265,7 +266,7 @@
"\n",
" def __init__(self, factor, **kwargs):\n",
" super().__init__(**kwargs)\n",
" self.factor = preprocessing_utils.parse_factor(factor)\n",
" self.factor = utils.parse_factor(factor)\n",
"\n",
" def augment_image(self, image, transformation=None):\n",
" [*others, blue] = tf.unstack(image, axis=-1)\n",
Expand Down Expand Up @@ -382,7 +383,7 @@
"\n",
" def __init__(self, factor, **kwargs):\n",
" super().__init__(**kwargs)\n",
" self.factor = preprocessing_utils.parse_factor(factor)\n",
" self.factor = utils.parse_factor(factor)\n",
"\n",
" def get_random_transformation(self, **kwargs):\n",
" # kwargs holds {\"images\": image, \"labels\": label, etc...}\n",
Expand Down Expand Up @@ -546,22 +547,18 @@
" def __init__(self, value_range, factor, **kwargs):\n",
" super().__init__(**kwargs)\n",
" self.value_range = value_range\n",
" self.factor = preprocessing_utils.parse_factor(factor)\n",
" self.factor = utils.parse_factor(factor)\n",
"\n",
" def get_random_transformation(self, **kwargs):\n",
" # kwargs holds {\"images\": image, \"labels\": label, etc...}\n",
" return self.factor() * 255\n",
"\n",
" def augment_image(self, image, transformation=None, **kwargs):\n",
" image = preprocessing_utils.transform_value_range(\n",
" image, self.value_range, (0, 255)\n",
" )\n",
" image = utils.transform_value_range(image, self.value_range, (0, 255))\n",
" [*others, blue] = tf.unstack(image, axis=-1)\n",
" blue = tf.clip_by_value(blue + transformation, 0.0, 255.0)\n",
" result = tf.stack([*others, blue], axis=-1)\n",
" result = preprocessing_utils.transform_value_range(\n",
" result, (0, 255), self.value_range\n",
" )\n",
" result = utils.transform_value_range(result, (0, 255), self.value_range)\n",
" return result\n",
"\n",
" def augment_label(self, label, transformation=None, **kwargs):\n",
Expand Down Expand Up @@ -685,7 +682,7 @@
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "writing_custom_image_augmentations_with_baseimageaugmentationlayer",
"name": "custom_image_augmentations",
"private_outputs": false,
"provenance": [],
"toc_visible": true
Expand Down
2 changes: 1 addition & 1 deletion guides/keras_cv/coco_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
Utility functions to manipulate bounding boxes, transform between formats, and
pad bounding box Tensors with `-1s` are available at
[`keras_cv.utils.bounding_box`](https://github.com/keras-team/keras-cv/blob/master/keras_cv/utils/bounding_box.py).
[`keras_cv.bounding_box`](https://github.com/keras-team/keras-cv/blob/master/keras_cv/bounding_box).
"""

Expand Down
18 changes: 7 additions & 11 deletions guides/keras_cv/custom_image_augmentations.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
from tensorflow import keras
import keras_cv
from tensorflow.keras import layers
from keras_cv.utils import preprocessing as preprocessing_utils
from keras_cv import utils
from keras_cv.layers import BaseImageAugmentationLayer
import matplotlib.pyplot as plt


tf.autograph.set_verbosity(0)
"""
First, let's implement some helper functions to visualize intermediate results
"""
Expand Down Expand Up @@ -155,7 +155,7 @@ class RandomBlueTint(keras_cv.layers.BaseImageAugmentationLayer):

def __init__(self, factor, **kwargs):
super().__init__(**kwargs)
self.factor = preprocessing_utils.parse_factor(factor)
self.factor = utils.parse_factor(factor)

def augment_image(self, image, transformation=None):
[*others, blue] = tf.unstack(image, axis=-1)
Expand Down Expand Up @@ -231,7 +231,7 @@ class RandomBlueTint(keras_cv.layers.BaseImageAugmentationLayer):

def __init__(self, factor, **kwargs):
super().__init__(**kwargs)
self.factor = preprocessing_utils.parse_factor(factor)
self.factor = utils.parse_factor(factor)

def get_random_transformation(self, **kwargs):
# kwargs holds {"images": image, "labels": label, etc...}
Expand Down Expand Up @@ -339,22 +339,18 @@ class RandomBlueTint(keras_cv.layers.BaseImageAugmentationLayer):
def __init__(self, value_range, factor, **kwargs):
super().__init__(**kwargs)
self.value_range = value_range
self.factor = preprocessing_utils.parse_factor(factor)
self.factor = utils.parse_factor(factor)

def get_random_transformation(self, **kwargs):
# kwargs holds {"images": image, "labels": label, etc...}
return self.factor() * 255

def augment_image(self, image, transformation=None, **kwargs):
image = preprocessing_utils.transform_value_range(
image, self.value_range, (0, 255)
)
image = utils.transform_value_range(image, self.value_range, (0, 255))
[*others, blue] = tf.unstack(image, axis=-1)
blue = tf.clip_by_value(blue + transformation, 0.0, 255.0)
result = tf.stack([*others, blue], axis=-1)
result = preprocessing_utils.transform_value_range(
result, (0, 255), self.value_range
)
result = utils.transform_value_range(result, (0, 255), self.value_range)
return result

def augment_label(self, label, transformation=None, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions guides/md/keras_cv/coco_metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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.utils.bounding_box`](https://github.com/keras-team/keras-cv/blob/master/keras_cv/utils/bounding_box.py).
[`keras_cv.bounding_box`](https://github.com/keras-team/keras-cv/blob/master/keras_cv/bounding_box).

---
## Independent metric use
Expand Down Expand Up @@ -158,7 +158,7 @@ model.evaluate(y_pred, y_true, return_dict=True)

<div class="k-default-codeblock">
```
1/1 [==============================] - 1s 615ms/step - loss: 0.0000e+00 - coco_recall: 1.0000
1/1 [==============================] - 1s 822ms/step - loss: 0.0000e+00 - coco_recall: 1.0000
{'loss': 0.0, 'coco_recall': 1.0}
Expand Down
48 changes: 23 additions & 25 deletions guides/md/keras_cv/custom_image_augmentations.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ import tensorflow as tf
from tensorflow import keras
import keras_cv
from tensorflow.keras import layers
from keras_cv.utils import preprocessing as preprocessing_utils
from keras_cv import utils
from keras_cv.layers import BaseImageAugmentationLayer
import matplotlib.pyplot as plt

tf.autograph.set_verbosity(0)
```

First, let's implement some helper functions to visualize intermediate results
Expand Down Expand Up @@ -120,9 +121,9 @@ imshow(elephants)
```



![png](/img/guides/custom_image_augmentations/custom_image_augmentations_9_0.png)



Next, let's augment it and visualize the result:
Expand All @@ -134,9 +135,10 @@ augmented = layer(elephants)
imshow(augmented.numpy())
```


![png](/img/guides/custom_image_augmentations/custom_image_augmentations_11_1.png)



![png](/img/guides/custom_image_augmentations/custom_image_augmentations_11_0.png)



Looks great! We can also call our layer on batched inputs:
Expand All @@ -149,9 +151,9 @@ imshow(augmented.numpy()[0])
```



![png](/img/guides/custom_image_augmentations/custom_image_augmentations_13_0.png)



---
Expand Down Expand Up @@ -184,7 +186,7 @@ class RandomBlueTint(keras_cv.layers.BaseImageAugmentationLayer):

def __init__(self, factor, **kwargs):
super().__init__(**kwargs)
self.factor = preprocessing_utils.parse_factor(factor)
self.factor = utils.parse_factor(factor)

def augment_image(self, image, transformation=None):
[*others, blue] = tf.unstack(image, axis=-1)
Expand All @@ -206,9 +208,9 @@ gallery_show(augmented.numpy())
```



![png](/img/guides/custom_image_augmentations/custom_image_augmentations_17_0.png)



Each image is augmented differently with a random factor sampled from the range
Expand All @@ -228,9 +230,9 @@ gallery_show(augmented.numpy())
```



![png](/img/guides/custom_image_augmentations/custom_image_augmentations_19_0.png)



As you can see, the augmentations now are drawn from a normal distributions.
Expand Down Expand Up @@ -275,7 +277,7 @@ class RandomBlueTint(keras_cv.layers.BaseImageAugmentationLayer):

def __init__(self, factor, **kwargs):
super().__init__(**kwargs)
self.factor = preprocessing_utils.parse_factor(factor)
self.factor = utils.parse_factor(factor)

def get_random_transformation(self, **kwargs):
# kwargs holds {"images": image, "labels": label, etc...}
Expand Down Expand Up @@ -371,9 +373,9 @@ min and max after augmentation: 0.0 26.488235
```
</div>

![png](/img/guides/custom_image_augmentations/custom_image_augmentations_27_2.png)



Note that this is an incredibly weak augmentation!
Expand Down Expand Up @@ -408,22 +410,18 @@ class RandomBlueTint(keras_cv.layers.BaseImageAugmentationLayer):
def __init__(self, value_range, factor, **kwargs):
super().__init__(**kwargs)
self.value_range = value_range
self.factor = preprocessing_utils.parse_factor(factor)
self.factor = utils.parse_factor(factor)

def get_random_transformation(self, **kwargs):
# kwargs holds {"images": image, "labels": label, etc...}
return self.factor() * 255

def augment_image(self, image, transformation=None, **kwargs):
image = preprocessing_utils.transform_value_range(
image, self.value_range, (0, 255)
)
image = utils.transform_value_range(image, self.value_range, (0, 255))
[*others, blue] = tf.unstack(image, axis=-1)
blue = tf.clip_by_value(blue + transformation, 0.0, 255.0)
result = tf.stack([*others, blue], axis=-1)
result = preprocessing_utils.transform_value_range(
result, (0, 255), self.value_range
)
result = utils.transform_value_range(result, (0, 255), self.value_range)
return result

def augment_label(self, label, transformation=None, **kwargs):
Expand Down Expand Up @@ -460,9 +458,9 @@ min and max after augmentation: 0.0 1.0
```
</div>

![png](/img/guides/custom_image_augmentations/custom_image_augmentations_29_1.png)



Now our elephants are only slgihtly blue tinted. This is the expected behavior when
Expand Down
22 changes: 11 additions & 11 deletions guides/md/keras_cv/cut_mix_mix_up_and_rand_augment.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ visualize_dataset(train_dataset, title="Before Augmentation")
```



![png](/img/guides/cut_mix_mix_up_and_rand_augment/cut_mix_mix_up_and_rand_augment_9_0.png)



Great! Now we can move onto the augmentation step.
Expand Down Expand Up @@ -182,9 +182,9 @@ visualize_dataset(train_dataset, title="After RandAugment")
```



![png](/img/guides/cut_mix_mix_up_and_rand_augment/cut_mix_mix_up_and_rand_augment_15_0.png)



Try tweaking the magnitude settings to see a wider variety of results.
Expand Down Expand Up @@ -230,9 +230,9 @@ visualize_dataset(train_dataset, title="After CutMix and MixUp")
```



![png](/img/guides/cut_mix_mix_up_and_rand_augment/cut_mix_mix_up_and_rand_augment_18_0.png)



Great! Looks like we have successfully added `CutMix` and `MixUp` to our preprocessing
Expand Down Expand Up @@ -305,7 +305,7 @@ visualize_dataset(train_dataset, title="After custom pipeline")
```

![png](/img/guides/cut_mix_mix_up_and_rand_augment/cut_mix_mix_up_and_rand_augment_30_12.png)



Awesome! As you can see, no images were randomly rotated. You can customize the
Expand Down Expand Up @@ -334,9 +334,9 @@ visualize_dataset(train_dataset, title="After custom pipeline")
```



![png](/img/guides/cut_mix_mix_up_and_rand_augment/cut_mix_mix_up_and_rand_augment_34_0.png)



Looks great! You can use `RandomAugmentationPipeline` however you want.
Expand Down Expand Up @@ -377,9 +377,9 @@ train_dataset = train_dataset
test_dataset = test_dataset
```


![png](/img/guides/cut_mix_mix_up_and_rand_augment/cut_mix_mix_up_and_rand_augment_37_12.png)



Next we should create a the model itself. Notice that we use `label_smoothing=0.1` in
Expand Down
4 changes: 4 additions & 0 deletions scripts/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,5 +503,9 @@
'path': 'keras_tuner/',
'title': 'KerasTuner',
},
{
'path': 'keras_cv/',
'title': 'KerasCV',
},
]
}
4 changes: 2 additions & 2 deletions templates/keras_cv/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ KerasCV is a toolbox of modular building blocks (layers, metrics, losses, data a
---
## Quick links

* [Image Data Augmentation](/guides/keras_cv/cut_mix_mix_up_and_rand_augment/)
* [Image data augmentation](/guides/keras_cv/cut_mix_mix_up_and_rand_augment/)
* [Using COCO metrics](/guides/keras_cv/coco_metrics/)

* [Writing custom data augmentation layers](/guides/keras_cv/custom_image_augmentations/)
---
## Installation

Expand Down

0 comments on commit da6a4bc

Please sign in to comment.