Fastai - simplification of work with neuralities

онлайн тренажер по питону
Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Key Features and Benefits of fastai

High‑Level API on top of PyTorch

fastai is built on PyTorch but offers a far more convenient and intuitive interface. This means you get the full power of PyTorch with minimal coding effort.

Support for a Wide Range of Tasks

The library supports a broad spectrum of machine‑learning tasks:

  • Image classification
  • Object segmentation
  • Natural language processing
  • Tabular data
  • Collaborative filtering
  • Image and text generation
  • Regression tasks

Built‑in Modern Training Techniques

fastai automatically applies cutting‑edge neural‑network training methods:

  • One Cycle Policy for learning‑rate optimization
  • Mixup and CutMix for data augmentation
  • Progressive resizing for image work
  • Label smoothing for better generalization
  • Automatic mixed precision for faster training

Pre‑trained Models and Transfer Learning

The library provides access to many pre‑trained architectures:

  • ResNet, EfficientNet, RegNet for computer vision
  • LSTM, AWD‑LSTM for text processing
  • UNet for segmentation
  • Transformers through HuggingFace integration

Powerful Visualization and Interpretation Tools

fastai includes built‑in tools for model analysis and interpretation, which are critical for understanding algorithm behavior and debugging.

Installation and Initial Setup

Installing the Library

To install fastai, it is recommended to use conda or pip:

# Install via conda (recommended)
conda install -c fastai fastai

# Or via pip
pip install fastai

Importing Core Modules

# For computer vision
from fastai.vision.all import *

# For NLP
from fastai.text.all import *

# For tabular data
from fastai.tabular.all import *

# For collaborative filtering
from fastai.collab import *

fastai Architecture and Core Components

DataBlock API

DataBlock API — a declarative approach to describing data that lets you precisely define how to load, process, and feed data to a model.

dblock = DataBlock(
    blocks=(ImageBlock, CategoryBlock),
    get_items=get_image_files,
    get_y=parent_label,
    splitter=RandomSplitter(),
    item_tfms=Resize(224),
    batch_tfms=aug_transforms()
)

Learner — the central training class

Learner encapsulates the model, data, loss function, optimizer, and metrics into a single object, simplifying the training process.

Callback System

Callbacks allow you to add functionality to the training loop:

  • EarlyStoppingCallback for early stopping
  • SaveModelCallback for saving the best weights
  • CSVLogger for metric logging
  • ReduceLROnPlateau for adaptive learning‑rate adjustment

Mid‑level API

When needed, fastai provides access to the PyTorch level for fine‑grained tuning and customization.

Working with Images

Image Classification

# Load data
path = untar_data(URLs.PETS)
files = get_image_files(path/"images")

# Function to extract labels
def label_func(fname):
    return re.match(r'^[a-zA-Z_]+', fname.name).group(0)

# Create DataLoaders
dls = ImageDataLoaders.from_name_func(
    path, files, label_func, 
    item_tfms=Resize(224),
    batch_tfms=aug_transforms()
)

# Build and train model
learn = vision_learner(dls, resnet34, metrics=accuracy)
learn.fine_tune(3)

Image Segmentation

# Load segmentation data
path = untar_data(URLs.CAMVID_TINY)
dls = SegmentationDataLoaders.from_label_func(
    path, bs=8, fnames=get_image_files(path/"images"),
    label_func=lambda o: path/"labels"/f"{o.stem}_P{o.suffix}",
    codes=np.loadtxt(path/"codes.txt", dtype=str)
)

# Build U‑Net model
learn = unet_learner(dls, resnet34, metrics=Dice())
learn.fine_tune(10)

Object Detection

# Load detection data
path = untar_data(URLs.COCO_SAMPLE)
dls = ObjectDetectionDataLoaders.from_df(
    df, path, folder="train", 
    label_func=get_annotations,
    bs=16
)

# Build detection model
learn = object_detection_learner(dls, arch=models.retinanet_resnet50_fpn)
learn.fine_tune(5)

Natural Language Processing

Text Classification

# Load IMDB data
path = untar_data(URLs.IMDB)
dls = TextDataLoaders.from_folder(path, valid='test')

# Build classifier
learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5, metrics=accuracy)
learn.fine_tune(4, 1e-2)

Language Modeling

# Build language model
dls_lm = TextDataLoaders.from_folder(path, is_lm=True)
learn_lm = language_model_learner(dls_lm, AWD_LSTM, drop_mult=0.3)
learn_lm.fit_one_cycle(1, 2e-2)

# Save encoder for later use
learn_lm.save_encoder('finetuned')

Transformer Integration

# Use BERT for classification
from transformers import AutoTokenizer, AutoModel

tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
model = AutoModel.from_pretrained('bert-base-uncased')

# Create DataLoaders with custom tokenizer
dls = TextDataLoaders.from_folder(path, tokenizer=tokenizer)

Working with Tabular Data

Tabular Classification

# Load Adult dataset
path = untar_data(URLs.ADULT_SAMPLE)
df = pd.read_csv(path/'adult.csv')

# Define categorical and continuous columns
cat_names = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race']
cont_names = ['age', 'fnlwgt', 'education-num', 'hours-per-week']

# Create DataLoaders
dls = TabularDataLoaders.from_df(
    df, path=path, y_names="salary",
    cat_names=cat_names, cont_names=cont_names,
    procs=[Categorify, FillMissing, Normalize]
)

# Train model
learn = tabular_learner(dls, metrics=accuracy)
learn.fit_one_cycle(5)

Tabular Regression

# For regression tasks
learn = tabular_learner(dls, loss_func=MSELossFlat(), metrics=rmse)
learn.fit_one_cycle(10, 1e-2)

Collaborative Filtering

Recommendation System

# Load MovieLens data
path = untar_data(URLs.ML_SAMPLE)
dls = CollabDataLoaders.from_csv(path/'ratings.csv')

# Build collaborative‑filtering model
learn = collab_learner(dls, n_factors=50, y_range=(0, 5.5))
learn.fit_one_cycle(10, 5e-3)

Training Optimization and Advanced Techniques

One Cycle Policy

One Cycle Policy — a training method that cyclically varies learning rate and momentum to achieve rapid convergence:

# Find optimal learning rate
learn.lr_find()

# Train with One Cycle Policy
learn.fit_one_cycle(5, lr_max=1e-2)

Gradient Accumulation

# For large batches with limited memory
with learn.no_bar():
    learn.fit(5, lr=1e-3, grad_accum=4)

Mixed Precision Training

# Use mixed precision for faster training
learn = learn.to_fp16()
learn.fit_one_cycle(5, 1e-2)

Progressive Resizing

# Gradually increase image size
learn.fine_tune(3, base_lr=1e-3, freeze_epochs=1)
learn.dls = learn.dls.new(size=256)
learn.fine_tune(3, base_lr=1e-4)

Model Interpretation and Analysis

Classification Error Analysis

# Build interpreter
interp = ClassificationInterpretation.from_learner(learn)

# Confusion matrix
interp.plot_confusion_matrix()

# Top losses
interp.plot_top_losses(9, figsize=(15,11))

# Most confused classes
interp.most_confused(min_val=5)

Activation Visualization

# Show what the model focuses on
learn.show_results(max_n=6, figsize=(15,8))

# Grad‑CAM for important regions
from fastai.vision.all import *
learn.show_cam(idx=0)

Feature Importance Analysis

# For tabular data
learn.feature_importance()

# Permutation importance
from sklearn.inspection import permutation_importance
perm_importance = permutation_importance(learn.model, X_val, y_val)

Saving and Deploying Models

Exporting and Loading Models

# Export trained model
learn.export('model.pkl')

# Load model for inference
learn_inf = load_learner('model.pkl')

# Predict on new data
pred, pred_idx, probs = learn_inf.predict(img)

Web‑Interface Deployment

# Simple web app with Gradio
import gradio as gr

def classify_image(img):
    pred, pred_idx, probs = learn_inf.predict(img)
    return {labels[i]: float(probs[i]) for i in range(len(labels))}

gr.Interface(
    fn=classify_image,
    inputs=gr.Image(shape=(224,224)),
    outputs=gr.Label(num_top_classes=3)
).launch()

Production Optimization

# Export to ONNX for cross‑platform use
learn.export_onnx('model.onnx')

# Quantization to reduce model size
learn.model = torch.quantization.quantize_dynamic(
    learn.model, {torch.nn.Linear}, dtype=torch.qint8
)

Integration with the Ecosystem

HuggingFace Integration

# Use pre‑trained transformers
from transformers import AutoTokenizer, AutoModelForSequenceClassification

tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
model = AutoModelForSequenceClassification.from_pretrained('bert-base-uncased')

# Adapt for fastai
learn = text_classifier_learner(dls, model, tokenizer=tokenizer)

PyTorch Lightning Integration

# Use fastai components inside PyTorch Lightning
import pytorch_lightning as pl

class FastaiLightningModule(pl.LightningModule):
    def __init__(self, arch, dls):
        super().__init__()
        self.model = create_cnn_model(arch, dls.c)
        self.dls = dls
    
    def training_step(self, batch, batch_idx):
        x, y = batch
        y_hat = self.model(x)
        loss = F.cross_entropy(y_hat, y)
        return loss

MLflow Integration

# Log experiments with MLflow
import mlflow
import mlflow.pytorch

with mlflow.start_run():
    learn.fit_one_cycle(5, 1e-2)
    mlflow.pytorch.log_model(learn.model, "model")
    mlflow.log_metric("accuracy", learn.recorder.values[-1][1])

Advanced Techniques and Customization

Creating Custom Architectures

# Build a custom architecture
class CustomNet(nn.Module):
    def __init__(self, n_classes):
        super().__init__()
        self.backbone = models.resnet50(pretrained=True)
        self.backbone.fc = nn.Linear(2048, n_classes)
        
    def forward(self, x):
        return self.backbone(x)

# Use in fastai
learn = Learner(dls, CustomNet(dls.c), metrics=accuracy)

Custom Loss Functions

# Define a custom loss
class FocalLoss(nn.Module):
    def __init__(self, alpha=1, gamma=2):
        super().__init__()
        self.alpha = alpha
        self.gamma = gamma
        
    def forward(self, inputs, targets):
        ce_loss = F.cross_entropy(inputs, targets, reduction='none')
        pt = torch.exp(-ce_loss)
        focal_loss = self.alpha * (1-pt)**self.gamma * ce_loss
        return focal_loss.mean()

# Use custom loss
learn = Learner(dls, model, loss_func=FocalLoss(), metrics=accuracy)

Custom Callbacks

# Create a custom callback
class PrintCallback(Callback):
    def after_epoch(self):
        print(f"Epoch {self.epoch}: loss={self.recorder.values[-1][0]:.4f}")

# Use the callback
learn = Learner(dls, model, cbs=PrintCallback())

Comprehensive Table of fastai Methods and Functions

Data Loading and Preparation

Function/Class Description Example Usage
untar_data() Download and extract datasets path = untar_data(URLs.PETS)
get_image_files() Get a list of image file paths files = get_image_files(path)
ImageDataLoaders.from_folder() Create DataLoaders from folder structures dls = ImageDataLoaders.from_folder(path)
ImageDataLoaders.from_name_func() Create DataLoaders with a custom labeling function dls = ImageDataLoaders.from_name_func(path, files, label_func)
TextDataLoaders.from_folder() Load text data from folders dls = TextDataLoaders.from_folder(path)
TabularDataLoaders.from_df() Load tabular data from a DataFrame dls = TabularDataLoaders.from_df(df, y_names='target')
CollabDataLoaders.from_csv() Load collaborative‑filtering data from CSV dls = CollabDataLoaders.from_csv(path)
DataBlock Flexible API for building DataLoaders dblock = DataBlock(blocks=(ImageBlock, CategoryBlock))

Model Creation and Training

Function/Class Description Example Usage
vision_learner() Create a computer‑vision model learn = vision_learner(dls, resnet34, metrics=accuracy)
text_classifier_learner() Create a text classifier learn = text_classifier_learner(dls, AWD_LSTM)
tabular_learner() Create a tabular model learn = tabular_learner(dls, metrics=accuracy)
collab_learner() Create a collaborative‑filtering model learn = collab_learner(dls, n_factors=50)
unet_learner() Create a U‑Net for segmentation learn = unet_learner(dls, resnet34, metrics=Dice())
language_model_learner() Create a language model learn = language_model_learner(dls, AWD_LSTM)
Learner() Base class for custom models learn = Learner(dls, model, loss_func, metrics)

Training Methods

Method Description Parameters
fit() Basic training loop epochs, lr, wd
fit_one_cycle() Train with One Cycle Policy epochs, lr_max, div, div_final, pct_start
fine_tune() Fine‑tune a pre‑trained model epochs, base_lr, freeze_epochs
fit_sgdr() Train with SGDR epochs, lr, cycle_len, cycle_mult
lr_find() Find optimal learning rate start_lr, end_lr, num_it
freeze() Freeze model layers n (number of layers)
unfreeze() Unfreeze all layers -

Prediction and Evaluation

Method Description Return Value
predict() Predict on a single example prediction, index, probabilities
get_preds() Get predictions for a dataset predictions, targets
validate() Validate the model validation_loss, metrics
tta() Test‑time augmentation tta_predictions, tta_targets
show_results() Visualize results -

Model Interpretation

Class/Method Description Use Case
ClassificationInterpretation Interpretation for classification models Error analysis, confusion matrix
plot_confusion_matrix() Draw a confusion matrix Classification
plot_top_losses() Visualize the worst predictions Model debugging
most_confused() Identify the most ambiguous classes Error analysis
show_cam() Grad‑CAM visualization Understanding CNN decisions
feature_importance() Feature importance for tabular data Tabular analysis

Saving and Loading

Method Description Format
save() Save model weights .pth
load() Load weights .pth
export() Export model for inference .pkl
load_learner() Load an exported model .pkl
save_encoder() Save language‑model encoder .pth
load_encoder() Load encoder .pth

Transforms and Augmentations

Function/Class Description Use Case
Resize() Resize images Computer vision
RandomCrop() Random cropping Image augmentation
aug_transforms() Standard set of augmentations Computer vision
Normalize() Data normalization Tabular data
Categorify() Encode categorical features Tabular data
FillMissing() Fill missing values Tabular data
Tokenize() Tokenize text NLP
Numericalize() Convert tokens to numbers NLP

Metrics

Metric Description Use Case
accuracy Classification accuracy Classification
error_rate Error frequency Classification
top_k_accuracy Top‑k accuracy Classification
Dice() Dice coefficient Segmentation
JaccardCoeff() Jaccard index Segmentation
F1Score() F1 score Classification
Precision() Precision Classification
Recall() Recall Classification
rmse Root mean squared error Regression
mae Mean absolute error Regression
R2Score() Coefficient of determination Regression
exp_rmspe Exponential RMSPE Regression

Callbacks

Callback Description Purpose
EarlyStoppingCallback Early stopping of training Prevent overfitting
SaveModelCallback Save best weights Preserve progress
ReduceLROnPlateau Reduce LR on plateau Training optimization
CSVLogger Log to CSV Training monitoring
WandbCallback Integration with Weights & Biases Experiment tracking
TensorBoardCallback Integration with TensorBoard Visualization
GradientClip Clip gradients Stabilize training
MixUp Apply MixUp augmentation Improve generalization

Loss Functions

Loss Function Description Use Case
CrossEntropyLossFlat Flat cross‑entropy Classification
MSELossFlat Mean squared error Regression
L1LossFlat Mean absolute error Regression
BCEWithLogitsLossFlat BCE with logits Binary classification
LabelSmoothingCrossEntropy Cross‑entropy with label smoothing Classification
FocalLoss Focal loss Imbalanced classification
DiceLoss Dice loss Segmentation

Real‑World Application Examples

Medical Diagnosis

# Chest X‑ray classification
path = Path('chest_xray_data')
dls = ImageDataLoaders.from_folder(
    path, train='train', valid='test',
    item_tfms=Resize(224),
    batch_tfms=aug_transforms(size=224, min_scale=0.8)
)

learn = vision_learner(dls, resnet50, metrics=[accuracy, F1Score()])
learn.fine_tune(5)

# Interpret results
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()

Sentiment Analysis in Reviews

# Binary sentiment classification
path = untar_data(URLs.IMDB)
dls = TextDataLoaders.from_folder(path, valid='test')

learn = text_classifier_learner(dls, AWD_LSTM, drop_mult=0.5)
learn.fine_tune(4, 1e-2)

# Predict on new text
text = "This movie was absolutely fantastic!"
pred = learn.predict(text)
print(f"Prediction: {pred[0]}, Confidence: {pred[2][pred[1]]:.4f}")

Satellite Image Segmentation

# Land‑use segmentation
path = Path('satellite_data')
codes = ['background', 'building', 'road', 'water', 'vegetation']

dls = SegmentationDataLoaders.from_label_func(
    path, bs=4, fnames=get_image_files(path/"images"),
    label_func=lambda o: path/"masks"/f"{o.stem}.png",
    codes=codes,
    item_tfms=Resize(512),
    batch_tfms=aug_transforms(size=512, min_scale=0.75)
)

learn = unet_learner(dls, resnet34, metrics=Dice())
learn.fine_tune(10)

Recommendation System

# Movie recommendation engine
path = untar_data(URLs.ML_100k)
ratings = pd.read_csv(path/'u.data', delimiter='\t', 
                     names=['user','movie','rating','timestamp'])

dls = CollabDataLoaders.from_df(ratings, item_name='movie', 
                               rating_name='rating', user_name='user')

learn = collab_learner(dls, n_factors=100, y_range=(0.5, 5.5))
learn.fit_one_cycle(10, 5e-3)

# Retrieve factors
movie_factors = learn.model.i_weight.weight
user_factors = learn.model.u_weight.weight

Frequently Asked Questions

What is fastai and how does it differ from PyTorch?

fastai — a high‑level library built on top of PyTorch that provides a simplified interface for creating and training neural networks. Main differences:

  • Ease of use: fastai lets you achieve excellent results in just a few lines of code
  • Built‑in best practices: automatic application of modern training techniques
  • Ready‑made architectures: a large selection of pre‑trained models
  • Integrated tools: visualization, interpretation, deployment

Is fastai suitable for beginners?

Yes, fastai is specifically designed to lower the entry barrier to machine learning. The library allows you to:

  • Focus on solving problems rather than low‑level details
  • Learn ML through practical examples
  • Gradually dive deeper into implementation specifics

Can I use my own PyTorch models with fastai?

Absolutely, fastai is fully compatible with PyTorch. You can:

  • Use any PyTorch model as a backbone
  • Combine fastai components with custom PyTorch code
  • Migrate gradually between pure PyTorch and fastai

What types of tasks does fastai support?

fastai supports a wide range of machine‑learning tasks:

  • Computer vision: classification, segmentation, object detection
  • NLP: text classification, language modeling, generation
  • Tabular data: classification, regression
  • Recommendation systems: collaborative filtering
  • Time series: forecasting, anomaly detection

Does fastai support GPU?

Yes, fastai automatically uses a GPU when available. The library supports:

  • Automatic CUDA detection and usage
  • Mixed‑precision training for speed
  • Multi‑GPU training
  • GPU memory optimizations

How can I deploy a trained model to production?

fastai offers several deployment options:

  • Export to .pkl: simple model saving
  • ONNX export: cross‑platform usage
  • Web interfaces: integration with Gradio, Streamlit
  • API services: deployment via FastAPI, Flask
  • Mobile apps: export for iOS/Android

Can fastai be used for research?

Yes, fastai is actively used in research. The library provides:

  • Flexibility for experiments
  • Full customizability of all components
  • Integration with experiment‑tracking systems
  • Reproducible results

Conclusion

fastai is a powerful tool that democratizes access to state‑of‑the‑art machine learning. The library successfully balances ease of use with flexibility, allowing newcomers to get started quickly while enabling experts to build complex solutions.

Key advantages of fastai:

Rapid development: thanks to the high‑level API and built‑in best practices, model development is significantly faster.

Result quality: automatic application of modern training techniques yields state‑of‑the‑art performance.

Educational value: the library is ideal for learning machine learning thanks to its clear structure and extensive documentation.

Production readiness: built‑in tools for interpretation, validation, and deployment make the transition from prototype to production smoother.

Active community: a large, active community continuously evolves the library and provides support.

fastai continues to evolve, regularly incorporating the latest advances in machine learning and making them accessible to a broad audience of developers. This makes the library an excellent choice for both educational projects and real‑world business problems.

News