Reputation: 1
I'm trying to train a neural net (autoencoder) reading the '.tif' images from a folder, so I decided to use ImageDataGenerator class. The images values are variables, sometimes the maximum can be 4000, sometimes can be 0.5, but when I use the above mentioned class and its methods (flow_from_directory or flow_from_dataframe) it's like the images are automatically rescaled. Is it possible to leave the values as they were before? Is there anything wrong with the code?
train_datagen = ImageDataGenerator(shear_range=0.2,zoom_range=0.2,horizontal_flip=True,dtype='float32')
train_generator = train_datagen.flow_from_directory(directory =train_data_dir,color_mode = 'grayscale',target_size=(img_width, img_height),batch_size=batch_size,class_mode='input',)
I control the input images in that way:
batch = np.concatenate([next(train_generator)[0] for _ in range(2)])
I expected the input images to have different range of values, but it seems that every image has pixel in range [0,255].
Upvotes: 0
Views: 293
Reputation: 7129
Under the hood, ImageDataGenerator uses PIL to load images. You'll find that your .tif images are opened with PIL and converted to 'L' mode (Luminance, see this excellent explanation on different color modes in PIL) when setting the color mode to grayscale:
...
img = pil_image.open(path)
if color_mode == 'grayscale':
if img.mode != 'L':
img = img.convert('L')
...
L mode means that your image will be represented by a single-channel array containing 1-byte luminance values. These are the values between 0 and 255 that you mention.
Now, PIL is probably not the best library to read in tiff images. If you want to pass the images with their original values to your neural network, you will probably need to write a custom python generator (there are plenty tutorials for this) which reads the images with a third-party library suited for reading tif and converts them to numpy arrays.
Upvotes: 2