aigarmic.train ============== .. py:module:: aigarmic.train .. autoapi-nested-parse:: Functions and classes that allow for training neural network models for colony image classification. Classes ------- .. autoapisummary:: aigarmic.train.ValidationThresholdCallback Functions --------- .. autoapisummary:: aigarmic.train.create_dataset_from_directory aigarmic.train.train_binary aigarmic.train.train_softmax aigarmic.train.visualise_training Module Contents --------------- .. py:function:: 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] Create a training and validation dataset from a directory containing subdirectories for each class of image data. :param directory: path containing images, each in subdirectory corresponding to class :param label_mode: Labelling depending on model type ("binary" for binary or "int" for softmax) :param image_width: Image width in pixels :param image_height: Image height in pixels :param seed: Random seed for dataset splitting :param val_split: Proportion of data to use for validation :param batch_size: Batch size for datasets :return: Tuple containing training and validation datasets .. py:function:: 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] 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. :param annotations_path: Path to directory containing annotated images, with subdirectories for each class (usually '0' and '1') :param model_design: Keras model design (Sequential) to inform neural network architecture, excluding final layer :param val_split: Validation split proportion :param image_width: Image width (pixels) :param image_height: Image height (pixels) :param batch_size: Training batch size :param epochs: Max number of training epochs :param stop_training_threshold: Accuracy threshold at which to accept model and stop training :param learning_rate: Learning rate for the optimizer :return: Tuple containing trained model, class names, training history, and evaluation results .. py:function:: 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] 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. :param annotations_path: Path to directory containing annotated images, with subdirectories for each class (e.g., '0', '1', '2', ...) :param model_design: Keras model design (Sequential) to inform neural network architecture, excluding final layer :param val_split: Validation split proportion :param image_width: Image width (pixels) :param image_height: Image height (pixels) :param batch_size: Training batch size :param epochs: Max number of training epochs :param stop_training_threshold: Accuracy threshold at which to accept model and stop training :return: Tuple containing trained model, class names, training history, and evaluation results .. py:function:: visualise_training(history: keras.callbacks.History) -> None Visualise training and validation accuracy and loss over epochs. :param history: Training history object (usually returned by model.fit(), train_binary(), or train_softmax()) .. py:class:: ValidationThresholdCallback(threshold) Bases: :py:obj:`tensorflow.keras.callbacks.Callback` .. py:method:: on_epoch_end(epoch, logs=None) -> None Determines whether to stop training based on validation accuracy