From 2830b107c677784c13e947255c9cf8270fbd883c Mon Sep 17 00:00:00 2001 From: divyashreepathihalli Date: Tue, 14 Nov 2023 23:58:00 +0000 Subject: [PATCH] reformat and resolve error --- benchmarks/vectorized_mosaic.py | 5 ++++- benchmarks/vectorized_random_flip.py | 6 +++--- benchmarks/vectorized_random_hue.py | 3 ++- benchmarks/vectorized_random_rotation.py | 3 ++- benchmarks/vectorized_random_shear.py | 5 +++-- benchmarks/vectorized_random_translation.py | 3 ++- benchmarks/vectorized_random_zoom.py | 3 ++- benchmarks/vectorized_randomly_zoomed_crop.py | 3 ++- .../base_image_augmentation_layer.py | 4 +++- .../base_image_augmentation_layer_test.py | 1 + .../vectorized_base_image_augmentation_layer.py | 4 +++- keras_cv/utils/preprocessing.py | 15 +++++++++++---- 12 files changed, 38 insertions(+), 17 deletions(-) diff --git a/benchmarks/vectorized_mosaic.py b/benchmarks/vectorized_mosaic.py index 51fdcaf8bb..2ca5a43a95 100644 --- a/benchmarks/vectorized_mosaic.py +++ b/benchmarks/vectorized_mosaic.py @@ -17,6 +17,8 @@ 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 ( @@ -24,10 +26,11 @@ ) 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 ( LABELS, ) from keras_cv.utils import preprocessing as preprocessing_utils -from tensorflow import keras class OldMosaic(BaseImageAugmentationLayer): diff --git a/benchmarks/vectorized_random_flip.py b/benchmarks/vectorized_random_flip.py index a930bc5441..2a1f963cfe 100644 --- a/benchmarks/vectorized_random_flip.py +++ b/benchmarks/vectorized_random_flip.py @@ -28,7 +28,7 @@ from keras_cv.layers.preprocessing.vectorized_base_image_augmentation_layer import ( # noqa: E501 BOUNDING_BOXES, ) -from keras_cv.layers.preprocessing.vectorized_base_image_augmentation_layer import ( # noqa: E501 +from keras_cv.layers.preprocessing.vectorized_base_image_augmentation_layer import ( IMAGES, ) @@ -103,11 +103,11 @@ def get_random_transformation(self, **kwargs): flip_vertical = False if self.horizontal: flip_horizontal = ( - random.uniform(shape=[]) > 0.5, seed=self._seed_generator + random.uniform(shape=[], seed=self._seed_generator) > 0.5 ) if self.vertical: flip_vertical = ( - random.uniform(shape=[]) > 0.5, seed=self._seed_generator + random.uniform(shape=[], seed=self._seed_generator) > 0.5 ) return { "flip_horizontal": tf.cast(flip_horizontal, dtype=tf.bool), diff --git a/benchmarks/vectorized_random_hue.py b/benchmarks/vectorized_random_hue.py index 83d2a0e056..58a6e81127 100644 --- a/benchmarks/vectorized_random_hue.py +++ b/benchmarks/vectorized_random_hue.py @@ -17,12 +17,13 @@ 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): diff --git a/benchmarks/vectorized_random_rotation.py b/benchmarks/vectorized_random_rotation.py index 9fbea887d6..4c83e6448d 100644 --- a/benchmarks/vectorized_random_rotation.py +++ b/benchmarks/vectorized_random_rotation.py @@ -17,6 +17,8 @@ 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 @@ -24,7 +26,6 @@ BaseImageAugmentationLayer, ) from keras_cv.utils import preprocessing as preprocessing_utils -from tensorflow import keras H_AXIS = -3 W_AXIS = -2 diff --git a/benchmarks/vectorized_random_shear.py b/benchmarks/vectorized_random_shear.py index 87040dfb25..ea0f310621 100644 --- a/benchmarks/vectorized_random_shear.py +++ b/benchmarks/vectorized_random_shear.py @@ -2,16 +2,17 @@ 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: diff --git a/benchmarks/vectorized_random_translation.py b/benchmarks/vectorized_random_translation.py index 269c086d00..9d883d5f36 100644 --- a/benchmarks/vectorized_random_translation.py +++ b/benchmarks/vectorized_random_translation.py @@ -18,13 +18,14 @@ 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 diff --git a/benchmarks/vectorized_random_zoom.py b/benchmarks/vectorized_random_zoom.py index c933650338..5253545557 100644 --- a/benchmarks/vectorized_random_zoom.py +++ b/benchmarks/vectorized_random_zoom.py @@ -18,13 +18,14 @@ 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 diff --git a/benchmarks/vectorized_randomly_zoomed_crop.py b/benchmarks/vectorized_randomly_zoomed_crop.py index 671c63e237..3d670dd0a8 100644 --- a/benchmarks/vectorized_randomly_zoomed_crop.py +++ b/benchmarks/vectorized_randomly_zoomed_crop.py @@ -16,13 +16,14 @@ 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): diff --git a/keras_cv/layers/preprocessing/base_image_augmentation_layer.py b/keras_cv/layers/preprocessing/base_image_augmentation_layer.py index aa79f88dbf..512568a405 100644 --- a/keras_cv/layers/preprocessing/base_image_augmentation_layer.py +++ b/keras_cv/layers/preprocessing/base_image_augmentation_layer.py @@ -14,9 +14,11 @@ import keras import tensorflow as tf + from keras_cv import bounding_box from keras_cv.api_export import keras_cv_export -from keras_cv.backend import keras, scope +from keras_cv.backend import keras +from keras_cv.backend import scope from keras_cv.backend.config import multi_backend from keras_cv.backend.random import SeedGenerator from keras_cv.utils import preprocessing diff --git a/keras_cv/layers/preprocessing/base_image_augmentation_layer_test.py b/keras_cv/layers/preprocessing/base_image_augmentation_layer_test.py index f6cc21f746..6ffa78250a 100644 --- a/keras_cv/layers/preprocessing/base_image_augmentation_layer_test.py +++ b/keras_cv/layers/preprocessing/base_image_augmentation_layer_test.py @@ -13,6 +13,7 @@ # 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 ( diff --git a/keras_cv/layers/preprocessing/vectorized_base_image_augmentation_layer.py b/keras_cv/layers/preprocessing/vectorized_base_image_augmentation_layer.py index 6fe3cf6798..3e633048f3 100644 --- a/keras_cv/layers/preprocessing/vectorized_base_image_augmentation_layer.py +++ b/keras_cv/layers/preprocessing/vectorized_base_image_augmentation_layer.py @@ -14,9 +14,11 @@ import keras import tensorflow as tf + from keras_cv import bounding_box from keras_cv.api_export import keras_cv_export -from keras_cv.backend import keras, scope +from keras_cv.backend import keras +from keras_cv.backend import scope from keras_cv.backend.config import multi_backend from keras_cv.backend.random import SeedGenerator from keras_cv.utils import preprocessing diff --git a/keras_cv/utils/preprocessing.py b/keras_cv/utils/preprocessing.py index b999db8e14..af9e6d1d29 100644 --- a/keras_cv/utils/preprocessing.py +++ b/keras_cv/utils/preprocessing.py @@ -13,11 +13,13 @@ # 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 +from keras_cv.backend import random + _TF_INTERPOLATION_METHODS = { "bilinear": tf.image.ResizeMethod.BILINEAR, "nearest": tf.image.ResizeMethod.NEAREST_NEIGHBOR, @@ -183,14 +185,19 @@ def random_inversion(seed_generator): Returns: either -1, or -1. """ - negate = keras.backend.uniform((), 0, 1, dtype=tf.float32) > 0.5 + negate = ( + keras.backend.uniform((), 0, 1, dtype=tf.float32, seed=seed_generator) + > 0.5 + ) negate = tf.cond(negate, lambda: -1.0, lambda: 1.0) return negate def batch_random_inversion(seed_generator, batch_size): """Same as `random_inversion` but for batched inputs.""" - negate = random.uniform((batch_size, 1), 0, 1, dtype=tf.float32) + negate = random.uniform( + (batch_size, 1), 0, 1, dtype=tf.float32, seed=seed_generator + ) negate = tf.where(negate > 0.5, -1.0, 1.0) return negate