图像数据生成器在将预处理函数参数作为 None 后不生成图像?

Image Data Generator not producing image after giving preprocessing function argument as None?

我正在了解使用 Keras 进行图像分类。 有一个称为图像数据生成器的函数,用于准备要处理的图像。

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)

然后我调用了这个函数来检查图像

plt.imshow(imgs[0])

它给了我一个原始图像的脱色和调整大小的版本。

然而,当我尝试这个时:-

train_batches = ImageDataGenerator(preprocessing_function=None) \
    .flow_from_directory(directory=train_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=10)

train_batches = ImageDataGenerator() \
    .flow_from_directory(directory=train_path, target_size=(224,224), classes=['cat', 'dog'], batch_size=10)

然后它给空白。

对于某些图像,我可以看到模糊的轮廓,但看不到图像本身。

理想情况下,它应该给出原始图像(调整大小)对吧?因为没有涉及预处理?

谁能告诉我如何从目录迭代器中获取原始图像?

涉及预处理。 tf.keras.applications.vgg16.preprocess_input 方法正在对您的第一个示例中的图像进行预处理:

The images are converted from RGB to BGR, then each color channel is zero-centered with respect to the ImageNet dataset, without scaling.

如果删除此预处理步骤并仅在 ImageDataGenerator imgs[0]/= 255 中重新缩放图像,您将看到原始图像。

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])