From cd9efdd409afaa8ede3240b34089de2e06516f64 Mon Sep 17 00:00:00 2001 From: Sharon Fitzpatrick Date: Fri, 17 Jan 2025 16:47:12 -0800 Subject: [PATCH] #263 add ability to save thresholds used for classification and segmentation models --- 5_zoo_workflow_local_model.py | 1 + 6_zoo_workflow_with_coregistration.py | 1 + 7_automatic_image_sorter.py | 2 +- src/coastseg/classifier.py | 25 ++++++++++++++----------- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/5_zoo_workflow_local_model.py b/5_zoo_workflow_local_model.py index 77526ad4..613434e9 100644 --- a/5_zoo_workflow_local_model.py +++ b/5_zoo_workflow_local_model.py @@ -29,6 +29,7 @@ "tta": False, # Test Time Augmentation "use_local_model": True, # Use local model (not one from zeneodo) "local_model_path": r"C:\development\doodleverse\coastseg\CoastSeg\src\coastseg\downloaded_models\non_validation_model", # path to the local model + "apply_segmentation_filter": True, # apply segmentation filter to the model outputs to sort them into good or bad } # Available models can run input "RGB" # or "MNDWI" or "NDWI" diff --git a/6_zoo_workflow_with_coregistration.py b/6_zoo_workflow_with_coregistration.py index 3caa570e..d84e6d1f 100644 --- a/6_zoo_workflow_with_coregistration.py +++ b/6_zoo_workflow_with_coregistration.py @@ -27,6 +27,7 @@ "model_type": "global_segformer_RGB_4class_14036903", # model name from the zoo "otsu": False, # Otsu Thresholding "tta": False, # Test Time Augmentation + "apply_segmentation_filter": True, # apply segmentation filter to the model outputs to sort them into good or bad } # Available models can run input "RGB" # or "MNDWI" or "NDWI" img_type = "RGB" # make sure the model name is compatible with the image type diff --git a/7_automatic_image_sorter.py b/7_automatic_image_sorter.py index 31c2a952..04302795 100644 --- a/7_automatic_image_sorter.py +++ b/7_automatic_image_sorter.py @@ -8,7 +8,7 @@ # It is meant be used on CoastSeg/data//jpg_files/preprocessed/RGB # select the RGB directory for an ROI from /data -input_directory =r"C:\development\doodleverse\coastseg\CoastSeg\data\ID_azb1_datetime11-01-24__02_43_46\jpg_files\preprocessed\RGB" +input_directory =r"C:\development\doodleverse\coastseg\CoastSeg\data\ID_wra5_datetime03-04-24__03_43_01\jpg_files\preprocessed\RGB" # run the classifier to automatically sort the images classifier.sort_images_with_model(input_directory,threshold=0.40) diff --git a/src/coastseg/classifier.py b/src/coastseg/classifier.py index 9d2da733..e0c53a45 100644 --- a/src/coastseg/classifier.py +++ b/src/coastseg/classifier.py @@ -1,6 +1,7 @@ import os import glob import pandas as pd +import numpy as np import shutil import pooch import tensorflow as tf @@ -12,6 +13,7 @@ # Some of these functions were originally written by Mark Lundine and have been modified for this project. + def filter_segmentations( session_path: str, threshold: float = 0.40, @@ -173,13 +175,13 @@ def run_inference_rgb_image_classifier(path_to_model_ckpt, path_to_model_ckpt (str): path to the saved keras model path_to_inference_imgs (str): path to the folder containing images to run the model on output_folder (str): path to save outputs to - csv_path (str): csv path to save results to. If not provided, the results will be saved to output_folder/classification_results.csv + csv_path (str): csv path to save results to. If not provided, the results will be saved to output_folder/image_classification_results.csv threshold (float): threshold on sigmoid of model output (ex: 0.6 means mark images as good if model output is >= 0.6, or 60% sure it's a good image) returns: csv_path (str): csv path of saved results """ if not csv_path: - csv_path = os.path.join(output_folder, 'classification_results.csv') + csv_path = os.path.join(output_folder, 'image_classification_results.csv') os.makedirs(output_folder,exist_ok=True) @@ -194,7 +196,6 @@ def run_inference_rgb_image_classifier(path_to_model_ckpt, im_classes = [None]*len(im_paths) i=0 for im_path in im_paths: - print(im_path) img = keras.utils.load_img(im_path, color_mode='rgb',target_size=image_size) img_array = keras.utils.img_to_array(img) img_array = tf.expand_dims(img_array, 0) @@ -204,7 +205,8 @@ def run_inference_rgb_image_classifier(path_to_model_ckpt, i=i+1 ##save results to a csv df = pd.DataFrame({'im_paths':im_paths, - 'model_scores':model_scores + 'model_scores':model_scores, + 'threshold':np.full(len(im_paths), threshold) } ) @@ -229,13 +231,13 @@ def run_inference_gray_image_classifier(path_to_model_ckpt, path_to_model_ckpt (str): path to the saved keras model path_to_inference_imgs (str): path to the folder containing images to run the model on output_folder (str): path to save outputs to - csv_path (str): csv path to save results to. If not provided, the results will be saved to output_folder/classification_results.csv + csv_path (str): csv path to save results to. If not provided, the results will be saved to output_folder/image_classification_results.csv threshold (float): threshold on sigmoid of model output (ex: 0.6 means mark images as good if model output is >= 0.6, or 60% sure it's a good image) returns: csv_path (str): csv path of saved results """ if not csv_path: - csv_path = os.path.join(output_folder, 'classification_results.csv') + csv_path = os.path.join(output_folder, 'image_classification_results.csv') os.makedirs(output_folder,exist_ok=True) image_size = (128, 128) @@ -258,7 +260,8 @@ def run_inference_gray_image_classifier(path_to_model_ckpt, i=i+1 ##save results to a csv df = pd.DataFrame({'im_paths':im_paths, - 'model_scores':model_scores + 'model_scores':model_scores, + 'threshold':np.full(len(im_paths), threshold) } ) df.to_csv(csv_path) @@ -349,7 +352,6 @@ def get_image_classifier(type:str='rgb') -> str: ) else: # get the grayscale model model_name ='ImageGrayClassifier' - print(model_name) model_directory = file_utilities.create_directory( downloaded_models_path, model_name ) @@ -429,7 +431,7 @@ def run_inference_segmentation_classifier(path_to_model_ckpt:str, path_to_inference_imgs (str): path to the folder containing images to run the model on output_folder (str): path to save outputs to csv_path (str): csv path to save results to - If not provided, the results will be saved to output_folder/classification_results.csv + If not provided, the results will be saved to output_folder/image_classification_results.csv threshold (float): threshold on sigmoid of model output (ex: 0.6 means mark images as good if model output is >= 0.6, or 60% sure it's a good image) returns: @@ -478,12 +480,13 @@ def run_inference_segmentation_classifier(path_to_model_ckpt:str, i=i+1 ##save results to a csv df = pd.DataFrame({'im_paths':im_paths, - 'model_scores':model_scores + 'model_scores':model_scores, + 'threshold':np.full(len(im_paths), threshold) } ) if not csv_path: - csv_path = os.path.join(output_folder, 'classification_results.csv') + csv_path = os.path.join(output_folder, 'segmentation_classification_results.csv') df.to_csv(csv_path) good_path,bad_path=sort_images(csv_path,