fastai

2.8.7 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to train an image classification model using fastai. It downloads the Oxford-IIIT Pet Dataset, creates `DataLoaders`, initializes a `Learner` with a pre-trained `resnet34` model, and fine-tunes it for one epoch. This showcases fastai's high-level API for rapidly achieving state-of-the-art results.

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.")

view raw JSON →