Skip to content

Commit

Permalink
syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonioCarta committed Oct 11, 2023
1 parent e3356a0 commit 9e882e2
Show file tree
Hide file tree
Showing 37 changed files with 398 additions and 213 deletions.
4 changes: 3 additions & 1 deletion avalanche/benchmarks/classic/classic_benchmarks_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
DatasetStream,
)

from avalanche.benchmarks.utils.classification_dataset import TaskAwareClassificationDataset
from avalanche.benchmarks.utils.classification_dataset import (
TaskAwareClassificationDataset,
)


def check_vision_benchmark(
Expand Down
10 changes: 6 additions & 4 deletions avalanche/benchmarks/classic/ctrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ def CTrL(
paths_dataset: PathsDataset[Image, int] = PathsDataset(
common_root, exp_paths_list
)
dataset: TaskAwareSupervisedClassificationDataset = _make_taskaware_classification_dataset(
paths_dataset,
task_labels=task_labels,
transform=transforms.Compose([transforms.ToTensor(), trans]),
dataset: TaskAwareSupervisedClassificationDataset = (
_make_taskaware_classification_dataset(
paths_dataset,
task_labels=task_labels,
transform=transforms.Compose([transforms.ToTensor(), trans]),
)
)
else:
dataset = _make_taskaware_tensor_classification_dataset(
Expand Down
8 changes: 6 additions & 2 deletions avalanche/benchmarks/classic/endless_cl_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,14 @@ def EndlessCLSim(
eval_data.transform = eval_transform

train_datasets.append(
_make_taskaware_classification_dataset(dataset=train_data, task_labels=task_order[i])
_make_taskaware_classification_dataset(
dataset=train_data, task_labels=task_order[i]
)
)
eval_datasets.append(
_make_taskaware_classification_dataset(dataset=eval_data, task_labels=task_order[i])
_make_taskaware_classification_dataset(
dataset=eval_data, task_labels=task_order[i]
)
)

scenario_obj = dataset_benchmark(train_datasets, eval_datasets)
Expand Down
41 changes: 24 additions & 17 deletions avalanche/benchmarks/scenarios/dataset_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
Union,
Tuple,
Optional,
Iterable, Dict,
Iterable,
Dict,
)

from .generic_scenario import EagerCLStream, CLScenario, CLExperience, make_stream
Expand All @@ -35,9 +36,7 @@
TCLDataset = TypeVar("TCLDataset", bound="AvalancheDataset")


def benchmark_from_datasets(
**dataset_streams: Sequence[TCLDataset]
) -> CLScenario:
def benchmark_from_datasets(**dataset_streams: Sequence[TCLDataset]) -> CLScenario:
"""Creates a benchmark given a list of datasets for each stream.
Each dataset will be considered as a separate experience.
Expand All @@ -59,7 +58,10 @@ def benchmark_from_datasets(
for dd in data_s:
if not isinstance(dd, AvalancheDataset):
raise ValueError("datasets must be AvalancheDatasets")
des = [DatasetExperience(dataset=dd, current_experience=eid) for eid, dd in enumerate(data_s)]
des = [
DatasetExperience(dataset=dd, current_experience=eid)
for eid, dd in enumerate(data_s)
]
s = EagerCLStream(stream_name, des)
exps_streams.append(s)
return CLScenario(exps_streams)
Expand All @@ -69,10 +71,7 @@ class DatasetExperience(CLExperience, Generic[TCLDataset]):
"""An Experience that provides a dataset."""

def __init__(
self,
*,
dataset: TCLDataset,
current_experience: Optional[int] = None
self, *, dataset: TCLDataset, current_experience: Optional[int] = None
):
super().__init__(current_experience=current_experience, origin_stream=None)
self._dataset: AvalancheDataset = dataset
Expand All @@ -84,7 +83,9 @@ def dataset(self) -> AvalancheDataset:
return data


def _split_dataset_by_attribute(data: AvalancheDataset, attr_name: str) -> Dict[int, AvalancheDataset]:
def _split_dataset_by_attribute(
data: AvalancheDataset, attr_name: str
) -> Dict[int, AvalancheDataset]:
"""Helper to split a dataset by attribute.
:param data: an Avalanche dataset.
Expand All @@ -101,8 +102,8 @@ def _split_dataset_by_attribute(data: AvalancheDataset, attr_name: str) -> Dict[
def split_validation_random(
validation_size: Union[int, float],
shuffle: bool,
seed: int=None,
dataset: AvalancheDataset=None,
seed: int = None,
dataset: AvalancheDataset = None,
) -> Tuple[AvalancheDataset, AvalancheDataset]:
"""Splits an `AvalancheDataset` in two splits.
Expand Down Expand Up @@ -170,7 +171,9 @@ def split_validation_random(
def split_validation_class_balanced(
validation_size: Union[int, float],
dataset: TaskAwareSupervisedClassificationDataset,
) -> Tuple[TaskAwareSupervisedClassificationDataset, TaskAwareSupervisedClassificationDataset]:
) -> Tuple[
TaskAwareSupervisedClassificationDataset, TaskAwareSupervisedClassificationDataset
]:
"""Class-balanced dataset split.
This splitting strategy splits `dataset` into train and validation data of
Expand Down Expand Up @@ -223,7 +226,8 @@ def split_validation_class_balanced(


class LazyTrainValSplitter:
def __init__(self,
def __init__(
self,
split_strategy: Callable[
[AvalancheDataset],
Tuple[AvalancheDataset, AvalancheDataset],
Expand All @@ -241,7 +245,7 @@ def __init__(self,
"""
self.split_strategy = split_strategy
self.experiences = experiences

def __iter__(self):
for new_experience in self.experiences:
yield self.split_strategy(new_experience.dataset)
Expand Down Expand Up @@ -300,6 +304,7 @@ def benchmark_with_validation_stream(
if split_strategy is None:
if seed is None:
seed = random.randint(0, 1000000)

# functools.partial is a more compact option
# However, MyPy does not understand what a partial is -_-
def random_validation_split_strategy_wrapper(data):
Expand Down Expand Up @@ -328,13 +333,15 @@ def random_validation_split_strategy_wrapper(data):
other_streams = benchmark.streams

del other_streams["train"]
return CLScenario(streams=[train_stream, valid_stream] + list(other_streams.values()))
return CLScenario(
streams=[train_stream, valid_stream] + list(other_streams.values())
)


__all__ = [
"_split_dataset_by_attribute",
"benchmark_from_datasets",
"DatasetExperience",
"split_validation_random",
"benchmark_with_validation_stream"
"benchmark_with_validation_stream",
]
4 changes: 3 additions & 1 deletion avalanche/benchmarks/scenarios/deprecated/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from .. import CLExperience, CLStream, CLScenario

TCLDataset = TypeVar("TCLDataset", bound="AvalancheDataset")
TDatasetExperience = TypeVar("TDatasetExperience", bound="DatasetExperience") # Implementation, defined here
TDatasetExperience = TypeVar(
"TDatasetExperience", bound="DatasetExperience"
) # Implementation, defined here


class DatasetExperience(CLExperience, Generic[TCLDataset], ABC):
Expand Down
25 changes: 17 additions & 8 deletions avalanche/benchmarks/scenarios/deprecated/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
LazyDatasetSequence,
)
from avalanche.benchmarks.scenarios.deprecated.new_classes.nc_scenario import NCScenario
from avalanche.benchmarks.scenarios.deprecated.new_instances.ni_scenario import NIScenario
from avalanche.benchmarks.scenarios.deprecated.new_instances.ni_scenario import (
NIScenario,
)
from avalanche.benchmarks.scenarios.online import FixedSizeExperienceSplitter
from avalanche.benchmarks.utils.classification_dataset import (
SupportedDataset,
Expand Down Expand Up @@ -211,7 +213,9 @@ class "34" will be mapped to "1", class "11" to "2" and so on.
train_dataset_sup = list(
map(_as_taskaware_supervised_classification_dataset, train_dataset)
)
test_dataset_sup = list(map(_as_taskaware_supervised_classification_dataset, test_dataset))
test_dataset_sup = list(
map(_as_taskaware_supervised_classification_dataset, test_dataset)
)

(
seq_train_dataset,
Expand All @@ -238,7 +242,9 @@ class "34" will be mapped to "1", class "11" to "2" and so on.
# Overrides n_experiences (and per_experience_classes, already done)
n_experiences = len(train_dataset)
else:
seq_train_dataset = _as_taskaware_supervised_classification_dataset(train_dataset)
seq_train_dataset = _as_taskaware_supervised_classification_dataset(
train_dataset
)
seq_test_dataset = _as_taskaware_supervised_classification_dataset(test_dataset)

transform_groups = dict(train=(train_transform, None), eval=(eval_transform, None))
Expand Down Expand Up @@ -366,7 +372,9 @@ def ni_benchmark(
train_dataset_sup = list(
map(_as_taskaware_supervised_classification_dataset, train_dataset)
)
test_dataset_sup = list(map(_as_taskaware_supervised_classification_dataset, test_dataset))
test_dataset_sup = list(
map(_as_taskaware_supervised_classification_dataset, test_dataset)
)

(
seq_train_dataset,
Expand All @@ -376,7 +384,9 @@ def ni_benchmark(
train_dataset_sup, test_dataset_sup
)
else:
seq_train_dataset = _as_taskaware_supervised_classification_dataset(train_dataset)
seq_train_dataset = _as_taskaware_supervised_classification_dataset(
train_dataset
)
seq_test_dataset = _as_taskaware_supervised_classification_dataset(test_dataset)

transform_groups = dict(train=(train_transform, None), eval=(eval_transform, None))
Expand Down Expand Up @@ -588,11 +598,10 @@ def data_incremental_benchmark(
# However, MyPy does not understand what a partial is -_-
def split_strategy_wrapper(exp):
ss = FixedSizeExperienceSplitter(
experience=exp,
experience_size=experience_size,
shuffle=shuffle
experience=exp, experience_size=experience_size, shuffle=shuffle
)
return [e.dataset for e in ss]

split_strategy = split_strategy_wrapper
else:
split_strategy = custom_split_strategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
from avalanche.benchmarks.utils.classification_dataset import (
TaskAwareClassificationDataset,
)
from avalanche.benchmarks.scenarios.deprecated.classification_scenario import GenericCLScenario
from avalanche.benchmarks.scenarios.deprecated.classification_scenario import (
GenericCLScenario,
)


def create_multi_dataset_generic_benchmark(
Expand Down Expand Up @@ -141,7 +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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
ClassificationStream,
ClassificationExperience,
)
from avalanche.benchmarks.utils.classification_dataset import _taskaware_classification_subset
from avalanche.benchmarks.utils.classification_dataset import (
_taskaware_classification_subset,
)
from avalanche.benchmarks.utils.classification_dataset import (
TaskAwareClassificationDataset,
TaskAwareSupervisedClassificationDataset,
Expand All @@ -28,7 +30,9 @@


class NCScenario(
ClassificationScenario["NCStream", "NCExperience", TaskAwareSupervisedClassificationDataset]
ClassificationScenario[
"NCStream", "NCExperience", TaskAwareSupervisedClassificationDataset
]
):

"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@


class NIScenario(
ClassificationScenario["NIStream", "NIExperience", TaskAwareSupervisedClassificationDataset]
ClassificationScenario[
"NIStream", "NIExperience", TaskAwareSupervisedClassificationDataset
]
):
"""
This class defines a "New Instance" scenario.
Expand Down Expand Up @@ -168,7 +170,9 @@ def __init__(
included_patterns: List[int] = list()
for exp_def in lst_fixed_exp_assignment:
included_patterns.extend(exp_def)
subset = _taskaware_classification_subset(train_dataset, indices=included_patterns)
subset = _taskaware_classification_subset(
train_dataset, indices=included_patterns
)
unique_targets, unique_count = torch.unique(
torch.as_tensor(subset.targets), return_counts=True
)
Expand Down
4 changes: 2 additions & 2 deletions avalanche/benchmarks/scenarios/generic_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ def make_stream(name: str, exps: Iterable[CLExperience]) -> CLStream:
s_wrapped: CLStream
if isinstance(exps, List): # Maintain indexing/slicing functionalities
return EagerCLStream(name=name, exps=exps)
elif hasattr(exps, '__len__'): # Sized stream
elif hasattr(exps, "__len__"): # Sized stream
return SizedCLStream(name=name, exps_iter=exps)
else: # Plain iterator
return CLStream(name=name, exps_iter=exps)
Expand All @@ -635,5 +635,5 @@ def make_stream(name: str, exps: Iterable[CLExperience]) -> CLStream:
"SequenceCLStream",
"EagerCLStream",
"CLScenario",
"make_stream"
"make_stream",
]
21 changes: 13 additions & 8 deletions avalanche/benchmarks/scenarios/online.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
List,
Optional,
TypeVar,
Union, Protocol,
Union,
Protocol,
)
from typing_extensions import Literal
import warnings
Expand Down Expand Up @@ -110,7 +111,7 @@ def __init__(
:type sub_stream_length: int, optional
:param access_task_boundaries: Whether to access task boundaries.
:type access_task_boundaries: bool, optional
"""
"""
super().__init__(dataset=dataset)
self.access_task_boundaries = access_task_boundaries
self.origin_experience: DatasetExperience = origin_experience
Expand Down Expand Up @@ -146,7 +147,7 @@ def __init__(
Experience decorators (e.g. class attributes) will be stripped from the experience.
You will need to re-apply them to the resulting experiences if you need them.
:param experience: The experience to split.
:param experience_size: The experience size (number of instances).
:param shuffle: If True, instances will be shuffled before splitting.
Expand All @@ -163,7 +164,7 @@ def __init__(

# we need to fix the seed because repeated calls to the generator
# must return the same order every time.
self.seed = random.randint(0, 2 ** 32 - 1)
self.seed = random.randint(0, 2**32 - 1)

def __iter__(self):
exp_dataset = self.experience.dataset
Expand Down Expand Up @@ -272,7 +273,9 @@ def split_online_stream(
# functools.partial is a more compact option
# However, MyPy does not understand what a partial is -_-
def default_online_split_wrapper(e, e_sz):
return _default_online_split(shuffle, drop_last, access_task_boundaries, e, e_sz)
return _default_online_split(
shuffle, drop_last, access_task_boundaries, e, e_sz
)

split_strategy = default_online_split_wrapper
else:
Expand Down Expand Up @@ -342,9 +345,11 @@ def __init__(
:param shuffle: If True, experiences will be split by first shuffling
instances in each experience. Defaults to True.
"""
warnings.warn("Deprecated. Use `split_online_stream` or similar methods to split"
"single streams or experiences instead")

warnings.warn(
"Deprecated. Use `split_online_stream` or similar methods to split"
"single streams or experiences instead"
)

if stream_split_strategy != "fixed_size_split":
raise ValueError("Unknown experience split strategy")

Expand Down
Loading

0 comments on commit 9e882e2

Please sign in to comment.