template.runner.image_classification package

Submodules

template.runner.image_classification.evaluate module

template.runner.image_classification.evaluate.evaluate(data_loader, model, criterion, writer, epoch, logging_label, no_cuda=False, log_interval=20, **kwargs)[source]

The evaluation routine

Parameters
  • data_loader (torch.utils.data.DataLoader) – The dataloader of the evaluation set

  • model (torch.nn.module) – The network model being used

  • criterion (torch.nn.loss) – The loss function used to compute the loss of the model

  • writer (tensorboardX.writer.SummaryWriter) – The tensorboard writer object. Used to log values on file for the tensorboard visualization.

  • epoch (int) – Number of the epoch (for logging purposes)

  • logging_label (string) – Label for logging purposes. Typically ‘test’ or ‘valid’. Its prepended to the logging output path and messages.

  • no_cuda (boolean) – Specifies whether the GPU should be used or not. A value of ‘True’ means the CPU will be used.

  • log_interval (int) – Interval limiting the logging of mini-batches. Default value of 10.

Returns

top1.avg – Accuracy of the model of the evaluated split

Return type

float

template.runner.image_classification.image_classification module

This file is the template for the boilerplate of train/test of a DNN for image classification

There are a lot of parameter which can be specified to modify the behaviour and they should be used instead of hard-coding stuff.

class template.runner.image_classification.image_classification.ImageClassification[source]

Bases: object

classmethod prepare(model_name, **kwargs)[source]

Loads and prepares the data, the optimizer and the criterion

Parameters
  • model_name (str) – Name of the model. Used for loading the model.

  • kwargs (dict) – Any additional arguments.

Returns

  • model (DataParallel) – The model to train

  • num_classes (int) – How many different classes there are in our problem. Used for loading the model.

  • best_value (float) – Best value of the model so far. Non-zero only in case of –resume being used

  • train_loader (torch.utils.data.dataloader.DataLoader) – Training dataloader

  • val_loader (torch.utils.data.dataloader.DataLoader) – Validation dataloader

  • test_loader (torch.utils.data.dataloader.DataLoader) – Test set dataloader

  • optimizer (torch.optim) – Optimizer to use during training, e.g. SGD

  • criterion (torch.nn.modules.loss) – Loss function to use, e.g. cross-entropy

classmethod single_run(**kwargs)[source]

This is the main routine where train(), validate() and test() are called.

Returns

  • train_value (ndarray[floats] of size (1, epochs)) – Accuracy values for train split

  • val_value (ndarray[floats] of size (1, `epochs`+1)) – Accuracy values for validation split

  • test_value (float) – Accuracy value for test split

classmethod test_routine(model_name, num_classes, criterion, epochs, current_log_folder, writer, **kwargs)[source]

Load the best model according to the validation score (early stopping) and runs the test routine.

Parameters
  • model_name (str) – name of the model. Used for loading the model.

  • num_classes (int) – How many different classes there are in our problem. Used for loading the model.

  • criterion (torch.nn.modules.loss) – Loss function to use, e.g. cross-entropy

  • epochs (int) – After how many epochs are we testing

  • current_log_folder (string) – Path to where logs/checkpoints are saved

  • writer (Tensorboard.SummaryWriter) – Responsible for writing logs in Tensorboard compatible format.

  • kwargs (dict) – Any additional arguments.

Returns

test_value – Accuracy value for test split

Return type

float

classmethod train_routine(best_value, decay_lr, validation_interval, start_epoch, epochs, checkpoint_all_epochs, current_log_folder, **kwargs)[source]

Performs the training and validatation routines

Parameters
  • best_value (float) – Best value of the model so far. Non-zero only in case of –resume being used

  • decay_lr (boolean) – Decay the lr flag

  • validation_interval (int) – Run evaluation on validation set every N epochs

  • start_epoch (int) – Int to initialize the starting epoch. Non-zero only in case of –resume being used

  • epochs (int) – Number of epochs to train

  • checkpoint_all_epochs (bool) – Save checkpoint at each epoch

  • current_log_folder (string) – Path to where logs/checkpoints are saved

  • kwargs (dict) – Any additional arguments.

Returns

  • train_value (ndarray[floats] of size (1, epochs)) – Accuracy values for train split

  • val_value (ndarray[floats] of size (1, `epochs`+1)) – Accuracy values for validation split

template.runner.image_classification.train module

template.runner.image_classification.train.train(train_loader, model, criterion, optimizer, writer, epoch, no_cuda=False, log_interval=25, **kwargs)[source]

Training routine

Parameters
  • train_loader (torch.utils.data.DataLoader) – The dataloader of the train set.

  • model (torch.nn.module) – The network model being used.

  • criterion (torch.nn.loss) – The loss function used to compute the loss of the model.

  • optimizer (torch.optim) – The optimizer used to perform the weight update.

  • writer (tensorboardX.writer.SummaryWriter) – The tensorboard writer object. Used to log values on file for the tensorboard visualization.

  • epoch (int) – Number of the epoch (for logging purposes).

  • no_cuda (boolean) – Specifies whether the GPU should be used or not. A value of ‘True’ means the CPU will be used.

  • log_interval (int) – Interval limiting the logging of mini-batches. Default value of 10.

Returns

top1.avg – Accuracy of the model of the evaluated split

Return type

float

template.runner.image_classification.train.train_one_mini_batch(model, criterion, optimizer, input, target, loss_meter, acc_meter)[source]

This routing train the model passed as parameter for one mini-batch

Parameters
  • model (torch.nn.module) – The network model being used.

  • criterion (torch.nn.loss) – The loss function used to compute the loss of the model.

  • optimizer (torch.optim) – The optimizer used to perform the weight update.

  • input (torch.autograd.Variable) – The input data for the mini-batch

  • target (torch.autograd.Variable) – The target data (labels) for the mini-batch

  • loss_meter (AverageMeter) – Tracker for the overall loss

  • acc_meter (AverageMeter) – Tracker for the overall accuracy

Returns

  • acc (float) – Accuracy for this mini-batch

  • loss (float) – Loss for this mini-batch

Module contents

class template.runner.image_classification.ImageClassification[source]

Bases: object

classmethod prepare(model_name, **kwargs)[source]

Loads and prepares the data, the optimizer and the criterion

Parameters
  • model_name (str) – Name of the model. Used for loading the model.

  • kwargs (dict) – Any additional arguments.

Returns

  • model (DataParallel) – The model to train

  • num_classes (int) – How many different classes there are in our problem. Used for loading the model.

  • best_value (float) – Best value of the model so far. Non-zero only in case of –resume being used

  • train_loader (torch.utils.data.dataloader.DataLoader) – Training dataloader

  • val_loader (torch.utils.data.dataloader.DataLoader) – Validation dataloader

  • test_loader (torch.utils.data.dataloader.DataLoader) – Test set dataloader

  • optimizer (torch.optim) – Optimizer to use during training, e.g. SGD

  • criterion (torch.nn.modules.loss) – Loss function to use, e.g. cross-entropy

classmethod single_run(**kwargs)[source]

This is the main routine where train(), validate() and test() are called.

Returns

  • train_value (ndarray[floats] of size (1, epochs)) – Accuracy values for train split

  • val_value (ndarray[floats] of size (1, `epochs`+1)) – Accuracy values for validation split

  • test_value (float) – Accuracy value for test split

classmethod test_routine(model_name, num_classes, criterion, epochs, current_log_folder, writer, **kwargs)[source]

Load the best model according to the validation score (early stopping) and runs the test routine.

Parameters
  • model_name (str) – name of the model. Used for loading the model.

  • num_classes (int) – How many different classes there are in our problem. Used for loading the model.

  • criterion (torch.nn.modules.loss) – Loss function to use, e.g. cross-entropy

  • epochs (int) – After how many epochs are we testing

  • current_log_folder (string) – Path to where logs/checkpoints are saved

  • writer (Tensorboard.SummaryWriter) – Responsible for writing logs in Tensorboard compatible format.

  • kwargs (dict) – Any additional arguments.

Returns

test_value – Accuracy value for test split

Return type

float

classmethod train_routine(best_value, decay_lr, validation_interval, start_epoch, epochs, checkpoint_all_epochs, current_log_folder, **kwargs)[source]

Performs the training and validatation routines

Parameters
  • best_value (float) – Best value of the model so far. Non-zero only in case of –resume being used

  • decay_lr (boolean) – Decay the lr flag

  • validation_interval (int) – Run evaluation on validation set every N epochs

  • start_epoch (int) – Int to initialize the starting epoch. Non-zero only in case of –resume being used

  • epochs (int) – Number of epochs to train

  • checkpoint_all_epochs (bool) – Save checkpoint at each epoch

  • current_log_folder (string) – Path to where logs/checkpoints are saved

  • kwargs (dict) – Any additional arguments.

Returns

  • train_value (ndarray[floats] of size (1, epochs)) – Accuracy values for train split

  • val_value (ndarray[floats] of size (1, `epochs`+1)) – Accuracy values for validation split