aigarmic.train

Functions and classes that allow for training neural network models for colony image classification.

Classes

ValidationThresholdCallback

Functions

create_dataset_from_directory(...)

Create a training and validation dataset from a directory containing subdirectories for each class of image data.

train_binary(...)

Train a binary classification model to differentiate between two classes of colony images.

train_softmax(...)

Train a softmax classification model to differentiate between multiple classes (2 or more) of colony images.

visualise_training(→ None)

Visualise training and validation accuracy and loss over epochs.

Module Contents

aigarmic.train.create_dataset_from_directory(directory: str, label_mode: str, image_width: int, image_height: int, seed: int = 12345, val_split: float = 0.2, batch_size: int = 32) tuple[tensorflow.data.Dataset, tensorflow.data.Dataset][source]

Create a training and validation dataset from a directory containing subdirectories for each class of image data.

Parameters:
  • directory – path containing images, each in subdirectory corresponding to class

  • label_mode – Labelling depending on model type (“binary” for binary or “int” for softmax)

  • image_width – Image width in pixels

  • image_height – Image height in pixels

  • seed – Random seed for dataset splitting

  • val_split – Proportion of data to use for validation

  • batch_size – Batch size for datasets

Returns:

Tuple containing training and validation datasets

aigarmic.train.train_binary(annotations_path, model_design: tensorflow.keras.models.Sequential, val_split: float = 0.2, image_width: int = 160, image_height: int = 160, batch_size: int = 64, epochs: int = 300, stop_training_threshold: float = 0.98, learning_rate: float = 0.0001) tuple[tensorflow.keras.models.Sequential, list[str], keras.callbacks.History, list][source]

Train a binary classification model to differentiate between two classes of colony images. Provide a keras sequential model design to inform the neural network architecture. The final binary/sigmoid layer should not be included in the sequential model design, as this is added by the function.

Parameters:
  • annotations_path – Path to directory containing annotated images, with subdirectories for each class (usually ‘0’ and ‘1’)

  • model_design – Keras model design (Sequential) to inform neural network architecture, excluding final layer

  • val_split – Validation split proportion

  • image_width – Image width (pixels)

  • image_height – Image height (pixels)

  • batch_size – Training batch size

  • epochs – Max number of training epochs

  • stop_training_threshold – Accuracy threshold at which to accept model and stop training

  • learning_rate – Learning rate for the optimizer

Returns:

Tuple containing trained model, class names, training history, and evaluation results

aigarmic.train.train_softmax(annotations_path, model_design: tensorflow.keras.models.Sequential, val_split: float = 0.2, image_height: int = 160, image_width: int = 160, batch_size: int = 64, epochs: int = 300, stop_training_threshold: float = 0.98) tuple[tensorflow.keras.models.Sequential, list[str], keras.callbacks.History, list][source]

Train a softmax classification model to differentiate between multiple classes (2 or more) of colony images. The final softmax layer should not be included in the sequential model design, as this is added by the function.

Parameters:
  • annotations_path – Path to directory containing annotated images, with subdirectories for each class (e.g., ‘0’, ‘1’, ‘2’, …)

  • model_design – Keras model design (Sequential) to inform neural network architecture, excluding final layer

  • val_split – Validation split proportion

  • image_width – Image width (pixels)

  • image_height – Image height (pixels)

  • batch_size – Training batch size

  • epochs – Max number of training epochs

  • stop_training_threshold – Accuracy threshold at which to accept model and stop training

Returns:

Tuple containing trained model, class names, training history, and evaluation results

aigarmic.train.visualise_training(history: keras.callbacks.History) None[source]

Visualise training and validation accuracy and loss over epochs.

Parameters:

history – Training history object (usually returned by model.fit(), train_binary(), or train_softmax())

class aigarmic.train.ValidationThresholdCallback(threshold)[source]

Bases: tensorflow.keras.callbacks.Callback

on_epoch_end(epoch, logs=None) None[source]

Determines whether to stop training based on validation accuracy