High level API to quickly get your data in a DataLoaders
📘 Note: Several domain-specific blocks such as ImageBlock, BBoxBlock, PointBlock, and CategoryBlock are implemented on top of TransformBlock. These blocks are designed to handle common tasks in computer vision, classification, and regression. See the Vision Blocks section for more details.
def TransformBlock( type_tfms:list=None, # One or more `Transform`s item_tfms:list=None, # `ItemTransform`s, applied on an item batch_tfms:list=None, # `Transform`s or `RandTransform`s, applied by batch dl_type:TfmdDL=None, # Task specific `TfmdDL`, defaults to `TfmdDL` dls_kwargs:dict=None, # Additional arguments to be passed to `DataLoaders`):
A basic wrapper that links defaults transforms for the data block API
def CategoryBlock( vocab:collections.abc.MutableSequence | pandas.Series=None, # List of unique class names sort:bool=True, # Sort the classes alphabetically add_na:bool=False, # Add `#na#` to `vocab`):
def MultiCategoryBlock( encoded:bool=False, # Whether the data comes in one-hot encoded vocab:collections.abc.MutableSequence | pandas.Series=None, # List of unique class names add_na:bool=False, # Add `#na#` to `vocab`):
def DataBlock( blocks:list=None, # One or more `TransformBlock`s dl_type:TfmdDL=None, # Task specific `TfmdDL`, defaults to `block`'s dl_type or`TfmdDL` getters:list=None, # Getter functions applied to results of `get_items` n_inp:int=None, # Number of inputs item_tfms:list=None, # `ItemTransform`s, applied on an item batch_tfms:list=None, # `Transform`s or `RandTransform`s, applied by batch get_items:NoneType=None, splitter:NoneType=None, get_y:NoneType=None, get_x:NoneType=None):
To build a DataBlock you need to give the library four things: the types of your input/labels, and at least two functions: get_items and splitter. You may also need to include get_x and get_y or a more generic list of getters that are applied to the results of get_items.
splitter is a callable which, when called with items, returns a tuple of iterables representing the indices of the training and validation data.
def dataloaders( source, # The data source path:str='.', # Data source and default `Learner` path verbose:bool=False, # Show verbose messages bs:int=64, # Size of batch shuffle:bool=False, # Whether to shuffle data num_workers:int=None, # Number of CPU cores to use in parallel (default: All available up to 16) do_setup:bool=True, # Whether to run `setup()` for batch transform(s) pin_memory:bool=False, timeout:int=0, batch_size:NoneType=None, drop_last:bool=False, indexed:NoneType=None, n:NoneType=None, device:NoneType=None, persistent_workers:bool=False, pin_memory_device:str='', wif:NoneType=None, before_iter:NoneType=None, after_item:NoneType=None, before_batch:NoneType=None, after_batch:NoneType=None, after_iter:NoneType=None, create_batches:NoneType=None, create_item:NoneType=None, create_batch:NoneType=None, retain:NoneType=None, get_idxs:NoneType=None, sample:NoneType=None, shuffle_fn:NoneType=None, do_batch:NoneType=None)->DataLoaders:
Each type comes with default transforms that will be applied:
at the base level to create items in a tuple (usually input,target) from the base elements (like filenames)
at the item level of the datasets
at the batch level
They are called respectively type transforms, item transforms, batch transforms. In the case of MNIST, the type transforms are the method to create a PILImageBW (for the input) and the Categorize transform (for the target), the item transform is ToTensor and the batch transforms are Cuda and IntToFloatTensor. You can add any other transforms by passing them in DataBlock.datasets or DataBlock.dataloaders.
with expect_fail(): DataBlock(wrong_kwarg=42, wrong_kwarg2='foo')
We can pass any number of blocks to DataBlock, we can then define what are the input and target blocks by changing n_inp. For example, defining n_inp=2 will consider the first two blocks passed as inputs and the others as targets.
with expect_fail(msg='get_y contains 2 functions, but must contain 1 (one for each output)'): DataBlock((ImageBlock, ImageBlock, CategoryBlock), get_items=get_image_files, splitter=GrandparentSplitter(), get_y=[parent_label, noop], n_inp=2)
pets.summary(path/"images", bs=8)
dls = pets.dataloaders(path/"images", bs=8)
dls.show_batch(unique=True,...) # See different tfms effect on the same image.