aigarmic.file_handlers ====================== .. py:module:: aigarmic.file_handlers .. autoapi-nested-parse:: Functions to facilitate working with image data files Functions --------- .. autoapisummary:: aigarmic.file_handlers.convert_cv2_to_keras aigarmic.file_handlers.create_dataset_from_directory aigarmic.file_handlers.predict_colony_images_from_directory aigarmic.file_handlers.save_training_log aigarmic.file_handlers.get_concentration_from_path aigarmic.file_handlers.get_paths_from_directory Module Contents --------------- .. py:function:: convert_cv2_to_keras(image, size_x=160, size_y=160) -> numpy.ndarray Convert a cv2 image to a keras image :param image: Image loaded using cv2.imread :param size_x: Width to resize image to (pixels) :param size_y: Height to resize image to (pixels) :return: Image as a numpy array .. 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:: predict_colony_images_from_directory(directory: Optional[Union[str, pathlib.Path]], model: tensorflow.keras.models.Model, class_names: list[str], image_width: int, image_height: int, model_type: str, save_path: Optional[Union[str, pathlib.Path]] = None, binary_threshold: float = 0.5) -> list[dict] Predict the class of images in a directory using a trained model, and compare prediction to true class (based on subdirectory in which image is located, which should correspond to class name). :param directory: Directory containing images to predict :param model: Model to use for prediction :param class_names: List of class names :param image_width: Image width in pixels :param image_height: Image height in pixels :param model_type: "binary" or "softmax" :param save_path: Path to save prediction log :param binary_threshold: For binary models, threshold for classifying as positive :return: List of dictionaries containing image, path, prediction, predicted class, and true class (for each image) .. py:function:: save_training_log(model_history: keras.callbacks.History, save_path: Union[str, pathlib.Path]) -> None Save training log to CSV file :param model_history: Training history object :param save_path: Directory to save training log .. py:function:: get_concentration_from_path(path: Union[str, pathlib.Path]) -> float get concentration from plate image path, e.g. antibiotic1/0.125.jpg -> 0.125 :param path: Path to plate image :return: Concentration .. py:function:: get_paths_from_directory(path: Union[str, pathlib.Path]) -> dict[str, list[str]] Returns a dict of abx_names: [image1_path, image2_path, etc.] If there are no antibiotic subdirectories, "unnamed" is used for abx_names (length = 1) :param path: Path to directory containing antibiotic subdirectories :return: dict of abx_names: [image1_path, image2_path, etc.]