Issue
I was understanding image classification using Keras. There was a function called image data generator which was used to prepare an image for processing.
train_batches = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
.flow_from_directory(directory=train_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=10)
imgs, labels = next(train_batches)
Then I called this function to check the image
plt.imshow(imgs[0])
It gave me a decolorized and resized version of the original image.
However, when i tried this: -
train_batches = ImageDataGenerator(preprocessing_function=None) \
.flow_from_directory(directory=train_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=10)
or
train_batches = ImageDataGenerator() \
.flow_from_directory(directory=train_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=10)
then it gave blank.
For certain images, I can see faint outlines but not the image itself.
Ideally, it should have given the original image (resized) right? Because there is no preprocessing involved?
Can anyone tell me how to get the original image from a Directory Iterator?
Solution
There is preprocessing involved. The tf.keras.applications.vgg16.preprocess_input method is preprocessing your images in your first example:
The images are converted from RGB to BGR, then each color channel is zero-centered with respect to the ImageNet dataset, without scaling.
If you remove this preprocessing step and just rescale the images either in the ImageDataGenerator
or simply imgs[0]/= 255
, you will see the original images.
import tensorflow as tf
import pathlib
import matplotlib.pyplot as plt
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
batch_size = 32
num_classes = 5
train_batches = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255) \
.flow_from_directory(directory=data_dir, batch_size=10)
imgs, labels = next(train_batches)
plt.imshow(imgs[0])
Answered By - AloneTogether
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.