Avalanche: Continual Learning Library

repository·master·Indexed 24 days ago

https://github.com/continualai/avalanche

An end-to-end PyTorch-based library for Continual Learning (CL) research. Avalanche provides a modular ecosystem including Benchmarks for data stream generation, Training utilities for CL strategies and algorithms, Evaluation metrics, Model architectures, and Logging. It features specialized abstractions like AvalancheDataset for task-based learning and supports FFCV data loading for classification and regression tasks.

Tokens
42.7K
Snippets
79
Records
167
Agent score
80%

What's inside Avalanche

  1. Overview of Avalanche modules

    master

    Avalanche is an end-to-end continual learning library built on PyTorch. It is organized into five core modules to streamline the research and development lifecycle:

    • Benchmarks: Provides a uniform API for data handling, primarily generating streams of data from one or more datasets (similar to torchvision).
    • Training: Contains utilities for model training, including implementations of continual learning strategies, baselines, and state-of-the-art algorithms.
    • Evaluation: Provides metrics and utilities to evaluate continual learning algorithms across various important factors.
    • Models: Offers various model architectures and pre-trained models ready for continual learning experiments.
    • Logging: Supports advanced logging and real-time metric tracking via stdout, files, and TensorBoard.
  2. Overview of Avalanche features

    master

    Avalanche is a framework for Continual Learning research that provides a complete ecosystem including datasets, benchmarks, strategies, models, and metrics.

    Key components include:

    • Datasets: Support for popular computer vision datasets (MNIST, CIFAR, ImageNet, etc.) with automatic downloading.
    • Benchmarks: Pre-configured streams for major CL benchmarks (SplitMNIST, SplitCIFAR10, CORe50, etc.).
    • Strategies: A wide range of algorithms including Baselines (Naive, JointTraining), Rehearsal (Replay, GSS, CoPE), Regularization (EWC, LwF, GEM), and Architectural methods.
    • Models: Integration with PyTorch nn.Module and torchvision, featuring support for dynamic output heads and expanding architectures.
    • Metrics: Automatic logging of performance (accuracy, loss), CL-specific metrics (forgetting, transfer), and computational resources (CPU, RAM, execution time).
  3. Adapt existing PyTorch models for continual learning

    master

    Avalanche provides support for defining custom models or adapting existing PyTorch models, specifically focusing on how models evolve or adapt over time during a continual learning stream.

    Common patterns for model usage in Avalanche include:

    • Using pre-trained models: Integrating models from libraries like pytorchcv and training them using specific strategies (e.g., rehearsal).
    • Multi-Head architectures: Implementing models where different tasks are handled by different output heads. This is useful when each experience has a distinct task label that can be used at test time to select the correct head.
  4. Understand the core concepts of Avalanche benchmarks

    master

    Avalanche benchmarks are built upon three hierarchical abstractions:

    1. Scenarios: The highest level of abstraction representing a complete continual learning setup (e.g., CLScenario, OnlineCLScenario, NCScenario).
    2. Streams: A sequence of data segments that the model encounters over time (e.g., CLStream, ClassificationStream).
    3. Experiences: The individual units of data within a stream (e.g., CLExperience, ClassificationExperience).

    All continual learning benchmarks in Avalanche are specific instantiations of these concepts.

  5. How to customize Avalanche strategies

    master

    There are two primary ways to customize or create new strategies in Avalanche:

    1. Plugins: The easiest way to implement specific behaviors (like regularization or replay). Plugins are executed at specific callback points during the training/evaluation loops. They are highly reusable and combinable if they don't modify the same state.
    2. Subclassing: For more fundamental changes, you can subclass a template (e.g., SupervisedTemplate or the high-level BaseTemplate). Templates provide the generic training and evaluation loops and define callback points for plugins.

    Training Loop Structure

    The training loop follows a specific sequence of phases including dataset adaptation, dataloader initialization, model adaptation, and optimizer initialization. Key callback points include:

    • before_training_epoch / after_training_epoch
    • before_training_iteration / after_training_iteration
    • before_forward / after_forward
    • before_backward / after_backward
    • before_update / after_update
  6. Implement Dynamic Model Expansion with `DynamicModule`

    master

    A DynamicModule is a torch.nn.Module that can change its architecture over time (e.g., adding new units for new classes).

    To use a dynamic module, you call its adaptation(dataset) method, which updates the architecture based on the current experience's data.

    Note: When using Avalanche strategies, you do not need to call adaptation manually; the strategies automatically call it and update the optimizer to include any new parameters.

    from avalanche.benchmarks import SplitMNIST
    from avalanche.models import IncrementalClassifier
    
    benchmark = SplitMNIST(5, shuffle=False, class_ids_from_zero_in_each_exp=False)
    model = IncrementalClassifier(in_features=784)
    
    print(model)
    for exp in benchmark.train_stream:
        model.adaptation(exp)
        print(model)
  7. How Training Templates and Plugins work together

    master

    Avalanche uses Templates to define the core training and evaluation loops for different continual learning settings (e.g., supervised CL, online CL, RL).

    Templates (found in avalanche.training.templates) provide the structure, while Plugins (found in avalanche.core or avalanche.training.plugins) allow you to inject custom behavior into these loops. Plugins can execute code at specific points during training or evaluation via callbacks.

    Common template types include:

    • BaseTemplate
    • BaseSGDTemplate
    • SupervisedTemplate

    When building custom logic, you should implement your plugin by inheriting from the appropriate Abstract Base Class (ABC), such as BasePlugin, BaseSGDPlugin, or SupervisedPlugin.

  8. Understand the different types of Avalanche metrics

    master

    Avalanche categorizes metrics based on when they are computed and what data they aggregate. Choosing the right type depends on whether you want to monitor training progress or evaluation performance:

    1. Evaluation-time Metrics

    These are used during the evaluation phase (e.g., when calling strategy.eval()):

    • Stream Metrics: Return the average of metric results over all experiences in the evaluation stream. Note that slicing the stream (e.g., benchmark.test_stream[0:2]) will only average over the sliced experiences.
    • Experience Metrics: Compute values updated after each experience. Most return the average results over all patterns within that specific experience.

    2. Training-time Metrics

    These monitor the model during the training loop:

    • Epoch Metrics: Return the average metric results over all patterns in the training dataset for a given epoch.
    • Running Epoch Metrics: Return the average results over all patterns encountered up to the current iteration within the training epoch.
    • Minibatch Metrics: Return the average metric results over all patterns in the current minibatch.

    3. Standalone Metrics

    These define the raw computation logic (e.g., Accuracy, LossMetric, BWT). They cannot be used directly in Avalanche strategies but can be used independently in non-Avalanche workflows.

  9. Understand AvalancheDataset DataAttributes

    master

    An AvalancheDataset can hold DataAttributes, which are named arrays carrying metadata (e.g., class labels or task labels). These attributes are automatically propagated through concat() and subset() operations, ensuring that even after manipulating the dataset, the relationship between samples and their labels/tasks remains intact.

    # Example of attribute propagation
    tls = [0 for _ in range(100)]
    sup_data = make_classification_dataset(torch_data, task_labels=tls)
    
    # Subsampling preserves the attributes
    sub_data = sup_data.subset(range(10))
    print(sub_data.targets.name, len(sub_data.targets._data))
    print(sub_data.targets_task_labels.name, len(sub_data.targets_task_labels._data))
  10. Understand the AvalancheDataset abstraction

    master

    The AvalancheDataset is a specialized implementation of the PyTorch Dataset class designed for continual learning. While it can be used as a standard PyTorch Dataset, it provides specific metadata required for task-based learning.

    For classification tasks, instead of returning the standard (input, target) tuple, an AvalancheDataset returns a triplet: (x, y, t):

    • x: The input data.
    • y: The target label.
    • t: The task label (identifying which task the sample belongs to).

    This task label is critical for implementing continual learning strategies that rely on task identity (e.g., task-aware regularization or loss functions).

  11. Augment Strategies with Plugins

    master
    from avalanche.training.templates import SupervisedTemplate
    from avalanche.training.plugins import ReplayPlugin, EWCPlugin
    
    replay = ReplayPlugin(mem_size=100)
    ewc = EWCPlugin(ewc_lambda=0.001)
    strategy = SupervisedTemplate(
        model, optimizer, criterion,
        plugins=[replay, ewc])
    from avalanche.training.plugins import EarlyStoppingPlugin
    
    strategy = Naive(
        model, optimizer, criterion,
        plugins=[EarlyStoppingPlugin(patience=10, val_stream_name='train')])