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

Python 3.x Keras.ImageDataGenerator结果显示[flow()],python-3.x,tensorflow,keras,tensorflow2.0,Python 3.x,Tensorflow,Keras,Tensorflow2.0,我试图显示Imagedatagenerator.flow()生成的图像,但无法显示 我使用单个图像并将其传递到.flow(img_路径)以生成增强图像,直到总数符合我们的要求: total = 0 for image in imageGen: total += 1 if total == 10: break .flow() 如何接收循环中生成的图像,以便在运行时显示?如果要使用可以使用的图像路径,并传递包含单个图像的图像文件夹。要从生成器获取图像,请使用dir\u It.next()并

我试图显示Imagedatagenerator.flow()生成的图像,但无法显示

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

total = 0
for image in imageGen:
total += 1
if total == 10:
    break
.flow()


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

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

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

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

Foder结构

├── data
│   └── smile
│       └── smile_8.jpg

# smile_8.jpg shape (256,256,3)

我只想使用flow()方法。感谢您给出这个解释,但我通过放置一个简单的for循环并在GUI上显示相同的图像,成功地使它工作。
├── data
│   └── smile
│       └── smile_8.jpg

# smile_8.jpg shape (256,256,3)
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt

datagen = keras.preprocessing.image.ImageDataGenerator(
    rescale=1./255,
    rotation_range=180,
    width_shift_range=0.2,
    height_shift_range=0.2,
)

dir_It = datagen.flow_from_directory(
    "data/",
    batch_size=1,
    save_to_dir="output/",
    save_prefix="",
    save_format='png',
)

for _ in range(5):
    img, label = dir_It.next()
    print(img.shape)   #  (1,256,256,3)
    plt.imshow(img[0])
    plt.show()