Obtaining y/labels from DataLoaders in fastai2

In fastai1 there was ImageDataBunch.train_ds.x and ImageDataBunch.train_ds.y. Instead of (Image)DataBunch I use DataLoaders now, which also has a train_ds attribute. But those don’t have x and y anymore.

In fastai2 how do I obtain a list of all y/labels?

3 Likes

labels = learn.dls.train_ds.items[learn.dls.train_ds.cols[1].items]

1 Like

I was getting this error

AttributeError: cols

Can anyone help me out with this one?

Struggled with finding the classes/ys/labels (data.classes from fastai1) myself. Seems like in fastai2 (version 2.5.2) they are stored under the dataloader’s vocab like this:

learn.dls.train.vocab
learn.dls.valid.vocab

You can also just do learn.dls.vocab too

1 Like

Thanks! You’re doing some great work, thanks for posting it all here. I have a couple of your blog articles open and am very much looking forward to reading them.

With fastai I sometimes find things hard to learn (in the beginning). Wasn’t very straightforward to find the vocab. Also I find the notebooks make it at times somewhat harder to “explore” than for example an IDE like PyCharm where I can easily step through code and see where stuff happens (in this case the resolution of labels). You seem to have found a good way through the library …