Skip to content

Commit

Permalink
Avoid segmentation if it is already cached (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
anwai98 authored Dec 14, 2024
1 parent 4e88e1c commit 0a62171
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions micro_sam/evaluation/multi_dimensional_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,12 @@ def segment_slices_from_ground_truth(
# Create an empty volume to store incoming segmentations
final_segmentation = np.zeros_like(ground_truth)

_segmentation_completed = False
if save_path is not None and os.path.exists(save_path):
_segmentation_completed = True # We avoid rerunning the segmentation if it is completed.

skipped_label_ids = []
for label_id in label_ids:
for label_id in tqdm(label_ids, desc="Segmenting per object in the volume"):
# Binary label volume per instance (also referred to as object)
this_seg = (ground_truth == label_id).astype("int")

Expand All @@ -127,6 +131,9 @@ def segment_slices_from_ground_truth(
skipped_label_ids.append(label_id)
continue

if _segmentation_completed:
continue

if verbose:
print(f"The object with id {label_id} lies in slice range: {slice_range}")

Expand Down Expand Up @@ -185,7 +192,10 @@ def segment_slices_from_ground_truth(

# Save the volumetric segmentation
if save_path is not None:
imageio.imwrite(save_path, final_segmentation, compression="zlib")
if _segmentation_completed:
final_segmentation = imageio.imread(save_path)
else:
imageio.imwrite(save_path, final_segmentation, compression="zlib")

# Evaluate the volumetric segmentation
if skipped_label_ids:
Expand Down

0 comments on commit 0a62171

Please sign in to comment.