I'm trying to cast the output data of my ImageDataGenerator
to integers. This is my generator:
train_mask_data_gen = ImageDataGenerator(rotation_range=10,
width_shift_range=10,
height_shift_range=10,
zoom_range=0.3,
horizontal_flip=True,
vertical_flip=True,
fill_mode='nearest',#interpolation used for augmenting the image
cval=0,
rescale=1./255,
dtype='int32')
And then:
train_mask_gen = train_mask_data_gen.flow_from_directory(os.path.join(training_dir, 'masks'),
target_size=(img_h, img_w),
batch_size=bs,
class_mode=None, # Because we have no class subfolders in this case
shuffle=True,
interpolation='nearest',#interpolation used for resizing
#color_mode='grayscale',
seed=SEED)
The inputs are binary images(0 or 255 as value), i normalize them and i would like to obtain integer values(for example, each pixel can have only 0 or 1 values).
So far, if i "test" the type of my generated data i obtain 'numpy.float32'
but i wrote dtype='int32
in my generator.
Seems that the dtype
entry is completely ignored and i just get the default one (as mentioned in the documentation: Keras Image Generator
Why is this happening? How do i "force" my data to be an integer?
The pressure is high on such old questions, but I will give it a try today.
When reviewing the documentation of ImageDataGenerator
, it appears that the dtype
parameter does not directly affect the data type of the generated images, but defines the:
"Dtype to use for the generated arrays."
So the dtype
parameter in ImageDataGenerator
is used to set the data type of the internal array that holds the augmented image data before it is returned by the generator. It does not affect the data type of the generated images themselves.
In the case of image data generation, the generated images will still have a data type of float32
, regardless of the value specified for dtype
in the generator.
How do I force my data to be integer?
To achieve integer values in the generated images, you will need to manually convert the generated images to integers after they have been generated.
import numpy as np
# Generate images using the generator
generated_images = train_mask_gen.next()
# Convert the images to integers
generated_images = np.round(generated_images).astype(np.int32)