micro_sam.v2.evaluation.evaluation
1import os 2from typing import Union, List 3 4import numpy as np 5import pandas as pd 6from tqdm import tqdm 7import imageio.v3 as imageio 8 9from elf.evaluation import mean_segmentation_accuracy 10 11from torch_em.util.segmentation import size_filter 12 13 14def run_evaluation_for_iterative_prompting( 15 labels: List[np.ndarray], 16 prediction_fnames: List[str], 17 prediction_dir: Union[os.PathLike, str], 18 experiment_folder: Union[os.PathLike, str], 19 start_with_box_prompt: bool, 20 n_iterations: int = 8, 21 min_size: int = 0, 22 use_masks: bool = False, 23): 24 """Run evaluation for predictions using iterative prompting for interactive segmentation. 25 26 The function computes the mean segmentation accuracy (mSA) and the segmentation accuracy at IoU 0.5 and 0.75 27 (SA50, SA75) for each iteration, averaged over all images. It saves the results as a csv file in 28 '<experiment_folder>/results/iterative_prompting_<with|without>_mask/start_with_<box|point>.csv'. 29 If this file exists, the function does nothing. 30 31 Args: 32 labels: The ground-truth instance segmentations, one per image. 33 prediction_fnames: The file names of the predictions without the '.tif' extension, in the same order 34 as `labels`. 35 prediction_dir: The folder of the predictions. It holds one subfolder per iteration: 'iteration00', 36 'iteration01' and so on, as `run_interactive_segmentation_2d` writes them. 37 experiment_folder: The folder where the results are saved. 38 start_with_box_prompt: Whether to start with box prompt for iterative prompting. 39 n_iterations: The number of iterations for iterative prompting. 40 min_size: The minimum pixel count to consider object for evaluation. 41 (NOTE: Similar filtering criterion is set for inference). 42 use_masks: Whether to use masks for iterative prompting. 43 """ 44 save_dir = os.path.join( 45 experiment_folder, "results", "iterative_prompting_" + ("with" if use_masks else "without") + "_mask" 46 ) 47 save_path = os.path.join(save_dir, "start_with_" + ("box.csv" if start_with_box_prompt else "point.csv")) 48 if os.path.exists(save_path): 49 print(f"The results are already stored at '{save_path}'.") 50 return 51 52 os.makedirs(save_dir, exist_ok=True) 53 54 results = [] 55 for i in tqdm(range(n_iterations)): 56 prediction_paths = [ 57 os.path.join(prediction_dir, f"iteration{i:02}", f"{fname}.tif") for fname in prediction_fnames 58 ] 59 res_per_iteration = [] 60 for prediction_path, _label in zip(prediction_paths, labels): 61 msa, sa = mean_segmentation_accuracy( 62 segmentation=imageio.imread(prediction_path), 63 groundtruth=size_filter(seg=_label, min_size=min_size), # NOTE: filter out small ground-truth labels, 64 return_accuracies=True 65 ) 66 res_per_iteration.append(pd.DataFrame.from_dict([{"mSA": msa, "SA50": sa[0], "SA75": sa[5]}])) 67 68 res_per_iteration = pd.concat(res_per_iteration) 69 results.append(pd.DataFrame(res_per_iteration.mean(axis=0)).transpose()) 70 71 results = pd.concat(results, ignore_index=True) 72 results.to_csv(save_path) 73 print(results)
def
run_evaluation_for_iterative_prompting( labels: List[numpy.ndarray], prediction_fnames: List[str], prediction_dir: Union[os.PathLike, str], experiment_folder: Union[os.PathLike, str], start_with_box_prompt: bool, n_iterations: int = 8, min_size: int = 0, use_masks: bool = False):
15def run_evaluation_for_iterative_prompting( 16 labels: List[np.ndarray], 17 prediction_fnames: List[str], 18 prediction_dir: Union[os.PathLike, str], 19 experiment_folder: Union[os.PathLike, str], 20 start_with_box_prompt: bool, 21 n_iterations: int = 8, 22 min_size: int = 0, 23 use_masks: bool = False, 24): 25 """Run evaluation for predictions using iterative prompting for interactive segmentation. 26 27 The function computes the mean segmentation accuracy (mSA) and the segmentation accuracy at IoU 0.5 and 0.75 28 (SA50, SA75) for each iteration, averaged over all images. It saves the results as a csv file in 29 '<experiment_folder>/results/iterative_prompting_<with|without>_mask/start_with_<box|point>.csv'. 30 If this file exists, the function does nothing. 31 32 Args: 33 labels: The ground-truth instance segmentations, one per image. 34 prediction_fnames: The file names of the predictions without the '.tif' extension, in the same order 35 as `labels`. 36 prediction_dir: The folder of the predictions. It holds one subfolder per iteration: 'iteration00', 37 'iteration01' and so on, as `run_interactive_segmentation_2d` writes them. 38 experiment_folder: The folder where the results are saved. 39 start_with_box_prompt: Whether to start with box prompt for iterative prompting. 40 n_iterations: The number of iterations for iterative prompting. 41 min_size: The minimum pixel count to consider object for evaluation. 42 (NOTE: Similar filtering criterion is set for inference). 43 use_masks: Whether to use masks for iterative prompting. 44 """ 45 save_dir = os.path.join( 46 experiment_folder, "results", "iterative_prompting_" + ("with" if use_masks else "without") + "_mask" 47 ) 48 save_path = os.path.join(save_dir, "start_with_" + ("box.csv" if start_with_box_prompt else "point.csv")) 49 if os.path.exists(save_path): 50 print(f"The results are already stored at '{save_path}'.") 51 return 52 53 os.makedirs(save_dir, exist_ok=True) 54 55 results = [] 56 for i in tqdm(range(n_iterations)): 57 prediction_paths = [ 58 os.path.join(prediction_dir, f"iteration{i:02}", f"{fname}.tif") for fname in prediction_fnames 59 ] 60 res_per_iteration = [] 61 for prediction_path, _label in zip(prediction_paths, labels): 62 msa, sa = mean_segmentation_accuracy( 63 segmentation=imageio.imread(prediction_path), 64 groundtruth=size_filter(seg=_label, min_size=min_size), # NOTE: filter out small ground-truth labels, 65 return_accuracies=True 66 ) 67 res_per_iteration.append(pd.DataFrame.from_dict([{"mSA": msa, "SA50": sa[0], "SA75": sa[5]}])) 68 69 res_per_iteration = pd.concat(res_per_iteration) 70 results.append(pd.DataFrame(res_per_iteration.mean(axis=0)).transpose()) 71 72 results = pd.concat(results, ignore_index=True) 73 results.to_csv(save_path) 74 print(results)
Run evaluation for predictions using iterative prompting for interactive segmentation.
The function computes the mean segmentation accuracy (mSA) and the segmentation accuracy at IoU 0.5 and 0.75
(SA50, SA75) for each iteration, averaged over all images. It saves the results as a csv file in
'
Arguments:
- labels: The ground-truth instance segmentations, one per image.
- prediction_fnames: The file names of the predictions without the '.tif' extension, in the same order
as
labels. - prediction_dir: The folder of the predictions. It holds one subfolder per iteration: 'iteration00',
'iteration01' and so on, as
run_interactive_segmentation_2dwrites them. - experiment_folder: The folder where the results are saved.
- start_with_box_prompt: Whether to start with box prompt for iterative prompting.
- n_iterations: The number of iterations for iterative prompting.
- min_size: The minimum pixel count to consider object for evaluation. (NOTE: Similar filtering criterion is set for inference).
- use_masks: Whether to use masks for iterative prompting.