本文介绍了绘制图像生成器中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to plot the images created by my image generator. So far this is the code of my data given to the 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 stackoverflow
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图片,但没有成功.

I followed this question: Keras images, but without any success.

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

How can i plot (for example) the first n images generated by my imageGenerator?

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

I added the code used in the above mentioned question, but i get this error:

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

推荐答案

最后,我解决了这个问题,这是尺寸方面的问题.

In the end i solved the problem, it was a problem with dimensions.

工作代码为:

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

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

The generator returns for each iteration a matrix with shape (4,256,256,3), which means that we have 4 images of size 256x256 and 3 channels (RGB).

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

The ImageDataGenerator works with "blocks" of 4 images at time(at least in this case, i have no official reference about how many images does it load everytime), since its main purpose is to load images on the fly while training the model (avoiding to pre-load a huge amount of data in memory).

这篇关于绘制图像生成器中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 17:14