Reputation: 3971
I am using colab and tf version is '2.2.0-rc2'. Using tf.keras.preprocessing.image.ImageDataGenerator to perform a single random transform using .random_transform on a single image does not rescale it (with 1/255.) Code snippet:
import tensorflow as tf
import PIL
img_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255)
img = PIL.Image.open("my_image.jpg")
img = img.resize((224, 224), PIL.Image.BICUBIC)
img_t = img_gen.random_transform(np.array(img))
img_t is still a tensor with range [0, 255] instead of [0.0, 1.0].
Is this by design or this is a bug? I don't remember the behavior before tensorflow 2.0 (i probably never use it that way).
Note: I don't see any issue if I use .flow or .flow_from_directory and their generators.
Upvotes: 0
Views: 905
Reputation: 11333
It looks to me like does make sense. flow
or flow_from_*
methods and random_transform()
transforms data differently.
The flow()
methods will take the configuration/transformations provided to ImageDataGenerator and spit out data according to those parameters
random_transform()
comes up with a random transformation configuration (based on the parameters you've setup in __init__
of your ImageDataGenerator
and apply that to the data. This means your rescale
parameter might be overridden by default.
random_transform()
I haven't used random_tranform()
myself before. But it seems this is how it works.
Let's say we define the following ImageDataGenerator
img_gen = tf.keras.preprocessing.image.ImageDataGenerator(
rescale=1./255, vertical_flip=True, brightness_range=(-0.1,0.1)
)
Now you can see what kind of random transformations are available using,
print(img_gen.get_random_transform((1,224,224,3)))
which will give different random values for vertical_flip
and brightness
. As you can see rescale
is apparently not a random parameter they consider. This explains why you didn't get the expected result.
Upvotes: 1