|
| 1 | +from typing import ( |
| 2 | + Callable, |
| 3 | + Generator, |
| 4 | + Generic, |
| 5 | + List, |
| 6 | + Sequence, |
| 7 | + TypeVar, |
| 8 | + Union, |
| 9 | + Tuple, |
| 10 | + Optional, |
| 11 | + Iterable, |
| 12 | + Dict, |
| 13 | +) |
| 14 | + |
| 15 | +import random |
| 16 | +from avalanche.benchmarks.utils.data import AvalancheDataset |
| 17 | +from .generic_scenario import EagerCLStream, CLScenario, CLExperience, make_stream |
| 18 | +from .dataset_scenario import ( |
| 19 | + LazyTrainValSplitter, |
| 20 | + DatasetExperience, |
| 21 | + split_validation_random, |
| 22 | +) |
| 23 | +from .supervised import with_classes_timeline |
| 24 | + |
| 25 | + |
| 26 | +def benchmark_with_validation_stream( |
| 27 | + benchmark: CLScenario, |
| 28 | + validation_size: Union[int, float] = 0.5, |
| 29 | + shuffle: bool = False, |
| 30 | + seed: Optional[int] = None, |
| 31 | + split_strategy: Optional[ |
| 32 | + Callable[[AvalancheDataset], Tuple[AvalancheDataset, AvalancheDataset]] |
| 33 | + ] = None, |
| 34 | +) -> CLScenario: |
| 35 | + """Helper to obtain a benchmark with a validation stream. |
| 36 | +
|
| 37 | + This generator accepts an existing benchmark instance and returns a version |
| 38 | + of it in which the train stream has been split into training and validation |
| 39 | + streams. |
| 40 | +
|
| 41 | + Each train/validation experience will be by splitting the original training |
| 42 | + experiences. Patterns selected for the validation experience will be removed |
| 43 | + from the training experiences. |
| 44 | +
|
| 45 | + The default splitting strategy is a random split as implemented by `split_validation_random`. |
| 46 | + If you want to use class balancing you can use `split_validation_class_balanced`, or |
| 47 | + use a custom `split_strategy`, as shown in the following example:: |
| 48 | +
|
| 49 | + validation_size = 0.2 |
| 50 | + foo = lambda exp: split_dataset_class_balanced(validation_size, exp) |
| 51 | + bm = benchmark_with_validation_stream(bm, custom_split_strategy=foo) |
| 52 | +
|
| 53 | + :param benchmark: The benchmark to split. |
| 54 | + :param validation_size: The size of the validation experience, as an int |
| 55 | + or a float between 0 and 1. Ignored if `custom_split_strategy` is used. |
| 56 | + :param shuffle: If True, patterns will be allocated to the validation |
| 57 | + stream randomly. This will use the default PyTorch random number |
| 58 | + generator at its current state. Defaults to False. Ignored if |
| 59 | + `custom_split_strategy` is used. If False, the first instances will be |
| 60 | + allocated to the training dataset by leaving the last ones to the |
| 61 | + validation dataset. |
| 62 | + :param split_strategy: A function that implements a custom splitting |
| 63 | + strategy. The function must accept an AvalancheDataset and return a tuple |
| 64 | + containing the new train and validation dataset. By default, the splitting |
| 65 | + strategy will split the data according to `validation_size` and `shuffle`). |
| 66 | + A good starting to understand the mechanism is to look at the |
| 67 | + implementation of the standard splitting function |
| 68 | + :func:`random_validation_split_strategy`. |
| 69 | +
|
| 70 | + :return: A benchmark instance in which the validation stream has been added. |
| 71 | + """ |
| 72 | + |
| 73 | + if split_strategy is None: |
| 74 | + if seed is None: |
| 75 | + seed = random.randint(0, 1000000) |
| 76 | + |
| 77 | + # functools.partial is a more compact option |
| 78 | + # However, MyPy does not understand what a partial is -_- |
| 79 | + def random_validation_split_strategy_wrapper(data): |
| 80 | + return split_validation_random(validation_size, shuffle, seed, data) |
| 81 | + |
| 82 | + split_strategy = random_validation_split_strategy_wrapper |
| 83 | + else: |
| 84 | + split_strategy = split_strategy |
| 85 | + |
| 86 | + stream = benchmark.streams["train"] |
| 87 | + if isinstance(stream, EagerCLStream): # eager split |
| 88 | + train_exps, valid_exps = [], [] |
| 89 | + |
| 90 | + exp: DatasetExperience |
| 91 | + for exp in stream: |
| 92 | + train_data, valid_data = split_strategy(exp.dataset) |
| 93 | + train_exps.append(DatasetExperience(dataset=train_data)) |
| 94 | + valid_exps.append(DatasetExperience(dataset=valid_data)) |
| 95 | + else: # Lazy splitting (based on a generator) |
| 96 | + split_generator = LazyTrainValSplitter(split_strategy, stream) |
| 97 | + train_exps = (DatasetExperience(dataset=a) for a, _ in split_generator) |
| 98 | + valid_exps = (DatasetExperience(dataset=b) for _, b in split_generator) |
| 99 | + |
| 100 | + train_stream = make_stream(name="train", exps=train_exps) |
| 101 | + valid_stream = make_stream(name="valid", exps=valid_exps) |
| 102 | + other_streams = benchmark.streams |
| 103 | + |
| 104 | + # don't drop classes-timeline for compatibility with old API |
| 105 | + e0 = next(iter(train_stream)) |
| 106 | + if hasattr(e0, "dataset") and hasattr(e0.dataset, "targets"): |
| 107 | + train_stream = with_classes_timeline(train_stream) |
| 108 | + valid_stream = with_classes_timeline(valid_stream) |
| 109 | + |
| 110 | + del other_streams["train"] |
| 111 | + return CLScenario( |
| 112 | + streams=[train_stream, valid_stream] + list(other_streams.values()) |
| 113 | + ) |
| 114 | + |
| 115 | + |
| 116 | +__all__ = ["benchmark_with_validation_stream"] |
0 commit comments