简体   繁体   English

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

[英]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:在使用 keras ImageDataGenerator 和 flow_from_directory 之后,我一直在尝试理解这段代码:

sample_training_images, _ = next(train_data_gen) sample_training_images, _ = next(train_data_gen)

plotImages(sample_training_images[:5]) 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?我之前对 next 的理解是它获得下一次迭代而不是所有迭代,但是在这种情况下,它似乎返回所有内容,然后“plotimages”可以绘制前 5 次迭代,有人可以向我解释这种行为吗?

*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) (1,0,1 等) *train_data_gen.target_size 是 (150,150) *sample_training_images.shape 是 (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这段代码取自这个挑战:https ://github.com/a-mt/fcc-cat-dog/blob/main/fcc_cat_dog.ipynb

def plotImages(images_arr, probabilities = False): 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()

It's because next function returns a batch of data.这是因为next函数返回了batch数据。 In the link you provided, the batch size is set to 128 hence it returns 128 images.在您提供的链接中, 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.

相关问题 为什么 keras Batchnorm 中只有一个动量参数? - Why is there just one momentum parameter in keras Batchnorm? keras cnn模型仅为所有测试图像预测一个类 - keras cnn model only predicts one class for all the test images 扩充 keras 文件夹中的所有图像 - Augment all the images in a folder in keras Keras ImageDataGenerator 返回具有意外失真的图像 - Keras ImageDataGenerator returns images with unexpected distortion Keras:损失函数为什么必须为每个批处理项目返回一个标量,而不仅仅是一个标量? - Keras: Why do loss functions have to return one scalar per batch item rather than just one scalar? 为什么 tf.keras.preprocessing.image_dataset_from_director 不拍摄所有图像? - Why does tf.keras.preprocessing.image_dataset_from_director not take all images? 为什么 keras 神经网络对所有不同的图像预测相同的数字? - Why does keras neural network predicts the same number for all different images? Keras:将所有图像保存在一个目录中 - Keras: keep all images in a single directory keras中多标签图像的一种热编码 - One hot encoding of multi label images in keras 为什么keras在一个终端会话中正确安装,但在所有后续会话和我的jupyter笔记本中它说它没有安装? - why is keras installed correctly in one terminal session but in all subsequent sessions and my jupyter notebook it says it's not installed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM