Intermediate
Computer Vision with FastAI
Build state-of-the-art image classifiers in just a few lines of code using vision_learner, fine_tune, built-in data augmentation, and powerful interpretation tools.
Your First Image Classifier
This is the power of FastAI — a complete, state-of-the-art image classifier in 4 lines:
Python
from fastai.vision.all import * # 1. Get data path = untar_data(URLs.PETS) # 2. Create DataLoaders dls = ImageDataLoaders.from_name_re( path, get_image_files(path/'images'), pat=r'/([^/]+)_\d+.jpg$', item_tfms=Resize(224), batch_tfms=aug_transforms(size=224) ) # 3. Create learner with pre-trained ResNet34 learn = vision_learner(dls, resnet34, metrics=error_rate) # 4. Fine-tune! learn.fine_tune(3)
What fine_tune() does automatically: (1) Freezes the pre-trained backbone, (2) trains only the new head for 1 epoch, (3) unfreezes everything, (4) trains the entire model with discriminative learning rates for your specified number of epochs. All best practices baked in!
The DataBlock API
For more control over data loading, use the flexible DataBlock API:
Python
dblock = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=RandomSplitter(valid_pct=0.2, seed=42),
get_y=parent_label,
item_tfms=Resize(460),
batch_tfms=aug_transforms(size=224, min_scale=0.75)
)
dls = dblock.dataloaders(path/'images')
dls.show_batch(max_n=9) # Visualize a batch
Data Augmentation
FastAI applies GPU-accelerated augmentation via aug_transforms():
Python
# Default augmentations (rotation, flip, zoom, warp, lighting) batch_tfms = aug_transforms(size=224) # Customize augmentations batch_tfms = aug_transforms( size=224, mult=2.0, # Increase augmentation intensity do_flip=True, flip_vert=False, # Only horizontal flips max_rotate=15.0, max_zoom=1.2, max_lighting=0.3, max_warp=0.2, p_affine=0.75, p_lighting=0.75 )
Model Interpretation
FastAI includes powerful tools to understand what your model learned and where it makes mistakes:
Python
# Create interpretation object interp = ClassificationInterpretation.from_learner(learn) # Confusion matrix interp.plot_confusion_matrix(figsize=(12, 12)) # Show top losses (images the model got most wrong) interp.plot_top_losses(9, figsize=(15, 10)) # Most confused categories interp.most_confused(min_val=5)
Choosing Architectures
| Architecture | Accuracy | Speed | Best For |
|---|---|---|---|
| resnet18 | Good | Very fast | Quick experiments, limited GPU |
| resnet34 | Better | Fast | Good default choice |
| resnet50 | Great | Moderate | Production models |
| convnext_tiny | Excellent | Moderate | Modern architecture |
| vit_small_patch16_224 | Excellent | Slower | Vision Transformer |
Next Up: Tabular Data
Learn how to apply FastAI's magic to structured/tabular data with TabularDataLoaders.
Next: Tabular Data →
Lilly Tech Systems