fastai
fastai is a deep learning library that simplifies training fast and accurate neural nets using modern best practices. It's built on top of PyTorch and offers both high-level APIs for quick model development and lower-level components for researchers. The library maintains an active development pace with frequent patch and minor releases, often tied to PyTorch version updates, to ensure compatibility and leverage the latest deep learning advancements.
Warnings
- breaking fastai v2 is a complete rewrite and is *not API-compatible* with fastai v1. Code written for v1 will break. Key changes include data loading (`DataBunch` in v1 to `DataBlock`/`DataLoaders` in v2) and the callback API (e.g., `on_*_begin` to `before_*`).
- gotcha PyTorch Version Compatibility: fastai generally requires specific PyTorch versions. While recent versions (2.8.7+) may be more lenient, historically, strict compatibility has been crucial. Installing an incompatible PyTorch version can lead to runtime errors or unexpected behavior.
- gotcha The heavy use of `from fastai.app_name.all import *` for convenience in interactive notebooks can lead to namespace pollution and make it difficult to trace the origin of functions and classes, potentially causing name clashes or confusion in larger projects.
- gotcha On Windows, when running fastai code within Jupyter notebooks, `num_workers` for `DataLoader` is automatically reset to 0 to avoid multiprocessing-related hangs. This significantly slows down data loading, especially for I/O-heavy tasks like computer vision.
- gotcha Dependency conflicts with NumPy: Newer versions of NumPy (e.g., NumPy 2.0) can cause incompatibility issues with other compiled modules that fastai, or its underlying libraries, might depend on. This can result in 'procedure not found' or similar errors during runtime.
Install
-
pip install fastai -
pip install "torch>=X.X,<Y.Y" torchvision torchaudio --index-url https://download.pytorch.org/whl/cuXX pip install fastai
Imports
- vision.all
from fastai.vision.all import *
- text.all
from fastai.text.all import *
- tabular.all
from fastai.tabular.all import *
- basics
from fastai.basics import *
Quickstart
import os
from fastai.vision.all import *
# Ensure the necessary directory for models is available
# os.environ['XDG_CACHE_HOME'] = os.environ.get('XDG_CACHE_HOME', './.cache')
# Download and untar the dataset (Oxford-IIIT Pet Dataset)
path = untar_data(URLs.PETS)/'images'
# Define a function to label images (e.g., determine if an image name starts with an uppercase letter, meaning it's a cat)
def is_cat(x): return x[0].isupper()
# Create DataLoaders from image files in the specified path
# valid_pct splits data for validation, seed ensures reproducibility
# label_func applies 'is_cat' for labels, item_tfms resizes images
dls = ImageDataLoaders.from_name_func(
path, get_image_files(path), valid_pct=0.2, seed=42,
label_func=is_cat, item_tfms=Resize(224)
)
# Create a Learner with a pre-trained ResNet34 model and error_rate as a metric
learn = vision_learner(dls, resnet34, metrics=error_rate)
# Fine-tune the model for one epoch. This is a form of transfer learning.
learn.fine_tune(1)
print("Model training complete for image classification. You can now use `learn.predict(img)` for inference.")