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