Basic classes to contain the data for model training.

Get your data ready for training

This module defines the basic DataBunch object that is used inside Learner to train a model. This is the generic class, that can take any kind of fastai Dataset or DataLoader. You'll find helpful functions in the data module of every application to directly create this DataBunch for you.

class DataBunch[source]

DataBunch(train_dl:DataLoader, valid_dl:DataLoader, fix_dl:DataLoader=None, test_dl:Optional[DataLoader]=None, device:device=None, tfms:Optional[Collection[Callable]]=None, path:PathOrStr='.', collate_fn:Callable='data_collate', no_check:bool=False)

Bind train_dl,valid_dl and test_dl in a a data object.

It also ensure all the dataloaders are on device and apply to them tfms as batch are drawn (like normalization). path is used internally to store temporary files, collate_fn is passed to the pytorch Dataloader (replacing the one there) to explain how to collate the samples picked for a batch. By default, it applies data to the object sent (see in vision.image or the data block API why this can be important).

train_dl, valid_dl and optionally test_dl will be wrapped in DeviceDataLoader.

Factory method

create[source]

create(train_ds:Dataset, valid_ds:Dataset, test_ds:Optional[Dataset]=None, path:PathOrStr='.', bs:int=64, num_workers:int=4, tfms:Optional[Collection[Callable]]=None, device:device=None, collate_fn:Callable='data_collate', no_check:bool=False) → DataBunch

Create a DataBunch from train_ds, valid_ds and maybe test_ds with a batch size of bs.

num_workers is the number of CPUs to use, tfms, device and collate_fn are passed to the init method.

Visualization

show_batch[source]

show_batch(rows:int=5, ds_type:DatasetType=<DatasetType.Train: 1>, kwargs)

Show a batch of data in ds_type on a few rows.

Grabbing some data

dl[source]

dl(ds_type:DatasetType=<DatasetType.Valid: 2>) → DeviceDataLoader

Returns appropriate Dataset for validation, training, or test (ds_type).

one_batch[source]

one_batch(ds_type:DatasetType=<DatasetType.Train: 1>, detach:bool=True, denorm:bool=True, cpu:bool=True) → Collection[Tensor]

Get one batch from the data loader of ds_type. Optionally detach and denorm.

one_item[source]

one_item(item, detach:bool=False, denorm:bool=False)

Get item into a batch. Optionally detach and denorm.

sanity_check[source]

sanity_check()

Check the underlying data in the training set can be properly loaded.

Empty DataBunch for inference

export[source]

export(fname:str='export.pkl')

Export the minimal state of self for inference in self.path/fname.

load_empty[source]

load_empty(path, fname:str='export.pkl')

Load an empty DataBunch from the exported file in path/fname with optional tfms.

This method should be used to create a DataBunch at inference, see the corresponding tutorial.

Dataloader transforms

add_tfm[source]

add_tfm(tfm:Callable)

Adds a transform to all dataloaders.

Using a custom Dataset in fastai

If you want to use yur pytorch Dataset in fastai, you may need to implement more attributes/methods if you want to use the full functionality of the library. Some functions can easily be used with your pytorch Dataset if you just add an attribute, for others, the best would be to create your own ItemList by following this tutorial. Here is a full list of what the library will expect.

Basics

First of all, you obviously need to implement the methods __len__ and __getitem__, as indicated by the pytorch docs. Then the most needed things would be:

For a specific application

In text, your dataset will need to have a vocab attribute that should be an instance of Vocab. It's used by text_classifier_learner and language_model_learner when building the model.

In tabular, your dataset will need to have a cont_names attribute (for the names of continuous variables) and a get_emb_szs method that returns a list of tuple (n_classes, emb_sz) representing, for each categorical variable, the number of different codes (don't forget to add 1 for nan) and the corresponding embedding size. Those two are used with the c attribute by tabular_learner.

Functions that really won't work

To make those last functions work, you really need to use the data block API and maybe write your own custom ItemList.

class DeviceDataLoader[source]

DeviceDataLoader(dl:DataLoader, device:device, tfms:List[Callable]=None, collate_fn:Callable='data_collate')

Bind a DataLoader to a torch.device.

Put the batches of dl on device after applying an optional list of tfms. collate_fn will replace the one of dl. All dataloaders of a DataBunch are of this type.

Factory method

create[source]

create(dataset:Dataset, bs:int=64, shuffle:bool=False, device:device=device(type='cuda'), tfms:Collection[Callable]=None, num_workers:int=4, collate_fn:Callable='data_collate', kwargs:Any)

Create DeviceDataLoader from dataset with bs and shuffle: processs using num_workers.

The given collate_fn will be used to put the samples together in one batch (by default it grabs their data attribute). shuffle means the dataloader will take the samples randomly if that flag is set to True, or in the right order otherwise. tfms are passed to the init method. All kwargs are passed to the pytorch DataLoader class initialization.

Methods

add_tfm[source]

add_tfm(tfm:Callable)

Add tfm to self.tfms.

remove_tfm[source]

remove_tfm(tfm:Callable)

Remove tfm from self.tfms.

new[source]

new(kwargs)

Create a new copy of self with kwargs replacing current values.

proc_batch[source]

proc_batch(b:Tensor) → Tensor

Proces batch b of TensorImage.

`DatasetType`

Enum = [Train, Valid, Test, Single, Fix]

Internal enumerator to name the training, validation and test dataset/dataloader.