What is the fastai API?

Lesson 1

Lesson Video:

Three Levels

Consists of 3 API levels:

If you ask me, the three API levels go {Task}DataLoaders.from_{source} -> DataBlock -> Datasets.

If you ask Jeremy it goes DataBlock -> Datasets -> raw PyTorch.

I don’t consider raw PyTorch part of the data API

  • Highest level of the API, but not much flexibility
  • Debateable, but I consider it to be

Consists of:

  • ImageDataLoaders
  • SegmentationDataLoaders
  • TextDataLoaders
  • TabularDataLoaders

Each have various class constructors

  • from_folder
  • from_path_func
  • from_name_func
  • from_path_re
  • from_name_re
  • from_df
  • from_csv
  • from_lists
  • from_label_func
  • from_folder
  • from_df
  • from_csv
  • from_df
  • from_csv

You should graduate from these well before you finish this course

  • Medium level API (debateable)
  • Medium flexibility
  • Building blocks of the framework
  • What we will focus on
Warning
  • It’s difficult to implement custom bits with this class
  • ImageBlock
  • MaskBlock
  • PointBlock
  • BBoxBlock
  • TextBlock
  • TabularPandas
  • CategoryBlock
  • MultiCategoryBlock
  • RegressionBlock

TransformBlock

  • Lowest Level
  • Highest Flexibility
  • Hardest to learn due to so much magic
  • Consists of the “groundwork” for all the other wrappers
  • PILBase
  • PILImage
  • PILImageBW
  • PILMask
  • TensorPoint
  • TensorBBox
  • LabelBBox
  • PointScaler
  • BBoxLabeler
  • Tabular
  • TensorText
  • LMTensorText
  • Tokenizer

How they all intertwine

Define a set of blocks for your problem



Write how to extract the information needed for each Block from the source



Create a splitting function that takes in some data and returns a tuple of indicies


List a set of item and batch transforms to be applied to the data



Call the dataloaders function and pass in a batch size

G A Define Block AA blocks = (ImageBlock, CategoryBlock) B Getters A->B BB def get_x(…): return x def get_y(…): return y C Split the data B->C CC splitter = RandomSplitter(split_pct=0.2) D Label and augment the data C->D DD [Resize(224)] [*aug_transforms(), Normalize()] E DataLoader D->E EE .dataloaders(bs=batch_size)

What are our options?

  • get_x
  • get_y
  • get_items
  • n_inp
  • ColSplitter
  • EndSplitter
  • FileSplitter
  • FuncSplitter
  • GrandparentSplitter
  • IndexSplitter
  • IndexSplitter
  • MaskSplitter
  • RandomSplitter
  • RandomSubsetSplitter
  • TrainTestSplitter
  • Categorize
  • ColReader
  • MultiCategorize
  • RegexLabeller
  • RegressionSetup
  • parent_label

Building some DataLoaders

When we are ready to create our DataLoaders, we pass in the items to use, a batch_size, and the transforms to be performed to the DataLoader constructor:

G B Items or path A DataLoader B->A C Batch Size (bs) C->A D Transforms D->A BB Item (CPU) BB->D CC Batch (GPU) CC->D

Item transforms are happened first and are used to prepare a batch. This includes transformations such as converting to a torch.tensor, ensuring that images, text, or tabular data can be collated together (the same size/shape).

Batch transforms are performed on an entire subset of data (after they have all been through the item transforms) at once as a big matrix. Examples can include further resizing, normalizing the data, and other data augmentation. As a result they are multitudes faster

Now let’s get back to the lesson