If a
vocabis passed, only the folders with names invocabare kept.
Hi,
Does anyone know how to use the vocab parameter?
I am looking for some examples and explanation to understand the usage.
Thnx
If a
vocabis passed, only the folders with names invocabare kept.
Hi,
Does anyone know how to use the vocab parameter?
I am looking for some examples and explanation to understand the usage.
Thnx
Suppose you have ten folders, where one includes pictures of airplanes, another automobiles, another birds, and so on. If you don’t pass in vocab, fastai will assume you have ten labels for the ten folders but you might only be interested in airplanes and automobiles, in which case you can pass vocab = ['airplane', 'automobile'] and fastai will set up your DataLoaders so there are only those two classes.
Example:
from fastai.vision.all import *
path = untar_data(URLs.CIFAR)/'train'
# In this case, dls will have all ten classes of CIFAR
# and there will thus be 10 categories.
dls = ImageDataLoaders.from_folder(path, valid_pct=0.1, seed=42)
# Here, your only labels are airplane and automobile.
# The rest of the folders are ignored.
dls = ImageDataLoaders.from_folder(path, valid_pct=0.1, seed=42,
vocab=['airplane', 'automobile'])
Hey @BobMcDear,
Thanks for the explanation and the example.
They show the very clear usage of vocab parameter.
By the way, sorry for the late acknowledgment.
Regards,
Choi
I wanted bring to notice that dls.show_batch() throws an error if dls is loaded with partial classes. For example:
dls = ImageDataLoaders.from_folder(path, valid_pct=0.1, seed=42,
vocab=['airplane', 'automobile'])
dls.show_batch()
This throws a KeyError if path containes classes other than airplane and automobile. Is there a way to show a batch in this scenario?
Hello @imagine,
The vocab parameter is used to limit or define the set of class labels when loading data—especially in image classification tasks.
In frameworks like fastai, when using ImageDataLoaders.from_folder, the hhaexchangevocab parameter lets you specify which folder names (i.e., class labels) to include. For example:
vocab = ['airplane', 'automobile']
dls = ImageDataLoaders.from_folder(path, vocab=vocab)
This ensures only images from folders named 'airplane' and 'automobile' are used, ignoring others. It’s useful for filtering classes or enforcing label order.
More details and examples are available on fastai’s forum.
Best Regards,
Donna Taylor