forked from ContinualAI/avalanche
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_nc_mt_scenario.py
493 lines (433 loc) · 16 KB
/
test_nc_mt_scenario.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import unittest
from os.path import expanduser
import torch
from torchvision.datasets import MNIST
from avalanche.benchmarks.datasets import default_dataset_location
from avalanche.benchmarks.scenarios.new_classes import NCExperience
from avalanche.benchmarks.utils import classification_subset
from avalanche.benchmarks.scenarios.new_classes.nc_utils import (
make_nc_transformation_subset,
)
from avalanche.benchmarks import nc_benchmark, ClassificationStream
class MultiTaskTests(unittest.TestCase):
def test_mt_single_dataset(self):
mnist_train = MNIST(
root=default_dataset_location("mnist"),
train=True,
download=True,
)
mnist_test = MNIST(
root=default_dataset_location("mnist"),
train=False,
download=True,
)
my_nc_benchmark = nc_benchmark(
mnist_train,
mnist_test,
5,
task_labels=True,
shuffle=True,
seed=1234,
class_ids_from_zero_in_each_exp=True,
)
self.assertEqual(5, my_nc_benchmark.n_experiences)
self.assertEqual(10, my_nc_benchmark.n_classes)
for task_id in range(5):
self.assertEqual(
2, len(my_nc_benchmark.classes_in_experience["train"][task_id])
)
all_classes = set()
all_original_classes = set()
for task_id in range(5):
all_classes.update(
my_nc_benchmark.classes_in_experience["train"][task_id]
)
all_original_classes.update(
my_nc_benchmark.original_classes_in_exp[task_id]
)
self.assertEqual(2, len(all_classes))
self.assertEqual(10, len(all_original_classes))
def test_mt_single_dataset_without_class_id_remap(self):
mnist_train = MNIST(
root=default_dataset_location("mnist"),
train=True,
download=True,
)
mnist_test = MNIST(
root=default_dataset_location("mnist"),
train=False,
download=True,
)
my_nc_benchmark = nc_benchmark(
mnist_train,
mnist_test,
5,
task_labels=True,
shuffle=True,
seed=1234,
class_ids_from_zero_in_each_exp=False,
)
self.assertEqual(5, my_nc_benchmark.n_experiences)
self.assertEqual(10, my_nc_benchmark.n_classes)
for task_id in range(5):
self.assertEqual(
2, len(my_nc_benchmark.classes_in_experience["train"][task_id])
)
all_classes = set()
for task_id in range(my_nc_benchmark.n_experiences):
all_classes.update(
my_nc_benchmark.classes_in_experience["train"][task_id]
)
self.assertEqual(10, len(all_classes))
def test_mt_single_dataset_fixed_order(self):
order = [2, 3, 5, 7, 8, 9, 0, 1, 4, 6]
mnist_train = MNIST(
root=default_dataset_location("mnist"),
train=True,
download=True,
)
mnist_test = MNIST(
root=default_dataset_location("mnist"),
train=False,
download=True,
)
my_nc_benchmark = nc_benchmark(
mnist_train,
mnist_test,
5,
task_labels=True,
fixed_class_order=order,
class_ids_from_zero_in_each_exp=False,
)
all_classes = []
for task_id in range(5):
all_classes.extend(
my_nc_benchmark.classes_in_experience["train"][task_id]
)
self.assertEqual(order, all_classes)
def test_sit_single_dataset_fixed_order_subset(self):
order = [2, 5, 7, 8, 9, 0, 1, 4]
mnist_train = MNIST(
root=default_dataset_location("mnist"),
train=True,
download=True,
)
mnist_test = MNIST(
root=default_dataset_location("mnist"),
train=False,
download=True,
)
my_nc_benchmark = nc_benchmark(
mnist_train,
mnist_test,
4,
task_labels=True,
fixed_class_order=order,
class_ids_from_zero_in_each_exp=True,
)
self.assertEqual(4, len(my_nc_benchmark.classes_in_experience["train"]))
all_classes = []
for task_id in range(4):
self.assertEqual(
2, len(my_nc_benchmark.classes_in_experience["train"][task_id])
)
self.assertEqual(
set(order[task_id * 2 : (task_id + 1) * 2]),
my_nc_benchmark.original_classes_in_exp[task_id],
)
all_classes.extend(
my_nc_benchmark.classes_in_experience["train"][task_id]
)
self.assertEqual([0, 1] * 4, all_classes)
def test_sit_single_dataset_fixed_subset_no_remap_idx(self):
order = [2, 5, 7, 8, 9, 0, 1, 4]
mnist_train = MNIST(
root=default_dataset_location("mnist"),
train=True,
download=True,
)
mnist_test = MNIST(
root=default_dataset_location("mnist"),
train=False,
download=True,
)
my_nc_benchmark = nc_benchmark(
mnist_train,
mnist_test,
2,
task_labels=True,
fixed_class_order=order,
class_ids_from_zero_in_each_exp=False,
)
self.assertEqual(2, len(my_nc_benchmark.classes_in_experience["train"]))
all_classes = set()
for task_id in range(2):
self.assertEqual(
4, len(my_nc_benchmark.classes_in_experience["train"][task_id])
)
self.assertEqual(
set(order[task_id * 4 : (task_id + 1) * 4]),
my_nc_benchmark.original_classes_in_exp[task_id],
)
all_classes.update(
my_nc_benchmark.classes_in_experience["train"][task_id]
)
self.assertEqual(set(order), all_classes)
def test_mt_single_dataset_reproducibility_data(self):
mnist_train = MNIST(
root=default_dataset_location("mnist"),
train=True,
download=True,
)
mnist_test = MNIST(
root=default_dataset_location("mnist"),
train=False,
download=True,
)
nc_benchmark_ref = nc_benchmark(
mnist_train,
mnist_test,
5,
task_labels=True,
shuffle=True,
seed=5678,
)
my_nc_benchmark = nc_benchmark(
mnist_train,
mnist_test,
-1,
task_labels=True,
reproducibility_data=nc_benchmark_ref.get_reproducibility_data(),
)
self.assertEqual(
nc_benchmark_ref.train_exps_patterns_assignment,
my_nc_benchmark.train_exps_patterns_assignment,
)
self.assertEqual(
nc_benchmark_ref.test_exps_patterns_assignment,
my_nc_benchmark.test_exps_patterns_assignment,
)
def test_mt_single_dataset_task_size(self):
mnist_train = MNIST(
root=default_dataset_location("mnist"),
train=True,
download=True,
)
mnist_test = MNIST(
root=default_dataset_location("mnist"),
train=False,
download=True,
)
my_nc_benchmark = nc_benchmark(
mnist_train,
mnist_test,
3,
task_labels=True,
per_exp_classes={0: 5, 2: 2},
class_ids_from_zero_in_each_exp=True,
)
self.assertEqual(3, my_nc_benchmark.n_experiences)
self.assertEqual(10, my_nc_benchmark.n_classes)
all_classes = set()
for task_id in range(3):
all_classes.update(
my_nc_benchmark.classes_in_experience["train"][task_id]
)
self.assertEqual(5, len(all_classes))
self.assertEqual(
5, len(my_nc_benchmark.classes_in_experience["train"][0])
)
self.assertEqual(
3, len(my_nc_benchmark.classes_in_experience["train"][1])
)
self.assertEqual(
2, len(my_nc_benchmark.classes_in_experience["train"][2])
)
def test_mt_multi_dataset_one_task_per_set(self):
split_mapping = [0, 1, 2, 0, 1, 2, 3, 4, 5, 6]
mnist_train = MNIST(
root=expanduser("~") + "/.avalanche/data/mnist/",
train=True,
download=True,
)
mnist_test = MNIST(
root=expanduser("~") + "/.avalanche/data/mnist/",
train=False,
download=True,
)
train_part1 = make_nc_transformation_subset(
mnist_train, None, None, range(3)
)
train_part2 = make_nc_transformation_subset(
mnist_train, None, None, range(3, 10)
)
train_part2 = classification_subset(
train_part2, class_mapping=split_mapping
)
test_part1 = make_nc_transformation_subset(
mnist_test, None, None, range(3)
)
test_part2 = make_nc_transformation_subset(
mnist_test, None, None, range(3, 10)
)
test_part2 = classification_subset(
test_part2, class_mapping=split_mapping
)
my_nc_benchmark = nc_benchmark(
[train_part1, train_part2],
[test_part1, test_part2],
2,
task_labels=True,
seed=1234,
class_ids_from_zero_in_each_exp=True,
one_dataset_per_exp=True,
)
self.assertEqual(2, my_nc_benchmark.n_experiences)
self.assertEqual(10, my_nc_benchmark.n_classes)
self.assertEqual(2, len(my_nc_benchmark.train_stream))
self.assertEqual(2, len(my_nc_benchmark.test_stream))
exp_classes_train = []
exp_classes_test = []
all_classes_train = set()
all_classes_test = set()
task_info: NCExperience
for task_id, task_info in enumerate(my_nc_benchmark.train_stream):
self.assertLessEqual(task_id, 1)
all_classes_train.update(
my_nc_benchmark.classes_in_experience["train"][task_id]
)
exp_classes_train.append(task_info.classes_in_this_experience)
self.assertEqual(7, len(all_classes_train))
for task_id, task_info in enumerate(my_nc_benchmark.test_stream):
self.assertLessEqual(task_id, 1)
all_classes_test.update(
my_nc_benchmark.classes_in_experience["test"][task_id]
)
exp_classes_test.append(task_info.classes_in_this_experience)
self.assertEqual(7, len(all_classes_test))
self.assertTrue(
(
my_nc_benchmark.classes_in_experience["train"][0] == {0, 1, 2}
and my_nc_benchmark.classes_in_experience["train"][1]
== set(range(0, 7))
)
or (
my_nc_benchmark.classes_in_experience["train"][0]
== set(range(0, 7))
and my_nc_benchmark.classes_in_experience["train"][1]
== {0, 1, 2}
)
)
exp_classes_ref1 = [list(range(3)), list(range(7))]
exp_classes_ref2 = [list(range(7)), list(range(3))]
self.assertTrue(
exp_classes_train == exp_classes_ref1
or exp_classes_train == exp_classes_ref2
)
if exp_classes_train == exp_classes_ref1:
self.assertTrue(exp_classes_test == exp_classes_ref1)
else:
self.assertTrue(exp_classes_test == exp_classes_ref2)
def test_nc_utils_corner_cases(self):
mnist_train = MNIST(
root=expanduser("~") + "/.avalanche/data/mnist/",
train=True,
download=True,
)
mnist_test = MNIST(
root=expanduser("~") + "/.avalanche/data/mnist/",
train=False,
download=True,
)
unique_train_targets, train_targets_count = \
torch.as_tensor(mnist_train.targets).unique(return_counts=True)
train_part1 = make_nc_transformation_subset(
mnist_train, None, None, None
)
test_part1 = make_nc_transformation_subset(
mnist_test, None, None, None, bucket_classes=True
)
my_nc_benchmark = nc_benchmark(
[train_part1],
[test_part1],
2,
task_labels=True,
seed=1234,
class_ids_from_zero_in_each_exp=True,
one_dataset_per_exp=True,
)
self.assertEqual(1, my_nc_benchmark.n_experiences)
self.assertEqual(10, my_nc_benchmark.n_classes)
self.assertEqual(1, len(my_nc_benchmark.train_stream))
self.assertEqual(1, len(my_nc_benchmark.test_stream))
train_exp = my_nc_benchmark.train_stream[0]
test_exp = my_nc_benchmark.test_stream[0]
self.assertSetEqual(set(range(10)), set(train_exp.dataset.targets))
self.assertSetEqual(set(range(10)), set(test_exp.dataset.targets))
self.assertEqual(len(mnist_train), len(train_exp.dataset))
self.assertEqual(len(mnist_test), len(test_exp.dataset))
other_split = make_nc_transformation_subset(
mnist_train, None, None, None,
bucket_classes=False,
sort_indexes=True
)
for b, s in zip([False, True], [False, True]):
other_split = make_nc_transformation_subset(
mnist_train, None, None, None,
bucket_classes=b,
sort_indexes=s
)
self.assertEqual(len(mnist_train), len(other_split))
unique_other_targets, other_targets_count = \
torch.as_tensor(other_split.targets).unique(return_counts=True)
self.assertTrue(torch.equal(unique_train_targets,
unique_other_targets))
self.assertTrue(torch.equal(train_targets_count,
other_targets_count))
def test_nc_mt_slicing(self):
mnist_train = MNIST(
root=default_dataset_location("mnist"),
train=True,
download=True,
)
mnist_test = MNIST(
root=default_dataset_location("mnist"),
train=False,
download=True,
)
my_nc_benchmark = nc_benchmark(
mnist_train,
mnist_test,
5,
task_labels=True,
shuffle=True,
seed=1234,
)
experience: NCExperience
for batch_id, experience in enumerate(my_nc_benchmark.train_stream):
self.assertEqual(batch_id, experience.current_experience)
self.assertIsInstance(experience, NCExperience)
for batch_id, experience in enumerate(my_nc_benchmark.test_stream):
self.assertEqual(batch_id, experience.current_experience)
self.assertIsInstance(experience, NCExperience)
iterable_slice = [3, 4, 1]
sliced_stream = my_nc_benchmark.train_stream[iterable_slice]
self.assertIsInstance(sliced_stream, ClassificationStream)
self.assertEqual(len(iterable_slice), len(sliced_stream))
self.assertEqual("train", sliced_stream.name)
for batch_id, experience in enumerate(sliced_stream):
self.assertEqual(
iterable_slice[batch_id], experience.current_experience
)
self.assertIsInstance(experience, NCExperience)
sliced_stream = my_nc_benchmark.test_stream[iterable_slice]
self.assertIsInstance(sliced_stream, ClassificationStream)
self.assertEqual(len(iterable_slice), len(sliced_stream))
self.assertEqual("test", sliced_stream.name)
for batch_id, experience in enumerate(sliced_stream):
self.assertEqual(
iterable_slice[batch_id], experience.current_experience
)
self.assertIsInstance(experience, NCExperience)
if __name__ == "__main__":
unittest.main()