fastai: Learner, DataLoaders & Transfer Learning
fastai is a deep learning library built on top of PyTorch, providing high-level APIs that accomplish common tasks (image classification, NLP, tabular data) with dramatically less boilerplate -- while still allowing access to raw PyTorch when its higher-level abstractions aren't enough.
Training an Image Classifier
from fastai.vision.all import *
# Sets up the full data pipeline: finds images, splits 20% for
# validation, resizes to a consistent size (required for batching)
dls = ImageDataLoaders.from_folder(
path,
valid_pct=0.2,
item_tfms=Resize(224),
batch_tfms=aug_transforms(), # augmentation applied per-batch on GPU
)
# vision_learner defaults to a PRETRAINED model -- transfer learning
# accessible with minimal code, no manual weight downloading needed
learn = vision_learner(dls, resnet34, metrics=accuracy)
# fine_tune(): trains new final layers first (frozen backbone), then
# unfreezes and trains everything at a lower rate -- a proven recipe
# packaged into one method call
learn.fine_tune(3)The Data Block API
# Declaratively configures each pipeline step -- flexible enough for
# many dataset organizations, far less code than a manual PyTorch
# Dataset class
dblock = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=RandomSplitter(valid_pct=0.2),
get_y=parent_label,
item_tfms=Resize(224),
)
dls = dblock.dataloaders(path)Finding a Good Learning Rate
# Trains briefly while increasing the learning rate, plotting loss
# vs. rate -- avoids guessing or expensive full-training trial-and-error
learn.lr_find()Beyond Vision: Tabular & Text
# Same high-level philosophy applied to tabular data -- categorical
# columns become learned embeddings, continuous columns are normalized
dls = TabularDataLoaders.from_df(
df,
cat_names=['occupation', 'workclass'],
cont_names=['age', 'hours_per_week'],
y_names='salary',
)
learn = tabular_learner(dls, metrics=accuracy)Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free