I always use this parameter to scale array of original image pixel values to be between [0,1] and specify the parameter rescale=1./255
.
Then when i do this:
from keras.preprocessing.image import load_img, img_to_array, ImageDataGenerator
img = load_img('val_00009301.JPEG')
img_arr = img_to_array(img)
datagen = ImageDataGenerator(rescale=1./255)
for batch in datagen.flow(img_arr,
batch_size=1,
save_to_dir='path/to/save',
save_prefix='1_param',
save_format='jpeg'):......`
When I check "path/to/save" directory, I see the picture generated by ImageDataGenerator class totally normal. How that is happen? I should see almost completely black image.
The ImageDataGenerator class can be used to rescale pixel values from the range of 0-255 to the range 0-1 preferred for neural network models. Scaling data to the range of 0-1 is traditionally referred to as normalization.
Keras ImageDataGenerator is a gem! It lets you augment your images in real-time while your model is still training! You can apply any random transformations on each training image as it is passed to the model. This will not only make your model robust but will also save up on the overhead memory!
flow_from_directory Method This method is useful when the images are sorted and placed in there respective class/label folders. This method will identify classes automatically from the folder name.
By default, Keras' ImageDataGenerator class performs in-place/on-the-fly data augmentation, meaning that the class: Accepts a batch of images used for training. Takes this batch and applies a series of random transformations to each image in the batch.
I altered your example a little to plot the image and to print a pixel value. It seems that the image is automagically rescaled back when plotted, because I did not noticed any difference between my input image and the plotted one. I assume the same happens when saving.
from keras.preprocessing.image import load_img, img_to_array, ImageDataGenerator
import numpy as np
from matplotlib import pyplot
img = load_img('capture102.jpg')
img_arr = np.expand_dims(img_to_array(img), axis=0)
datagen = ImageDataGenerator(rescale=1./255)
for batch in datagen.flow(img_arr, batch_size=1, save_to_dir='path/to/save', save_prefix='1_param', save_format='jpeg'):
print(batch[0][0][0])
pyplot.imshow(batch[0])
pyplot.show()
break
The printed values are:[0.21960786 0.23529413 0.27058825]
This is because when you save it to disk, array_to_img()
function rescale it back to the image range, i.e. 0-255 for uint8. See the keras image data generator implementation for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With