buffer#

Classes

class utils.buffer.Buffer(buffer_size, device='cpu')[source]#

Bases: object

The memory buffer of rehearsal method.

add_data(examples, labels=None, logits=None, task_labels=None, attention_maps=None)[source]#

Adds the data to the memory buffer according to the reservoir strategy.

Parameters:
  • examples – tensor containing the images

  • labels – tensor containing the labels

  • logits – tensor containing the outputs of the network

  • task_labels – tensor containing the task labels

Note

Only the examples are required. The other tensors are initialized only if they are provided.

empty()[source]#

Set all the tensors to None.

get_all_data(transform=None, device=None)[source]#

Return all the items in the memory buffer.

Parameters:

transform (Module | None) – the transformation to be applied (data augmentation)

Returns:

a tuple with all the items in the memory buffer

Return type:

Tuple

get_data(size, transform=None, return_index=False, device=None, mask_task_out=None, cpt=None)[source]#

Random samples a batch of size items.

Parameters:
  • size (int) – the number of requested items

  • transform (Module | None) – the transformation to be applied (data augmentation)

  • return_index – if True, returns the indexes of the sampled items

  • mask_task – if not None, masks OUT the examples from the given task

  • cpt – the number of classes per task (required if mask_task is not None and task_labels are not present)

Returns:

a tuple containing the requested items. If return_index is True, the tuple contains the indexes as first element.

Return type:

Tuple

get_data_by_index(indexes, transform=None, device=None)[source]#

Returns the data by the given index.

Parameters:
  • index – the index of the item

  • transform (Module | None) – the transformation to be applied (data augmentation)

Returns:

a tuple containing the requested items. The returned items depend on the attributes stored in the buffer from previous calls to add_data.

Return type:

Tuple

init_tensors(examples, labels, logits, task_labels)[source]#

Initializes just the required tensors.

Parameters:
  • examples (Tensor) – tensor containing the images

  • labels (Tensor) – tensor containing the labels

  • logits (Tensor) – tensor containing the outputs of the network

  • task_labels (Tensor) – tensor containing the task labels

is_empty()[source]#

Returns true if the buffer is empty, false otherwise.

Return type:

bool

to(device)[source]#

Move the buffer and its attributes to the specified device.

Parameters:

device – The device to move the buffer and its attributes to.

Returns:

The buffer instance with the updated device and attributes.

property used_attributes#

Returns a list of attributes that are currently being used by the object.

Functions

utils.buffer.fill_buffer(buffer, dataset, t_idx, net=None, use_herding=False, required_attributes=None)[source]#

Adds examples from the current task to the memory buffer. Supports images, labels, task_labels, and logits.

Parameters:
  • buffer (Buffer) – the memory buffer

  • dataset (ContinualDataset) – the dataset from which take the examples

  • t_idx (int) – the task index

  • net (ContinualModel) – (optional) the model instance. Used if logits are in buffer. If provided, adds logits.

  • use_herding – (optional) if True, uses herding strategy. Otherwise, random sampling.

  • required_attributes (List[str]) – (optional) the attributes to be added to the buffer. If None and buffer is empty, adds only examples and labels.

utils.buffer.icarl_replay(self, dataset, val_set_split=0)[source]#

Merge the replay buffer with the current task data. Optionally split the replay buffer into a validation set.

Parameters:
  • self (ContinualModel) – the model instance

  • dataset – the dataset

  • val_set_split – the fraction of the replay buffer to be used as validation set

utils.buffer.reservoir(num_seen_examples, buffer_size)[source]#

Reservoir sampling algorithm.

Parameters:
  • num_seen_examples (int) – the number of seen examples

  • buffer_size (int) – the maximum buffer size

Returns:

the target index if the current image is sampled, else -1

Return type:

int