osculari.paradigms
This module implements psychophysical paradigms to experiment with deep networks.
osculari.paradigms.adaptive_psychophysics
A collection of adaptive psychophysical experimental methods.
- osculari.paradigms.adaptive_psychophysics.staircase(model: Module, test_fun: Callable[[Module, DataLoader, device], Dict], dataset_fun: Callable[[float], Tuple], low_val: float, high_val: float, device: Optional[device] = None, max_attempts: Optional[int] = 20) ndarray[Any, dtype[ScalarType]][source]
Computes the psychometric function following the staircase procedure.
- Parameters:
model – The neural network model to be evaluated.
test_fun – Function for evaluating the model. This function must accept three positional arguments (i.e., model, db_loader, device). The output of this function should be a dictionary containing the key accuracy.
dataset_fun – Function for creating the dataset and dataloader. This function must accept one argument (mid_val, i.e., the current value to be tested). This funtion must return a tuple of three elements (i.e., dataset, batch_size, threshold).
low_val – The lower bound of the stimulus range.
high_val – The upper bound of the stimulus range.
device – The device to which the model and data will be transferred (default: CUDA if available) (optional).
max_attempts – The maximum number of attempts allowed in the staircase procedure.
- Returns:
A NumPy array containing the psychometric function data points containing two columns, the first column tested values and the second column obtained accuracies.
osculari.paradigms.forced_choice
Generic template for paradigms linear classifiers on top of pretrained networks.
- osculari.paradigms.forced_choice.epoch_loop(model: Module, dataset: Iterator, optimiser: Optional[Optimizer], device: Optional[device] = None, return_outputs: Optional[bool] = False) Dict[source]
Executes an epoch of training or testing.
- Parameters:
model – The neural network model to be trained or tested.
dataset – The data loader providing data batches for training or testing.
optimiser – The optimiser used for training (if None it switches to testing mode).
device – The device to which the model and data will be transferred (default: CUDA if available) (optional).
return_outputs – Whether to return model’s outputs.
- Returns:
A dictionary containing ‘accuracy’ and ‘loss’. If return_outputs is True, it will also include the ‘output’ key, which contains the model’s outputs for each data batch.
- osculari.paradigms.forced_choice.predict_dataset(model: Module, dataset: Iterator, device: Optional[device] = None) Dict[source]
Executes an epoch of prediction.
- Parameters:
model – The neural network model to be trained or tested.
dataset – The data loader providing data batches for training or testing.
device – The device to which the model and data will be transferred (default: CUDA if available) (optional).
- Returns:
A dictionary containing ‘accuracy’, ‘loss’ and ‘output’ (model’s outputs).
- osculari.paradigms.forced_choice.test_dataset(model: Module, dataset: Iterator, device: Optional[device] = None) Dict[source]
Executes an epoch of testing.
- Parameters:
model – The neural network model to be trained or tested.
dataset – The data loader providing data batches for training or testing.
device – The device to which the model and data will be transferred (default: CUDA if available) (optional).
- Returns:
A dictionary containing ‘accuracy’ and ‘loss’.
osculari.paradigms.paradigm_utils
Utility function for paradigms.
- osculari.paradigms.paradigm_utils.train_linear_probe(model: ProbeNet, dataset: Union[Dataset, DataLoader], epoch_loop: Callable[[Module, DataLoader, Any, device], Dict], out_dir: str, device: Optional[device] = None, epochs: Optional[int] = 10, optimiser: Optional[Optimizer] = None, scheduler: Optional[LRScheduler] = None) Dict[source]
Train a linear probe on top of a frozen backbone model.
- Parameters:
model (ProbeNet) – Linear probe model.
dataset (Union[TorchDataset, TorchDataLoader]) – Training dataset or data loader.
epoch_loop (Callable) – Function defining the training loop for one epoch. This function must accept for positional arguments (i.e., model, train_loader, optimiser, device). This function should return a dictionary.
out_dir (str) – Output directory to save checkpoints.
device (Optional[torch.device]) – Device on which to perform training.
epochs (Optional[int]) – Number of training epochs. Default is 10.
optimiser (Optional[torch.optim.Optimizer]) – Optimization algorithm. Default is SGD.
scheduler (Optional[lr_scheduler.LRScheduler]) – Learning rate scheduler. Default is MultiStepLR at 50 and 80% of epochs
- Returns:
Training logs containing statistics.
- Return type:
Dict