• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ContinualAI / avalanche / 5268393053

pending completion
5268393053

Pull #1397

github

web-flow
Merge 60d244754 into e91562200
Pull Request #1397: Specialize benchmark creation helpers

417 of 538 new or added lines in 30 files covered. (77.51%)

43 existing lines in 5 files now uncovered.

16586 of 22630 relevant lines covered (73.29%)

2.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

28.57
/avalanche/benchmarks/classic/endless_cl_sim.py
1
################################################################################
2
# Copyright (c) 2021 ContinualAI.                                              #
3
# Copyrights licensed under the MIT License.                                   #
4
# See the accompanying LICENSE file for terms.                                 #
5
#                                                                              #
6
# Date: 28-06-2021                                                             #
7
# Author: Timm Hess                                                            #
8
# E-mail: hess@ccc.cs.uni-frankfurt.de                                         #
9
# Website: www.continualai.org                                                 #
10
################################################################################
11

12
"""
4✔
13
This module contains the high-level EndlessCLSim scenario 
14
generator. It returns an iterable scenario object 
15
:class:`ClassificationScenario` given a number of configuration parameters.
16
"""
17

18
from avalanche.benchmarks.generators.benchmark_generators import (
4✔
19
    dataset_classification_benchmark,
20
)
21
from avalanche.benchmarks.scenarios.classification_scenario import (
4✔
22
    CommonClassificationScenarioType,
23
)
24
from avalanche.benchmarks.utils.classification_dataset import (
4✔
25
    make_classification_dataset,
26
)
27
from avalanche.benchmarks.datasets.endless_cl_sim.endless_cl_sim import (
4✔
28
    EndlessCLSimDataset,
29
)
30
from pathlib import Path
4✔
31
from typing import List, Union, Optional, Any
4✔
32

33
from torchvision.transforms import ToTensor
4✔
34
from torchvision.transforms.transforms import Compose
4✔
35

36
from avalanche.benchmarks.datasets import default_dataset_location
4✔
37
from avalanche.benchmarks.utils import make_classification_dataset
4✔
38

39
_default_transform = Compose([ToTensor()])
4✔
40

41
_scenario_names = ["Classes", "Illumination", "Weather"]
4✔
42

43

44
def EndlessCLSim(
4✔
45
    *,
46
    scenario: str = _scenario_names[0],
47
    patch_size: int = 64,
48
    sequence_order: Optional[List[int]] = None,
49
    task_order: Optional[List[int]] = None,
50
    train_transform: Optional[Any] = _default_transform,
51
    eval_transform: Optional[Any] = _default_transform,
52
    dataset_root: Optional[Union[str, Path]] = None,
53
    semseg=False
54
):
55
    """Creates a CL scenario for the Endless-Continual-Learning Simulator's
56
    derived `datasets <https://zenodo.org/record/4899267>`__, or custom
57
    datasets created from
58
    the Endless-Continual-Learning-Simulator's `standalone application <
59
    https://zenodo.org/record/4899294>`__.
60
    Both are part of the publication of `A Procedural World Generation
61
    Framework for Systematic Evaluation of Continual Learning
62
    <https://arxiv.org/abs/2106.02585>`__.
63

64
    If the dataset is not present in the computer, this method will
65
    automatically download and store it.
66

67
    All generated scenarios make use of 'task labels'. We regard a full dataset
68
    as one learning 'sequence', aligned to the terminology in the above paper,
69
    with 'subsequences' being the iterative learning tasks. Each subsequence
70
    is realized as one `AvalancheDataset` with ordering inforaced by task
71
    labels.
72

73
    :param scenario: Available, predefined, learning scenarios are:
74
        'Classes': An learning scenario based on incremental availability of
75
        object class examples,
76
        'Illumination': A learning scenario based on iteratively decreasing
77
        scene illumination.
78
        'Weather': A learning scenario based on iteratively shifting weather
79
        conditions.
80
    :param patch_size: The dimension of the image-patches. Int in the case of
81
            image-patch classification, because the image-patches need to be
82
            quadratic. Tuple of integers for image segmentation tasks.
83
    :param sequence_order: List of intergers indexing the subsequences,
84
            enables reordering of the subsequences, especially subsequences can
85
            be omitted. Defaults to None, loading subsequences in their
86
            original order.
87
    :param task_order: List of intergers, assigning task labels to each
88
            respective subsequence.
89
    :param train_transform: The transformation to apply to the training data.
90
            Defaults to `_default_transform`, i.e. conversion ToTensor of
91
            torchvision.
92
    :param eval_transform: The transformation to apply to the eval data.
93
            Defaults to `_default_transform`, i.e. conversion ToTensor of
94
            torchvision.
95
    :param dataset_root: Absolute path indicating where to store the dataset.
96
            Defaults to None, which means the default location for
97
            'endless-cl-sim' will be used.
98
    :param semseg: boolean to indicate the use of targets for a semantic
99
            segmentation task. Defaults to False.
100

101
    :returns: A properly initialized :class:`EndlessCLSim` instance.
102
    """
103
    # Check scenario name is valid
104
    assert scenario in _scenario_names, (
×
105
        "The selected scenario is not "
106
        "recognized: it should be "
107
        "'Classes', 'Illumination', "
108
        "or 'Weather'."
109
    )
110

111
    # Assign default dataset root if None provided
112
    if dataset_root is None:
×
113
        dataset_root = default_dataset_location("endless-cl-sim")
×
114

115
    # Download and prepare the dataset
116
    endless_cl_sim_dataset = EndlessCLSimDataset(
×
117
        root=dataset_root,
118
        scenario=scenario,
119
        transform=None,
120
        download=True,
121
        semseg=semseg,
122
    )
123

124
    # Default sequence_order if None
125
    if sequence_order is None:
×
126
        sequence_order = list(range(len(endless_cl_sim_dataset)))
×
127

128
    # Default sequence_order if None
129
    if task_order is None:
×
130
        task_order = list(range(len(endless_cl_sim_dataset)))
×
131

132
    train_datasets = []
×
133
    eval_datasets = []
×
134
    for i in range(len(sequence_order)):
×
135
        train_data, eval_data = endless_cl_sim_dataset[sequence_order[i]]
×
136

137
        train_data.transform = train_transform
×
138
        eval_data.transform = eval_transform
×
139

140
        train_datasets.append(
×
141
            make_classification_dataset(
142
                dataset=train_data, task_labels=task_order[i]
143
            )
144
        )
145
        eval_datasets.append(
×
146
            make_classification_dataset(
147
                dataset=eval_data, task_labels=task_order[i]
148
            )
149
        )
150

NEW
151
    scenario_obj: CommonClassificationScenarioType = \
×
152
        dataset_classification_benchmark(
153
            train_datasets,
154
            eval_datasets
155
        )
156

157
    return scenario_obj
×
158

159

160
__all__ = ["EndlessCLSim"]
4✔
161

162

163
if __name__ == "__main__":
4✔
164
    from torch.utils.data.dataloader import DataLoader
×
165
    from torchvision.transforms import ToPILImage, ToTensor
×
166
    import matplotlib.pyplot as plt
×
167

168
    scenario_obj = EndlessCLSim(
×
169
        scenario="Classes",
170
        sequence_order=[0, 1, 2, 3],
171
        task_order=[0, 1, 2, 3],
172
        semseg=True,
173
        dataset_root="/data/avalanche",
174
    )
175

176
    # FIXME: check_vision_benchmark function is crashing -> this is not..
177
    # check_vision_benchmark(scenario_obj)
178
    print(
×
179
        "The benchmark instance contains",
180
        len(scenario_obj.train_stream),
181
        "training experiences.",
182
    )
183

184
    for i, exp in enumerate(scenario_obj.train_stream):
×
185
        dataset, t = exp.dataset, exp.task_label
×
186
        print(dataset, t)
×
187
        print(len(dataset))
×
188

189
    dataloader = DataLoader(dataset, batch_size=300)
×
190
    print("Train experience", exp.current_experience)
×
191

192
    for batch in dataloader:
×
193
        x, y, *other = batch
×
194
        print("X tensor:", x.shape)
×
195
        print("Y tensor:", y.shape)
×
196
        if len(other) > 0:
×
197
            print("T tensor:", other[0].shape)
×
198

199
        img = ToPILImage()(x[0])
×
200
        plt.title("Experience: " + str(exp.current_experience))
×
201
        plt.imshow(img)
×
202
        # plt.show()
203
        break
×
204

205
    print("Done..")
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc