### Comprehensive Avalanche Example Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/from-zero-to-hero-tutorial/07_putting-all-together.md A complete example demonstrating Avalanche usage, including benchmark setup, model definition, evaluation plugin configuration with various metrics and loggers, strategy instantiation (Naive), and the training/evaluation loop over experiences. ```python from torch.optim import SGD from torch.nn import CrossEntropyLoss from avalanche.benchmarks.classic import SplitMNIST from avalanche.evaluation.metrics import forgetting_metrics, accuracy_metrics, \ loss_metrics, timing_metrics, cpu_usage_metrics, confusion_matrix_metrics, disk_usage_metrics from avalanche.models import SimpleMLP from avalanche.logging import InteractiveLogger, TextLogger, TensorboardLogger from avalanche.training.plugins import EvaluationPlugin from avalanche.training.supervised import Naive scenario = SplitMNIST(n_experiences=5) # MODEL CREATION model = SimpleMLP(num_classes=scenario.n_classes) # DEFINE THE EVALUATION PLUGIN and LOGGERS # The evaluation plugin manages the metrics computation. # It takes as argument a list of metrics, collectes their results and returns # them to the strategy it is attached to. # log to Tensorboard tb_logger = TensorboardLogger() # log to text file text_logger = TextLogger(open('log.txt', 'a')) # print to stdout interactive_logger = InteractiveLogger() eval_plugin = EvaluationPlugin( accuracy_metrics(minibatch=True, epoch=True, experience=True, stream=True), loss_metrics(minibatch=True, epoch=True, experience=True, stream=True), timing_metrics(epoch=True, epoch_running=True), forgetting_metrics(experience=True, stream=True), cpu_usage_metrics(experience=True), confusion_matrix_metrics(num_classes=scenario.n_classes, save_image=False, stream=True), disk_usage_metrics(minibatch=True, epoch=True, experience=True, stream=True), loggers=[interactive_logger, text_logger, tb_logger] ) # CREATE THE STRATEGY INSTANCE (NAIVE) cl_strategy = Naive( model, SGD(model.parameters(), lr=0.001, momentum=0.9), CrossEntropyLoss(), train_mb_size=500, train_epochs=1, eval_mb_size=100, evaluator=eval_plugin) # TRAINING LOOP print('Starting experiment...') results = [] for experience in scenario.train_stream: print("Start of experience: ", experience.current_experience) print("Current Classes: ", experience.classes_in_this_experience) # train returns a dictionary which contains all the metric values res = cl_strategy.train(experience) print('Training completed') print('Computing accuracy on the whole test set') # test also returns a dictionary which contains all the metric values results.append(cl_strategy.eval(scenario.test_stream)) ``` -------------------------------- ### Install Build Tools and Upload to TestPyPI Source: https://github.com/continualai/avalanche/blob/master/docs/release_process.md Installs necessary build tools, builds the package binaries, checks the build, and uploads the package to the test PyPI repository. ```bash python -m pip install build twine python -m build twine check dist/* twine upload -r testpypi dist/* ``` -------------------------------- ### Launch TensorBoard Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/from-zero-to-hero-tutorial/06_loggers.md Starts a TensorBoard server to visualize logged metrics. Specify the log directory and a port for accessing the TensorBoard interface. ```python %tensorboard --logdir tb_data --port 6066 ``` -------------------------------- ### Install Avalanche Library Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/from-zero-to-hero-tutorial/02_models.md Installs the Avalanche library. Ensure you have version 0.6 or later. ```python ! ``` -------------------------------- ### Install Avalanche Core with Pip Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/getting-started/how-to-install.md Installs the core version of Avalanche without extra packages. Use this for a minimal setup. ```bash pip install avalanche-lib ``` -------------------------------- ### Install from TestPyPI Source: https://github.com/continualai/avalanche/blob/master/docs/release_process.md Installs the avalanche-lib package from the test PyPI repository, requiring an extra index URL for dependencies. ```bash python -m pip install --extra-index-url https://pypi.python.org/simple -i https://test.pypi.org/simple/ avalanche-lib ``` -------------------------------- ### Install Avalanche Library Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/getting-started/learn-avalanche-in-5-minutes.md Installs the Avalanche library using pip. It's recommended to check the official installation guide for more details. ```python !pip install avalanche-lib ``` ```python !pip show avalanche-lib ``` -------------------------------- ### Instantiate Naive Strategy Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/from-zero-to-hero-tutorial/04_training.md Instantiate the Naive strategy with a PyTorch model, optimizer, loss function, and training parameters. This is a basic setup for continual learning. ```python from torch.optim import SGD from torch.nn import CrossEntropyLoss from avalanche.models import SimpleMLP from avalanche.training.supervised import Naive, CWRStar, Replay, GDumb, Cumulative, LwF, GEM, AGEM, EWC # and many more! model = SimpleMLP(num_classes=10) optimizer = SGD(model.parameters(), lr=0.001, momentum=0.9) criterion = CrossEntropyLoss() cl_strategy = Naive( model, optimizer, criterion, train_mb_size=100, train_epochs=4, eval_mb_size=100 ) ``` -------------------------------- ### Initialize Strategy with Checkpoint Plugin Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/how-tos/checkpoints.md Demonstrates how to conditionally create the training strategy and plugins, ensuring that existing plugins are not recreated if a checkpoint is loaded. This setup includes adding the checkpoint plugin and other essential components like loggers and evaluators. ```python if strategy is None: plugins = [ checkpoint_plugin, # Add the checkpoint plugin to the list! ReplayPlugin(mem_size=500), # Other plugins you want to use # ... ] # Create loggers (as usual) # Note that the checkpoint plugin will automatically correctly # resume loggers! os.makedirs(f'./logs/checkpointing_example', exist_ok=True) loggers = [ TextLogger( open(f'./logs/checkpointing_example/log.txt', 'w')), InteractiveLogger(), TensorboardLogger(f'./logs/checkpointing_example') ] # The W&B logger is correctly resumed without resulting in # duplicated runs! use_wandb = False if use_wandb: loggers.append(WandBLogger( project_name='AvalancheCheckpointing', run_name=f'checkpointing_example' )) # Create the evaluation plugin (as usual) evaluation_plugin = EvaluationPlugin( accuracy_metrics(minibatch=False, epoch=True, experience=True, stream=True), loss_metrics(minibatch=False, epoch=True, experience=True, stream=True), class_accuracy_metrics( stream=True ), loggers=loggers ) # Create the strategy (as usual) strategy = Naive( model=model, optimizer=optimizer, criterion=criterion, train_mb_size=128, train_epochs=2, eval_mb_size=128, device=device, plugins=plugins, evaluator=evaluation_plugin ) ``` -------------------------------- ### Setup and Configure Evaluation Plugin Source: https://github.com/continualai/avalanche/blob/master/notebooks/from-zero-to-hero-tutorial/05_evaluation.ipynb Imports necessary modules and defines metrics for evaluation. Configure the Evaluation Plugin with desired metrics, loggers, and benchmark instance. Set strict_checks to False to allow for warnings instead of errors on inconsistencies. ```python from torch.nn import CrossEntropyLoss from torch.optim import SGD from avalanche.benchmarks.classic import SplitMNIST from avalanche.evaluation.metrics import forgetting_metrics, \ accuracy_metrics, loss_metrics, timing_metrics, cpu_usage_metrics, \ confusion_matrix_metrics, disk_usage_metrics from avalanche.models import SimpleMLP from avalanche.logging import InteractiveLogger from avalanche.training.plugins import EvaluationPlugin from avalanche.training import Naive benchmark = SplitMNIST(n_experiences=5) # MODEL CREATION model = SimpleMLP(num_classes=benchmark.n_classes) # DEFINE THE EVALUATION PLUGIN # The evaluation plugin manages the metrics computation. # It takes as argument a list of metrics, collectes their results and returns # them to the strategy it is attached to. eval_plugin = EvaluationPlugin( accuracy_metrics(minibatch=True, epoch=True, experience=True, stream=True), loss_metrics(minibatch=True, epoch=True, experience=True, stream=True), timing_metrics(epoch=True), forgetting_metrics(experience=True, stream=True), cpu_usage_metrics(experience=True), confusion_matrix_metrics(num_classes=benchmark.n_classes, save_image=False, stream=True), disk_usage_metrics(minibatch=True, epoch=True, experience=True, stream=True), loggers=[InteractiveLogger()], strict_checks=False ) # CREATE THE STRATEGY INSTANCE (NAIVE) cl_strategy = Naive( model, SGD(model.parameters(), lr=0.001, momentum=0.9), CrossEntropyLoss(), train_mb_size=500, train_epochs=1, eval_mb_size=100, evaluator=eval_plugin) # TRAINING LOOP print('Starting experiment...') results = [] for experience in benchmark.train_stream: # train returns a dictionary which contains all the metric values res = cl_strategy.train(experience) print('Training completed') print('Computing accuracy on the whole test set') # test also returns a dictionary which contains all the metric values results.append(cl_strategy.eval(benchmark.test_stream)) ``` -------------------------------- ### Complete Avalanche Experiment Setup Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/getting-started/learn-avalanche-in-5-minutes.md This snippet demonstrates how to set up a complete continual learning experiment using Avalanche. It includes benchmark creation, model definition, evaluation plugin configuration with various metrics and loggers, and the training loop for a Naive strategy. ```Python from avalanche.benchmarks.classic import SplitMNIST from avalanche.evaluation.metrics import forgetting_metrics, accuracy_metrics, loss_metrics, timing_metrics, cpu_usage_metrics, StreamConfusionMatrix, disk_usage_metrics, gpu_usage_metrics from avalanche.models import SimpleMLP from avalanche.logging import InteractiveLogger, TextLogger, TensorboardLogger from avalanche.training.plugins import EvaluationPlugin from avalanche.training import Naive from torch.optim import SGD from torch.nn import CrossEntropyLoss benchmark = SplitMNIST(n_experiences=5) # MODEL CREATION model = SimpleMLP(num_classes=benchmark.n_classes) # DEFINE THE EVALUATION PLUGIN and LOGGERS # The evaluation plugin manages the metrics computation. # It takes as argument a list of metrics, collectes their results and returns # them to the strategy it is attached to. # log to Tensorboard tb_logger = TensorboardLogger() # log to text file text_logger = TextLogger(open('log.txt', 'a')) # print to stdout interactive_logger = InteractiveLogger() eval_plugin = EvaluationPlugin( accuracy_metrics(minibatch=True, epoch=True, experience=True, stream=True), loss_metrics(minibatch=True, epoch=True, experience=True, stream=True), timing_metrics(epoch=True), cpu_usage_metrics(experience=True), forgetting_metrics(experience=True, stream=True), StreamConfusionMatrix(num_classes=benchmark.n_classes, save_image=False), disk_usage_metrics(minibatch=True, epoch=True, experience=True, stream=True), loggers=[interactive_logger, text_logger, tb_logger] ) # CREATE THE STRATEGY INSTANCE (NAIVE) cl_strategy = Naive( model, SGD(model.parameters(), lr=0.001, momentum=0.9), CrossEntropyLoss(), train_mb_size=500, train_epochs=1, eval_mb_size=100, evaluator=eval_plugin) # TRAINING LOOP print('Starting experiment...') results = [] for experience in benchmark.train_stream: print("Start of experience: ", experience.current_experience) print("Current Classes: ", experience.classes_in_this_experience) # train returns a dictionary which contains all the metric values res = cl_strategy.train(experience, num_workers=4) print('Training completed') print('Computing accuracy on the whole test set') # eval also returns a dictionary which contains all the metric values results.append(cl_strategy.eval(benchmark.test_stream, num_workers=4)) ``` -------------------------------- ### Install Avalanche Library Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/from-zero-to-hero-tutorial/03_benchmarks.md Installs the Avalanche library with version 0.6. Use this command before running any Avalanche code. ```python %pip install avalanche-lib==0.6 ``` -------------------------------- ### Import and Use Classic Benchmarks Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/getting-started/learn-avalanche-in-5-minutes.md Import and instantiate classic continual learning benchmarks like PermutedMNIST. This example shows how to access training and testing streams and iterate through experiences. ```python from avalanche.benchmarks.classic import CORe50, SplitTinyImageNet, SplitCIFAR10, \ SplitCIFAR100, SplitCIFAR110, SplitMNIST, RotatedMNIST, PermutedMNIST, SplitCUB200 # creating the benchmark (scenario object) perm_mnist = PermutedMNIST( n_experiences=3, seed=1234, ) # recovering the train and test streams train_stream = perm_mnist.train_stream test_stream = perm_mnist.test_stream # iterating over the train stream for experience in train_stream: print("Start of task ", experience.task_label) print('Classes in this task:', experience.classes_in_this_experience) # The current Pytorch training set can be easily recovered through the # experience current_training_set = experience.dataset # ...as well as the task_label print('Task {}'.format(experience.task_label)) print('This task contains', len(current_training_set), 'training examples') # we can recover the corresponding test experience in the test stream current_test_set = test_stream[experience.current_experience].dataset print('This task contains', len(current_test_set), 'test examples') ``` -------------------------------- ### Replay Plugin Example Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/from-zero-to-hero-tutorial/04_training.md A simple replay plugin that uses reservoir sampling to update a memory buffer after each training experience and customizes the dataloader before training. ```python from avalanche.benchmarks.utils.data_loader import ReplayDataLoader from avalanche.core import SupervisedPlugin from avalanche.training.storage_policy import ReservoirSamplingBuffer class ReplayP(SupervisedPlugin): def __init__(self, mem_size): """ A simple replay plugin with reservoir sampling. """ super().__init__() self.buffer = ReservoirSamplingBuffer(max_size=mem_size) def before_training_exp(self, strategy: "SupervisedTemplate", num_workers: int = 0, shuffle: bool = True, **kwargs): """ Use a custom dataloader to combine samples from the current data and memory buffer. """ if len(self.buffer.buffer) == 0: # first experience. We don't use the buffer, no need to change # the dataloader. return strategy.dataloader = ReplayDataLoader( strategy.adapted_dataset, self.buffer.buffer, oversample_small_tasks=True, num_workers=num_workers, batch_size=strategy.train_mb_size, shuffle=shuffle) def after_training_exp(self, strategy: "SupervisedTemplate", **kwargs): """ Update the buffer. """ self.buffer.update(strategy, **kwargs) benchmark = SplitMNIST(n_experiences=5, seed=1) model = SimpleMLP(num_classes=10) optimizer = SGD(model.parameters(), lr=0.01, momentum=0.9) criterion = CrossEntropyLoss() strategy = Naive(model=model, optimizer=optimizer, criterion=criterion, train_mb_size=128, plugins=[ReplayP(mem_size=2000)]) strategy.train(benchmark.train_stream) strategy.eval(benchmark.test_stream) ``` -------------------------------- ### Task-Aware Benchmark Setup Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/from-zero-to-hero-tutorial/03_benchmarks.md Creates a task-incremental benchmark by adding task labels to a class-incremental benchmark. Each sample is a triplet of . ```python from avalanche.benchmarks.scenarios.supervised import class_incremental_benchmark from avalanche.benchmarks.scenarios.task_aware import task_incremental_benchmark bm = class_incremental_benchmark({'train': train_MNIST, 'test': test_MNIST}, num_experiences=5) # we take the class-incremental benchmark defined above and # add an incremental task label to each experience # each sample will have its own task label bm = task_incremental_benchmark(bm) for exp in bm.train_stream: print(f"Experience {exp.current_experience}") # in Avalanche an experience may have multiple task labels # if the samples in its dataset come from different tasks # here we just have one task label per experience print(f"\tTask labels: {exp.task_labels}") # samples are now triplets print(f"\tSample: {exp.dataset[0]}") ``` -------------------------------- ### Install Avalanche Library Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/from-zero-to-hero-tutorial/08_extending-avalanche.md Installs the Avalanche library with a specific version. Use this command in environments like Google Colab to set up the necessary tools. ```python !pip install avalanche-lib==0.6 ``` -------------------------------- ### Define Benchmark, Model, Optimizer, and Loss Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/how-tos/checkpoints.md Sets up the computing device, creates a SplitMNIST benchmark, instantiates a multi-task SimpleMLP model, and defines the optimizer and loss function. These are standard Avalanche setup steps and not directly related to checkpointing. ```python # Nothing new here... device = torch.device( f"cuda:0" if torch.cuda.is_available() else "cpu" ) print('Using device', device) # CL Benchmark Creation n_experiences = 5 benchmark = SplitMNIST(n_experiences=n_experiences, return_task_id=True) input_size = 28*28*1 # Define the model (and load initial weights if necessary) # Again, not checkpoint-related model = SimpleMLP(input_size=input_size, num_classes=benchmark.n_classes // n_experiences) model = as_multitask(model, 'classifier') # Prepare for training & testing: not checkpoint-related optimizer = SGD(model.parameters(), lr=0.01, momentum=0.9) criterion = CrossEntropyLoss() ``` -------------------------------- ### Classification Benchmark Setup Source: https://github.com/continualai/avalanche/blob/master/notebooks/from-zero-to-hero-tutorial/03_benchmarks.ipynb Sets up a classification benchmark using MNIST with class-incremental splitting. Useful for evaluating models on class-incremental learning tasks. ```python from avalanche.benchmarks.scenarios.supervised import class_incremental_benchmark from avalanche.benchmarks.datasets import MNIST from avalanche.benchmarks.utils import as_classification_dataset from avalanche.benchmarks.utils.data_loader import default_dataset_location datadir = default_dataset_location('mnist') train_MNIST = as_classification_dataset(MNIST(datadir, train=True, download=True)) test_MNIST = as_classification_dataset(MNIST(datadir, train=False, download=True)) # a class-incremental split # 5 experiences, 2 classes per experience bm = class_incremental_benchmark({'train': train_MNIST, 'test': test_MNIST}, num_experiences=5) exp = bm.train_stream[0] print(f"Experience {exp.current_experience}") print(f"Classes in this experience: {exp.classes_in_this_experience}") print(f"Previous classes: {exp.classes_seen_so_far}") print(f"Future classes: {exp.future_classes}") ``` -------------------------------- ### Install Avalanche with All Extras via Pip Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/getting-started/how-to-install.md Installs Avalanche along with all available extra packages, such as object detection and reinforcement learning support. Note that this may limit the PyTorch version. ```bash pip install avalanche-lib[all] ``` -------------------------------- ### Initialize EvaluationPlugin with collect_all=True Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/from-zero-to-hero-tutorial/05_evaluation.md Instantiate the EvaluationPlugin, ensuring `collect_all` is set to `True` to gather all metric results. This example includes various metric types and loggers. ```python eval_plugin2 = EvaluationPlugin( accuracy_metrics(minibatch=True, epoch=True, experience=True, stream=True), loss_metrics(minibatch=True, epoch=True, experience=True, stream=True), forgetting_metrics(experience=True, stream=True), timing_metrics(epoch=True), cpu_usage_metrics(experience=True), confusion_matrix_metrics(num_classes=benchmark.n_classes, save_image=False, stream=True), disk_usage_metrics(minibatch=True, epoch=True, experience=True, stream=True), collect_all=True, # this is default value anyway loggers=[InteractiveLogger()] ) # since no training and evaluation has been performed, this will return an empty dict. metric_dict = eval_plugin2.get_all_metrics() print(metric_dict) ``` -------------------------------- ### Avalanche Continual Learning Workflow Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/README.md Demonstrates a basic continual learning workflow using Avalanche, including model setup, benchmark creation, training, and evaluation. Ensure necessary imports and configurations are in place before running. ```python import torch from torch.nn import CrossEntropyLoss from torch.optim import SGD from avalanche.benchmarks.classic import PermutedMNIST from avalanche.training.plugins import EvaluationPlugin from avalanche.evaluation.metrics import accuracy_metrics from avalanche.models import SimpleMLP from avalanche.training.supervised import Naive # Config device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # model model = SimpleMLP(num_classes=10) # CL Benchmark Creation perm_mnist = PermutedMNIST(n_experiences=3) train_stream = perm_mnist.train_stream test_stream = perm_mnist.test_stream # Prepare for training & testing optimizer = SGD(model.parameters(), lr=0.001, momentum=0.9) criterion = CrossEntropyLoss() eval_plugin = EvaluationPlugin( accuracy_metrics(minibatch=True, epoch=True, epoch_running=True, experience=True, stream=True)) # Continual learning strategy cl_strategy = Naive( model, optimizer, criterion, train_mb_size=32, train_epochs=2, eval_mb_size=32, evaluator=eval_plugin, device=device) # train and test loop results = [] for train_task in train_stream: cl_strategy.train(train_task, num_workers=4) results.append(cl_strategy.eval(test_stream)) ``` -------------------------------- ### Resume Training from Initial Experience Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/how-tos/checkpoints.md Iterates through the training stream starting from the `initial_exp` value, which is determined by the loaded checkpoint. This ensures training continues from the correct point. Checkpoints are saved at the end of each evaluation phase. ```python exit_early = False for train_task in benchmark.train_stream[initial_exp:]: strategy.train(train_task) strategy.eval(benchmark.test_stream) if exit_early: exit(0) ``` -------------------------------- ### Train SSD300 VGG16 Source: https://github.com/continualai/avalanche/blob/master/examples/tvdetection/README.md Script for training an SSD300 model with a VGG16 backbone. This example specifies a longer epoch count, specific learning rate steps, batch size, weight decay, and data augmentation. ```bash torchrun --nproc_per_node=8 train.py\ --dataset coco --model ssd300_vgg16 --epochs 120\ --lr-steps 80 110 --aspect-ratio-group-factor 3 --lr 0.002 --batch-size 4\ --weight-decay 0.0005 --data-augmentation ssd ``` -------------------------------- ### Basic Continual Learning Training Loop in Avalanche Source: https://github.com/continualai/avalanche/blob/master/README.md Demonstrates a basic training and evaluation loop using Avalanche with PermutedMNIST. Requires PyTorch and Avalanche installed. Sets up model, optimizer, loss function, and a continual learning strategy. ```python import torch from torch.nn import CrossEntropyLoss from torch.optim import SGD from avalanche.benchmarks.classic import PermutedMNIST from avalanche.models import SimpleMLP from avalanche.training import Naive # Config device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # model model = SimpleMLP(num_classes=10) # CL Benchmark Creation perm_mnist = PermutedMNIST(n_experiences=3) train_stream = perm_mnist.train_stream test_stream = perm_mnist.test_stream # Prepare for training & testing optimizer = SGD(model.parameters(), lr=0.001, momentum=0.9) criterion = CrossEntropyLoss() # Continual learning strategy cl_strategy = Naive( model, optimizer, criterion, train_mb_size=32, train_epochs=2, eval_mb_size=32, device=device) # train and test loop over the stream of experiences results = [] for train_exp in train_stream: cl_strategy.train(train_exp) results.append(cl_strategy.eval(test_stream)) ``` -------------------------------- ### Implementing a Cumulative Training Strategy Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/from-zero-to-hero-tutorial/04_training.md This example demonstrates how to create a custom 'Cumulative' strategy by overriding the 'train_dataset_adaptation' method of 'SupervisedTemplate'. It concatenates all encountered experiences into a single dataset for continual training. Ensure to call the superclass's adaptation method and handle dataset concatenation correctly. ```python from avalanche.benchmarks.utils import concat_datasets from avalanche.training.templates import SupervisedTemplate class Cumulative(SupervisedTemplate): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.dataset = None # cumulative dataset def train_dataset_adaptation(self, **kwargs): super().train_dataset_adaptation(**kwargs) curr_data = self.experience.dataset if self.dataset is None: self.dataset = curr_data else: self.dataset = concat_datasets([self.dataset, curr_data]) self.adapted_dataset = self.dataset.train() strategy = Cumulative(model=model, optimizer=optimizer, criterion=criterion, train_mb_size=128) strategy.train(benchmark.train_stream) ``` -------------------------------- ### Install Avalanche Master Branch via Pip Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/getting-started/how-to-install.md Installs the latest version of Avalanche directly from the master branch on GitHub. Ensure PyTorch is already installed. ```shell pip install git+https://github.com/ContinualAI/avalanche.git ``` -------------------------------- ### Install Avalanche in Developer Mode Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/getting-started/how-to-install.md Installs Avalanche in editable mode, allowing for direct modification of the package code. Also installs development dependencies for testing and documentation. ```pip pip install -e ".[dev]" ``` -------------------------------- ### Install Specific Avalanche Extras via Pip Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/getting-started/how-to-install.md Installs Avalanche with specific extra packages by naming them within square brackets. Use this to tailor the installation to your needs. ```bash pip install avalanche-lib[extra] # supports for specific functionalities (e.g. specific strategies) ``` ```bash pip install avalanche-lib[rl] # reinforcement learning support ``` ```bash pip install avalanche-lib[detection] # object detection support ``` -------------------------------- ### Initialize EvaluationPlugin with Metrics and Loggers Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/getting-started/learn-avalanche-in-5-minutes.md Demonstrates how to create an EvaluationPlugin instance, specifying which metrics to compute and when, and configuring loggers for reporting. Pass this plugin to your training strategy. ```python from avalanche.evaluation.metrics import accuracy_metrics, loss_metrics, forgetting_metrics from avalanche.logging import InteractiveLogger, TensorboardLogger from avalanche.training.plugins import EvaluationPlugin eval_plugin = EvaluationPlugin( # accuracy after each training epoch # and after each evaluation experience accuracy_metrics(epoch=True, experience=True), # loss after each training minibatch and each # evaluation stream loss_metrics(minibatch=True, stream=True), # catastrophic forgetting after each evaluation # experience forgetting_metrics(experience=True, stream=True), # add as many metrics as you like loggers=[InteractiveLogger(), TensorboardLogger()]) # pass the evaluation plugin instance to the strategy # strategy = EWC(..., evaluator=eval_plugin) # THAT'S IT!! ``` -------------------------------- ### Plugin Metric Initialization Source: https://github.com/continualai/avalanche/blob/master/notebooks/from-zero-to-hero-tutorial/05_evaluation.ipynb Shows how to initialize various plugin metrics using helper functions. These metrics can be automatically integrated into the training and evaluation flow via the EvaluationPlugin. ```python from avalanche.evaluation.metrics import accuracy_metrics, \ loss_metrics, forgetting_metrics, bwt_metrics, \ confusion_matrix_metrics, cpu_usage_metrics, \ disk_usage_metrics, gpu_usage_metrics, MAC_metrics, \ ram_usage_metrics, timing_metrics # you may pass the result to the EvaluationPlugin metrics = accuracy_metrics(epoch=True, experience=True) ``` -------------------------------- ### Loading and Iterating SplitMNIST Benchmark Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/from-zero-to-hero-tutorial/03_benchmarks.md Demonstrates how to instantiate the SplitMNIST benchmark with specified parameters and iterate through its training and testing streams to access experience details. ```python from avalanche.benchmarks.classic import SplitMNIST bm = SplitMNIST( n_experiences=5, # 5 incremental experiences return_task_id=True, # add task labels seed=1 # you can set the seed for reproducibility. This will fix the order of classes ) # streams have a name, used for logging purposes # each metric will be logged with the stream name print(f'--- Stream: {bm.train_stream.name}') # each stream is an iterator of experiences for exp in bm.train_stream: # experiences have an ID that denotes its position in the stream # this is used only for logging (don't rely on it for training!) eid = exp.current_experience # for classification benchmarks, experiences have a list of classes in this experience clss = exp.classes_in_this_experience # you may also have task labels tls = exp.task_labels print(f"EID={eid}, classes={clss}, tasks={tls}") # the experience provides a dataset print(f"data: {len(exp.dataset)} samples") for exp in bm.test_stream: print(f"EID={exp.current_experience}, classes={exp.classes_in_this_experience}, task={tls}") ``` -------------------------------- ### Object Detection Metrics Source: https://github.com/continualai/avalanche/blob/master/docs/evaluation.rst Provides metrics specifically designed for object detection tasks. Refer to the examples folder for usage. ```APIDOC ## Object Detection Metrics ### Description Metrics for Object Detection tasks. Please, take a look at the examples in the `examples` folder of Avalanche to better understand how to use these metrics. ### Functions - **make_lvis_metrics** - **get_detection_api_from_dataset** ### Classes - **DetectionMetrics** ``` -------------------------------- ### Training with Custom Replay Plugin Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/how-tos/dataloading_buffers_replay.md Demonstrates how to integrate the custom replay plugin into the Naive training strategy for a continual learning scenario. Requires setting up the model, storage policy, and evaluation plugins. ```python from torch.nn import CrossEntropyLoss from avalanche.training import Naive from avalanche.evaluation.metrics import accuracy_metrics from avalanche.training.plugins import EvaluationPlugin from avalanche.logging import InteractiveLogger from avalanche.models import SimpleMLP import torch scenario = SplitMNIST(5) model = SimpleMLP(num_classes=scenario.n_classes) storage_p = ParametricBuffer( max_size=500, groupby='class', selection_strategy=RandomExemplarsSelectionStrategy() ) # choose some metrics and evaluation method interactive_logger = InteractiveLogger() eval_plugin = EvaluationPlugin( accuracy_metrics(experience=True, stream=True), loggers=[interactive_logger]) # CREATE THE STRATEGY INSTANCE (NAIVE) cl_strategy = Naive(model, torch.optim.Adam(model.parameters(), lr=0.001), CrossEntropyLoss(), train_mb_size=100, train_epochs=1, eval_mb_size=100, plugins=[CustomReplay(storage_p)], evaluator=eval_plugin ) # TRAINING LOOP print('Starting experiment...') results = [] for experience in scenario.train_stream: print("Start of experience ", experience.current_experience) cl_strategy.train(experience) print('Training completed') print('Computing accuracy on the whole test set') results.append(cl_strategy.eval(scenario.test_stream)) ``` -------------------------------- ### Build API Documentation Source: https://github.com/continualai/avalanche/blob/master/docs/README.md Command to build the API documentation using Sphinx. Execute this command in the `docs` folder. ```bash sphinx-build . _build -j auto ``` -------------------------------- ### GroupBalancedDataLoader Example Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/how-tos/dataloading_buffers_replay.md Demonstrates how to use GroupBalancedDataLoader to iterate over datasets with balanced mini-batches across different groups. This dataloader is useful for handling unbalanced data. ```python from avalanche.benchmarks import SplitMNIST from avalanche.benchmarks.utils.data_loader import GroupBalancedDataLoader benchmark = SplitMNIST(5, return_task_id=True) dl = GroupBalancedDataLoader([exp.dataset for exp in benchmark.train_stream], batch_size=5) for x, y, t in dl: print(t.tolist()) break ``` -------------------------------- ### Instantiate a Naive Strategy in Avalanche Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/getting-started/learn-avalanche-in-5-minutes.md Use this snippet to quickly set up and instantiate a standard continual learning strategy like Naive. Ensure you have the necessary model, optimizer, and loss function defined. ```python from avalanche.models import SimpleMLP from avalanche.training import Naive, CWRStar, Replay, GDumb, \ Cumulative, LwF, GEM, AGEM, EWC, AR1 from torch.optim import SGD from torch.nn import CrossEntropyLoss model = SimpleMLP(num_classes=10) cl_strategy = Naive( model, SGD(model.parameters(), lr=0.001, momentum=0.9), CrossEntropyLoss(), train_mb_size=100, train_epochs=4, eval_mb_size=100 ) ``` -------------------------------- ### Task-Aware Benchmark Setup Source: https://github.com/continualai/avalanche/blob/master/notebooks/from-zero-to-hero-tutorial/03_benchmarks.ipynb Configures a task-aware benchmark by adding incremental task labels to a class-incremental benchmark. This is suitable for scenarios where tasks change over time. ```python from avalanche.benchmarks.scenarios.supervised import class_incremental_benchmark from avalanche.benchmarks.scenarios.task_aware import task_incremental_benchmark from avalanche.benchmarks.datasets import MNIST from avalanche.benchmarks.utils import as_classification_dataset from avalanche.benchmarks.utils.data_loader import default_dataset_location datadir = default_dataset_location('mnist') train_MNIST = as_classification_dataset(MNIST(datadir, train=True, download=True)) test_MNIST = as_classification_dataset(MNIST(datadir, train=False, download=True)) bm = class_incremental_benchmark({'train': train_MNIST, 'test': test_MNIST}, num_experiences=5) # we take the class-incremental benchmark defined above and # add an incremental task label to each experience # each sample will have its own task label bm = task_incremental_benchmark(bm) for exp in bm.train_stream: print(f"Experience {exp.current_experience}") # in Avalanche an experience may have multiple task labels # if the samples in its dataset come from different tasks # here we just have one task label per experience print(f"\tTask labels: {exp.task_labels}") # samples are now triplets print(f"\tSample: {exp.dataset[0]}") ``` -------------------------------- ### Instantiate CheckpointPlugin and Load Checkpoint Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/how-tos/checkpoints.md Initializes the CheckpointPlugin with a FileSystemCheckpointStorage pointing to a specific directory. It then attempts to load an existing checkpoint; if none is found, the strategy and initial experience will be reset. ```python checkpoint_plugin = CheckpointPlugin( FileSystemCheckpointStorage( directory='./checkpoints/task_incremental', ), map_location=device ) # Load checkpoint (if exists in the given storage) # If it does not exist, strategy will be None and initial_exp will be 0 strategy, initial_exp = checkpoint_plugin.load_checkpoint_if_exists() ``` -------------------------------- ### Import Avalanche and Utilities Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/how-tos/checkpoints.md Imports necessary modules from Avalanche, PyTorch, and system utilities for setting up experiments. Ensure the Avalanche path is correctly added if running from a local clone. ```python import sys sys.path.append('/home/lorenzo/Desktop/ProjectsVCS/avalanche/') import os from typing import Sequence import torch from torch.nn import CrossEntropyLoss from torch.optim import SGD from avalanche.benchmarks import CLExperience, SplitMNIST from avalanche.evaluation.metrics import accuracy_metrics, loss_metrics, \ class_accuracy_metrics from avalanche.logging import InteractiveLogger, TensorboardLogger, \ WandBLogger, TextLogger from avalanche.models import SimpleMLP, as_multitask from avalanche.training.determinism.rng_manager import RNGManager from avalanche.training.plugins import EvaluationPlugin, ReplayPlugin from avalanche.training.plugins.checkpoint import CheckpointPlugin, \ FileSystemCheckpointStorage from avalanche.training.supervised import Naive ``` -------------------------------- ### Integrate and Run a Custom Strategy with a Benchmark Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/getting-started/learn-avalanche-in-5-minutes.md This snippet demonstrates how to instantiate your custom strategy (MyStrategy) and integrate it into a training loop using an Avalanche benchmark. It iterates through experiences, trains the model, and evaluates on test sets. ```python from avalanche.models import SimpleMLP from avalanche.benchmarks import SplitMNIST # Benchmark creation benchmark = SplitMNIST(n_experiences=5) # Model Creation model = SimpleMLP(num_classes=benchmark.n_classes) # Create the Strategy Instance (MyStrategy) cl_strategy = MyStrategy( model, SGD(model.parameters(), lr=0.001, momentum=0.9), CrossEntropyLoss()) # Training Loop print('Starting experiment...') for exp_id, experience in enumerate(benchmark.train_stream): print("Start of experience ", experience.current_experience) cl_strategy.train(experience) print('Training completed') print('Computing accuracy on the current test set') cl_strategy.eval(benchmark.test_stream[exp_id]) ``` -------------------------------- ### Train Keypoint R-CNN Source: https://github.com/continualai/avalanche/blob/master/examples/tvdetection/README.md Command to train a Keypoint R-CNN model. This example is configured for the COCO keypoint dataset and includes specific learning rate steps. ```bash torchrun --nproc_per_node=8 train.py\ --dataset coco_kp --model keypointrcnn_resnet50_fpn --epochs 46\ --lr-steps 36 43 --aspect-ratio-group-factor 3 ``` -------------------------------- ### Classification Benchmark Setup Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/from-zero-to-hero-tutorial/03_benchmarks.md Defines a class-incremental benchmark for classification tasks using MNIST. Accesses attributes like classes in the current experience and seen so far. ```python from avalanche.benchmarks.scenarios.supervised import class_incremental_benchmark datadir = default_dataset_location('mnist') train_MNIST = as_classification_dataset(MNIST(datadir, train=True, download=True)) test_MNIST = as_classification_dataset(MNIST(datadir, train=False, download=True)) # a class-incremental split # 5 experiences, 2 classes per experience bm = class_incremental_benchmark({'train': train_MNIST, 'test': test_MNIST}, num_experiences=5) exp = bm.train_stream[0] print(f"Experience {exp.current_experience}") print(f"Classes in this experience: {exp.classes_in_this_experience}") print(f"Previous classes: {exp.classes_seen_so_far}") print(f"Future classes: {exp.future_classes}") ``` -------------------------------- ### Configure and Use Avalanche Evaluation Plugin Source: https://github.com/continualai/avalanche/blob/master/docs/gitbook/from-zero-to-hero-tutorial/05_evaluation.md Instantiate the Evaluation Plugin with desired metrics, loggers, and benchmark instance. Configure strict checks for metric computation consistency. Then, integrate it into a training strategy like Naive. ```python from torch.nn import CrossEntropyLoss from torch.optim import SGD from avalanche.benchmarks.classic import SplitMNIST from avalanche.evaluation.metrics import forgetting_metrics, \ accuracy_metrics, loss_metrics, timing_metrics, cpu_usage_metrics, \ confusion_matrix_metrics, disk_usage_metrics from avalanche.models import SimpleMLP from avalanche.logging import InteractiveLogger from avalanche.training.plugins import EvaluationPlugin from avalanche.training import Naive benchmark = SplitMNIST(n_experiences=5) # MODEL CREATION model = SimpleMLP(num_classes=benchmark.n_classes) # DEFINE THE EVALUATION PLUGIN # The evaluation plugin manages the metrics computation. # It takes as argument a list of metrics, collectes their results and returns # them to the strategy it is attached to. eval_plugin = EvaluationPlugin( accuracy_metrics(minibatch=True, epoch=True, experience=True, stream=True), loss_metrics(minibatch=True, epoch=True, experience=True, stream=True), timing_metrics(epoch=True), forgetting_metrics(experience=True, stream=True), cpu_usage_metrics(experience=True), confusion_matrix_metrics(num_classes=benchmark.n_classes, save_image=False, stream=True), disk_usage_metrics(minibatch=True, epoch=True, experience=True, stream=True), loggers=[InteractiveLogger()], strict_checks=False ) # CREATE THE STRATEGY INSTANCE (NAIVE) cl_strategy = Naive( model, SGD(model.parameters(), lr=0.001, momentum=0.9), CrossEntropyLoss(), train_mb_size=500, train_epochs=1, eval_mb_size=100, evaluator=eval_plugin) # TRAINING LOOP print('Starting experiment...') results = [] for experience in benchmark.train_stream: # train returns a dictionary which contains all the metric values res = cl_strategy.train(experience) print('Training completed') print('Computing accuracy on the whole test set') # test also returns a dictionary which contains all the metric values results.append(cl_strategy.eval(benchmark.test_stream)) ```