From aa880fd67a1a02ca4e1bcff8b0f40299425dc714 Mon Sep 17 00:00:00 2001 From: Antonio Carta Date: Wed, 14 Feb 2024 12:09:46 +0100 Subject: [PATCH 1/3] update benchmarks api docs --- README.md | 2 +- avalanche/benchmarks/scenarios/__init__.py | 1 + .../benchmarks/scenarios/dataset_scenario.py | 3 +- avalanche/benchmarks/scenarios/supervised.py | 6 ++ .../scenarios/validation_scenario.py | 9 +-- docs/benchmarks.rst | 55 +++++++++---------- .../03_benchmarks.md | 3 +- docs/models.rst | 35 ++++++------ 8 files changed, 55 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index c6e963ee4..ea8809f5f 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -
+div align="center"> # Avalanche: an End-to-End Library for Continual Learning **[Avalanche Website](https://avalanche.continualai.org)** | **[Getting Started](https://avalanche.continualai.org/getting-started)** | **[Examples](https://avalanche.continualai.org/examples)** | **[Tutorial](https://avalanche.continualai.org/from-zero-to-hero-tutorial)** | **[API Doc](https://avalanche-api.continualai.org)** | **[Paper](https://arxiv.org/abs/2104.00405)** | **[Twitter](https://twitter.com/AvalancheLib)** diff --git a/avalanche/benchmarks/scenarios/__init__.py b/avalanche/benchmarks/scenarios/__init__.py index 83db82055..a3ecad792 100644 --- a/avalanche/benchmarks/scenarios/__init__.py +++ b/avalanche/benchmarks/scenarios/__init__.py @@ -8,4 +8,5 @@ from .dataset_scenario import * from .exmodel_scenario import * from .online import * +from .supervised import * from .validation_scenario import * diff --git a/avalanche/benchmarks/scenarios/dataset_scenario.py b/avalanche/benchmarks/scenarios/dataset_scenario.py index cf5f7afa3..d00f2ac04 100644 --- a/avalanche/benchmarks/scenarios/dataset_scenario.py +++ b/avalanche/benchmarks/scenarios/dataset_scenario.py @@ -28,7 +28,7 @@ Dict, ) -from .generic_scenario import EagerCLStream, CLScenario, CLExperience, make_stream +from .generic_scenario import EagerCLStream, CLScenario, CLExperience from ..utils import TaskAwareSupervisedClassificationDataset @@ -257,4 +257,5 @@ def __iter__( "benchmark_from_datasets", "DatasetExperience", "split_validation_random", + "split_validation_class_balanced", ] diff --git a/avalanche/benchmarks/scenarios/supervised.py b/avalanche/benchmarks/scenarios/supervised.py index 8e46446db..c81102071 100644 --- a/avalanche/benchmarks/scenarios/supervised.py +++ b/avalanche/benchmarks/scenarios/supervised.py @@ -417,3 +417,9 @@ def _decorate_stream(obj: CLStream): raise ValueError( "Unsupported object type: must be one of {CLScenario, CLStream}" ) + +__all__ = [ + "class_incremental_benchmark", + "new_instances_benchmark", + "with_classes_timeline" +] \ No newline at end of file diff --git a/avalanche/benchmarks/scenarios/validation_scenario.py b/avalanche/benchmarks/scenarios/validation_scenario.py index b3cb08eb3..74d3bb2ad 100644 --- a/avalanche/benchmarks/scenarios/validation_scenario.py +++ b/avalanche/benchmarks/scenarios/validation_scenario.py @@ -1,20 +1,13 @@ from typing import ( Callable, - Generator, - Generic, - List, - Sequence, - TypeVar, Union, Tuple, Optional, - Iterable, - Dict, ) import random from avalanche.benchmarks.utils.data import AvalancheDataset -from .generic_scenario import EagerCLStream, CLScenario, CLExperience, make_stream +from .generic_scenario import EagerCLStream, CLScenario, make_stream from .dataset_scenario import ( LazyTrainValSplitter, DatasetExperience, diff --git a/docs/benchmarks.rst b/docs/benchmarks.rst index 5eb0efc8e..6f4b8c226 100644 --- a/docs/benchmarks.rst +++ b/docs/benchmarks.rst @@ -20,7 +20,7 @@ avalanche.benchmarks .. currentmodule:: avalanche.benchmarks.scenarios Continual Learning Scenarios -^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +---------------------------------------- | Generic definitions for scenarios, streams and experiences. All the continual learning benchmarks are specific instantiations of these concepts. @@ -49,7 +49,7 @@ Streams ClassificationStream Experiences -""""""""" +""""""""""""" .. autosummary:: :toctree: generated @@ -209,7 +209,7 @@ Benchmarks for learning from pretrained models or multi-agent continual learning .. currentmodule:: avalanche.benchmarks.datasets Datasets -^^^^^^^^^^^^^^^^^^^^^^^^^^ +---------------------------------------- | The **datasets** sub-module provides PyTorch dataset implementations for datasets missing from the torchvision/audio/* libraries. These datasets can also be used in a standalone way! @@ -235,52 +235,53 @@ Datasets torchaudio_wrapper.SpeechCommands -.. currentmodule:: avalanche.benchmarks.scenarios.deprecated.generators +.. currentmodule:: avalanche.benchmarks Benchmark Generators -^^^^^^^^^^^^^^^^^^^^^^^^^^ -| The **generators** sub-module provides a lot of functions that can be used to create a new benchmark. -| This set of functions tries to cover most common use cases (Class/Task-Incremental, Domain-Incremental, ...) but it also allows for the creation of entirely custom benchmarks (based on lists of tensors, on file lists, ...). - +---------------------------------------- -Generators for Class/Task/Domain-incremental benchmarks -........................................................ +| The **generators** sub-module provides a lot of functions that can be used to create a new benchmark. +| This set of functions tries to cover most common use cases (Class/Task-Incremental, Domain-Incremental, ...) but it also allows for the creation of entirely custom benchmarks from AvalancheDatasets. .. autosummary:: :toctree: generated - nc_benchmark - ni_benchmark - + benchmark_from_datasets + class_incremental_benchmark + new_instances_benchmark + task_incremental_benchmark -Starting from tensor lists, file lists, PyTorch datasets -.......................................................... +If you want to add attributes to experiences (such as `classes_in_this_experiences` or `task_labels`) you can use the generic decorators: .. autosummary:: :toctree: generated - dataset_benchmark - filelist_benchmark - paths_benchmark - tensors_benchmark + with_classes_timeline + with_task_labels +Online streams where experiences are made of small minibatches: -Misc (make data-incremental, add a validation stream, ...) -.............................................................. +.. autosummary:: + :toctree: generated -| Avalanche offers utilities to adapt a previously instantiated benchmark object. -| More utilities to come! + split_online_stream + split_continuous_linear_decay_stream + +Train/Validation splits for streams: .. autosummary:: :toctree: generated - data_incremental_benchmark benchmark_with_validation_stream + split_validation_class_balanced + split_validation_random + .. currentmodule:: avalanche.benchmarks.utils Utils (Data Loading and AvalancheDataset) -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +-------------------------------------------------------------------------------- + | The custom dataset and dataloader implementations contained in this sub-module are described in more detailed in the How-Tos about `"data loading and replay" ` and `"Avalanche Dataset" `. @@ -306,9 +307,5 @@ AvalancheDataset AvalancheDataset make_avalanche_dataset - make_classification_dataset - classification_subset - make_tensor_classification_dataset - concat_classification_datasets TaskSet DataAttribute diff --git a/docs/gitbook/from-zero-to-hero-tutorial/03_benchmarks.md b/docs/gitbook/from-zero-to-hero-tutorial/03_benchmarks.md index fd0e674d0..0d9c83ee9 100644 --- a/docs/gitbook/from-zero-to-hero-tutorial/03_benchmarks.md +++ b/docs/gitbook/from-zero-to-hero-tutorial/03_benchmarks.md @@ -186,10 +186,9 @@ print(f"{bm.test_stream.name} - len {len(bm.test_stream)}") we can also split a validation stream from the training stream - ```python -from avalanche.benchmarks.scenarios.dataset_scenario import benchmark_with_validation_stream +from avalanche.benchmarks.scenarios.validation_scenario import benchmark_with_validation_stream print(f"original training samples = {len(bm.train_stream[0].dataset)}") diff --git a/docs/models.rst b/docs/models.rst index 0b2ab30eb..a80abe459 100644 --- a/docs/models.rst +++ b/docs/models.rst @@ -14,7 +14,7 @@ models .. currentmodule:: avalanche.models Dynamic Modules -^^^^^^^^^^^^^^^ +------------------ Dynamic Modules are Pytorch modules that can be incrementally expanded to allow architectural modifications (multi-head classifiers, progressive @@ -28,9 +28,22 @@ networks, ...). IncrementalClassifier MultiHeadClassifier +Progressive Neural Networks +^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Modules that implement progressive neural networks models, layers, and adapters. + +.. autosummary:: + :toctree: generated + + PNN + PNNLayer + PNNColumn + LinearAdapter + MLPAdapter + Models -^^^^^^^^^^^^^^^^^^^^^^^ +------------------ | Neural network architectures that can be used as backbones for CL experiments. @@ -57,22 +70,8 @@ Models ExpertGate -Progressive Neural Networks -^^^^^^^^^^^^^^^^^^^^^^^^^^^ -Modules that implement progressive neural networks models, layers, and adapters. - -.. autosummary:: - :toctree: generated - - PNN - PNNLayer - PNNColumn - LinearAdapter - MLPAdapter - - Model Wrappers and Utilities -^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +------------------------------------ Wrappers and functions that add utility support to your models. .. autosummary:: @@ -87,7 +86,7 @@ Wrappers and functions that add utility support to your models. pytorchcv_wrapper.get_model Dynamic optimizer utilities -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +------------------------------------ Utilities to handle optimizer's update when using dynamic architectures. Dynamic Modules (e.g. multi-head) can change their parameters dynamically From 2b6253787dfd59277901cf030fe128ee8e17a241 Mon Sep 17 00:00:00 2001 From: AntonioCarta Date: Tue, 27 Feb 2024 16:28:55 +0100 Subject: [PATCH 2/3] a --- .../deprecated/classification_scenario.py | 6 +- .../scenarios/deprecated/dataset_scenario.py | 18 ++--- .../deprecated/generic_benchmark_creation.py | 6 +- .../deprecated/lazy_dataset_sequence.py | 18 ++--- .../deprecated/new_classes/nc_scenario.py | 6 +- .../scenarios/detection_scenario.py | 6 +- .../benchmarks/scenarios/generic_scenario.py | 6 +- avalanche/benchmarks/scenarios/supervised.py | 5 +- .../utils/classification_dataset.py | 35 ++++++---- avalanche/benchmarks/utils/data.py | 6 +- avalanche/benchmarks/utils/data_attribute.py | 6 +- .../benchmarks/utils/dataset_definitions.py | 6 +- avalanche/benchmarks/utils/dataset_utils.py | 6 +- .../benchmarks/utils/detection_dataset.py | 27 +++++--- avalanche/benchmarks/utils/flat_data.py | 18 +++-- avalanche/evaluation/metric_definitions.py | 6 +- .../evaluation/metrics/forgetting_bwt.py | 9 ++- .../evaluation/metrics/labels_repartition.py | 6 +- avalanche/training/plugins/bic.py | 6 +- avalanche/training/supervised/expert_gate.py | 6 +- .../training/supervised/joint_training.py | 6 +- avalanche/training/templates/base.py | 12 ++-- .../templates/strategy_mixin_protocol.py | 66 ++++++++++++------- examples/detection.py | 8 ++- examples/detection_lvis.py | 8 ++- 25 files changed, 192 insertions(+), 116 deletions(-) diff --git a/avalanche/benchmarks/scenarios/deprecated/classification_scenario.py b/avalanche/benchmarks/scenarios/deprecated/classification_scenario.py index b195a27f6..ecdabacaa 100644 --- a/avalanche/benchmarks/scenarios/deprecated/classification_scenario.py +++ b/avalanche/benchmarks/scenarios/deprecated/classification_scenario.py @@ -257,10 +257,12 @@ def __len__(self) -> int: return len(self._benchmark.streams[self._stream]) @overload - def __getitem__(self, exp_id: int) -> Optional[Set[int]]: ... + def __getitem__(self, exp_id: int) -> Optional[Set[int]]: + ... @overload - def __getitem__(self, exp_id: slice) -> Tuple[Optional[Set[int]], ...]: ... + def __getitem__(self, exp_id: slice) -> Tuple[Optional[Set[int]], ...]: + ... def __getitem__(self, exp_id: Union[int, slice]) -> LazyClassesInExpsRet: indexing_collate = _LazyClassesInClassificationExps._slice_collate diff --git a/avalanche/benchmarks/scenarios/deprecated/dataset_scenario.py b/avalanche/benchmarks/scenarios/deprecated/dataset_scenario.py index 5af73eb9b..5547a23b5 100644 --- a/avalanche/benchmarks/scenarios/deprecated/dataset_scenario.py +++ b/avalanche/benchmarks/scenarios/deprecated/dataset_scenario.py @@ -184,17 +184,17 @@ def __init__( invoking the super constructor) to specialize the experience class. """ - self.experience_factory: Callable[[TCLStream, int], TDatasetExperience] = ( - experience_factory - ) + self.experience_factory: Callable[ + [TCLStream, int], TDatasetExperience + ] = experience_factory - self.stream_factory: Callable[[str, TDatasetScenario], TCLStream] = ( - stream_factory - ) + self.stream_factory: Callable[ + [str, TDatasetScenario], TCLStream + ] = stream_factory - self.stream_definitions: Dict[str, StreamDef[TCLDataset]] = ( - DatasetScenario._check_stream_definitions(stream_definitions) - ) + self.stream_definitions: Dict[ + str, StreamDef[TCLDataset] + ] = DatasetScenario._check_stream_definitions(stream_definitions) """ A structure containing the definition of the streams. """ diff --git a/avalanche/benchmarks/scenarios/deprecated/generic_benchmark_creation.py b/avalanche/benchmarks/scenarios/deprecated/generic_benchmark_creation.py index 351d7b6f3..9bf1e9b6e 100644 --- a/avalanche/benchmarks/scenarios/deprecated/generic_benchmark_creation.py +++ b/avalanche/benchmarks/scenarios/deprecated/generic_benchmark_creation.py @@ -143,9 +143,9 @@ def create_multi_dataset_generic_benchmark( "complete_test_set_only is True" ) - stream_definitions: Dict[str, Tuple[Iterable[TaskAwareClassificationDataset]]] = ( - dict() - ) + stream_definitions: Dict[ + str, Tuple[Iterable[TaskAwareClassificationDataset]] + ] = dict() for stream_name, dataset_list in input_streams.items(): initial_transform_group = "train" diff --git a/avalanche/benchmarks/scenarios/deprecated/lazy_dataset_sequence.py b/avalanche/benchmarks/scenarios/deprecated/lazy_dataset_sequence.py index 82251d0da..1d20fb774 100644 --- a/avalanche/benchmarks/scenarios/deprecated/lazy_dataset_sequence.py +++ b/avalanche/benchmarks/scenarios/deprecated/lazy_dataset_sequence.py @@ -99,9 +99,9 @@ def __init__( now, including the ones of dropped experiences. """ - self.task_labels_field_sequence: Dict[int, Optional[Sequence[int]]] = ( - defaultdict(lambda: None) - ) + self.task_labels_field_sequence: Dict[ + int, Optional[Sequence[int]] + ] = defaultdict(lambda: None) """ A dictionary mapping each experience to its `targets_task_labels` field. @@ -118,10 +118,12 @@ def __len__(self) -> int: return self._stream_length @overload - def __getitem__(self, exp_idx: int) -> TCLDataset: ... + def __getitem__(self, exp_idx: int) -> TCLDataset: + ... @overload - def __getitem__(self, exp_idx: slice) -> Sequence[TCLDataset]: ... + def __getitem__(self, exp_idx: slice) -> Sequence[TCLDataset]: + ... def __getitem__( self, exp_idx: Union[int, slice] @@ -133,9 +135,9 @@ def __getitem__( :return: The dataset associated to the experience. """ # A lot of unuseful lines needed for MyPy -_- - indexing_collate: Callable[[Iterable[TCLDataset]], Sequence[TCLDataset]] = ( - lambda x: list(x) - ) + indexing_collate: Callable[ + [Iterable[TCLDataset]], Sequence[TCLDataset] + ] = lambda x: list(x) result = manage_advanced_indexing( exp_idx, self._get_experience_and_load_if_needed, diff --git a/avalanche/benchmarks/scenarios/deprecated/new_classes/nc_scenario.py b/avalanche/benchmarks/scenarios/deprecated/new_classes/nc_scenario.py index a5509b18d..fcbe8610b 100644 --- a/avalanche/benchmarks/scenarios/deprecated/new_classes/nc_scenario.py +++ b/avalanche/benchmarks/scenarios/deprecated/new_classes/nc_scenario.py @@ -327,9 +327,9 @@ class "34" will be mapped to "1", class "11" to "2" and so on. # used, the user may have defined an amount of classes less than # the overall amount of classes in the dataset. if class_id in self.classes_order_original_ids: - self.class_mapping[class_id] = ( - self.classes_order_original_ids.index(class_id) - ) + self.class_mapping[ + class_id + ] = self.classes_order_original_ids.index(class_id) elif self.class_ids_from_zero_in_each_exp: # Method 2: remap class IDs so that they appear in range [0, N] in # each experience diff --git a/avalanche/benchmarks/scenarios/detection_scenario.py b/avalanche/benchmarks/scenarios/detection_scenario.py index 111c5ac11..8b16be027 100644 --- a/avalanche/benchmarks/scenarios/detection_scenario.py +++ b/avalanche/benchmarks/scenarios/detection_scenario.py @@ -254,10 +254,12 @@ def __len__(self): return len(self._benchmark.streams[self._stream]) @overload - def __getitem__(self, exp_id: int) -> Optional[Set[int]]: ... + def __getitem__(self, exp_id: int) -> Optional[Set[int]]: + ... @overload - def __getitem__(self, exp_id: slice) -> Tuple[Optional[Set[int]], ...]: ... + def __getitem__(self, exp_id: slice) -> Tuple[Optional[Set[int]], ...]: + ... def __getitem__(self, exp_id: Union[int, slice]) -> LazyClassesInExpsRet: indexing_collate = _LazyClassesInDetectionExps._slice_collate diff --git a/avalanche/benchmarks/scenarios/generic_scenario.py b/avalanche/benchmarks/scenarios/generic_scenario.py index 34da0d249..d7b5ba09c 100644 --- a/avalanche/benchmarks/scenarios/generic_scenario.py +++ b/avalanche/benchmarks/scenarios/generic_scenario.py @@ -427,10 +427,12 @@ def __iter__(self) -> Iterator[TCLExperience]: yield exp @overload - def __getitem__(self, item: int) -> TCLExperience: ... + def __getitem__(self, item: int) -> TCLExperience: + ... @overload - def __getitem__(self: TSequenceCLStream, item: slice) -> TSequenceCLStream: ... + def __getitem__(self: TSequenceCLStream, item: slice) -> TSequenceCLStream: + ... @final def __getitem__( diff --git a/avalanche/benchmarks/scenarios/supervised.py b/avalanche/benchmarks/scenarios/supervised.py index c81102071..c9a5f36ce 100644 --- a/avalanche/benchmarks/scenarios/supervised.py +++ b/avalanche/benchmarks/scenarios/supervised.py @@ -418,8 +418,9 @@ def _decorate_stream(obj: CLStream): "Unsupported object type: must be one of {CLScenario, CLStream}" ) + __all__ = [ "class_incremental_benchmark", "new_instances_benchmark", - "with_classes_timeline" -] \ No newline at end of file + "with_classes_timeline", +] diff --git a/avalanche/benchmarks/utils/classification_dataset.py b/avalanche/benchmarks/utils/classification_dataset.py index ad6db47f6..acb4f880b 100644 --- a/avalanche/benchmarks/utils/classification_dataset.py +++ b/avalanche/benchmarks/utils/classification_dataset.py @@ -175,7 +175,8 @@ def _make_taskaware_classification_dataset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareSupervisedClassificationDataset: ... +) -> TaskAwareSupervisedClassificationDataset: + ... @overload @@ -189,7 +190,8 @@ def _make_taskaware_classification_dataset( task_labels: Union[int, Sequence[int]], targets: Sequence[TTargetType], collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareSupervisedClassificationDataset: ... +) -> TaskAwareSupervisedClassificationDataset: + ... @overload @@ -203,7 +205,8 @@ def _make_taskaware_classification_dataset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareClassificationDataset: ... +) -> TaskAwareClassificationDataset: + ... def _make_taskaware_classification_dataset( @@ -383,7 +386,8 @@ def _taskaware_classification_subset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareSupervisedClassificationDataset: ... +) -> TaskAwareSupervisedClassificationDataset: + ... @overload @@ -399,7 +403,8 @@ def _taskaware_classification_subset( task_labels: Union[int, Sequence[int]], targets: Sequence[TTargetType], collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareSupervisedClassificationDataset: ... +) -> TaskAwareSupervisedClassificationDataset: + ... @overload @@ -415,7 +420,8 @@ def _taskaware_classification_subset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareClassificationDataset: ... +) -> TaskAwareClassificationDataset: + ... def _taskaware_classification_subset( @@ -613,7 +619,8 @@ def _make_taskaware_tensor_classification_dataset( task_labels: Union[int, Sequence[int]], targets: Union[Sequence[TTargetType], int], collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareSupervisedClassificationDataset: ... +) -> TaskAwareSupervisedClassificationDataset: + ... @overload @@ -626,9 +633,8 @@ def _make_taskaware_tensor_classification_dataset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Union[Sequence[TTargetType], int]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> Union[ - TaskAwareClassificationDataset, TaskAwareSupervisedClassificationDataset -]: ... +) -> Union[TaskAwareClassificationDataset, TaskAwareSupervisedClassificationDataset]: + ... def _make_taskaware_tensor_classification_dataset( @@ -753,7 +759,8 @@ def _concat_taskaware_classification_datasets( Union[Sequence[TTargetType], Sequence[Sequence[TTargetType]]] ] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareSupervisedClassificationDataset: ... +) -> TaskAwareSupervisedClassificationDataset: + ... @overload @@ -767,7 +774,8 @@ def _concat_taskaware_classification_datasets( task_labels: Union[int, Sequence[int], Sequence[Sequence[int]]], targets: Union[Sequence[TTargetType], Sequence[Sequence[TTargetType]]], collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareSupervisedClassificationDataset: ... +) -> TaskAwareSupervisedClassificationDataset: + ... @overload @@ -783,7 +791,8 @@ def _concat_taskaware_classification_datasets( Union[Sequence[TTargetType], Sequence[Sequence[TTargetType]]] ] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareClassificationDataset: ... +) -> TaskAwareClassificationDataset: + ... def _concat_taskaware_classification_datasets( diff --git a/avalanche/benchmarks/utils/data.py b/avalanche/benchmarks/utils/data.py index 9985e19fa..76908345e 100644 --- a/avalanche/benchmarks/utils/data.py +++ b/avalanche/benchmarks/utils/data.py @@ -344,10 +344,12 @@ def __eq__(self, other: object): ) @overload - def __getitem__(self, exp_id: int) -> T_co: ... + def __getitem__(self, exp_id: int) -> T_co: + ... @overload - def __getitem__(self: TAvalancheDataset, exp_id: slice) -> TAvalancheDataset: ... + def __getitem__(self: TAvalancheDataset, exp_id: slice) -> TAvalancheDataset: + ... def __getitem__( self: TAvalancheDataset, idx: Union[int, slice] diff --git a/avalanche/benchmarks/utils/data_attribute.py b/avalanche/benchmarks/utils/data_attribute.py index d3ee4b0d9..3cb88e58c 100644 --- a/avalanche/benchmarks/utils/data_attribute.py +++ b/avalanche/benchmarks/utils/data_attribute.py @@ -62,10 +62,12 @@ def __iter__(self): yield self[i] @overload - def __getitem__(self, item: int) -> T_co: ... + def __getitem__(self, item: int) -> T_co: + ... @overload - def __getitem__(self, item: slice) -> Sequence[T_co]: ... + def __getitem__(self, item: slice) -> Sequence[T_co]: + ... def __getitem__(self, item: Union[int, slice]) -> Union[T_co, Sequence[T_co]]: return self.data[item] diff --git a/avalanche/benchmarks/utils/dataset_definitions.py b/avalanche/benchmarks/utils/dataset_definitions.py index de9fa05da..ca8de5b54 100644 --- a/avalanche/benchmarks/utils/dataset_definitions.py +++ b/avalanche/benchmarks/utils/dataset_definitions.py @@ -38,9 +38,11 @@ class IDataset(Protocol[T_co]): Note: no __add__ method is defined. """ - def __getitem__(self, index: int) -> T_co: ... + def __getitem__(self, index: int) -> T_co: + ... - def __len__(self) -> int: ... + def __len__(self) -> int: + ... class IDatasetWithTargets(IDataset[T_co], Protocol[T_co, TTargetType_co]): diff --git a/avalanche/benchmarks/utils/dataset_utils.py b/avalanche/benchmarks/utils/dataset_utils.py index 867ba9cc6..717d67e75 100644 --- a/avalanche/benchmarks/utils/dataset_utils.py +++ b/avalanche/benchmarks/utils/dataset_utils.py @@ -64,10 +64,12 @@ def __iter__(self) -> Iterator[TData]: yield el @overload - def __getitem__(self, item: int) -> TData: ... + def __getitem__(self, item: int) -> TData: + ... @overload - def __getitem__(self: TSliceSequence, item: slice) -> TSliceSequence: ... + def __getitem__(self: TSliceSequence, item: slice) -> TSliceSequence: + ... @final def __getitem__( diff --git a/avalanche/benchmarks/utils/detection_dataset.py b/avalanche/benchmarks/utils/detection_dataset.py index 6c5efb43f..7f3b3b632 100644 --- a/avalanche/benchmarks/utils/detection_dataset.py +++ b/avalanche/benchmarks/utils/detection_dataset.py @@ -151,7 +151,8 @@ def make_detection_dataset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> SupervisedDetectionDataset: ... +) -> SupervisedDetectionDataset: + ... @overload @@ -165,7 +166,8 @@ def make_detection_dataset( task_labels: Union[int, Sequence[int]], targets: Sequence[TTargetType], collate_fn: Optional[Callable[[List], Any]] = None -) -> SupervisedDetectionDataset: ... +) -> SupervisedDetectionDataset: + ... @overload @@ -179,7 +181,8 @@ def make_detection_dataset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> DetectionDataset: ... +) -> DetectionDataset: + ... def make_detection_dataset( @@ -370,7 +373,8 @@ def detection_subset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> SupervisedDetectionDataset: ... +) -> SupervisedDetectionDataset: + ... @overload @@ -386,7 +390,8 @@ def detection_subset( task_labels: Union[int, Sequence[int]], targets: Sequence[TTargetType], collate_fn: Optional[Callable[[List], Any]] = None -) -> SupervisedDetectionDataset: ... +) -> SupervisedDetectionDataset: + ... @overload @@ -402,7 +407,8 @@ def detection_subset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> DetectionDataset: ... +) -> DetectionDataset: + ... def detection_subset( @@ -589,7 +595,8 @@ def concat_detection_datasets( Union[Sequence[TTargetType], Sequence[Sequence[TTargetType]]] ] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> SupervisedDetectionDataset: ... +) -> SupervisedDetectionDataset: + ... @overload @@ -603,7 +610,8 @@ def concat_detection_datasets( task_labels: Union[int, Sequence[int], Sequence[Sequence[int]]], targets: Union[Sequence[TTargetType], Sequence[Sequence[TTargetType]]], collate_fn: Optional[Callable[[List], Any]] = None -) -> SupervisedDetectionDataset: ... +) -> SupervisedDetectionDataset: + ... @overload @@ -619,7 +627,8 @@ def concat_detection_datasets( Union[Sequence[TTargetType], Sequence[Sequence[TTargetType]]] ] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> DetectionDataset: ... +) -> DetectionDataset: + ... def concat_detection_datasets( diff --git a/avalanche/benchmarks/utils/flat_data.py b/avalanche/benchmarks/utils/flat_data.py index 02d3681cd..d9fd73ee0 100644 --- a/avalanche/benchmarks/utils/flat_data.py +++ b/avalanche/benchmarks/utils/flat_data.py @@ -66,9 +66,9 @@ def __init__( else: new_lists.append(ll) - self._lists: Optional[List[Sequence[int]]] = ( - new_lists # freed after eagerification - ) + self._lists: Optional[ + List[Sequence[int]] + ] = new_lists # freed after eagerification self._offset: int = int(offset) self._eager_list: Optional[np.ndarray] = None """This is the list where we save indices @@ -408,10 +408,12 @@ def _get_idx(self, idx) -> Tuple[int, int]: return dataset_idx, int(idx) @overload - def __getitem__(self, item: int) -> T_co: ... + def __getitem__(self, item: int) -> T_co: + ... @overload - def __getitem__(self: TFlatData, item: slice) -> TFlatData: ... + def __getitem__(self: TFlatData, item: slice) -> TFlatData: + ... def __getitem__(self: TFlatData, item: Union[int, slice]) -> Union[T_co, TFlatData]: if isinstance(item, (int, np.integer)): @@ -468,10 +470,12 @@ def __len__(self): return self._size @overload - def __getitem__(self, index: int) -> DataT: ... + def __getitem__(self, index: int) -> DataT: + ... @overload - def __getitem__(self, index: slice) -> "ConstantSequence[DataT]": ... + def __getitem__(self, index: slice) -> "ConstantSequence[DataT]": + ... def __getitem__( self, index: Union[int, slice] diff --git a/avalanche/evaluation/metric_definitions.py b/avalanche/evaluation/metric_definitions.py index c91a7122c..1934d5392 100644 --- a/avalanche/evaluation/metric_definitions.py +++ b/avalanche/evaluation/metric_definitions.py @@ -215,7 +215,8 @@ def __init__( ] = "experience", emit_at: Literal["iteration", "epoch", "experience", "stream"] = "experience", mode: Literal["train"] = "train", - ): ... + ): + ... @overload def __init__( @@ -224,7 +225,8 @@ def __init__( reset_at: Literal["iteration", "experience", "stream", "never"] = "experience", emit_at: Literal["iteration", "experience", "stream"] = "experience", mode: Literal["eval"] = "eval", - ): ... + ): + ... def __init__( self, metric: TMetric, reset_at="experience", emit_at="experience", mode="eval" diff --git a/avalanche/evaluation/metrics/forgetting_bwt.py b/avalanche/evaluation/metrics/forgetting_bwt.py index 29d9ffd28..9e5f79094 100644 --- a/avalanche/evaluation/metrics/forgetting_bwt.py +++ b/avalanche/evaluation/metrics/forgetting_bwt.py @@ -526,15 +526,18 @@ def forgetting_metrics(*, experience=False, stream=False) -> List[PluginMetric]: @overload -def forgetting_to_bwt(f: float) -> float: ... +def forgetting_to_bwt(f: float) -> float: + ... @overload -def forgetting_to_bwt(f: Dict[int, float]) -> Dict[int, float]: ... +def forgetting_to_bwt(f: Dict[int, float]) -> Dict[int, float]: + ... @overload -def forgetting_to_bwt(f: None) -> None: ... +def forgetting_to_bwt(f: None) -> None: + ... def forgetting_to_bwt(f: Optional[Union[float, Dict[int, float]]]): diff --git a/avalanche/evaluation/metrics/labels_repartition.py b/avalanche/evaluation/metrics/labels_repartition.py index 7a5304696..71d6fb0e9 100644 --- a/avalanche/evaluation/metrics/labels_repartition.py +++ b/avalanche/evaluation/metrics/labels_repartition.py @@ -85,7 +85,8 @@ def __init__( ] = default_history_repartition_image_creator, mode: Literal["train"] = "train", emit_reset_at: Literal["stream", "experience", "epoch"] = "epoch", - ): ... + ): + ... @overload def __init__( @@ -96,7 +97,8 @@ def __init__( ] = default_history_repartition_image_creator, mode: Literal["eval"] = "eval", emit_reset_at: Literal["stream", "experience"], - ): ... + ): + ... def __init__( self, diff --git a/avalanche/training/plugins/bic.py b/avalanche/training/plugins/bic.py index caef605b9..61535fd37 100644 --- a/avalanche/training/plugins/bic.py +++ b/avalanche/training/plugins/bic.py @@ -454,9 +454,9 @@ def _classes_groups(self, strategy: SupervisedTemplate): # - "current" classes: seen in current_experience # "initial" classes - initial_classes: Set[int] = ( - set() - ) # pre_initial_cl in the original implementation + initial_classes: Set[ + int + ] = set() # pre_initial_cl in the original implementation previous_classes: Set[int] = set() # pre_new_cl in the original implementation current_classes: Set[int] = set() # new_cl in the original implementation # Note: pre_initial_cl + pre_new_cl is "initial_cl" in the original implementation diff --git a/avalanche/training/supervised/expert_gate.py b/avalanche/training/supervised/expert_gate.py index abb249216..a76637514 100644 --- a/avalanche/training/supervised/expert_gate.py +++ b/avalanche/training/supervised/expert_gate.py @@ -239,9 +239,9 @@ def _select_expert(self, strategy: "SupervisedTemplate", task_label): # Iterate through all autoencoders to get error values for autoencoder_id in strategy.model.autoencoder_dict: - error_dict[str(autoencoder_id)] = ( - self._get_average_reconstruction_error(strategy, autoencoder_id) - ) + error_dict[ + str(autoencoder_id) + ] = self._get_average_reconstruction_error(strategy, autoencoder_id) # Send error dictionary to get most relevant autoencoder relatedness_dict = self._task_relatedness(strategy, error_dict, task_label) diff --git a/avalanche/training/supervised/joint_training.py b/avalanche/training/supervised/joint_training.py index 08294cfc0..4e145532e 100644 --- a/avalanche/training/supervised/joint_training.py +++ b/avalanche/training/supervised/joint_training.py @@ -139,9 +139,9 @@ def train( ) # Normalize training and eval data. - experiences_list: Iterable[TDatasetExperience] = ( - _experiences_parameter_as_iterable(experiences) - ) + experiences_list: Iterable[ + TDatasetExperience + ] = _experiences_parameter_as_iterable(experiences) if eval_streams is None: eval_streams = [experiences_list] diff --git a/avalanche/training/templates/base.py b/avalanche/training/templates/base.py index 2ec228291..8dd5c8c03 100644 --- a/avalanche/training/templates/base.py +++ b/avalanche/training/templates/base.py @@ -147,9 +147,9 @@ def train( self.model.to(self.device) # Normalize training and eval data. - experiences_list: Iterable[TExperienceType] = ( - _experiences_parameter_as_iterable(experiences) - ) + experiences_list: Iterable[ + TExperienceType + ] = _experiences_parameter_as_iterable(experiences) if eval_streams is None: eval_streams = [experiences_list] @@ -201,9 +201,9 @@ def eval( self.is_training = False self.model.eval() - experiences_list: Iterable[TExperienceType] = ( - _experiences_parameter_as_iterable(experiences) - ) + experiences_list: Iterable[ + TExperienceType + ] = _experiences_parameter_as_iterable(experiences) self.current_eval_stream = experiences_list self._before_eval(**kwargs) diff --git a/avalanche/training/templates/strategy_mixin_protocol.py b/avalanche/training/templates/strategy_mixin_protocol.py index 4596c9d39..847d6ef78 100644 --- a/avalanche/training/templates/strategy_mixin_protocol.py +++ b/avalanche/training/templates/strategy_mixin_protocol.py @@ -58,37 +58,53 @@ class SGDStrategyProtocol( _criterion: CriterionType - def forward(self) -> TMBOutput: ... + def forward(self) -> TMBOutput: + ... - def criterion(self) -> Tensor: ... + def criterion(self) -> Tensor: + ... - def backward(self) -> None: ... + def backward(self) -> None: + ... - def _make_empty_loss(self) -> Tensor: ... + def _make_empty_loss(self) -> Tensor: + ... - def make_optimizer(self, **kwargs): ... + def make_optimizer(self, **kwargs): + ... - def optimizer_step(self) -> None: ... + def optimizer_step(self) -> None: + ... - def model_adaptation(self, model: Optional[Module] = None) -> Module: ... + def model_adaptation(self, model: Optional[Module] = None) -> Module: + ... - def _unpack_minibatch(self): ... + def _unpack_minibatch(self): + ... - def _before_training_iteration(self, **kwargs): ... + def _before_training_iteration(self, **kwargs): + ... - def _before_forward(self, **kwargs): ... + def _before_forward(self, **kwargs): + ... - def _after_forward(self, **kwargs): ... + def _after_forward(self, **kwargs): + ... - def _before_backward(self, **kwargs): ... + def _before_backward(self, **kwargs): + ... - def _after_backward(self, **kwargs): ... + def _after_backward(self, **kwargs): + ... - def _before_update(self, **kwargs): ... + def _before_update(self, **kwargs): + ... - def _after_update(self, **kwargs): ... + def _after_update(self, **kwargs): + ... - def _after_training_iteration(self, **kwargs): ... + def _after_training_iteration(self, **kwargs): + ... class SupervisedStrategyProtocol( @@ -106,17 +122,23 @@ class MetaLearningStrategyProtocol( SGDStrategyProtocol[TSGDExperienceType, TMBInput, TMBOutput], Protocol[TSGDExperienceType, TMBInput, TMBOutput], ): - def _before_inner_updates(self, **kwargs): ... + def _before_inner_updates(self, **kwargs): + ... - def _inner_updates(self, **kwargs): ... + def _inner_updates(self, **kwargs): + ... - def _after_inner_updates(self, **kwargs): ... + def _after_inner_updates(self, **kwargs): + ... - def _before_outer_update(self, **kwargs): ... + def _before_outer_update(self, **kwargs): + ... - def _outer_update(self, **kwargs): ... + def _outer_update(self, **kwargs): + ... - def _after_outer_update(self, **kwargs): ... + def _after_outer_update(self, **kwargs): + ... __all__ = [ diff --git a/examples/detection.py b/examples/detection.py index ec2e92119..3e7f7d94e 100644 --- a/examples/detection.py +++ b/examples/detection.py @@ -160,11 +160,15 @@ def obtain_base_model(segmentation: bool): pretrain_argument["pretrained"] = True else: if segmentation: - pretrain_argument["weights"] = ( + pretrain_argument[ + "weights" + ] = ( torchvision.models.detection.mask_rcnn.MaskRCNN_ResNet50_FPN_Weights.DEFAULT ) else: - pretrain_argument["weights"] = ( + pretrain_argument[ + "weights" + ] = ( torchvision.models.detection.faster_rcnn.FasterRCNN_ResNet50_FPN_Weights.DEFAULT ) diff --git a/examples/detection_lvis.py b/examples/detection_lvis.py index 330b7a9ec..ed86e5b2c 100644 --- a/examples/detection_lvis.py +++ b/examples/detection_lvis.py @@ -142,11 +142,15 @@ def obtain_base_model(segmentation: bool): pretrain_argument["pretrained"] = True else: if segmentation: - pretrain_argument["weights"] = ( + pretrain_argument[ + "weights" + ] = ( torchvision.models.detection.mask_rcnn.MaskRCNN_ResNet50_FPN_Weights.DEFAULT ) else: - pretrain_argument["weights"] = ( + pretrain_argument[ + "weights" + ] = ( torchvision.models.detection.faster_rcnn.FasterRCNN_ResNet50_FPN_Weights.DEFAULT ) From 2d4414dea000178b6b42beac56a60320a9c704c2 Mon Sep 17 00:00:00 2001 From: AntonioCarta Date: Tue, 27 Feb 2024 16:32:31 +0100 Subject: [PATCH 3/3] black --- .../deprecated/classification_scenario.py | 6 +- .../scenarios/deprecated/dataset_scenario.py | 18 ++--- .../deprecated/generic_benchmark_creation.py | 6 +- .../deprecated/lazy_dataset_sequence.py | 18 +++-- .../deprecated/new_classes/nc_scenario.py | 6 +- .../scenarios/detection_scenario.py | 6 +- .../benchmarks/scenarios/generic_scenario.py | 6 +- .../utils/classification_dataset.py | 35 ++++------ avalanche/benchmarks/utils/data.py | 6 +- avalanche/benchmarks/utils/data_attribute.py | 6 +- .../benchmarks/utils/dataset_definitions.py | 6 +- avalanche/benchmarks/utils/dataset_utils.py | 6 +- .../benchmarks/utils/detection_dataset.py | 27 +++----- avalanche/benchmarks/utils/flat_data.py | 18 ++--- avalanche/evaluation/metric_definitions.py | 6 +- .../evaluation/metrics/forgetting_bwt.py | 9 +-- .../evaluation/metrics/labels_repartition.py | 6 +- avalanche/training/plugins/bic.py | 6 +- avalanche/training/supervised/expert_gate.py | 6 +- .../training/supervised/joint_training.py | 6 +- avalanche/training/templates/base.py | 12 ++-- .../templates/strategy_mixin_protocol.py | 66 +++++++------------ examples/detection.py | 8 +-- examples/detection_lvis.py | 8 +-- 24 files changed, 114 insertions(+), 189 deletions(-) diff --git a/avalanche/benchmarks/scenarios/deprecated/classification_scenario.py b/avalanche/benchmarks/scenarios/deprecated/classification_scenario.py index ecdabacaa..b195a27f6 100644 --- a/avalanche/benchmarks/scenarios/deprecated/classification_scenario.py +++ b/avalanche/benchmarks/scenarios/deprecated/classification_scenario.py @@ -257,12 +257,10 @@ def __len__(self) -> int: return len(self._benchmark.streams[self._stream]) @overload - def __getitem__(self, exp_id: int) -> Optional[Set[int]]: - ... + def __getitem__(self, exp_id: int) -> Optional[Set[int]]: ... @overload - def __getitem__(self, exp_id: slice) -> Tuple[Optional[Set[int]], ...]: - ... + def __getitem__(self, exp_id: slice) -> Tuple[Optional[Set[int]], ...]: ... def __getitem__(self, exp_id: Union[int, slice]) -> LazyClassesInExpsRet: indexing_collate = _LazyClassesInClassificationExps._slice_collate diff --git a/avalanche/benchmarks/scenarios/deprecated/dataset_scenario.py b/avalanche/benchmarks/scenarios/deprecated/dataset_scenario.py index 5547a23b5..5af73eb9b 100644 --- a/avalanche/benchmarks/scenarios/deprecated/dataset_scenario.py +++ b/avalanche/benchmarks/scenarios/deprecated/dataset_scenario.py @@ -184,17 +184,17 @@ def __init__( invoking the super constructor) to specialize the experience class. """ - self.experience_factory: Callable[ - [TCLStream, int], TDatasetExperience - ] = experience_factory + self.experience_factory: Callable[[TCLStream, int], TDatasetExperience] = ( + experience_factory + ) - self.stream_factory: Callable[ - [str, TDatasetScenario], TCLStream - ] = stream_factory + self.stream_factory: Callable[[str, TDatasetScenario], TCLStream] = ( + stream_factory + ) - self.stream_definitions: Dict[ - str, StreamDef[TCLDataset] - ] = DatasetScenario._check_stream_definitions(stream_definitions) + self.stream_definitions: Dict[str, StreamDef[TCLDataset]] = ( + DatasetScenario._check_stream_definitions(stream_definitions) + ) """ A structure containing the definition of the streams. """ diff --git a/avalanche/benchmarks/scenarios/deprecated/generic_benchmark_creation.py b/avalanche/benchmarks/scenarios/deprecated/generic_benchmark_creation.py index 9bf1e9b6e..351d7b6f3 100644 --- a/avalanche/benchmarks/scenarios/deprecated/generic_benchmark_creation.py +++ b/avalanche/benchmarks/scenarios/deprecated/generic_benchmark_creation.py @@ -143,9 +143,9 @@ def create_multi_dataset_generic_benchmark( "complete_test_set_only is True" ) - stream_definitions: Dict[ - str, Tuple[Iterable[TaskAwareClassificationDataset]] - ] = dict() + stream_definitions: Dict[str, Tuple[Iterable[TaskAwareClassificationDataset]]] = ( + dict() + ) for stream_name, dataset_list in input_streams.items(): initial_transform_group = "train" diff --git a/avalanche/benchmarks/scenarios/deprecated/lazy_dataset_sequence.py b/avalanche/benchmarks/scenarios/deprecated/lazy_dataset_sequence.py index 1d20fb774..82251d0da 100644 --- a/avalanche/benchmarks/scenarios/deprecated/lazy_dataset_sequence.py +++ b/avalanche/benchmarks/scenarios/deprecated/lazy_dataset_sequence.py @@ -99,9 +99,9 @@ def __init__( now, including the ones of dropped experiences. """ - self.task_labels_field_sequence: Dict[ - int, Optional[Sequence[int]] - ] = defaultdict(lambda: None) + self.task_labels_field_sequence: Dict[int, Optional[Sequence[int]]] = ( + defaultdict(lambda: None) + ) """ A dictionary mapping each experience to its `targets_task_labels` field. @@ -118,12 +118,10 @@ def __len__(self) -> int: return self._stream_length @overload - def __getitem__(self, exp_idx: int) -> TCLDataset: - ... + def __getitem__(self, exp_idx: int) -> TCLDataset: ... @overload - def __getitem__(self, exp_idx: slice) -> Sequence[TCLDataset]: - ... + def __getitem__(self, exp_idx: slice) -> Sequence[TCLDataset]: ... def __getitem__( self, exp_idx: Union[int, slice] @@ -135,9 +133,9 @@ def __getitem__( :return: The dataset associated to the experience. """ # A lot of unuseful lines needed for MyPy -_- - indexing_collate: Callable[ - [Iterable[TCLDataset]], Sequence[TCLDataset] - ] = lambda x: list(x) + indexing_collate: Callable[[Iterable[TCLDataset]], Sequence[TCLDataset]] = ( + lambda x: list(x) + ) result = manage_advanced_indexing( exp_idx, self._get_experience_and_load_if_needed, diff --git a/avalanche/benchmarks/scenarios/deprecated/new_classes/nc_scenario.py b/avalanche/benchmarks/scenarios/deprecated/new_classes/nc_scenario.py index fcbe8610b..a5509b18d 100644 --- a/avalanche/benchmarks/scenarios/deprecated/new_classes/nc_scenario.py +++ b/avalanche/benchmarks/scenarios/deprecated/new_classes/nc_scenario.py @@ -327,9 +327,9 @@ class "34" will be mapped to "1", class "11" to "2" and so on. # used, the user may have defined an amount of classes less than # the overall amount of classes in the dataset. if class_id in self.classes_order_original_ids: - self.class_mapping[ - class_id - ] = self.classes_order_original_ids.index(class_id) + self.class_mapping[class_id] = ( + self.classes_order_original_ids.index(class_id) + ) elif self.class_ids_from_zero_in_each_exp: # Method 2: remap class IDs so that they appear in range [0, N] in # each experience diff --git a/avalanche/benchmarks/scenarios/detection_scenario.py b/avalanche/benchmarks/scenarios/detection_scenario.py index 8b16be027..111c5ac11 100644 --- a/avalanche/benchmarks/scenarios/detection_scenario.py +++ b/avalanche/benchmarks/scenarios/detection_scenario.py @@ -254,12 +254,10 @@ def __len__(self): return len(self._benchmark.streams[self._stream]) @overload - def __getitem__(self, exp_id: int) -> Optional[Set[int]]: - ... + def __getitem__(self, exp_id: int) -> Optional[Set[int]]: ... @overload - def __getitem__(self, exp_id: slice) -> Tuple[Optional[Set[int]], ...]: - ... + def __getitem__(self, exp_id: slice) -> Tuple[Optional[Set[int]], ...]: ... def __getitem__(self, exp_id: Union[int, slice]) -> LazyClassesInExpsRet: indexing_collate = _LazyClassesInDetectionExps._slice_collate diff --git a/avalanche/benchmarks/scenarios/generic_scenario.py b/avalanche/benchmarks/scenarios/generic_scenario.py index d7b5ba09c..34da0d249 100644 --- a/avalanche/benchmarks/scenarios/generic_scenario.py +++ b/avalanche/benchmarks/scenarios/generic_scenario.py @@ -427,12 +427,10 @@ def __iter__(self) -> Iterator[TCLExperience]: yield exp @overload - def __getitem__(self, item: int) -> TCLExperience: - ... + def __getitem__(self, item: int) -> TCLExperience: ... @overload - def __getitem__(self: TSequenceCLStream, item: slice) -> TSequenceCLStream: - ... + def __getitem__(self: TSequenceCLStream, item: slice) -> TSequenceCLStream: ... @final def __getitem__( diff --git a/avalanche/benchmarks/utils/classification_dataset.py b/avalanche/benchmarks/utils/classification_dataset.py index acb4f880b..ad6db47f6 100644 --- a/avalanche/benchmarks/utils/classification_dataset.py +++ b/avalanche/benchmarks/utils/classification_dataset.py @@ -175,8 +175,7 @@ def _make_taskaware_classification_dataset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareSupervisedClassificationDataset: - ... +) -> TaskAwareSupervisedClassificationDataset: ... @overload @@ -190,8 +189,7 @@ def _make_taskaware_classification_dataset( task_labels: Union[int, Sequence[int]], targets: Sequence[TTargetType], collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareSupervisedClassificationDataset: - ... +) -> TaskAwareSupervisedClassificationDataset: ... @overload @@ -205,8 +203,7 @@ def _make_taskaware_classification_dataset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareClassificationDataset: - ... +) -> TaskAwareClassificationDataset: ... def _make_taskaware_classification_dataset( @@ -386,8 +383,7 @@ def _taskaware_classification_subset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareSupervisedClassificationDataset: - ... +) -> TaskAwareSupervisedClassificationDataset: ... @overload @@ -403,8 +399,7 @@ def _taskaware_classification_subset( task_labels: Union[int, Sequence[int]], targets: Sequence[TTargetType], collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareSupervisedClassificationDataset: - ... +) -> TaskAwareSupervisedClassificationDataset: ... @overload @@ -420,8 +415,7 @@ def _taskaware_classification_subset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareClassificationDataset: - ... +) -> TaskAwareClassificationDataset: ... def _taskaware_classification_subset( @@ -619,8 +613,7 @@ def _make_taskaware_tensor_classification_dataset( task_labels: Union[int, Sequence[int]], targets: Union[Sequence[TTargetType], int], collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareSupervisedClassificationDataset: - ... +) -> TaskAwareSupervisedClassificationDataset: ... @overload @@ -633,8 +626,9 @@ def _make_taskaware_tensor_classification_dataset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Union[Sequence[TTargetType], int]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> Union[TaskAwareClassificationDataset, TaskAwareSupervisedClassificationDataset]: - ... +) -> Union[ + TaskAwareClassificationDataset, TaskAwareSupervisedClassificationDataset +]: ... def _make_taskaware_tensor_classification_dataset( @@ -759,8 +753,7 @@ def _concat_taskaware_classification_datasets( Union[Sequence[TTargetType], Sequence[Sequence[TTargetType]]] ] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareSupervisedClassificationDataset: - ... +) -> TaskAwareSupervisedClassificationDataset: ... @overload @@ -774,8 +767,7 @@ def _concat_taskaware_classification_datasets( task_labels: Union[int, Sequence[int], Sequence[Sequence[int]]], targets: Union[Sequence[TTargetType], Sequence[Sequence[TTargetType]]], collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareSupervisedClassificationDataset: - ... +) -> TaskAwareSupervisedClassificationDataset: ... @overload @@ -791,8 +783,7 @@ def _concat_taskaware_classification_datasets( Union[Sequence[TTargetType], Sequence[Sequence[TTargetType]]] ] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> TaskAwareClassificationDataset: - ... +) -> TaskAwareClassificationDataset: ... def _concat_taskaware_classification_datasets( diff --git a/avalanche/benchmarks/utils/data.py b/avalanche/benchmarks/utils/data.py index 76908345e..9985e19fa 100644 --- a/avalanche/benchmarks/utils/data.py +++ b/avalanche/benchmarks/utils/data.py @@ -344,12 +344,10 @@ def __eq__(self, other: object): ) @overload - def __getitem__(self, exp_id: int) -> T_co: - ... + def __getitem__(self, exp_id: int) -> T_co: ... @overload - def __getitem__(self: TAvalancheDataset, exp_id: slice) -> TAvalancheDataset: - ... + def __getitem__(self: TAvalancheDataset, exp_id: slice) -> TAvalancheDataset: ... def __getitem__( self: TAvalancheDataset, idx: Union[int, slice] diff --git a/avalanche/benchmarks/utils/data_attribute.py b/avalanche/benchmarks/utils/data_attribute.py index 3cb88e58c..d3ee4b0d9 100644 --- a/avalanche/benchmarks/utils/data_attribute.py +++ b/avalanche/benchmarks/utils/data_attribute.py @@ -62,12 +62,10 @@ def __iter__(self): yield self[i] @overload - def __getitem__(self, item: int) -> T_co: - ... + def __getitem__(self, item: int) -> T_co: ... @overload - def __getitem__(self, item: slice) -> Sequence[T_co]: - ... + def __getitem__(self, item: slice) -> Sequence[T_co]: ... def __getitem__(self, item: Union[int, slice]) -> Union[T_co, Sequence[T_co]]: return self.data[item] diff --git a/avalanche/benchmarks/utils/dataset_definitions.py b/avalanche/benchmarks/utils/dataset_definitions.py index ca8de5b54..de9fa05da 100644 --- a/avalanche/benchmarks/utils/dataset_definitions.py +++ b/avalanche/benchmarks/utils/dataset_definitions.py @@ -38,11 +38,9 @@ class IDataset(Protocol[T_co]): Note: no __add__ method is defined. """ - def __getitem__(self, index: int) -> T_co: - ... + def __getitem__(self, index: int) -> T_co: ... - def __len__(self) -> int: - ... + def __len__(self) -> int: ... class IDatasetWithTargets(IDataset[T_co], Protocol[T_co, TTargetType_co]): diff --git a/avalanche/benchmarks/utils/dataset_utils.py b/avalanche/benchmarks/utils/dataset_utils.py index 717d67e75..867ba9cc6 100644 --- a/avalanche/benchmarks/utils/dataset_utils.py +++ b/avalanche/benchmarks/utils/dataset_utils.py @@ -64,12 +64,10 @@ def __iter__(self) -> Iterator[TData]: yield el @overload - def __getitem__(self, item: int) -> TData: - ... + def __getitem__(self, item: int) -> TData: ... @overload - def __getitem__(self: TSliceSequence, item: slice) -> TSliceSequence: - ... + def __getitem__(self: TSliceSequence, item: slice) -> TSliceSequence: ... @final def __getitem__( diff --git a/avalanche/benchmarks/utils/detection_dataset.py b/avalanche/benchmarks/utils/detection_dataset.py index 7f3b3b632..6c5efb43f 100644 --- a/avalanche/benchmarks/utils/detection_dataset.py +++ b/avalanche/benchmarks/utils/detection_dataset.py @@ -151,8 +151,7 @@ def make_detection_dataset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> SupervisedDetectionDataset: - ... +) -> SupervisedDetectionDataset: ... @overload @@ -166,8 +165,7 @@ def make_detection_dataset( task_labels: Union[int, Sequence[int]], targets: Sequence[TTargetType], collate_fn: Optional[Callable[[List], Any]] = None -) -> SupervisedDetectionDataset: - ... +) -> SupervisedDetectionDataset: ... @overload @@ -181,8 +179,7 @@ def make_detection_dataset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> DetectionDataset: - ... +) -> DetectionDataset: ... def make_detection_dataset( @@ -373,8 +370,7 @@ def detection_subset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> SupervisedDetectionDataset: - ... +) -> SupervisedDetectionDataset: ... @overload @@ -390,8 +386,7 @@ def detection_subset( task_labels: Union[int, Sequence[int]], targets: Sequence[TTargetType], collate_fn: Optional[Callable[[List], Any]] = None -) -> SupervisedDetectionDataset: - ... +) -> SupervisedDetectionDataset: ... @overload @@ -407,8 +402,7 @@ def detection_subset( task_labels: Optional[Union[int, Sequence[int]]] = None, targets: Optional[Sequence[TTargetType]] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> DetectionDataset: - ... +) -> DetectionDataset: ... def detection_subset( @@ -595,8 +589,7 @@ def concat_detection_datasets( Union[Sequence[TTargetType], Sequence[Sequence[TTargetType]]] ] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> SupervisedDetectionDataset: - ... +) -> SupervisedDetectionDataset: ... @overload @@ -610,8 +603,7 @@ def concat_detection_datasets( task_labels: Union[int, Sequence[int], Sequence[Sequence[int]]], targets: Union[Sequence[TTargetType], Sequence[Sequence[TTargetType]]], collate_fn: Optional[Callable[[List], Any]] = None -) -> SupervisedDetectionDataset: - ... +) -> SupervisedDetectionDataset: ... @overload @@ -627,8 +619,7 @@ def concat_detection_datasets( Union[Sequence[TTargetType], Sequence[Sequence[TTargetType]]] ] = None, collate_fn: Optional[Callable[[List], Any]] = None -) -> DetectionDataset: - ... +) -> DetectionDataset: ... def concat_detection_datasets( diff --git a/avalanche/benchmarks/utils/flat_data.py b/avalanche/benchmarks/utils/flat_data.py index d9fd73ee0..02d3681cd 100644 --- a/avalanche/benchmarks/utils/flat_data.py +++ b/avalanche/benchmarks/utils/flat_data.py @@ -66,9 +66,9 @@ def __init__( else: new_lists.append(ll) - self._lists: Optional[ - List[Sequence[int]] - ] = new_lists # freed after eagerification + self._lists: Optional[List[Sequence[int]]] = ( + new_lists # freed after eagerification + ) self._offset: int = int(offset) self._eager_list: Optional[np.ndarray] = None """This is the list where we save indices @@ -408,12 +408,10 @@ def _get_idx(self, idx) -> Tuple[int, int]: return dataset_idx, int(idx) @overload - def __getitem__(self, item: int) -> T_co: - ... + def __getitem__(self, item: int) -> T_co: ... @overload - def __getitem__(self: TFlatData, item: slice) -> TFlatData: - ... + def __getitem__(self: TFlatData, item: slice) -> TFlatData: ... def __getitem__(self: TFlatData, item: Union[int, slice]) -> Union[T_co, TFlatData]: if isinstance(item, (int, np.integer)): @@ -470,12 +468,10 @@ def __len__(self): return self._size @overload - def __getitem__(self, index: int) -> DataT: - ... + def __getitem__(self, index: int) -> DataT: ... @overload - def __getitem__(self, index: slice) -> "ConstantSequence[DataT]": - ... + def __getitem__(self, index: slice) -> "ConstantSequence[DataT]": ... def __getitem__( self, index: Union[int, slice] diff --git a/avalanche/evaluation/metric_definitions.py b/avalanche/evaluation/metric_definitions.py index 1934d5392..c91a7122c 100644 --- a/avalanche/evaluation/metric_definitions.py +++ b/avalanche/evaluation/metric_definitions.py @@ -215,8 +215,7 @@ def __init__( ] = "experience", emit_at: Literal["iteration", "epoch", "experience", "stream"] = "experience", mode: Literal["train"] = "train", - ): - ... + ): ... @overload def __init__( @@ -225,8 +224,7 @@ def __init__( reset_at: Literal["iteration", "experience", "stream", "never"] = "experience", emit_at: Literal["iteration", "experience", "stream"] = "experience", mode: Literal["eval"] = "eval", - ): - ... + ): ... def __init__( self, metric: TMetric, reset_at="experience", emit_at="experience", mode="eval" diff --git a/avalanche/evaluation/metrics/forgetting_bwt.py b/avalanche/evaluation/metrics/forgetting_bwt.py index 9e5f79094..29d9ffd28 100644 --- a/avalanche/evaluation/metrics/forgetting_bwt.py +++ b/avalanche/evaluation/metrics/forgetting_bwt.py @@ -526,18 +526,15 @@ def forgetting_metrics(*, experience=False, stream=False) -> List[PluginMetric]: @overload -def forgetting_to_bwt(f: float) -> float: - ... +def forgetting_to_bwt(f: float) -> float: ... @overload -def forgetting_to_bwt(f: Dict[int, float]) -> Dict[int, float]: - ... +def forgetting_to_bwt(f: Dict[int, float]) -> Dict[int, float]: ... @overload -def forgetting_to_bwt(f: None) -> None: - ... +def forgetting_to_bwt(f: None) -> None: ... def forgetting_to_bwt(f: Optional[Union[float, Dict[int, float]]]): diff --git a/avalanche/evaluation/metrics/labels_repartition.py b/avalanche/evaluation/metrics/labels_repartition.py index 71d6fb0e9..7a5304696 100644 --- a/avalanche/evaluation/metrics/labels_repartition.py +++ b/avalanche/evaluation/metrics/labels_repartition.py @@ -85,8 +85,7 @@ def __init__( ] = default_history_repartition_image_creator, mode: Literal["train"] = "train", emit_reset_at: Literal["stream", "experience", "epoch"] = "epoch", - ): - ... + ): ... @overload def __init__( @@ -97,8 +96,7 @@ def __init__( ] = default_history_repartition_image_creator, mode: Literal["eval"] = "eval", emit_reset_at: Literal["stream", "experience"], - ): - ... + ): ... def __init__( self, diff --git a/avalanche/training/plugins/bic.py b/avalanche/training/plugins/bic.py index 61535fd37..caef605b9 100644 --- a/avalanche/training/plugins/bic.py +++ b/avalanche/training/plugins/bic.py @@ -454,9 +454,9 @@ def _classes_groups(self, strategy: SupervisedTemplate): # - "current" classes: seen in current_experience # "initial" classes - initial_classes: Set[ - int - ] = set() # pre_initial_cl in the original implementation + initial_classes: Set[int] = ( + set() + ) # pre_initial_cl in the original implementation previous_classes: Set[int] = set() # pre_new_cl in the original implementation current_classes: Set[int] = set() # new_cl in the original implementation # Note: pre_initial_cl + pre_new_cl is "initial_cl" in the original implementation diff --git a/avalanche/training/supervised/expert_gate.py b/avalanche/training/supervised/expert_gate.py index a76637514..abb249216 100644 --- a/avalanche/training/supervised/expert_gate.py +++ b/avalanche/training/supervised/expert_gate.py @@ -239,9 +239,9 @@ def _select_expert(self, strategy: "SupervisedTemplate", task_label): # Iterate through all autoencoders to get error values for autoencoder_id in strategy.model.autoencoder_dict: - error_dict[ - str(autoencoder_id) - ] = self._get_average_reconstruction_error(strategy, autoencoder_id) + error_dict[str(autoencoder_id)] = ( + self._get_average_reconstruction_error(strategy, autoencoder_id) + ) # Send error dictionary to get most relevant autoencoder relatedness_dict = self._task_relatedness(strategy, error_dict, task_label) diff --git a/avalanche/training/supervised/joint_training.py b/avalanche/training/supervised/joint_training.py index 4e145532e..08294cfc0 100644 --- a/avalanche/training/supervised/joint_training.py +++ b/avalanche/training/supervised/joint_training.py @@ -139,9 +139,9 @@ def train( ) # Normalize training and eval data. - experiences_list: Iterable[ - TDatasetExperience - ] = _experiences_parameter_as_iterable(experiences) + experiences_list: Iterable[TDatasetExperience] = ( + _experiences_parameter_as_iterable(experiences) + ) if eval_streams is None: eval_streams = [experiences_list] diff --git a/avalanche/training/templates/base.py b/avalanche/training/templates/base.py index 8dd5c8c03..2ec228291 100644 --- a/avalanche/training/templates/base.py +++ b/avalanche/training/templates/base.py @@ -147,9 +147,9 @@ def train( self.model.to(self.device) # Normalize training and eval data. - experiences_list: Iterable[ - TExperienceType - ] = _experiences_parameter_as_iterable(experiences) + experiences_list: Iterable[TExperienceType] = ( + _experiences_parameter_as_iterable(experiences) + ) if eval_streams is None: eval_streams = [experiences_list] @@ -201,9 +201,9 @@ def eval( self.is_training = False self.model.eval() - experiences_list: Iterable[ - TExperienceType - ] = _experiences_parameter_as_iterable(experiences) + experiences_list: Iterable[TExperienceType] = ( + _experiences_parameter_as_iterable(experiences) + ) self.current_eval_stream = experiences_list self._before_eval(**kwargs) diff --git a/avalanche/training/templates/strategy_mixin_protocol.py b/avalanche/training/templates/strategy_mixin_protocol.py index 847d6ef78..4596c9d39 100644 --- a/avalanche/training/templates/strategy_mixin_protocol.py +++ b/avalanche/training/templates/strategy_mixin_protocol.py @@ -58,53 +58,37 @@ class SGDStrategyProtocol( _criterion: CriterionType - def forward(self) -> TMBOutput: - ... + def forward(self) -> TMBOutput: ... - def criterion(self) -> Tensor: - ... + def criterion(self) -> Tensor: ... - def backward(self) -> None: - ... + def backward(self) -> None: ... - def _make_empty_loss(self) -> Tensor: - ... + def _make_empty_loss(self) -> Tensor: ... - def make_optimizer(self, **kwargs): - ... + def make_optimizer(self, **kwargs): ... - def optimizer_step(self) -> None: - ... + def optimizer_step(self) -> None: ... - def model_adaptation(self, model: Optional[Module] = None) -> Module: - ... + def model_adaptation(self, model: Optional[Module] = None) -> Module: ... - def _unpack_minibatch(self): - ... + def _unpack_minibatch(self): ... - def _before_training_iteration(self, **kwargs): - ... + def _before_training_iteration(self, **kwargs): ... - def _before_forward(self, **kwargs): - ... + def _before_forward(self, **kwargs): ... - def _after_forward(self, **kwargs): - ... + def _after_forward(self, **kwargs): ... - def _before_backward(self, **kwargs): - ... + def _before_backward(self, **kwargs): ... - def _after_backward(self, **kwargs): - ... + def _after_backward(self, **kwargs): ... - def _before_update(self, **kwargs): - ... + def _before_update(self, **kwargs): ... - def _after_update(self, **kwargs): - ... + def _after_update(self, **kwargs): ... - def _after_training_iteration(self, **kwargs): - ... + def _after_training_iteration(self, **kwargs): ... class SupervisedStrategyProtocol( @@ -122,23 +106,17 @@ class MetaLearningStrategyProtocol( SGDStrategyProtocol[TSGDExperienceType, TMBInput, TMBOutput], Protocol[TSGDExperienceType, TMBInput, TMBOutput], ): - def _before_inner_updates(self, **kwargs): - ... + def _before_inner_updates(self, **kwargs): ... - def _inner_updates(self, **kwargs): - ... + def _inner_updates(self, **kwargs): ... - def _after_inner_updates(self, **kwargs): - ... + def _after_inner_updates(self, **kwargs): ... - def _before_outer_update(self, **kwargs): - ... + def _before_outer_update(self, **kwargs): ... - def _outer_update(self, **kwargs): - ... + def _outer_update(self, **kwargs): ... - def _after_outer_update(self, **kwargs): - ... + def _after_outer_update(self, **kwargs): ... __all__ = [ diff --git a/examples/detection.py b/examples/detection.py index 3e7f7d94e..ec2e92119 100644 --- a/examples/detection.py +++ b/examples/detection.py @@ -160,15 +160,11 @@ def obtain_base_model(segmentation: bool): pretrain_argument["pretrained"] = True else: if segmentation: - pretrain_argument[ - "weights" - ] = ( + pretrain_argument["weights"] = ( torchvision.models.detection.mask_rcnn.MaskRCNN_ResNet50_FPN_Weights.DEFAULT ) else: - pretrain_argument[ - "weights" - ] = ( + pretrain_argument["weights"] = ( torchvision.models.detection.faster_rcnn.FasterRCNN_ResNet50_FPN_Weights.DEFAULT ) diff --git a/examples/detection_lvis.py b/examples/detection_lvis.py index ed86e5b2c..330b7a9ec 100644 --- a/examples/detection_lvis.py +++ b/examples/detection_lvis.py @@ -142,15 +142,11 @@ def obtain_base_model(segmentation: bool): pretrain_argument["pretrained"] = True else: if segmentation: - pretrain_argument[ - "weights" - ] = ( + pretrain_argument["weights"] = ( torchvision.models.detection.mask_rcnn.MaskRCNN_ResNet50_FPN_Weights.DEFAULT ) else: - pretrain_argument[ - "weights" - ] = ( + pretrain_argument["weights"] = ( torchvision.models.detection.faster_rcnn.FasterRCNN_ResNet50_FPN_Weights.DEFAULT )