template.runner.triplet package

Submodules

template.runner.triplet.evaluate module

template.runner.triplet.evaluate.test(test_loader, model, writer, epoch, no_cuda=False, log_interval=20, **kwargs)[source]

Wrapper for _evaluate() with the intent to test the model

template.runner.triplet.evaluate.validate(val_loader, model, writer, epoch, no_cuda=False, log_interval=20, **kwargs)[source]

Wrapper for _evaluate() with the intent to validate the model.

template.runner.triplet.setup module

template.runner.triplet.setup.setup_dataloaders(model_expected_input_size, dataset_folder, n_triplets, batch_size, workers, inmem, only_evaluate=False, **kwargs)[source]

Set up the dataloaders for the specified datasets.

Parameters
  • model_expected_input_size (tuple) – Specify the height and width that the model expects.

  • dataset_folder (string) – Path string that points to the three folder train/val/test. Example: ~/../../data/svhn

  • n_triplets (int) – Number of triplets to generate for train/val/tes

  • batch_size (int) – Number of datapoints to process at once

  • workers (int) – Number of workers to use for the dataloaders

  • inmem (boolean) – Flag : if False, the dataset is loaded in an online fashion i.e. only file names are stored and images are loaded on demand. This is slower than storing everything in memory.

  • only_evaluate (boolean) – Flag : if True, only the test set is loaded.

Returns

  • train_loader (torch.utils.data.DataLoader)

  • val_loader (torch.utils.data.DataLoader)

  • test_loader (torch.utils.data.DataLoader) – Dataloaders for train, val and test.

template.runner.triplet.train module

template.runner.triplet.train.train(train_loader, model, criterion, optimizer, writer, epoch, no_cuda, 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

Placeholder 0. In the future this should become the FPR95

Return type

int

template.runner.triplet.transforms module

class template.runner.triplet.transforms.MultiCrop(size, n_crops)[source]

Bases: object

Crop the given PIL Image into multiple random crops

Parameters
  • size (tuple or int) – Desired output size of the crop. If size is an int instead of sequence like (h, w), a square crop of size (size, size) is made.

  • n_crops (int) – The number of crops to be generated from a page.

Returns

Return type

None

Example

>>> MultiCrop(size=model_expected_input_size, n_crops=multi_crop),
>>> transforms.Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])),
>>> transforms.Lambda(lambda items: torch.stack([transforms.Normalize(mean=mean, std=std)(item) for item in items]))
>>> transform = Compose([
>>> MultiCrop(size), # this is a list of PIL Images
>>> Lambda(lambda crops: torch.stack([ToTensor()(crop) for crop in crops])) # returns a 4D tensor
>>> ])
>>> #In your test loop you can do the following:
>>> input, target = batch # input is a 5d tensor, target is 2d
>>> bs, ncrops, c, h, w = input.size()
>>> result = model(input.view(-1, c, h, w)) # fuse batch size and ncrops
>>> result_avg = result.view(bs, ncrops, -1).mean(1) # avg over crops
template.runner.triplet.transforms.multi_crop(img, size, n_crops)[source]

Crop the given PIL Image into multiple random crops.

Parameters
  • img (PIL.Image) – The Image to be processed.

  • size (tuple or int) – Desired output size of the crop. If size is an int instead of sequence like (h, w), a square crop of size (size, size) is made.

  • n_crops (int) – The number of crops to be generated from a page.

Returns

crops – A list of PIL.Images which are the crops from the page.

Return type

list of PIL.Images

template.runner.triplet.triplet module

This file is the template for the boilerplate of train/test of a triplet network. This code has initially been adapted to our purposes from http://www.iis.ee.ic.ac.uk/%7Evbalnt/shallow_descr/TFeat_paper.pdf

class template.runner.triplet.triplet.Triplet[source]

Bases: object

static single_run(writer, current_log_folder, model_name, epochs, lr, decay_lr, margin, anchor_swap, validation_interval, regenerate_every, checkpoint_all_epochs, only_evaluate, **kwargs)[source]

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

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

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

  • model_name (string) – Name of the model

  • epochs (int) – Number of epochs to train

  • lr (float) – Value for learning rate

  • margin (float) – The margin value for the triplet loss function

  • anchor_swap (boolean) – Turns on anchor swap

  • decay_lr (boolean) – Decay the lr flag

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

  • regenerate_every (int) – Re-generate triplets every N epochs

  • checkpoint_all_epochs (bool) – If enabled, save checkpoint after every epoch.

  • only_evaluate (boolean) – Flag : if True, only the test set is loaded.

Returns

Mean Average Precision values for train and validation splits.

Return type

train_value, val_value, test_value

Module contents

class template.runner.triplet.Triplet[source]

Bases: object

static single_run(writer, current_log_folder, model_name, epochs, lr, decay_lr, margin, anchor_swap, validation_interval, regenerate_every, checkpoint_all_epochs, only_evaluate, **kwargs)[source]

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

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

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

  • model_name (string) – Name of the model

  • epochs (int) – Number of epochs to train

  • lr (float) – Value for learning rate

  • margin (float) – The margin value for the triplet loss function

  • anchor_swap (boolean) – Turns on anchor swap

  • decay_lr (boolean) – Decay the lr flag

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

  • regenerate_every (int) – Re-generate triplets every N epochs

  • checkpoint_all_epochs (bool) – If enabled, save checkpoint after every epoch.

  • only_evaluate (boolean) – Flag : if True, only the test set is loaded.

Returns

Mean Average Precision values for train and validation splits.

Return type

train_value, val_value, test_value