AttributeError: module 'tensorflow_core.keras.layers.experimental.preprocessing' has no attribute 'RandomFlip'

Viewed 20696

I use Tensorflow 2.1.0 In this code

data_augmentation = tf.keras.Sequential([
    tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal'),
    tf.keras.layers.experimental.preprocessing.RandomRotation(0.3)
]) 

I find this error:

AttributeError: module 'tensorflow_core.keras.layers.experimental.preprocessing' has no attribute 'RandomFlip'

So how can I change it without changing version of tensorflow

3 Answers

To work your code as expected, firstly Tensorflow has to be upgrade to the latest version

! pip install tensorflow --upgrade

If you are looking for solution in TF 2.1.0, then there are two options are available

First solution: tf.image.random_flip_left_right ( horizontal flip)

tf.image.random_flip_left_right(
    image, seed=None)

Second solution: tf.keras.preprocessing.image.ImageDataGenerator

tf.keras.preprocessing.image.ImageDataGenerator(
   rotation_range=30, horizontal_flip=True)
! pip install tensorflow --upgrade --user

--user option can help you without the permission problem

Add this line to the importing section (of course after import tensorflow as tf) tf.config.experimental_run_functions_eagerly(True)

Almost any tf.keras.layers.experimental.preprocessing.SomeClass in the listed classes here, should work.

But need to do sanity check with plotting results.

Related