Skip to content

Commit 0a62171

Browse files
authored
Avoid segmentation if it is already cached (#802)
1 parent 4e88e1c commit 0a62171

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

micro_sam/evaluation/multi_dimensional_segmentation.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,12 @@ def segment_slices_from_ground_truth(
111111
# Create an empty volume to store incoming segmentations
112112
final_segmentation = np.zeros_like(ground_truth)
113113

114+
_segmentation_completed = False
115+
if save_path is not None and os.path.exists(save_path):
116+
_segmentation_completed = True # We avoid rerunning the segmentation if it is completed.
117+
114118
skipped_label_ids = []
115-
for label_id in label_ids:
119+
for label_id in tqdm(label_ids, desc="Segmenting per object in the volume"):
116120
# Binary label volume per instance (also referred to as object)
117121
this_seg = (ground_truth == label_id).astype("int")
118122

@@ -127,6 +131,9 @@ def segment_slices_from_ground_truth(
127131
skipped_label_ids.append(label_id)
128132
continue
129133

134+
if _segmentation_completed:
135+
continue
136+
130137
if verbose:
131138
print(f"The object with id {label_id} lies in slice range: {slice_range}")
132139

@@ -185,7 +192,10 @@ def segment_slices_from_ground_truth(
185192

186193
# Save the volumetric segmentation
187194
if save_path is not None:
188-
imageio.imwrite(save_path, final_segmentation, compression="zlib")
195+
if _segmentation_completed:
196+
final_segmentation = imageio.imread(save_path)
197+
else:
198+
imageio.imwrite(save_path, final_segmentation, compression="zlib")
189199

190200
# Evaluate the volumetric segmentation
191201
if skipped_label_ids:

0 commit comments

Comments
 (0)