3

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

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

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

EDIT :

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)
Mattia Surricchio
  • 1,362
  • 2
  • 21
  • 49
  • did you get error message? always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Dec 06 '19 at 17:16
  • Edited with error – Mattia Surricchio Dec 06 '19 at 21:28
  • assing result to one variable `result = next(train_img_gen)` and print it `print(result)` - it seems it returns only one value, not two as you expect. Maybe you will need on `x_batch = next(train_img_gen)`. – furas Dec 06 '19 at 21:31
  • result returns a matrix of elements with this shape: (4, 256, 256, 3) – Mattia Surricchio Dec 06 '19 at 21:36
  • so you may have 4 images with size `256x256` and RGB (3) colors. You can try to display `plt.imshow( result[0] )` `plt.imshow( result[1] )`, etc. – furas Dec 06 '19 at 21:51

2 Answers2

7

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

The working code is:

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

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

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

Mattia Surricchio
  • 1,362
  • 2
  • 21
  • 49
0
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

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214