Avalanche: an End-to-End Library for Continual Learning
Powered by ContinualAI
Avalanche is an End-to-End Continual Learning Library based on PyTorch, born within ContinualAI with the unique goal of providing a shared and collaborative open-source (MIT licensed) codebase for fast prototyping, training and reproducible evaluation of continual learning algorithms.
Avalanche can help Continual Learning researchers and practitioners in several ways:
  • Write less code, prototype faster & reduce errors
  • Improve reproducibility
  • Improve modularity and reusability
  • Increase code efficiency, scalability & portability
  • Augment impact and usability of your research products
The library is organized in five main modules:
  • Benchmarks: This module maintains a uniform API for data handling: mostly generating a stream of data from one or more datasets. It contains all the major CL benchmarks (similar to what has been done for torchvision).
  • Training: This module provides all the necessary utilities concerning model training. This includes simple and efficient ways of implement new continual learning strategies as well as a set pre-implemented CL baselines and state-of-the-art algorithms you will be able to use for comparison!
  • Evaluation: This modules provides all the utilities and metrics that can help evaluate a CL algorithm with respect to all the factors we believe to be important for a continually learning system.
  • Models: In this module you'll be able to find several model architectures and pre-trained models that can be used for your continual learning experiment (similar to what has been done in torchvision.models).
  • Logging: It includes advanced logging and plotting features, including native stdout, file and TensorBoard support (How cool it is to have a complete, interactive dashboard, tracking your experiment metrics in real-time with a single line of code?)
Avalanche the first experiment of a End-to-end Library for reproducible continual learning research & development where you can find benchmarks, algorithms, evaluation metrics and much more, in the same place.
Let's make it together ๐Ÿ‘ซ a wonderful ride! ๐ŸŽˆ
Check out how your code changes when you start using Avalanche! ๐Ÿ‘‡
With Avalanche
Without Avalanche
1
import torch
2
from torch.nn import CrossEntropyLoss
3
from torch.optim import SGD
4
โ€‹
5
from avalanche.benchmarks.classic import PermutedMNIST
6
from avalanche.extras.models import SimpleMLP
7
from avalanche.training.strategies import Naive
8
โ€‹
9
# Config
10
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
11
โ€‹
12
# model
13
model = SimpleMLP(num_classes=10)
14
โ€‹
15
# CL Benchmark Creation
16
perm_mnist = PermutedMNIST(n_experiences=3)
17
train_stream = perm_mnist.train_stream
18
test_stream = perm_mnist.test_stream
19
โ€‹
20
# Prepare for training & testing
21
optimizer = SGD(model.parameters(), lr=0.001, momentum=0.9)
22
criterion = CrossEntropyLoss()
23
โ€‹
24
# Continual learning strategy
25
cl_strategy = Naive(
26
model, optimizer, criterion, train_mb_size=32, train_epochs=2,
27
eval_mb_size=32, device=device)
28
โ€‹
29
# train and test loop
30
results = []
31
for train_task in train_stream:
32
cl_strategy.train(train_task, num_workers=4)
33
results.append(cl_strategy.eval(test_stream))
Copied!
1
import torch
2
from torch.nn import CrossEntropyLoss
3
from torch.optim import SGD
4
from torchvision import transforms
5
from torchvision.datasets import MNIST
6
from torchvision.transforms import ToTensor, RandomCrop
7
โ€‹
8
# Config
9
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
10
n_tasks = 5
11
n_classes = 10
12
train_ep = 2
13
mb_size = 32
14
โ€‹
15
# model
16
class SimpleMLP(nn.Module):
17
โ€‹
18
def __init__(self, num_classes=10, input_size=28*28):
19
super(SimpleMLP, self).__init__()
20
โ€‹
21
self.features = nn.Sequential(
22
nn.Linear(input_size, 512),
23
nn.ReLU(inplace=True),
24
nn.Dropout(),
25
)
26
self.classifier = nn.Linear(512, num_classes)
27
self._input_size = input_size
28
โ€‹
29
def forward(self, x):
30
x = x.contiguous()
31
x = x.view(x.size(0), self._input_size)
32
x = self.features(x)
33
x = self.classifier(x)
34
return x
35
model = SimpleMLP(num_classes=n_classes)
36
โ€‹
37
# CL Benchmark Creation
38
list_train_dataset = []
39
list_test_dataset = []
40
rng_permute = np.random.RandomState(seed)
41
train_transform = transforms.Compose([
42
RandomCrop(28, padding=4),
43
ToTensor(),
44
transforms.Normalize((0.1307,), (0.3081,))
45
])
46
test_transform = transforms.Compose([
47
ToTensor(),
48
transforms.Normalize((0.1307,), (0.3081,))
49
])
50
โ€‹
51
# for every incremental experience
52
for _ in range(n_tasks):
53
# choose a random permutation of the pixels in the image
54
idx_permute = torch.from_numpy(rng_permute.permutation(784)).type(torch.int64)
55
โ€‹
56
# add the permutation to the default dataset transformation
57
train_transform_list = train_transform.transforms.copy()
58
train_transform_list.append(
59
transforms.Lambda(lambda x: x.view(-1)[idx_permute].view(1, 28, 28))
60
)
61
new_train_transform = transforms.Compose(train_transform_list)
62
โ€‹
63
test_transform_list = test_transform.transforms.copy()
64
test_transform_list.append(
65
transforms.Lambda(lambda x: x.view(-1)[idx_permute].view(1, 28, 28))
66
)
67
new_test_transform = transforms.Compose(test_transform_list)
68
โ€‹
69
# get the datasets with the constructed transformation
70
permuted_train = MNIST(root='./data/mnist',
71
download=True, transform=train_transformation)
72
permuted_test = MNIST(root='./data/mnist',
73
train=False,
74
download=True, transform=test_transformation)
75
list_train_dataset.append(permuted_train)
76
list_test_dataset.append(permuted_test)
77
โ€‹
78
# Train
79
optimizer = SGD(model.parameters(), lr=0.001, momentum=0.9)
80
criterion = CrossEntropyLoss()
81
โ€‹
82
for task_id, train_dataset in enumerate(list_train_dataset):
83
โ€‹
84
train_data_loader = DataLoader(
85
train_dataset, num_workers=num_workers, batch_size=train_mb_size)
86
โ€‹
87
for ep in range(train_ep):
88
for iteration, (train_mb_x, train_mb_y) in enumerate(train_data_loader):
89
optimizer.zero_grad()
90
train_mb_x = train_mb_x.to(device)
91
train_mb_y = train_mb_y.to(device)
92
โ€‹
93
# Forward
94
logits = model(train_mb_x)
95
# Loss
96
loss = criterion(logits, train_mb_y)
97
# Backward
98
loss.backward()
99
# Update
100
optimizer.step()
101
โ€‹
102
# Test
103
acc_results = []
104
for task_id, test_dataset in enumerate(list_test_dataset):
105
โ€‹
106
train_data_loader = DataLoader(
107
train_dataset, num_workers=num_workers, batch_size=train_mb_size)
108
โ€‹
109
correct = 0
110
for iteration, (test_mb_x, test_mb_y) in enumerate(test_data_loader):
111
โ€‹
112
# Move mini-batch data to device
113
test_mb_x = test_mb_x.to(device)
114
test_mb_y = test_mb_y.to(device)
115
โ€‹
116
# Forward
117
test_logits = model(test_mb_x)
118
โ€‹
119
# Loss
120
test_loss = criterion(test_logits, test_mb_y)
121
โ€‹
122
# compute acc
123
correct += (test_mb_y.eq(test_logits.long())).sum()
124
โ€‹
125
acc_results.append(len(test_dataset)/correct)
Copied!

๐Ÿšฆ Getting Started

We know that learning a new tool may be tough at first. This is why we made Avalanche as easy as possible to learn with a set of resources that will help you along the way.
For example, you may start with our 5-minutes guide that will let you acquire the basics about Avalanche and how you can use it in your research project:
We have also prepared for you a large set of examples & snippets you can plug-in directly into your code and play with:
Having completed these two sections, you will already feel with superpowers โšก, this is why we have also created an in-depth tutorial that will cover all the aspect of Avalanche in details and make you a true Continual Learner! ๐Ÿ‘จโ€๐ŸŽ“๏ธ

๐Ÿ“‘ Cite Avalanche

If you used Avalanche in your research project, please remember to cite our reference paper "Avalanche: an End-to-End Library for Continual Learning". This will help us make Avalanche better known in the machine learning community, ultimately making a better tool for everyone:
1
@InProceedings{lomonaco2021avalanche,
2
title={Avalanche: an End-to-End Library for Continual Learning},
3
author={Vincenzo Lomonaco and Lorenzo Pellegrini and Andrea Cossu and Antonio Carta and Gabriele Graffieti and Tyler L. Hayes and Matthias De Lange and Marc Masana and Jary Pomponi and Gido van de Ven and Martin Mundt and Qi She and Keiland Cooper and Jeremy Forest and Eden Belouadah and Simone Calderara and German I. Parisi and Fabio Cuzzolin and Andreas Tolias and Simone Scardapane and Luca Antiga and Subutai Amhad and Adrian Popescu and Christopher Kanan and Joost van de Weijer and Tinne Tuytelaars and Davide Bacciu and Davide Maltoni},
4
booktitle={Proceedings of IEEE Conference on Computer Vision and Pattern Recognition},
5
series={2nd Continual Learning in Computer Vision Workshop},
6
year={2021}
7
}
Copied!

๐Ÿ—‚๏ธ Maintained by ContinualAI Lab

Avalanche is the flagship open-source collaborative project of ContinualAI: a non profit research organization and the largest open community on Continual Learning for AI.
Do you have a question, do you want to report an issue or simply ask for a new feature? Check out the Questions & Issues center. Do you want to improve Avalanche yourself? Follow these simple rules on How to Contribute.
The Avalanche project is maintained by the collaborative research team ContinualAI Lab and used extensively by the Units of the ContinualAI Research (CLAIR) consortium, a research network of the major continual learning stakeholders around the world.
We are always looking for new awesome members willing to join the ContinualAI Lab, so check out our official website if you want to learn more about us and our activities, or contact us.
Last modified 1mo ago