multi_scale_expansion package

Submodules

multi_scale_expansion.classification module

multi_scale_expansion.classification.setup_classification_model(device, ds_path, model, transforms, mock_datasets=None, lr=0.001, momentum=0.9, step_size=7, gamma=0.1, epochs=15, testing=False)

Set up dataset and return trained model and info.

Parameters:
  • ds_path – Path containing ds with train and test dirs.

  • model – Model that we’d like to adapt for our classification purposes.

  • transforms – Transformations to apply on train and test data, depend on model.

  • lr – Learning rate.

  • momentum – Momentum hyperparameter.

  • step_size – Step size hyperparameter for scheduler.

  • gamma – Gamma hyperparameter.

  • num_epochs – Epochs to train on.

  • testing – Whether we are testing the function or running it normally.

Return model:

Trained model.

Return type:

torch.nn.Module

Return train/val_losses:

Per epoch train and val losses

Return type:

list()

Return train/val_accuracies:

Per epoch train and val accuracies

Return type:

list()

multi_scale_expansion.classification.train_model(device, dataset_sizes, dataloaders, model, criterion, optimizer, scheduler, num_epochs=25, testing=False)

Train and fine-tune previously auto-generated model.

Parameters:
  • device – To work allow PyTorch to work with GPU.

  • dataset_sizes – Stores the dataset sizes for the training and test data.

  • dataloaders – 2 dataloaders containing the training and testing data.

  • model – The model to train.

  • criterion – Loss function.

  • optimizer – Optimization algorithm choice.

  • scheduler – Modifies learning rate gradually with training.

  • num_epochs – Epochs to train on.

  • testing – Whether we are testing the function or running it normally.

Return model:

Trained model.

Return type:

torch.nn.Module

Return train/val_losses:

Per epoch train and val losses

Return type:

list()

Return train/val_accuracies:

Per epoch train and val accuracies

Return type:

list()

multi_scale_expansion.dataset module

class multi_scale_expansion.dataset.PlantDataset(images_dir, image_transform)

Bases: Dataset

Collects Generalizable Dataset from strict directory structure.

get_class_names(images_dir)

Given a directory of images, underneath which we have directories of class names, collect all these class names and return as a list of strings.

Parameters:

images_dir – Path to directory containing images.

Return class_names:

List of unique class names.

Return type:

list()

get_image_filenames_with_labels(images_dir, class_names, class_labels)

Get all image filenames for files containing proper images of our dataset.

Parameters:
  • images_dir – Path to directory containing images.

  • class_names – List of unique class names.

  • class_labels – Maps to idx of class name.

Type:

list()

Return image_files:

Literally it’s the image file paths.

Return type:

list()

Return labels:

Labels for each of the images.

Return type:

list()

multi_scale_expansion.dataset.collate_fn(batch)

Properly collates images into mini-batches for specific task and from a particular batch.

Parameters:

batch – batch of images and labels.

Return images:

Image after applying transform.

Return type:

torch.Tensor

Return labels:

LongTensor of labels.

Return type:

torch.LongTensor

multi_scale_expansion.dataset.get_dataloaders(image_datasets)

Now take the datasets and make them dataloaders with specificed params.

Parameters:

image_datasets – Actual torch train and test datasets.

Return dataloaders:

Dataloaders from Datasets.

Return type:

dict() containing DataLoaders.

multi_scale_expansion.dataset.get_datasets(data_dir, data_transforms)

Create train and test dataset into a dictionary.

Parameters:
  • data_dir – Directory path of data folder.

  • data_transforms – Transformations to apply to images.

Return images:

Image after applying transform.

Return type:

torch.Tensor

multi_scale_expansion.model module

multi_scale_expansion.model.get_plant_model(model, class_names)

Train and fine-tune previously auto-generated model.

Parameters:
  • model – The model to build on.

  • class_names – Classes for classification task.

Return model:

Model with additional classification layer.

Return type:

torch.nn.Module

multi_scale_expansion.model.get_train_loss_needs(model_ft, lr, momentum, step_size, gamma)

Train and fine-tune previously auto-generated model.

Parameters:
  • model_ft – The actual model.

  • lr – Learning rate.

  • momentum – Momentum parameter to be specified.

  • step_size – step_size

  • gamma – Gamma value to be specified for lr_scheduler.

Return criterion:

Trained model.

Return type:

function, nn.CrossEntropyLoss()

Return optimizer_ft:

The defined optimizer.

Return type:

torch.optim.SGD

Return exp_lr_scheduler:

Defined learning rate scheduler

Return type:

torch.optim.lr_scheduler.StepLR

Module contents