'DataBunch' object is not subscriptable (Custom dataloader)

Has anyone here succeed to load a normal dataloader or dataset (in pytorch) into a databunch of fastai?
Me when i tried to load it into databunch, then when i try to display one batch in dataloader, it gives me an error ‘DataBunch’ object is not subscriptable

Here what i’ve done so far:

train_dataset = ...
val_dataset = ...
collate_fn = ...
train_dl = torch.utils.data.Dataloader(train_dataset, batch_size=4, collate_fn = _collate_fn, num_workers=1)
val_dl = torch.utils.data.Dataloader(val_dataset, batch_size=4, collate_fn= _collate_fn, num_workers=1)

#load into databunch
from fastai.basic_data import DataBunch
data = DataBunch(train_dl=train_dl, valid_dl= val_dl)

Then when i try to run each batch:

for xb, yb in progress_bar(data.train_dl):
    print(xb)

It gives me an error: TypeError: ‘DataBunch’ object is not subscriptable
It seems the library lacks a flexibility when we want to customize our dataset and model or I miss something here?

Databunch expects DeviceDataLoaders (fastai) not pytorch dataloaders. The easiest way to use semi-regular pytorch datasets is to use the factory method create to create your databunch from pytorch datasets:

https://docs.fast.ai/basic_data.html#DataBunch.create

you also have to create some special attributes in order to make pytorch datasets work with fastai:

https://docs.fast.ai/basic_data.html#Using-a-custom-Dataset-in-fastai

4 Likes

Thanks for your reply. Do you have any examples/repo/projects that show explicitly how to use it?