简体   繁体   中英

Why Next on Keras DictionaryIterator returns all the images and not just one?

I've been trying to understand this piece of code after using keras ImageDataGenerator and flow_from_directory:

sample_training_images, _ = next(train_data_gen)

plotImages(sample_training_images[:5])

My previous understanding of next is that it gets the next iteration and not all the iterations, however in this case it seems to return everything and then "plotimages" can plot the first 5 iteration, can anyone explain to me this behavior?

*Some additional information - the underscore is used to discard the return of all labels. (1,0,1, etc) *train_data_gen.target_size is (150,150) *sample_training_images.shape is (128, 150, 150, 3)

This code was taken from this challenge:https://github.com/a-mt/fcc-cat-dog/blob/main/fcc_cat_dog.ipynb

def plotImages(images_arr, probabilities = False):

fig, axes = plt.subplots(len(images_arr), 1, figsize=(5,len(images_arr) * 3))
if probabilities is False:
  for img, ax in zip( images_arr, axes):
      ax.imshow(img)
      ax.axis('off')
else:
  for img, probability, ax in zip( images_arr, probabilities, axes):
      ax.imshow(img)
      ax.axis('off')
      if probability > 0.5:
          ax.set_title("%.2f" % (probability*100) + "% dog")
      else:
          ax.set_title("%.2f" % ((1-probability)*100) + "% cat")
plt.show()

It's because next function returns a batch of data. In the link you provided, the batch size is set to 128 hence it returns 128 images.

sample_training_images, _ = next(train_data_gen)  # returns 128 images
plotImages(sample_training_images[:5])  # selects the first 5 images of those 128 images

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM