aigarmic.file_handlers
Functions to facilitate working with image data files
Functions
|
Convert a cv2 image to a keras image |
Create a training and validation dataset from a directory containing subdirectories for each class of image data. |
|
|
Predict the class of images in a directory using a trained model, and compare prediction to |
|
Save training log to CSV file |
|
get concentration from plate image path, e.g. |
|
Returns a dict of abx_names: [image1_path, image2_path, etc.] |
Module Contents
- aigarmic.file_handlers.convert_cv2_to_keras(image, size_x=160, size_y=160) numpy.ndarray[source]
Convert a cv2 image to a keras image
- Parameters:
image – Image loaded using cv2.imread
size_x – Width to resize image to (pixels)
size_y – Height to resize image to (pixels)
- Returns:
Image as a numpy array
- aigarmic.file_handlers.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.file_handlers.predict_colony_images_from_directory(directory: str | pathlib.Path | None, model: tensorflow.keras.models.Model, class_names: list[str], image_width: int, image_height: int, model_type: str, save_path: str | pathlib.Path | None = None, binary_threshold: float = 0.5) list[dict][source]
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).
- Parameters:
directory – Directory containing images to predict
model – Model to use for prediction
class_names – List of class names
image_width – Image width in pixels
image_height – Image height in pixels
model_type – “binary” or “softmax”
save_path – Path to save prediction log
binary_threshold – For binary models, threshold for classifying as positive
- Returns:
List of dictionaries containing image, path, prediction, predicted class, and true class (for each image)
- aigarmic.file_handlers.save_training_log(model_history: keras.callbacks.History, save_path: str | pathlib.Path) None[source]
Save training log to CSV file
- Parameters:
model_history – Training history object
save_path – Directory to save training log
- aigarmic.file_handlers.get_concentration_from_path(path: str | pathlib.Path) float[source]
get concentration from plate image path, e.g. antibiotic1/0.125.jpg -> 0.125
- Parameters:
path – Path to plate image
- Returns:
Concentration
- aigarmic.file_handlers.get_paths_from_directory(path: str | pathlib.Path) dict[str, list[str]][source]
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)
- Parameters:
path – Path to directory containing antibiotic subdirectories
- Returns:
dict of abx_names: [image1_path, image2_path, etc.]