clarena.cl_datasets.permuted_flowers102

The submodule in cl_datasets for Permuted Oxford 102 Flower dataset.

  1r"""
  2The submodule in `cl_datasets` for Permuted Oxford 102 Flower dataset.
  3"""
  4
  5__all__ = ["PermutedFlowers102"]
  6
  7import logging
  8from typing import Callable
  9
 10from torch.utils.data import Dataset
 11from torchvision.datasets import Flowers102
 12from torchvision.transforms import transforms
 13
 14from clarena.cl_datasets import CLPermutedDataset
 15
 16# always get logger for built-in logging in each module
 17pylogger = logging.getLogger(__name__)
 18
 19
 20class PermutedFlowers102(CLPermutedDataset):
 21    r"""Permuted Oxford 102 Flower dataset. The [Oxford 102 Flower dataset](https://www.robots.ox.ac.uk/~vgg/data/flowers/102/) is a collection of flower pictures. It consists of 8,189 images of 102 kinds of flowers (classes), each color image."""
 22
 23    original_dataset_python_class: type[Dataset] = Flowers102
 24    r"""The original dataset class."""
 25
 26    def __init__(
 27        self,
 28        root: str,
 29        num_tasks: int,
 30        batch_size: int | dict[int, int] = 1,
 31        num_workers: int | dict[int, int] = 0,
 32        custom_transforms: (
 33            Callable
 34            | transforms.Compose
 35            | None
 36            | dict[int, Callable | transforms.Compose | None]
 37        ) = None,
 38        repeat_channels: int | None | dict[int, int | None] = None,
 39        to_tensor: bool | dict[int, bool] = True,
 40        resize: tuple[int, int] | None | dict[int, tuple[int, int] | None] = None,
 41        permutation_mode: str = "first_channel_only",
 42        permutation_seeds: dict[int, int] | None = None,
 43    ) -> None:
 44        r"""
 45        **Args:**
 46        - **root** (`str`): the root directory where the original Oxford 102 Flower data 'Flower102/' live.
 47        - **num_tasks** (`int`): the maximum number of tasks supported by the CL dataset. This decides the valid task IDs from 1 to `num_tasks`.
 48        - **batch_size** (`int` | `dict[int, int]`): the batch size for train, val, and test dataloaders.
 49        If it is a dict, the keys are task IDs and the values are the batch sizes for each task. If it is an `int`, it is the same batch size for all tasks.
 50        - **num_workers** (`int` | `dict[int, int]`): the number of workers for dataloaders.
 51        If it is a dict, the keys are task IDs and the values are the number of workers for each task. If it is an `int`, it is the same number of workers for all tasks.
 52        - **custom_transforms** (`transform` or `transforms.Compose` or `None` or dict of them): the custom transforms to apply ONLY to the TRAIN dataset. Can be a single transform, composed transforms, or no transform. `ToTensor()`, normalization, permute, and so on are not included.
 53        If it is a dict, the keys are task IDs and the values are the custom transforms for each task. If it is a single transform or composed transforms, it is applied to all tasks. If it is `None`, no custom transforms are applied.
 54        - **repeat_channels** (`int` | `None` | dict of them): the number of channels to repeat for each task. Default is `None`, which means no repeat.
 55        If it is a dict, the keys are task IDs and the values are the number of channels to repeat for each task. If it is an `int`, it is the same number of channels to repeat for all tasks. If it is `None`, no repeat is applied.
 56        - **to_tensor** (`bool` | `dict[int, bool]`): whether to include the `ToTensor()` transform. Default is `True`.
 57        If it is a dict, the keys are task IDs and the values are whether to include the `ToTensor()` transform for each task. If it is a single boolean value, it is applied to all tasks.
 58        - **resize** (`tuple[int, int]` | `None` or dict of them): the size to resize the images to. Default is `None`, which means no resize.
 59        If it is a dict, the keys are task IDs and the values are the sizes to resize for each task. If it is a single tuple of two integers, it is applied to all tasks. If it is `None`, no resize is applied.
 60        - **permutation_mode** (`str`): the mode of permutation; one of:
 61            1. 'all': permute all pixels.
 62            2. 'by_channel': permute channel by channel separately. All channels are applied the same permutation order.
 63            3. 'first_channel_only': permute only the first channel.
 64        - **permutation_seeds** (`dict[int, int]` | `None`): the dict of seeds for permutation operations used to construct each task. Keys are task IDs and the values are permutation seeds for each task. Default is `None`, which creates a dict of seeds from 0 to `num_tasks`-1.
 65        """
 66
 67        super().__init__(
 68            root=root,
 69            num_tasks=num_tasks,
 70            batch_size=batch_size,
 71            num_workers=num_workers,
 72            custom_transforms=custom_transforms,
 73            repeat_channels=repeat_channels,
 74            to_tensor=to_tensor,
 75            resize=resize,
 76            permutation_mode=permutation_mode,
 77            permutation_seeds=permutation_seeds,
 78        )
 79
 80    def prepare_data(self) -> None:
 81        r"""Download the original Oxford 102 Flower dataset if haven't."""
 82
 83        if self.task_id != 1:
 84            return  # download all original datasets only at the beginning of first task
 85
 86        Flowers102(root=self.root_t, split="train", download=True)
 87        Flowers102(root=self.root_t, split="val", download=True)
 88        Flowers102(root=self.root_t, split="test", download=True)
 89
 90        pylogger.debug(
 91            "The original Oxford 102 Flower dataset has been downloaded to %s.",
 92            self.root_t,
 93        )
 94
 95    def train_and_val_dataset(self) -> tuple[Dataset, Dataset]:
 96        """Get the training and validation dataset of task `self.task_id`.
 97
 98        **Returns:**
 99        - **train_and_val_dataset** (`tuple[Dataset, Dataset]`): the train and validation dataset of task `self.task_id`.
100        """
101        dataset_train = Flowers102(
102            root=self.root_t,
103            split="train",
104            transform=self.train_and_val_transforms(),
105            target_transform=self.target_transform(),
106            download=False,
107        )
108
109        dataset_val = Flowers102(
110            root=self.root_t,
111            split="val",
112            transform=self.train_and_val_transforms(),
113            target_transform=self.target_transform(),
114            download=False,
115        )
116
117        return dataset_train, dataset_val
118
119    def test_dataset(self) -> Dataset:
120        r"""Get the test dataset of task `self.task_id`.
121
122        **Returns:**
123        - **test_dataset** (`Dataset`): the test dataset of task `self.task_id`.
124        """
125        dataset_test = Flowers102(
126            root=self.root_t,
127            split="test",
128            transform=self.test_transforms(),
129            target_transform=self.target_transform(),
130            download=False,
131        )
132
133        return dataset_test
class PermutedFlowers102(clarena.cl_datasets.base.CLPermutedDataset):
 21class PermutedFlowers102(CLPermutedDataset):
 22    r"""Permuted Oxford 102 Flower dataset. The [Oxford 102 Flower dataset](https://www.robots.ox.ac.uk/~vgg/data/flowers/102/) is a collection of flower pictures. It consists of 8,189 images of 102 kinds of flowers (classes), each color image."""
 23
 24    original_dataset_python_class: type[Dataset] = Flowers102
 25    r"""The original dataset class."""
 26
 27    def __init__(
 28        self,
 29        root: str,
 30        num_tasks: int,
 31        batch_size: int | dict[int, int] = 1,
 32        num_workers: int | dict[int, int] = 0,
 33        custom_transforms: (
 34            Callable
 35            | transforms.Compose
 36            | None
 37            | dict[int, Callable | transforms.Compose | None]
 38        ) = None,
 39        repeat_channels: int | None | dict[int, int | None] = None,
 40        to_tensor: bool | dict[int, bool] = True,
 41        resize: tuple[int, int] | None | dict[int, tuple[int, int] | None] = None,
 42        permutation_mode: str = "first_channel_only",
 43        permutation_seeds: dict[int, int] | None = None,
 44    ) -> None:
 45        r"""
 46        **Args:**
 47        - **root** (`str`): the root directory where the original Oxford 102 Flower data 'Flower102/' live.
 48        - **num_tasks** (`int`): the maximum number of tasks supported by the CL dataset. This decides the valid task IDs from 1 to `num_tasks`.
 49        - **batch_size** (`int` | `dict[int, int]`): the batch size for train, val, and test dataloaders.
 50        If it is a dict, the keys are task IDs and the values are the batch sizes for each task. If it is an `int`, it is the same batch size for all tasks.
 51        - **num_workers** (`int` | `dict[int, int]`): the number of workers for dataloaders.
 52        If it is a dict, the keys are task IDs and the values are the number of workers for each task. If it is an `int`, it is the same number of workers for all tasks.
 53        - **custom_transforms** (`transform` or `transforms.Compose` or `None` or dict of them): the custom transforms to apply ONLY to the TRAIN dataset. Can be a single transform, composed transforms, or no transform. `ToTensor()`, normalization, permute, and so on are not included.
 54        If it is a dict, the keys are task IDs and the values are the custom transforms for each task. If it is a single transform or composed transforms, it is applied to all tasks. If it is `None`, no custom transforms are applied.
 55        - **repeat_channels** (`int` | `None` | dict of them): the number of channels to repeat for each task. Default is `None`, which means no repeat.
 56        If it is a dict, the keys are task IDs and the values are the number of channels to repeat for each task. If it is an `int`, it is the same number of channels to repeat for all tasks. If it is `None`, no repeat is applied.
 57        - **to_tensor** (`bool` | `dict[int, bool]`): whether to include the `ToTensor()` transform. Default is `True`.
 58        If it is a dict, the keys are task IDs and the values are whether to include the `ToTensor()` transform for each task. If it is a single boolean value, it is applied to all tasks.
 59        - **resize** (`tuple[int, int]` | `None` or dict of them): the size to resize the images to. Default is `None`, which means no resize.
 60        If it is a dict, the keys are task IDs and the values are the sizes to resize for each task. If it is a single tuple of two integers, it is applied to all tasks. If it is `None`, no resize is applied.
 61        - **permutation_mode** (`str`): the mode of permutation; one of:
 62            1. 'all': permute all pixels.
 63            2. 'by_channel': permute channel by channel separately. All channels are applied the same permutation order.
 64            3. 'first_channel_only': permute only the first channel.
 65        - **permutation_seeds** (`dict[int, int]` | `None`): the dict of seeds for permutation operations used to construct each task. Keys are task IDs and the values are permutation seeds for each task. Default is `None`, which creates a dict of seeds from 0 to `num_tasks`-1.
 66        """
 67
 68        super().__init__(
 69            root=root,
 70            num_tasks=num_tasks,
 71            batch_size=batch_size,
 72            num_workers=num_workers,
 73            custom_transforms=custom_transforms,
 74            repeat_channels=repeat_channels,
 75            to_tensor=to_tensor,
 76            resize=resize,
 77            permutation_mode=permutation_mode,
 78            permutation_seeds=permutation_seeds,
 79        )
 80
 81    def prepare_data(self) -> None:
 82        r"""Download the original Oxford 102 Flower dataset if haven't."""
 83
 84        if self.task_id != 1:
 85            return  # download all original datasets only at the beginning of first task
 86
 87        Flowers102(root=self.root_t, split="train", download=True)
 88        Flowers102(root=self.root_t, split="val", download=True)
 89        Flowers102(root=self.root_t, split="test", download=True)
 90
 91        pylogger.debug(
 92            "The original Oxford 102 Flower dataset has been downloaded to %s.",
 93            self.root_t,
 94        )
 95
 96    def train_and_val_dataset(self) -> tuple[Dataset, Dataset]:
 97        """Get the training and validation dataset of task `self.task_id`.
 98
 99        **Returns:**
100        - **train_and_val_dataset** (`tuple[Dataset, Dataset]`): the train and validation dataset of task `self.task_id`.
101        """
102        dataset_train = Flowers102(
103            root=self.root_t,
104            split="train",
105            transform=self.train_and_val_transforms(),
106            target_transform=self.target_transform(),
107            download=False,
108        )
109
110        dataset_val = Flowers102(
111            root=self.root_t,
112            split="val",
113            transform=self.train_and_val_transforms(),
114            target_transform=self.target_transform(),
115            download=False,
116        )
117
118        return dataset_train, dataset_val
119
120    def test_dataset(self) -> Dataset:
121        r"""Get the test dataset of task `self.task_id`.
122
123        **Returns:**
124        - **test_dataset** (`Dataset`): the test dataset of task `self.task_id`.
125        """
126        dataset_test = Flowers102(
127            root=self.root_t,
128            split="test",
129            transform=self.test_transforms(),
130            target_transform=self.target_transform(),
131            download=False,
132        )
133
134        return dataset_test

Permuted Oxford 102 Flower dataset. The Oxford 102 Flower dataset is a collection of flower pictures. It consists of 8,189 images of 102 kinds of flowers (classes), each color image.

PermutedFlowers102( root: str, num_tasks: int, batch_size: int | dict[int, int] = 1, num_workers: int | dict[int, int] = 0, custom_transforms: Union[Callable, torchvision.transforms.transforms.Compose, NoneType, dict[int, Union[Callable, torchvision.transforms.transforms.Compose, NoneType]]] = None, repeat_channels: int | None | dict[int, int | None] = None, to_tensor: bool | dict[int, bool] = True, resize: tuple[int, int] | None | dict[int, tuple[int, int] | None] = None, permutation_mode: str = 'first_channel_only', permutation_seeds: dict[int, int] | None = None)
27    def __init__(
28        self,
29        root: str,
30        num_tasks: int,
31        batch_size: int | dict[int, int] = 1,
32        num_workers: int | dict[int, int] = 0,
33        custom_transforms: (
34            Callable
35            | transforms.Compose
36            | None
37            | dict[int, Callable | transforms.Compose | None]
38        ) = None,
39        repeat_channels: int | None | dict[int, int | None] = None,
40        to_tensor: bool | dict[int, bool] = True,
41        resize: tuple[int, int] | None | dict[int, tuple[int, int] | None] = None,
42        permutation_mode: str = "first_channel_only",
43        permutation_seeds: dict[int, int] | None = None,
44    ) -> None:
45        r"""
46        **Args:**
47        - **root** (`str`): the root directory where the original Oxford 102 Flower data 'Flower102/' live.
48        - **num_tasks** (`int`): the maximum number of tasks supported by the CL dataset. This decides the valid task IDs from 1 to `num_tasks`.
49        - **batch_size** (`int` | `dict[int, int]`): the batch size for train, val, and test dataloaders.
50        If it is a dict, the keys are task IDs and the values are the batch sizes for each task. If it is an `int`, it is the same batch size for all tasks.
51        - **num_workers** (`int` | `dict[int, int]`): the number of workers for dataloaders.
52        If it is a dict, the keys are task IDs and the values are the number of workers for each task. If it is an `int`, it is the same number of workers for all tasks.
53        - **custom_transforms** (`transform` or `transforms.Compose` or `None` or dict of them): the custom transforms to apply ONLY to the TRAIN dataset. Can be a single transform, composed transforms, or no transform. `ToTensor()`, normalization, permute, and so on are not included.
54        If it is a dict, the keys are task IDs and the values are the custom transforms for each task. If it is a single transform or composed transforms, it is applied to all tasks. If it is `None`, no custom transforms are applied.
55        - **repeat_channels** (`int` | `None` | dict of them): the number of channels to repeat for each task. Default is `None`, which means no repeat.
56        If it is a dict, the keys are task IDs and the values are the number of channels to repeat for each task. If it is an `int`, it is the same number of channels to repeat for all tasks. If it is `None`, no repeat is applied.
57        - **to_tensor** (`bool` | `dict[int, bool]`): whether to include the `ToTensor()` transform. Default is `True`.
58        If it is a dict, the keys are task IDs and the values are whether to include the `ToTensor()` transform for each task. If it is a single boolean value, it is applied to all tasks.
59        - **resize** (`tuple[int, int]` | `None` or dict of them): the size to resize the images to. Default is `None`, which means no resize.
60        If it is a dict, the keys are task IDs and the values are the sizes to resize for each task. If it is a single tuple of two integers, it is applied to all tasks. If it is `None`, no resize is applied.
61        - **permutation_mode** (`str`): the mode of permutation; one of:
62            1. 'all': permute all pixels.
63            2. 'by_channel': permute channel by channel separately. All channels are applied the same permutation order.
64            3. 'first_channel_only': permute only the first channel.
65        - **permutation_seeds** (`dict[int, int]` | `None`): the dict of seeds for permutation operations used to construct each task. Keys are task IDs and the values are permutation seeds for each task. Default is `None`, which creates a dict of seeds from 0 to `num_tasks`-1.
66        """
67
68        super().__init__(
69            root=root,
70            num_tasks=num_tasks,
71            batch_size=batch_size,
72            num_workers=num_workers,
73            custom_transforms=custom_transforms,
74            repeat_channels=repeat_channels,
75            to_tensor=to_tensor,
76            resize=resize,
77            permutation_mode=permutation_mode,
78            permutation_seeds=permutation_seeds,
79        )

Args:

  • root (str): the root directory where the original Oxford 102 Flower data 'Flower102/' live.
  • num_tasks (int): the maximum number of tasks supported by the CL dataset. This decides the valid task IDs from 1 to num_tasks.
  • batch_size (int | dict[int, int]): the batch size for train, val, and test dataloaders. If it is a dict, the keys are task IDs and the values are the batch sizes for each task. If it is an int, it is the same batch size for all tasks.
  • num_workers (int | dict[int, int]): the number of workers for dataloaders. If it is a dict, the keys are task IDs and the values are the number of workers for each task. If it is an int, it is the same number of workers for all tasks.
  • custom_transforms (transform or transforms.Compose or None or dict of them): the custom transforms to apply ONLY to the TRAIN dataset. Can be a single transform, composed transforms, or no transform. ToTensor(), normalization, permute, and so on are not included. If it is a dict, the keys are task IDs and the values are the custom transforms for each task. If it is a single transform or composed transforms, it is applied to all tasks. If it is None, no custom transforms are applied.
  • repeat_channels (int | None | dict of them): the number of channels to repeat for each task. Default is None, which means no repeat. If it is a dict, the keys are task IDs and the values are the number of channels to repeat for each task. If it is an int, it is the same number of channels to repeat for all tasks. If it is None, no repeat is applied.
  • to_tensor (bool | dict[int, bool]): whether to include the ToTensor() transform. Default is True. If it is a dict, the keys are task IDs and the values are whether to include the ToTensor() transform for each task. If it is a single boolean value, it is applied to all tasks.
  • resize (tuple[int, int] | None or dict of them): the size to resize the images to. Default is None, which means no resize. If it is a dict, the keys are task IDs and the values are the sizes to resize for each task. If it is a single tuple of two integers, it is applied to all tasks. If it is None, no resize is applied.
  • permutation_mode (str): the mode of permutation; one of:
    1. 'all': permute all pixels.
    2. 'by_channel': permute channel by channel separately. All channels are applied the same permutation order.
    3. 'first_channel_only': permute only the first channel.
  • permutation_seeds (dict[int, int] | None): the dict of seeds for permutation operations used to construct each task. Keys are task IDs and the values are permutation seeds for each task. Default is None, which creates a dict of seeds from 0 to num_tasks-1.
original_dataset_python_class: type[torch.utils.data.dataset.Dataset] = <class 'torchvision.datasets.flowers102.Flowers102'>

The original dataset class.

def prepare_data(self) -> None:
81    def prepare_data(self) -> None:
82        r"""Download the original Oxford 102 Flower dataset if haven't."""
83
84        if self.task_id != 1:
85            return  # download all original datasets only at the beginning of first task
86
87        Flowers102(root=self.root_t, split="train", download=True)
88        Flowers102(root=self.root_t, split="val", download=True)
89        Flowers102(root=self.root_t, split="test", download=True)
90
91        pylogger.debug(
92            "The original Oxford 102 Flower dataset has been downloaded to %s.",
93            self.root_t,
94        )

Download the original Oxford 102 Flower dataset if haven't.

def train_and_val_dataset( self) -> tuple[torch.utils.data.dataset.Dataset, torch.utils.data.dataset.Dataset]:
 96    def train_and_val_dataset(self) -> tuple[Dataset, Dataset]:
 97        """Get the training and validation dataset of task `self.task_id`.
 98
 99        **Returns:**
100        - **train_and_val_dataset** (`tuple[Dataset, Dataset]`): the train and validation dataset of task `self.task_id`.
101        """
102        dataset_train = Flowers102(
103            root=self.root_t,
104            split="train",
105            transform=self.train_and_val_transforms(),
106            target_transform=self.target_transform(),
107            download=False,
108        )
109
110        dataset_val = Flowers102(
111            root=self.root_t,
112            split="val",
113            transform=self.train_and_val_transforms(),
114            target_transform=self.target_transform(),
115            download=False,
116        )
117
118        return dataset_train, dataset_val

Get the training and validation dataset of task self.task_id.

Returns:

  • train_and_val_dataset (tuple[Dataset, Dataset]): the train and validation dataset of task self.task_id.
def test_dataset(self) -> torch.utils.data.dataset.Dataset:
120    def test_dataset(self) -> Dataset:
121        r"""Get the test dataset of task `self.task_id`.
122
123        **Returns:**
124        - **test_dataset** (`Dataset`): the test dataset of task `self.task_id`.
125        """
126        dataset_test = Flowers102(
127            root=self.root_t,
128            split="test",
129            transform=self.test_transforms(),
130            target_transform=self.target_transform(),
131            download=False,
132        )
133
134        return dataset_test

Get the test dataset of task self.task_id.

Returns:

  • test_dataset (Dataset): the test dataset of task self.task_id.