【问题标题】:Image Data Generator not producing image after giving preprocessing function argument as None?图像数据生成器在将预处理函数参数设为无后不生成图像?
【发布时间】:2022-01-13 23:45:10
【问题描述】:

我正在理解使用 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)

然后它给了空白。

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

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

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

【问题讨论】:

    标签: python tensorflow image-processing keras


    【解决方案1】:

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

    图像从 RGB 转换为 BGR,然后每个颜色通道相对于 ImageNet 数据集以零为中心,不进行缩放。

    如果您删除此预处理步骤并仅在 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])
    

    【讨论】:

    • 实际使用 astype(unit8) 给出了正确答案
    • 它产生相同的输出。是的。
    猜你喜欢
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 2022-10-01
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多