Skip to content

Commit

Permalink
add more layers
Browse files Browse the repository at this point in the history
  • Loading branch information
divyashreepathihalli committed Nov 15, 2023
1 parent f977767 commit 6758d69
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 28 deletions.
5 changes: 3 additions & 2 deletions benchmarks/vectorized_jittered_resize.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 JitteredResize
from keras_cv.layers.preprocessing.base_image_augmentation_layer import (
BaseImageAugmentationLayer,
Expand Down Expand Up @@ -258,8 +259,8 @@ def test_consistency_with_old_impl(self):

# makes offsets fixed to (0.5, 0.5)
with unittest.mock.patch.object(
layer._random_generator,
"random_uniform",
random,
"uniform",
return_value=tf.convert_to_tensor([[0.5, 0.5]]),
):
output = layer(image)
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/vectorized_random_flip.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,14 @@ def test_consistency_with_old_impl(self):
)

with unittest.mock.patch.object(
layer._random_generator,
"random_uniform",
random,
"uniform",
return_value=tf.convert_to_tensor([[0.6]]),
):
output = layer(image)
with unittest.mock.patch.object(
old_layer._random_generator,
"random_uniform",
random,
"uniform",
return_value=tf.convert_to_tensor(0.6),
):
old_output = old_layer(image)
Expand Down
1 change: 1 addition & 0 deletions keras_cv/layers/preprocessing/random_channel_shift.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import tensorflow as tf

from keras_cv.api_export import keras_cv_export
from keras_cv.layers.preprocessing.base_image_augmentation_layer import (
BaseImageAugmentationLayer,
Expand Down
4 changes: 3 additions & 1 deletion keras_cv/layers/preprocessing/random_cutout.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
# limitations under the License.

import tensorflow as tf

from keras_cv.api_export import keras_cv_export
from keras_cv.backend import random
from keras_cv.layers.preprocessing.base_image_augmentation_layer import (
BaseImageAugmentationLayer,
)
from keras_cv.utils import fill_utils, preprocessing
from keras_cv.utils import fill_utils
from keras_cv.utils import preprocessing


@keras_cv_export("keras_cv.layers.RandomCutout")
Expand Down
5 changes: 3 additions & 2 deletions keras_cv/layers/preprocessing/random_shear.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
import warnings

import tensorflow as tf

from keras_cv import bounding_box
from keras_cv.api_export import keras_cv_export
from keras_cv.layers.preprocessing.vectorized_base_image_augmentation_layer import (
from keras_cv.layers.preprocessing.vectorized_base_image_augmentation_layer import ( # noqa: E501
VectorizedBaseImageAugmentationLayer,
) # noqa: E501
)
from keras_cv.utils import preprocessing


Expand Down
8 changes: 5 additions & 3 deletions keras_cv/layers/preprocessing/random_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
# limitations under the License.

import tensorflow as tf
from keras_cv import bounding_box, random

from keras_cv import bounding_box
from keras_cv import random
from keras_cv.api_export import keras_cv_export
from keras_cv.layers.preprocessing.vectorized_base_image_augmentation_layer import (
from keras_cv.layers.preprocessing.vectorized_base_image_augmentation_layer import ( # noqa: E501
VectorizedBaseImageAugmentationLayer,
) # noqa: E501
)
from keras_cv.utils import preprocessing as preprocessing_utils

H_AXIS = -3
Expand Down
8 changes: 5 additions & 3 deletions keras_cv/layers/preprocessing/random_zoom.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@


import tensorflow as tf
from tensorflow.keras import backend

from keras_cv.api_export import keras_cv_export
from keras_cv.backend import random
from keras_cv.layers.preprocessing.vectorized_base_image_augmentation_layer import \
VectorizedBaseImageAugmentationLayer # noqa: E501
from keras_cv.layers.preprocessing.vectorized_base_image_augmentation_layer import ( # noqa: E501
VectorizedBaseImageAugmentationLayer,
)
from keras_cv.utils import preprocessing as preprocessing_utils
from tensorflow.keras import backend

# In order to support both unbatched and batched inputs, the horizontal
# and vertical axis is reverse indexed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def __init__(self):
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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import tensorflow as tf

from keras_cv import bounding_box
from keras_cv.backend import random
from keras_cv.layers.preprocessing.vectorized_base_image_augmentation_layer import ( # noqa: E501
VectorizedBaseImageAugmentationLayer,
)
Expand All @@ -33,8 +34,11 @@ def augment_ragged_image(self, image, transformation, **kwargs):
def get_random_transformation_batch(self, batch_size, **kwargs):
if self.fixed_value:
return tf.ones((batch_size,)) * self.fixed_value
return self._random_generator.random_uniform(
(batch_size,), minval=self.add_range[0], maxval=self.add_range[1]
return random.uniform(
(batch_size,),
minval=self.add_range[0],
maxval=self.add_range[1],
seed=self._seed_generator,
)

def augment_images(self, images, transformations, **kwargs):
Expand Down Expand Up @@ -100,7 +104,7 @@ def get_random_transformation_batch(
assert isinstance(bounding_boxes["classes"], TF_ALL_TENSOR_TYPES)
assert isinstance(keypoints, TF_ALL_TENSOR_TYPES)
assert isinstance(segmentation_masks, TF_ALL_TENSOR_TYPES)
return self._random_generator.random_uniform((batch_size,))
return random.uniform((batch_size,), seed=self._seed_generator)

def augment_images(
self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,15 @@ def augment_pointclouds(self, point_clouds, 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):
# To-do: remove this once th elayer is ported to keras 3
# https://github.com/keras-team/keras-cv/issues/2136
self._seed_generator = SeedGenerator(
seed=seed,
)
if config.keras_3():
raise ValueError(
"This layer is not yet compatible with Keras 3."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import numpy as np
import tensorflow as tf

from keras_cv.backend import random
from keras_cv.layers.preprocessing_3d import base_augmentation_layer_3d
from keras_cv.tests.test_case import TestCase

Expand All @@ -27,14 +28,23 @@ def __init__(self, translate_noise=(0.0, 0.0, 0.0), **kwargs):
self._translate_noise = translate_noise

def get_random_transformation(self, **kwargs):
random_x = self._random_generator.random_normal(
(), mean=0.0, stddev=self._translate_noise[0]
random_x = random.normal(
(),
mean=0.0,
stddev=self._translate_noise[0],
seed=self._seed_generator,
)
random_y = self._random_generator.random_normal(
(), mean=0.0, stddev=self._translate_noise[1]
random_y = random.normal(
(),
mean=0.0,
stddev=self._translate_noise[1],
seed=self._seed_generator,
)
random_z = self._random_generator.random_normal(
(), mean=0.0, stddev=self._translate_noise[2]
random_z = random.normal(
(),
mean=0.0,
stddev=self._translate_noise[2],
seed=self._seed_generator,
)

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from keras_cv import point_cloud
from keras_cv.api_export import keras_cv_export
from keras_cv.backend import random
from keras_cv.layers.preprocessing_3d import base_augmentation_layer_3d

POINT_CLOUDS = base_augmentation_layer_3d.POINT_CLOUDS
Expand Down Expand Up @@ -122,8 +123,11 @@ def get_random_transformation(self, point_clouds, **kwargs):
frustum_mask = tf.concat(frustum_mask, axis=0)
# Generate mask along point dimension.
random_point_mask = (
self._random_generator.random_uniform(
[1, num_points, 1], minval=0.0, maxval=1
random.uniform(
[1, num_points, 1],
minval=0.0,
maxval=1,
seed=self._seed_generator,
)
< self._keep_probability
)
Expand Down

0 comments on commit 6758d69

Please sign in to comment.