From ff42665854d9556b421c78e0c49236586f8cca51 Mon Sep 17 00:00:00 2001 From: Marcel <46164444+MSt-10@users.noreply.github.com> Date: Mon, 31 Mar 2025 16:16:28 +0200 Subject: [PATCH 1/2] Fixed error if no cells detected, return type more consistent - Return type of model.predict(...) was changed here: https://github.com/vanvalenlab/cellSAM/commit/73bd4e3c5c96935bb854c4910bc49471c56bb3b9#diff-f21d549921220b19fc1455312955b96866ecbf013502dc5e85974d1995559e1fR303, caused error if no cells were detected. - In case of no cells, returned values consistent (single channel uint8 mask, empty bbox list) --- cellSAM/model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cellSAM/model.py b/cellSAM/model.py index 23283be..95bbe9c 100644 --- a/cellSAM/model.py +++ b/cellSAM/model.py @@ -144,9 +144,9 @@ def segment_cellular_image( model, img = model.to(device), img.to(device) preds = model.predict(img, x=None, boxes_per_heatmap=bounding_boxes, device=device, fast=fast) - if preds is None: + if preds[0] is None: warn("No cells detected in the image.") - return np.zeros(img.shape[1:], dtype=np.int32), None, None + return np.zeros(img.shape[-2:], dtype=np.uint8), None, torch.empty((1, 4)) segmentation_predictions, _, x, bounding_boxes = preds From 3cd21a4a9fa1e98953c12717b109f74c470ee48d Mon Sep 17 00:00:00 2001 From: Marcel <46164444+MSt-10@users.noreply.github.com> Date: Wed, 2 Apr 2025 08:06:42 +0200 Subject: [PATCH 2/2] Fixed bug in postprocess_predictions parameter mask array (containing all masks) was overwritten with binary mask of first mask resulting in the removal of all other masks during postprocess --- cellSAM/model.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cellSAM/model.py b/cellSAM/model.py index 95bbe9c..617c001 100644 --- a/cellSAM/model.py +++ b/cellSAM/model.py @@ -160,12 +160,12 @@ def segment_cellular_image( return mask, x.cpu().numpy(), bounding_boxes -def postprocess_predictions(mask: np.ndarray): - mask_values = np.unique(mask) +def postprocess_predictions(masks: np.ndarray): + mask_values = np.unique(masks) new_masks = [] selem = disk(2) for mask_value in mask_values[1:]: - mask = mask == mask_value + mask = masks == mask_value mask, _ = remove_small_regions(mask, 20, mode="holes") mask, _ = remove_small_regions(mask, 20, mode="islands") opened_mask = binary_opening(mask, selem)