繁体   English   中英

为什么 Keras DictionaryIterator 上的 Next 返回所有图像而不仅仅是一个?

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

在使用 keras ImageDataGenerator 和 flow_from_directory 之后,我一直在尝试理解这段代码:

sample_training_images, _ = next(train_data_gen)

plotImages(sample_training_images[:5])

我之前对 next 的理解是它获得下一次迭代而不是所有迭代,但是在这种情况下,它似乎返回所有内容,然后“plotimages”可以绘制前 5 次迭代,有人可以向我解释这种行为吗?

*一些附加信息 - 下划线用于丢弃所有标签的返回。 (1,0,1 等) *train_data_gen.target_size 是 (150,150) *sample_training_images.shape 是 (128, 150, 150, 3)

这段代码取自这个挑战:https ://github.com/a-mt/fcc-cat-dog/blob/main/fcc_cat_dog.ipynb

def plotImages(images_arr,概率=假):

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

这是因为next函数返回了batch数据。 在您提供的链接中, batch size设置为 128,因此它返回 128 张图像。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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