Purpose behind next(iter(md.val_dl)) or next(iter(md.aug_dl))

It seems that from an Image Dataset if I want to see images from a set of validation images, if I run

next(iter(md.val_dl)) or next(iter(md.aug_dl)) multiple times

I do not get the next batch. I get the same batch over and over. (I see the same image)

however, with next(iter(md.trn_dl)) I see a different image each time

Can someone throw some light on this?

Init method seems to have shuffle=True for only trn_dl and not for val_dl or aug_dl. Although IMHO shuffle=False and next(iter(val_dl)) should still work

2 Likes

Each time you call iter() on the data loader, a new iterator is generated. To loop through all the images, you can repeatedly call next on the same iterator:

new_iter = iter(md.val_dl)
first_batch = next(new_iter)
second_batch= next(new_iter)

https://docs.python.org/3.5/glossary.html#term-iterator

3 Likes

I see…
I will try that right away.
Thanks
Update: @kosborne is so right !!
Thank you sir!

Hi Sam,
1 related question, I am referring to pascal notebook and have slight confusion on md.aug_dl. In what manner does it differ to md.val_dl ?
if x,y=next(iter(md.aug_dl)) returns same batch of x for each i inside loop then why do we get augmented images?

idx=3
fig,axes = plt.subplots(3,3, figsize=(9,9))
for i,ax in enumerate(axes.flat):
    x,y=next(iter(md.aug_dl))
    ima=md.val_ds.denorm(to_np(x))[idx]
    b = bb_hw(to_np(y[idx]))
    print(b)
    show_img(ima, ax=ax)
    draw_rect(ax, b)

Output:

[115. 63. 241. 312.]
[115. 63. 241. 312.]
[115. 63. 241. 312.]
[115. 63. 241. 312.]
[115. 63. 241. 312.]
[115. 63. 241. 312.]
[115. 63. 241. 312.]
[115. 63. 241. 312.]
[115. 63. 241. 312.]

@abhishekmishra
The entire code in this cell is to grab random augmentation of an image using iter(md.aug_dl). (You will get a whole batch really), denorm it using md.val_ds.denorm and then show it in a grid (i.e 3 X 3 grid).

Now if you run next( iter(md.aug_dl)) you get a batch of images that is subjected to some randomized augmentation. The third of the images in the girl smoking a sparkling cigarette. :slight_smile: It is this image that you show in the grid after normalization

Each time you run next( iter(md.aug_dl)) in the for loop, you get the same batch with different random augmentation. This third image is still the girl smoking cigarettle but augmented differently in a random fashion.

If you want to show different image in the 3 x 3 grid (which is not what Jeremy wanted to do, but I did for experimentation) I would have to change the two lines of code:

for i,ax in enumerate(axes.flat):
x,y=next(iter(md.aug_dl))

to

my_iter = iter(md.aug_dl)
for i,ax in enumerate(axes.flat):
x,y=next(my_iter)

Having said that…your specific question was how is val_dl different from aug_dl. (both do not shuffle images to build a batch so every run of next(iter(md.aug_dl)) or next(iter(md.val_dl)) will give you same batch of images but aug_dl will augment each image and val_dl will not.

I am sorry for a long explanation but I am trying to reinforce my own learning in the process

1 Like

Thanks @sam2 .
I understood now after listening lesson 2.