从 Image Generator 绘制图像

Plot images from Image Generator

我正在尝试绘制由我的图像生成器创建的图像。到目前为止,这是我提供给生成器的数据代码:

train_img_gen = train_img_data_gen.flow_from_directory(os.path.join(training_dir, 'images'),
                                                   target_size=(img_h, img_w),
                                                   batch_size=bs, 
                                                   class_mode=None, # Because we have no class subfolders in this case
                                                   shuffle=True,
                                                   interpolation='bilinear',
                                                   seed=SEED)
#edited part following the already existing answer on Whosebug
x_batch, y_batch = next(train_img_gen)
for i in range (0,32):
    image = x_batch[i]
    plt.imshow(image.transpose(2,1,0))
    plt.show()

我关注了这个问题:Keras images,但没有成功。

我如何绘制(例如)我的 imageGenerator 生成的前 n 个图像?

编辑:

我添加了上述问题中使用的代码,但出现此错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-1a18ce1c1a76> in <module>
     54 valid_gen = zip(valid_img_gen, valid_mask_gen)
     55 
---> 56 x_batch, y_batch = next(train_img_gen)
     57 for i in range (0,32):
     58     image = x_batch[i]

ValueError: too many values to unpack (expected 2)
for i in range(len(train_img_gen)):
    batch = train_img_gen[i]

    if batch is made of x and y: #this line is pseudocode
        x, y = batch

    if batch has only x: #this line is peudocode
        x = batch 

    print('images in batch:', len(x))

    for image in x:
        plot

最后我解决了问题,是维度的问题

工作代码是:

x= train_img_gen.next()
for i in range(0,4):
    image = x[i]
    plt.imshow(image)
    plt.show()

生成器 returns 每次迭代都会生成一个形状为 (4,256,256,3) 的矩阵,这意味着我们有 4 张大小为 256x256 的图像和 3 个通道 (RGB)。

ImageDataGenerator 一次处理 4 个图像的“块”(至少在这种情况下,我没有关于它每次加载多少图像的官方参考),因为它的主要目的是加载训练模型时动态图像(避免在内存中预加载大量数据)。