[英]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.