lifelong_methods.methods package¶
Submodules¶
lifelong_methods.methods.agem module¶
-
class
lifelong_methods.methods.agem.
Model
(n_cla_per_tsk: Union[numpy.ndarray, List[int]], class_names_to_idx: Dict[str, int], config: Dict)¶ Bases:
lifelong_methods.methods.base_method.BaseMethod
- An implementation of A-GEM from
Arslan Chaudhry, Marc’Aurelio Ranzato, Marcus Rohrbach, and Mohamed Elhoseiny. Efficient Lifelong Learning with A-GEM. ICLR, 2019.
-
observe
(x: torch.Tensor, y: torch.Tensor, in_buffer: Optional[torch.Tensor] = None, train: bool = True) → Tuple[torch.Tensor, float]¶ The method used for training and validation, returns a tensor of model predictions and the loss This function needs to be defined in the inheriting method class
- Parameters
x (torch.Tensor) – The batch of images
y (torch.Tensor) – A 2-d batch indicator tensor of shape (number of samples x number of classes)
in_buffer (Optional[torch.Tensor]) – A 1-d boolean tensor which indicates which sample is from the buffer.
train (bool) – Whether this is training or validation/test
- Returns
predictions (torch.Tensor) : a 2-d float tensor of the model predictions of shape (number of samples x number of classes) loss (float): the value of the loss
- Return type
Tuple[torch.Tensor, float]
-
forward
(x: torch.Tensor) → torch.Tensor¶ The method used during inference, returns a tensor of model predictions
- Parameters
x (torch.Tensor) – The batch of images
- Returns
a 2-d float tensor of the model predictions of shape (number of samples x number of classes)
- Return type
torch.Tensor
-
consolidate_task_knowledge
(**kwargs) → None¶ Takes place after training on each task
-
class
lifelong_methods.methods.agem.
Buffer
(config: Dict, buffer_dir: Optional[str] = None, map_size: int = 1000000000.0, essential_transforms_fn: Optional[Callable[[PIL.Image.Image], torch.Tensor]] = None, augmentation_transforms_fn: Optional[Callable[[PIL.Image.Image], torch.Tensor]] = None)¶
lifelong_methods.methods.base_method module¶
-
class
lifelong_methods.methods.base_method.
BaseMethod
(n_cla_per_tsk: Union[numpy.ndarray, List[int]], class_names_to_idx: Dict[str, int], config: Dict)¶ Bases:
abc.ABC
,torch.nn.modules.module.Module
A base model for all the lifelong learning methods to inherit, which contains all common functionality
- Parameters
n_cla_per_tsk (Union[np.ndarray, List[int]]) – An integer numpy array including the number of classes per each task.
class_names_to_idx (Dict[str, int]) – The index of each class name
config (Dict) –
A dictionary that has the following key value pairs: temperature (float): the temperature to divide the logits by memory_strength (float): The weight to add for the samples from the buffer when computing the loss
(not implemented yet)
- n_layers (int): The number of layers for the network used (not all values are allowed depending on the
architecture)
dataset (str): The name of the dataset (for ex: iirc_cifar100) optimizer (str): The type of optimizer (“momentum” or “adam”) lr (float): The initial learning rate lr_schedule (Optional[list[int]]): The epochs for which the learning rate changes lr_gamma (float): The multiplier multiplied by the learning rate at the epochs specified in lr_schedule reduce_lr_on_plateau (bool): reduce learning rate on plateau weight_decay (float): the weight decay multiplier
-
method_state_dict
() → Dict[str, Dict]¶ - This function returns a dict that contains the state dictionaries of this method (including the model, the
optimizer, the scheduler, as well as the values of the variables whose names are inside the self.method_variables), so that they can be used for checkpointing.
- Returns
a dictionary with the state dictionaries of this method, the optimizer, the scheduler, and the values of the variables whose names are inside the self.method_variables
- Return type
Dict
-
load_method_state_dict
(state_dicts: Dict[str, Dict]) → None¶ - This function loads the state dicts of the various parts of this method (along with the variables in
self.method_variables)
- Parameters
state_dicts (Dict[str, Dict]) – a dictionary with the state dictionaries of this method, the optimizer, the
scheduler –
the values of the variables whose names are inside the self.method_variables (and) –
-
reset_optimizer_and_scheduler
(optimizable_parameters: Optional[Iterator[torch.nn.parameter.Parameter]] = None) → None¶ Reset the optimizer and scheduler after a task is done (with the option to specify which parameters to optimize
- Parameters
(Optional[Iterator[nn.parameter.Parameter]] (optimizable_parameters) – specify the parameters that should be optimized, in case some parameters needs to be frozen (default: None)
-
get_last_lr
() → List[float]¶ Get the current learning rate
-
step_scheduler
(val_metric: Optional = None) → None¶ Take a step with the scheduler (should be called after each epoch)
- Parameters
val_metric (Optional) – a metric to compare in case of reducing the learning rate on plateau (default: None)
-
forward_net
(x: torch.Tensor) → Tuple[torch.Tensor, torch.Tensor]¶ an alias for self.net(x)
- Parameters
x (torch.Tensor) – The batch of images
- Returns
output (torch.Tensor): The network output of shape (minibatch size x output size) latent (torch.Tensor): The network latent variable of shape (minibatch size x last hidden size)
- Return type
Tuple[torch.Tensor, torch.Tensor]
-
prepare_model_for_new_task
(task_data: Optional[iirc.lifelong_dataset.torch_dataset.Dataset] = None, dist_args: Optional[dict] = None, **kwargs) → None¶ Takes place before the starting epoch of each new task.
The shared functionality among the methods is that the seen classes are updated and the optimizer and scheduler are reset. (see _prepare_model_for_new_task for method specific functionality)
- Parameters
task_data (Optional[Dataset]) – The new task data (default: None)
dist_args (Optional[Dict]) – a dictionary of the distributed processing values in case of multiple gpu (ex:
of the device) (default (rank) – None)
**kwargs – arguments that are method specific
-
consolidate_epoch_knowledge
(val_metric=None, **kwargs) → None¶ Takes place after training on each epoch
The shared functionality among the methods is that the scheduler takes a step. (see _consolidate_epoch_knowledge for method specific functionality)
- Parameters
val_metric (Optional) – a metric to compare in case of reducing the learning rate on plateau (default: None)
**kwargs – arguments that are method specific
-
abstract
observe
(x: torch.Tensor, y: torch.Tensor, in_buffer: Optional[torch.Tensor] = None, train: bool = True) → Tuple[torch.Tensor, float]¶ The method used for training and validation, returns a tensor of model predictions and the loss This function needs to be defined in the inheriting method class
- Parameters
x (torch.Tensor) – The batch of images
y (torch.Tensor) – A 2-d batch indicator tensor of shape (number of samples x number of classes)
in_buffer (Optional[torch.Tensor]) – A 1-d boolean tensor which indicates which sample is from the buffer.
train (bool) – Whether this is training or validation/test
- Returns
predictions (torch.Tensor) : a 2-d float tensor of the model predictions of shape (number of samples x number of classes) loss (float): the value of the loss
- Return type
Tuple[torch.Tensor, float]
-
abstract
consolidate_task_knowledge
(**kwargs) → None¶ Takes place after training each task This function needs to be defined in the inheriting method class
- Parameters
**kwargs – arguments that are method specific
-
abstract
forward
(x: torch.Tensor) → torch.Tensor¶ The method used during inference, returns a tensor of model predictions This function needs to be defined in the inheriting method class
- Parameters
x (torch.Tensor) – The batch of images
- Returns
a 2-d float tensor of the model predictions of shape (number of samples x number of classes)
- Return type
torch.Tensor
lifelong_methods.methods.finetune module¶
-
class
lifelong_methods.methods.finetune.
Model
(n_cla_per_tsk: Union[numpy.ndarray, List[int]], class_names_to_idx: Dict[str, int], config: Dict)¶ Bases:
lifelong_methods.methods.base_method.BaseMethod
A finetuning (Experience Replay) baseline.
-
observe
(x: torch.Tensor, y: torch.Tensor, in_buffer: Optional[torch.Tensor] = None, train: bool = True) → Tuple[torch.Tensor, float]¶ The method used for training and validation, returns a tensor of model predictions and the loss This function needs to be defined in the inheriting method class
- Parameters
x (torch.Tensor) – The batch of images
y (torch.Tensor) – A 2-d batch indicator tensor of shape (number of samples x number of classes)
in_buffer (Optional[torch.Tensor]) – A 1-d boolean tensor which indicates which sample is from the buffer.
train (bool) – Whether this is training or validation/test
- Returns
predictions (torch.Tensor) : a 2-d float tensor of the model predictions of shape (number of samples x number of classes) loss (float): the value of the loss
- Return type
Tuple[torch.Tensor, float]
-
forward
(x: torch.Tensor) → torch.Tensor¶ The method used during inference, returns a tensor of model predictions
- Parameters
x (torch.Tensor) – The batch of images
- Returns
a 2-d float tensor of the model predictions of shape (number of samples x number of classes)
- Return type
torch.Tensor
-
consolidate_task_knowledge
(**kwargs) → None¶ Takes place after training on each task
-
-
class
lifelong_methods.methods.finetune.
Buffer
(config: Dict, buffer_dir: Optional[str] = None, map_size: int = 1000000000.0, essential_transforms_fn: Optional[Callable[[PIL.Image.Image], torch.Tensor]] = None, augmentation_transforms_fn: Optional[Callable[[PIL.Image.Image], torch.Tensor]] = None)¶
lifelong_methods.methods.icarl module¶
-
class
lifelong_methods.methods.icarl.
Model
(n_cla_per_tsk: Union[numpy.ndarray, List[int]], class_names_to_idx: Dict[str, int], config: Dict)¶ Bases:
lifelong_methods.methods.base_method.BaseMethod
- An implementation of iCaRL from
S.-A. Rebuffi, A. Kolesnikov, G. Sperl, and C. H. Lampert. iCaRL: Incremental classifier and representation learning. CVPR, 2017.
-
observe
(x: torch.Tensor, y: torch.Tensor, in_buffer: Optional[torch.Tensor] = None, train: bool = True) → Tuple[torch.Tensor, float]¶ The method used for training and validation, returns a tensor of model predictions and the loss This function needs to be defined in the inheriting method class
- Parameters
x (torch.Tensor) – The batch of images
y (torch.Tensor) – A 2-d batch indicator tensor of shape (number of samples x number of classes)
in_buffer (Optional[torch.Tensor]) – A 1-d boolean tensor which indicates which sample is from the buffer.
train (bool) – Whether this is training or validation/test
- Returns
predictions (torch.Tensor) : a 2-d float tensor of the model predictions of shape (number of samples x number of classes) loss (float): the value of the loss
- Return type
Tuple[torch.Tensor, float]
-
forward
(x: torch.Tensor) → torch.Tensor¶ The method used during inference, returns a tensor of model predictions Classification is done during inference using the nearest class mean
- Parameters
x (torch.Tensor) – The batch of images
- Returns
a 2-d float tensor of the model predictions of shape (number of samples x number of classes)
- Return type
torch.Tensor
-
consolidate_task_knowledge
(buffer: lifelong_methods.buffer.buffer.BufferBase, device: torch.device, batch_size: int, **kwargs) → None¶ Takes place after training each task. It updates the class means based on the new set of exemplars
- Parameters
buffer (BufferBase) – The replay buffer
device (torch.device) – The device used (cpu or gpu)
batch_size (int) – The batch size to be used for calculating the class mean
-
class
lifelong_methods.methods.icarl.
Buffer
(config: Dict, buffer_dir: Optional[str] = None, map_size: int = 1000000000.0, essential_transforms_fn: Optional[Callable[[PIL.Image.Image], torch.Tensor]] = None, augmentation_transforms_fn: Optional[Callable[[PIL.Image.Image], torch.Tensor]] = None)¶
lifelong_methods.methods.icarl_cnn module¶
-
class
lifelong_methods.methods.icarl_cnn.
Model
(n_cla_per_tsk: Union[numpy.ndarray, List[int]], class_names_to_idx: Dict[str, int], config: Dict)¶ Bases:
lifelong_methods.methods.base_method.BaseMethod
An implementation of modified version of iCaRL that doesn’t use the nearest class mean during inference
-
observe
(x: torch.Tensor, y: torch.Tensor, in_buffer: Optional[torch.Tensor] = None, train: bool = True) → Tuple[torch.Tensor, float]¶ The method used for training and validation, returns a tensor of model predictions and the loss This function needs to be defined in the inheriting method class
- Parameters
x (torch.Tensor) – The batch of images
y (torch.Tensor) – A 2-d batch indicator tensor of shape (number of samples x number of classes)
in_buffer (Optional[torch.Tensor]) – A 1-d boolean tensor which indicates which sample is from the buffer.
train (bool) – Whether this is training or validation/test
- Returns
predictions (torch.Tensor) : a 2-d float tensor of the model predictions of shape (number of samples x number of classes) loss (float): the value of the loss
- Return type
Tuple[torch.Tensor, float]
-
forward
(x: torch.Tensor) → torch.Tensor¶ The method used during inference, returns a tensor of model predictions
- Parameters
x (torch.Tensor) – The batch of images
- Returns
a 2-d float tensor of the model predictions of shape (number of samples x number of classes)
- Return type
torch.Tensor
-
consolidate_task_knowledge
(**kwargs) → None¶ Takes place after training on each task
-
-
class
lifelong_methods.methods.icarl_cnn.
Buffer
(config: Dict, buffer_dir: Optional[str] = None, map_size: int = 1000000000.0, essential_transforms_fn: Optional[Callable[[PIL.Image.Image], torch.Tensor]] = None, augmentation_transforms_fn: Optional[Callable[[PIL.Image.Image], torch.Tensor]] = None)¶
lifelong_methods.methods.icarl_norm module¶
-
class
lifelong_methods.methods.icarl_norm.
Model
(n_cla_per_tsk: Union[numpy.ndarray, List[int]], class_names_to_idx: Dict[str, int], config: Dict)¶ Bases:
lifelong_methods.methods.base_method.BaseMethod
- An implementation of modified version of iCaRL that doesn’t use the nearest class mean during inference, and the
the output layer with a cosine similarity layer (where the weights and features are normalized before applying the dot product)
-
observe
(x: torch.Tensor, y: torch.Tensor, in_buffer: Optional[torch.Tensor] = None, train: bool = True) → Tuple[torch.Tensor, float]¶ The method used for training and validation, returns a tensor of model predictions and the loss This function needs to be defined in the inheriting method class
- Parameters
x (torch.Tensor) – The batch of images
y (torch.Tensor) – A 2-d batch indicator tensor of shape (number of samples x number of classes)
in_buffer (Optional[torch.Tensor]) – A 1-d boolean tensor which indicates which sample is from the buffer.
train (bool) – Whether this is training or validation/test
- Returns
predictions (torch.Tensor) : a 2-d float tensor of the model predictions of shape (number of samples x number of classes) loss (float): the value of the loss
- Return type
Tuple[torch.Tensor, float]
-
forward
(x: torch.Tensor) → torch.Tensor¶ The method used during inference, returns a tensor of model predictions
- Parameters
x (torch.Tensor) – The batch of images
- Returns
a 2-d float tensor of the model predictions of shape (number of samples x number of classes)
- Return type
torch.Tensor
-
consolidate_task_knowledge
(**kwargs) → None¶ Takes place after training each task
-
class
lifelong_methods.methods.icarl_norm.
Buffer
(config: Dict, buffer_dir: Optional[str] = None, map_size: int = 1000000000.0, essential_transforms_fn: Optional[Callable[[PIL.Image.Image], torch.Tensor]] = None, augmentation_transforms_fn: Optional[Callable[[PIL.Image.Image], torch.Tensor]] = None)¶
lifelong_methods.methods.lucir module¶
-
class
lifelong_methods.methods.lucir.
Model
(n_cla_per_tsk: Union[numpy.ndarray, List[int]], class_names_to_idx: Dict[str, int], config: Dict)¶ Bases:
lifelong_methods.methods.base_method.BaseMethod
- An implementation of LUCIR from
Saihui Hou, Xinyu Pan, Chen Change Loy, Zilei Wang, and Dahua Lin. Learning a Unified Classifier Incrementally via Rebalancing. CVPR, 2019.
-
observe
(x: torch.Tensor, y: torch.Tensor, in_buffer: Optional[torch.Tensor] = None, train: bool = True) → Tuple[torch.Tensor, float]¶ The method used for training and validation, returns a tensor of model predictions and the loss This function needs to be defined in the inheriting method class
- Parameters
x (torch.Tensor) – The batch of images
y (torch.Tensor) – A 2-d batch indicator tensor of shape (number of samples x number of classes)
in_buffer (Optional[torch.Tensor]) – A 1-d boolean tensor which indicates which sample is from the buffer.
train (bool) – Whether this is training or validation/test
- Returns
predictions (torch.Tensor) : a 2-d float tensor of the model predictions of shape (number of samples x number of classes) loss (float): the value of the loss
- Return type
Tuple[torch.Tensor, float]
-
forward
(x: torch.Tensor) → torch.Tensor¶ The method used during inference, returns a tensor of model predictions
- Parameters
x (torch.Tensor) – The batch of images
- Returns
a 2-d float tensor of the model predictions of shape (number of samples x number of classes)
- Return type
torch.Tensor
-
consolidate_task_knowledge
(**kwargs) → None¶ Takes place after training on each task
-
class
lifelong_methods.methods.lucir.
Buffer
(config: Dict, buffer_dir: Optional[str] = None, map_size: int = 1000000000.0, essential_transforms_fn: Optional[Callable[[PIL.Image.Image], torch.Tensor]] = None, augmentation_transforms_fn: Optional[Callable[[PIL.Image.Image], torch.Tensor]] = None)¶
lifelong_methods.methods.mask_seen_classes module¶
-
class
lifelong_methods.methods.mask_seen_classes.
Model
(n_cla_per_tsk: Union[numpy.ndarray, List[int]], class_names_to_idx: Dict[str, int], config: Dict)¶ Bases:
lifelong_methods.methods.base_method.BaseMethod
A method that only provides feedback for the classes that belong to the task of the sample (masks other tasks classes when computing the loss).
-
observe
(x: torch.Tensor, y: torch.Tensor, in_buffer: Optional[torch.Tensor] = None, train: bool = True) → Tuple[torch.Tensor, float]¶ The method used for training and validation, returns a tensor of model predictions and the loss This function needs to be defined in the inheriting method class
- Parameters
x (torch.Tensor) – The batch of images
y (torch.Tensor) – A 2-d batch indicator tensor of shape (number of samples x number of classes)
in_buffer (Optional[torch.Tensor]) – A 1-d boolean tensor which indicates which sample is from the buffer.
train (bool) – Whether this is training or validation/test
- Returns
predictions (torch.Tensor) : a 2-d float tensor of the model predictions of shape (number of samples x number of classes) loss (float): the value of the loss
- Return type
Tuple[torch.Tensor, float]
-
forward
(x: torch.Tensor) → torch.Tensor¶ The method used during inference, returns a tensor of model predictions
- Parameters
x (torch.Tensor) – The batch of images
- Returns
a 2-d float tensor of the model predictions of shape (number of samples x number of classes)
- Return type
torch.Tensor
-
consolidate_task_knowledge
(**kwargs) → None¶ Takes place after training on each task
-
-
class
lifelong_methods.methods.mask_seen_classes.
Buffer
(config: Dict, buffer_dir: Optional[str] = None, map_size: int = 1000000000.0, essential_transforms_fn: Optional[Callable[[PIL.Image.Image], torch.Tensor]] = None, augmentation_transforms_fn: Optional[Callable[[PIL.Image.Image], torch.Tensor]] = None)¶