Auxiliary Funcitons

Auxiliary functions

src.auxiliaries.create_test_img(size=(200, 200), num_points=100, radius_min=1, radius_max=10, random_seed=42)

Creates randomly distributed bright dots on black background

Parameters
  • size (tuple) – dimensions of test image

  • num_points (int) – number of bright spots

  • radius_min (int) – minimum radius for bright spots

  • radius_max (int) – maximum radius for bright spots

  • random_seed (int) – random seed

Returns

test_img (ndarray), center_list (list), radius_list (list)

src.auxiliaries.get_images(path)

Return all images in path as list of strings

Parameters

path (str) – path to image directory

Returns

list of images in path

src.auxiliaries.initialize_model(model_name, num_classes, feature_extract, use_pretrained=True)

Source: https://pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html Initialize these variables which will be set in this if statement. Each of these variables is model specific.

Parameters
  • model_name (str) – model to be loaded

  • num_classes (int) – number of classes

  • feature_extract (bool) – deactivate gradients

  • use_pretrained (bool) – load pretrained weights

Returns

pretrained model, input_size

src.auxiliaries.rgb2gray(rgb)

Converts RGB images to black and white

Parameters

rgb (ndarray) – RGB image - shape: (n, m, 3)

Returns

image - shape: (n, m, 1)

src.auxiliaries.run_t(model, device, test_loader, loss, secure_evaluation=False)

Test function for NNs

Parameters
  • model – PyTorch model child of torch.nn.Module

  • device (str) – device to run the model on

  • test_loader (torch.utils.data.DataLoader()) – Dataloader

  • loss – Loss function from torch.nn

src.auxiliaries.set_parameter_requires_grad(model, feature_extracting)

Function for model finetuning to freeze feature extractor and retrain last layers

Parameters
  • model – PyTorch model child of torch.nn.Module

  • feature_extracting (bool) – True disables gradients

src.auxiliaries.train(model, device, train_loader, optimizer, epoch, loss, federated=False, random_background=False)

Training function for NNs

Parameters
  • model (torch.nn.Module) – PyTorch model child of

  • device (str) – device to run the model on

  • train_loader (torch.utils.data.DataLoader) – Dataloader

  • optimizer (torch.optim) – Optimizer from

  • epoch (int) – number of epoches to train

  • loss – Loss function from

  • federated (bool) – federated training