Skip to content

Commit

Permalink
upload updated aug layers
Browse files Browse the repository at this point in the history
  • Loading branch information
divyashreepathihalli committed Nov 14, 2023
1 parent 3385708 commit 31ebec7
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 68 deletions.
7 changes: 2 additions & 5 deletions benchmarks/vectorized_mosaic.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,17 @@
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow import keras

from keras_cv import bounding_box
from keras_cv.layers import Mosaic
from keras_cv.layers.preprocessing.base_image_augmentation_layer import (
BaseImageAugmentationLayer,
)
from keras_cv.layers.preprocessing.vectorized_base_image_augmentation_layer import ( # noqa: E501
IMAGES,
)
from keras_cv.layers.preprocessing.vectorized_base_image_augmentation_layer import ( # noqa: E501
LABELS,
)
from keras_cv.utils import preprocessing as preprocessing_utils
from tensorflow import keras


class OldMosaic(BaseImageAugmentationLayer):
Expand Down Expand Up @@ -101,7 +98,7 @@ def _batch_augment(self, inputs):
minval=0,
maxval=batch_size,
dtype=tf.int32,
seed=self._random_generator.make_legacy_seed(),
seed=self._seed_generator.next(),
)
# concatenate the batches with permutation order to get all 4 images of
# the mosaic
Expand Down
5 changes: 3 additions & 2 deletions benchmarks/vectorized_random_flip.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from tensorflow import keras

from keras_cv import bounding_box
from keras_cv.backend import random
from keras_cv.layers import RandomFlip
from keras_cv.layers.preprocessing.base_image_augmentation_layer import (
BaseImageAugmentationLayer,
Expand Down Expand Up @@ -102,11 +103,11 @@ def get_random_transformation(self, **kwargs):
flip_vertical = False
if self.horizontal:
flip_horizontal = (
self._random_generator.random_uniform(shape=[]) > 0.5
random.uniform(shape=[]) > 0.5, seed=self._seed_generator
)
if self.vertical:
flip_vertical = (
self._random_generator.random_uniform(shape=[]) > 0.5
random.uniform(shape=[]) > 0.5, seed=self._seed_generator
)
return {
"flip_horizontal": tf.cast(flip_horizontal, dtype=tf.bool),
Expand Down
5 changes: 2 additions & 3 deletions benchmarks/vectorized_random_hue.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow import keras

from keras_cv.layers import RandomHue
from keras_cv.layers.preprocessing.base_image_augmentation_layer import (
BaseImageAugmentationLayer,
)
from keras_cv.utils import preprocessing as preprocessing_utils
from tensorflow import keras


class OldRandomHue(BaseImageAugmentationLayer):
Expand Down Expand Up @@ -63,7 +62,7 @@ def __init__(self, factor, value_range, seed=None, **kwargs):
self.seed = seed

def get_random_transformation(self, **kwargs):
invert = preprocessing_utils.random_inversion(self._random_generator)
invert = preprocessing_utils.random_inversion(self._seed_generator)
# We must scale self.factor() to the range [-0.5, 0.5]. This is because
# the tf.image operation performs rotation on the hue saturation value
# orientation. This can be thought of as an angle in the range
Expand Down
11 changes: 7 additions & 4 deletions benchmarks/vectorized_random_rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow import keras

from keras_cv import bounding_box
from keras_cv.backend import random
from keras_cv.layers import RandomRotation
from keras_cv.layers.preprocessing.base_image_augmentation_layer import (
BaseImageAugmentationLayer,
)
from keras_cv.utils import preprocessing as preprocessing_utils
from tensorflow import keras

H_AXIS = -3
W_AXIS = -2
Expand Down Expand Up @@ -122,8 +122,11 @@ def __init__(
def get_random_transformation(self, **kwargs):
min_angle = self.lower * 2.0 * np.pi
max_angle = self.upper * 2.0 * np.pi
angle = self._random_generator.random_uniform(
shape=[1], minval=min_angle, maxval=max_angle
angle = random.uniform(
shape=[1],
minval=min_angle,
maxval=max_angle,
seed=self._seed_generator,
)
return {"angle": angle}

Expand Down
7 changes: 3 additions & 4 deletions benchmarks/vectorized_random_shear.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
import warnings
from unittest.mock import MagicMock

import keras_cv
import numpy as np
import tensorflow as tf
from matplotlib import pyplot as plt

import keras_cv
from keras_cv import bounding_box
from keras_cv.layers import RandomShear
from keras_cv.layers.preprocessing.base_image_augmentation_layer import (
BaseImageAugmentationLayer,
)
from keras_cv.utils import preprocessing
from matplotlib import pyplot as plt


# Copied from:
Expand Down Expand Up @@ -107,7 +106,7 @@ def _get_shear_amount(self, constraint):
if constraint is None:
return None

invert = preprocessing.random_inversion(self._random_generator)
invert = preprocessing.random_inversion(self._seed_generator)
return invert * constraint()

def augment_image(self, image, transformation=None, **kwargs):
Expand Down
10 changes: 6 additions & 4 deletions benchmarks/vectorized_random_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
import numpy as np
import tensorflow as tf
from keras import backend
from tensorflow import keras

from keras_cv.backend import random
from keras_cv.layers import RandomTranslation
from keras_cv.layers.preprocessing.base_image_augmentation_layer import (
BaseImageAugmentationLayer,
)
from keras_cv.utils import preprocessing as preprocessing_utils
from tensorflow import keras

H_AXIS = -3
W_AXIS = -2
Expand Down Expand Up @@ -217,17 +217,19 @@ def augment_image(self, image, transformation, **kwargs):

def get_random_transformation(self, image=None, **kwargs):
batch_size = 1
height_translation = self._random_generator.random_uniform(
height_translation = random.uniform(
shape=[batch_size, 1],
minval=self.height_lower,
maxval=self.height_upper,
dtype=tf.float32,
seed=self._seed_generator,
)
width_translation = self._random_generator.random_uniform(
width_translation = random.uniform(
shape=[batch_size, 1],
minval=self.width_lower,
maxval=self.width_upper,
dtype=tf.float32,
seed=self._seed_generator,
)
return {
"height_translation": height_translation,
Expand Down
10 changes: 6 additions & 4 deletions benchmarks/vectorized_random_zoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
import numpy as np
import tensorflow as tf
from keras import backend
from tensorflow import keras

from keras_cv.backend import random
from keras_cv.layers import RandomZoom
from keras_cv.layers.preprocessing.base_image_augmentation_layer import (
BaseImageAugmentationLayer,
)
from keras_cv.utils import preprocessing as preprocessing_utils
from tensorflow import keras

# In order to support both unbatched and batched inputs, the horizontal
# and vertical axis is reverse indexed
Expand Down Expand Up @@ -143,16 +143,18 @@ def __init__(
self.seed = seed

def get_random_transformation(self, image=None, **kwargs):
height_zoom = self._random_generator.random_uniform(
height_zoom = random.uniform(
shape=[1, 1],
minval=1.0 + self.height_lower,
maxval=1.0 + self.height_upper,
seed=self._seed_generator,
)
if self.width_factor is not None:
width_zoom = self._random_generator.random_uniform(
width_zoom = random.uniform(
shape=[1, 1],
minval=1.0 + self.width_lower,
maxval=1.0 + self.width_upper,
seed=self._seed_generator,
)
else:
width_zoom = height_zoom
Expand Down
9 changes: 5 additions & 4 deletions benchmarks/vectorized_randomly_zoomed_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from tensorflow import keras

from keras_cv import core
from keras_cv.layers import RandomlyZoomedCrop
from keras_cv.layers.preprocessing.base_image_augmentation_layer import (
BaseImageAugmentationLayer,
)
from keras_cv.utils import preprocessing as preprocessing_utils
from tensorflow import keras


class OldRandomlyZoomedCrop(BaseImageAugmentationLayer):
Expand Down Expand Up @@ -109,18 +108,20 @@ def get_random_transformation(

new_width = crop_size[1] * tf.sqrt(aspect_ratio)

height_offset = self._random_generator.random_uniform(
height_offset = random.uniform(
(),
minval=tf.minimum(0.0, original_height - new_height),
maxval=tf.maximum(0.0, original_height - new_height),
dtype=tf.float32,
seed=self._seed_generator,
)

width_offset = self._random_generator.random_uniform(
width_offset = random.uniform(
(),
minval=tf.minimum(0.0, original_width - new_width),
maxval=tf.maximum(0.0, original_width - new_width),
dtype=tf.float32,
seed=self._seed_generator,
)

new_height = new_height / original_height
Expand Down
17 changes: 5 additions & 12 deletions keras_cv/layers/preprocessing/base_image_augmentation_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@

import keras
import tensorflow as tf

if hasattr(keras, "src"):
keras_backend = keras.src.backend
else:
keras_backend = keras.backend

from keras_cv import bounding_box
from keras_cv.api_export import keras_cv_export
from keras_cv.backend import keras
from keras_cv.backend import scope
from keras_cv.backend import keras, scope
from keras_cv.backend.config import multi_backend
from keras_cv.backend.random import SeedGenerator
from keras_cv.utils import preprocessing

# In order to support both unbatched and batched inputs, the horizontal
Expand Down Expand Up @@ -126,13 +120,12 @@ def augment_image(self, image, transformation):
Note that since the randomness is also a common functionality, this layer
also includes a keras_backend.RandomGenerator, which can be used to
produce the random numbers. The random number generator is stored in the
`self._random_generator` attribute.
`self._seed_generator` attribute.
"""

def __init__(self, seed=None, **kwargs):
force_generator = kwargs.pop("force_generator", False)
self._random_generator = keras_backend.RandomGenerator(
seed=seed, force_generator=force_generator
self._seed_generator = SeedGenerator(
seed=seed,
)
super().__init__(**kwargs)
self.built = True
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
# limitations under the License.
import numpy as np
import tensorflow as tf

from keras_cv import bounding_box
from keras_cv.backend import random
from keras_cv.layers.preprocessing.base_image_augmentation_layer import (
BaseImageAugmentationLayer,
)
Expand All @@ -30,8 +30,11 @@ def __init__(self, value_range=(0.0, 1.0), fixed_value=None, **kwargs):
def get_random_transformation(self, **kwargs):
if self.fixed_value:
return self.fixed_value
return self._random_generator.random_uniform(
[], minval=self.value_range[0], maxval=self.value_range[1]
return random.uniform(
[],
minval=self.value_range[0],
maxval=self.value_range[1],
seed=self._seed_generator,
)

def augment_image(self, image, transformation, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,11 @@

import keras
import tensorflow as tf

if hasattr(keras, "src"):
keras_backend = keras.src.backend
else:
keras_backend = keras.backend

from keras_cv import bounding_box
from keras_cv.api_export import keras_cv_export
from keras_cv.backend import keras
from keras_cv.backend import scope
from keras_cv.backend import keras, scope
from keras_cv.backend.config import multi_backend
from keras_cv.backend.random import SeedGenerator
from keras_cv.utils import preprocessing

H_AXIS = -3
Expand Down Expand Up @@ -109,9 +103,8 @@ def __init__(self):
"""

def __init__(self, seed=None, **kwargs):
force_generator = kwargs.pop("force_generator", False)
self._random_generator = keras_backend.RandomGenerator(
seed=seed, force_generator=force_generator
self._seed_generator = SeedGenerator(
seed=seed,
)
super().__init__(**kwargs)
self._convert_input_args = False
Expand Down
21 changes: 9 additions & 12 deletions keras_cv/utils/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@
# limitations under the License.

import tensorflow as tf
from keras_cv import core
from keras_cv.backend import ops, random
from tensorflow import keras
from tensorflow.keras import backend

from keras_cv import core
from keras_cv.backend import ops

_TF_INTERPOLATION_METHODS = {
"bilinear": tf.image.ResizeMethod.BILINEAR,
"nearest": tf.image.ResizeMethod.NEAREST_NEIGHBOR,
Expand Down Expand Up @@ -171,29 +170,27 @@ def parse_factor(
return core.UniformFactorSampler(param[0], param[1], seed=seed)


def random_inversion(random_generator):
"""Randomly returns a -1 or a 1 based on the provided random_generator.
def random_inversion(seed_generator):
"""Randomly returns a -1 or a 1 based on the provided seed_generator.
This can be used by KPLs to randomly invert sampled values.
Args:
random_generator: a Keras random number generator. An instance can be
passed from the `self._random_generator` attribute of
seed_generator: a Keras random number generator. An instance can be
passed from the `self._seed_generator` attribute of
a `BaseImageAugmentationLayer`.
Returns:
either -1, or -1.
"""
negate = random_generator.random_uniform((), 0, 1, dtype=tf.float32) > 0.5
negate = keras.backend.uniform((), 0, 1, dtype=tf.float32) > 0.5
negate = tf.cond(negate, lambda: -1.0, lambda: 1.0)
return negate


def batch_random_inversion(random_generator, batch_size):
def batch_random_inversion(seed_generator, batch_size):
"""Same as `random_inversion` but for batched inputs."""
negate = random_generator.random_uniform(
(batch_size, 1), 0, 1, dtype=tf.float32
)
negate = random.uniform((batch_size, 1), 0, 1, dtype=tf.float32)
negate = tf.where(negate > 0.5, -1.0, 1.0)
return negate

Expand Down

0 comments on commit 31ebec7

Please sign in to comment.