Repository navigation
Expand file tree
/
Copy pathgenerative_replay_MNIST_generator.py
More file actions
82 lines (69 loc) · 2.73 KB
/
Copy pathgenerative_replay_MNIST_generator.py
File metadata and controls
82 lines (69 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
################################################################################
# Copyright (c) 2021 ContinualAI. #
# Copyrights licensed under the MIT License. #
# See the accompanying LICENSE file for terms. #
# #
# Date: 01-04-2022 #
# Author(s): Florian Mies #
# E-mail: contact@continualai.org #
# Website: avalanche.continualai.org #
################################################################################
"""
This is a simple example on how to use the Replay strategy.
"""
import argparse
import torch
import torch.optim.lr_scheduler
import matplotlib.pyplot as plt
import numpy as np
from avalanche.benchmarks import SplitMNIST
from avalanche.models import MlpVAE
from avalanche.training.supervised import VAETraining
from avalanche.training.plugins import GenerativeReplayPlugin
def main(args):
# --- CONFIG
device = torch.device(
f"cuda:{args.cuda}" if torch.cuda.is_available() and args.cuda >= 0 else "cpu"
)
# --- BENCHMARK CREATION
benchmark = SplitMNIST(n_experiences=10, seed=1234)
# ---------
# MODEL CREATION
model = MlpVAE((1, 28, 28), nhid=2, device=device)
# CREATE THE STRATEGY INSTANCE (GenerativeReplay)
cl_strategy = VAETraining(
model,
torch.optim.Adam(model.parameters(), lr=0.001),
train_mb_size=100,
train_epochs=4,
device=device,
plugins=[GenerativeReplayPlugin()],
)
# TRAINING LOOP
print("Starting experiment...")
f, axarr = plt.subplots(benchmark.n_experiences, 10)
k = 0
for experience in benchmark.train_stream:
print("Start of experience ", experience.current_experience)
cl_strategy.train(experience)
print("Training completed")
samples = model.generate(10)
samples = samples.detach().cpu().numpy()
for j in range(10):
axarr[k, j].imshow(samples[j, 0], cmap="gray")
axarr[k, 4].set_title("Generated images for experience " + str(k))
np.vectorize(lambda ax: ax.axis("off"))(axarr)
k += 1
f.subplots_adjust(hspace=1.2)
plt.savefig("VAE_output_per_exp")
plt.show()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--cuda",
type=int,
default=0,
help="Select zero-indexed cuda device. -1 to use CPU.",
)
args = parser.parse_args()
main(args)