Keras.ImageDataGenerator结果显示[flow()]

Jim*_*ela 3 python-3.x keras tensorflow tensorflow2.0

我正在尝试显示 Imagedatagenerator.flow() 生成的图像,但我无法这样做。

我使用单个图像并将其传递给 .flow(img_path) 来生成增强图像,直到总数符合我们的要求:

total = 0
for image in imageGen:
total += 1
if total == 10:
    break
Run Code Online (Sandbox Code Playgroud)

.flow()

imageGen = aug.flow(image_path, batch_size=1,
                      save_to_dir="/path/to/save_dir",
                      save_prefix="", save_format='png')
Run Code Online (Sandbox Code Playgroud)

如何接收在循环中生成的图像,以便我可以在运行时显示它?

n1c*_*s.m 12

如果您想使用图像路径,可以使用flow_from_directory,并传递包含单个图像的图像文件夹。要从生成器获取图像,请使用dir_It.next()并访问第一个元素,因为该函数的返回是:

\n
\n

生成 (x, y) 元组的 DirectoryIterator,其中 x 是一个 numpy 数组,其中包含一批形状为 (batch_size、*target_size、channels) 的图像,y 是相应标签的 numpy 数组。

\n
\n

要显示图像,您可以plt.imshow通过matplotlib传递批次的第一个(也是唯一一个)图像来使用plt.imshow(img[0])

\n

福德结构

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 data\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 smile\n\xe2\x94\x82\xc2\xa0\xc2\xa0     \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 smile_8.jpg\n\n# smile_8.jpg shape (256,256,3)\n
Run Code Online (Sandbox Code Playgroud)\n
import tensorflow as tf\nfrom tensorflow import keras\nimport matplotlib.pyplot as plt\n\ndatagen = keras.preprocessing.image.ImageDataGenerator(\n    rescale=1./255,\n    rotation_range=180,\n    width_shift_range=0.2,\n    height_shift_range=0.2,\n)\n\ndir_It = datagen.flow_from_directory(\n    "data/",\n    batch_size=1,\n    save_to_dir="output/",\n    save_prefix="",\n    save_format=\'png\',\n)\n\nfor _ in range(5):\n    img, label = dir_It.next()\n    print(img.shape)   #  (1,256,256,3)\n    plt.imshow(img[0])\n    plt.show()\n
Run Code Online (Sandbox Code Playgroud)\n

smile_gen_hstack

\n