Skip to content

Commit ead065e

Browse files
Merge pull request #1467 from MouseLand/small_utils_io_updates
small fixes for uint32, colab notebooks, cellprob return
2 parents 422ab5d + d74acfa commit ead065e

7 files changed

Lines changed: 45 additions & 28 deletions

File tree

cellpose/dynamics.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,10 @@ def compute_masks(dP, cellprob, p=None, niter=200, cellprob_threshold=0.0,
734734
mask = utils.fill_holes_and_remove_small_masks(mask, min_size=min_size)
735735

736736
if mask.dtype == np.uint32:
737-
dynamics_logger.warning(
737+
if mask.max() < 2**16:
738+
mask = mask.astype("uint16")
739+
else:
740+
dynamics_logger.warning(
738741
"more than 65535 masks in image, masks returned as np.uint32")
739742

740743
return mask

cellpose/io.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,3 +894,5 @@ def save_masks(images, masks, flows, file_names, png=True, tif=False, channels=[
894894
)
895895
#save full flow data
896896
imsave(os.path.join(flowdir, basename + '_dP' + suffix + '.tif'), flows[1])
897+
# save cellprob
898+
imsave(os.path.join(flowdir, basename + '_cellprob' + suffix + '.tif'), flows[2])

cellpose/train.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ def _reshape_norm(data, channel_axis=None, normalize_params={"normalize": False}
7272
# put channel axis first
7373
td = np.moveaxis(td, channel_axis0, 0)
7474
td = td[:3] # keep at most 3 channels
75-
if td.ndim == 2 or (td.ndim == 3 and td.shape[0] == 1):
75+
if td.ndim == 2:
7676
td = np.stack((td, 0*td, 0*td), axis=0)
7777
elif td.ndim == 3 and td.shape[0] < 3:
78-
td = np.concatenate((td, 0*td[:1]), axis=0)
78+
td = np.concatenate((td, np.zeros((3 - td.shape[0], *td.shape[1:]), dtype=td.dtype)), axis=0)
7979
data_new.append(td)
8080
data = data_new
8181
if normalize_params["normalize"]:

cellpose/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,12 @@ def masks_to_outlines(masks):
213213
return outlines
214214

215215

216-
def outlines_list(masks, multiprocessing_threshold=1000, multiprocessing=None):
216+
def outlines_list(masks, multiprocessing_threshold=50000, multiprocessing=None):
217217
"""Get outlines of masks as a list to loop over for plotting.
218218
219219
Args:
220220
masks (ndarray): Array of masks.
221-
multiprocessing_threshold (int, optional): Threshold for enabling multiprocessing. Defaults to 1000.
221+
multiprocessing_threshold (int, optional): Threshold for enabling multiprocessing. Defaults to 50000.
222222
multiprocessing (bool, optional): Flag to enable multiprocessing. Defaults to None.
223223
224224
Returns:
@@ -529,6 +529,8 @@ def stitch3D(masks, stitch_threshold=0.25):
529529
mmax = masks[0].max()
530530
empty = 0
531531
for i in trange(len(masks) - 1):
532+
if masks.dtype == "uint16" and int(mmax) > max(0, 2**16 - 5 - masks[i + 1].max()):
533+
masks = masks.astype("uint32")
532534
iou = metrics._intersection_over_union(masks[i + 1], masks[i])[1:, 1:]
533535
if not iou.size and empty == 0:
534536
masks[i + 1] = masks[i + 1]

notebooks/run_Cellpose-SAM.ipynb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@
101101
},
102102
"outputs": [],
103103
"source": [
104-
"!pip install git+https://www.github.com/mouseland/cellpose.git"
104+
"!uv pip install git+https://www.github.com/mouseland/cellpose.git\n",
105+
"!uv pip install git+https://github.com/facebookresearch/dinov3.git"
105106
]
106107
},
107108
{
@@ -135,7 +136,11 @@
135136
"if core.use_gpu()==False:\n",
136137
" raise ImportError(\"No GPU access, change your runtime\")\n",
137138
"\n",
138-
"model = models.CellposeModel(gpu=True)"
139+
"model = models.CellposeModel(gpu=True)\n",
140+
"\n",
141+
"### You can also use other pretrained models, like the DINO models \n",
142+
"# model = models.CellposeModel(gpu=True, pretrained_model=\"cpdino\")\n",
143+
"# model = models.CellposeModel(gpu=True, pretrained_model=\"cpdino-vitb\")"
139144
]
140145
},
141146
{
@@ -225,7 +230,7 @@
225230
"\n",
226231
"- If you have a histological image taken in brightfield, you don't need to adjust the channels.\n",
227232
"\n",
228-
"- If you have a fluroescent image with multiple stains, you should choose one channel with a cytoplasm/membrane stain, one channel with a nuclear stain, and set the third channel to `None`. Choosing multiple channels may produce segmentaiton of all the structures in the image. If you have retrained the model on your data with a thrid stain (described below), you can run segmentation with all channels. "
233+
"- If you have a fluroescent image with multiple stains, you should choose one channel with a cytoplasm/membrane stain, one channel with a nuclear stain, and set the third channel to `None`. Choosing multiple channels may produce segmentaiton of all the structures in the image. If you have retrained the model on your data with a third stain (described below), you can run segmentation with all channels. "
229234
]
230235
},
231236
{

notebooks/test_Cellpose-SAM.ipynb

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
{
44
"cell_type": "markdown",
55
"metadata": {
6-
"id": "view-in-github",
7-
"colab_type": "text"
6+
"colab_type": "text",
7+
"id": "view-in-github"
88
},
99
"source": [
1010
"<a href=\"https://colab.research.google.com/github/MouseLand/cellpose/blob/main/notebooks/test_Cellpose-SAM.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
@@ -54,7 +54,8 @@
5454
},
5555
"outputs": [],
5656
"source": [
57-
"!pip install git+https://www.github.com/mouseland/cellpose.git"
57+
"!uv pip install git+https://www.github.com/mouseland/cellpose.git\n",
58+
"!uv pip install git+https://github.com/facebookresearch/dinov3.git"
5859
]
5960
},
6061
{
@@ -87,7 +88,11 @@
8788
"if core.use_gpu()==False:\n",
8889
" raise ImportError(\"No GPU access, change your runtime\")\n",
8990
"\n",
90-
"model = models.CellposeModel(gpu=True)"
91+
"model = models.CellposeModel(gpu=True)\n",
92+
"\n",
93+
"### You can also use other pretrained models, like the DINO models \n",
94+
"# model = models.CellposeModel(gpu=True, pretrained_model=\"cpdino\")\n",
95+
"# model = models.CellposeModel(gpu=True, pretrained_model=\"cpdino-vitb\")"
9196
]
9297
},
9398
{
@@ -314,6 +319,11 @@
314319
},
315320
{
316321
"cell_type": "code",
322+
"execution_count": null,
323+
"metadata": {
324+
"id": "fd-6Hji-n9_H"
325+
},
326+
"outputs": [],
317327
"source": [
318328
"# DISPLAY RESULTS stitching\n",
319329
"plt.figure(figsize=(15,3))\n",
@@ -326,22 +336,17 @@
326336
" imgout[outX, outY] = np.array([255,75,75])\n",
327337
" plt.imshow(imgout)\n",
328338
" plt.title('iplane = %d'%iplane)"
329-
],
330-
"metadata": {
331-
"id": "fd-6Hji-n9_H"
332-
},
333-
"execution_count": null,
334-
"outputs": []
339+
]
335340
}
336341
],
337342
"metadata": {
338343
"accelerator": "GPU",
339344
"colab": {
340-
"provenance": [],
341-
"include_colab_link": true
345+
"include_colab_link": true,
346+
"provenance": []
342347
},
343348
"kernelspec": {
344-
"display_name": "cellpose",
349+
"display_name": "s2p_test",
345350
"language": "python",
346351
"name": "python3"
347352
},
@@ -355,9 +360,9 @@
355360
"name": "python",
356361
"nbconvert_exporter": "python",
357362
"pygments_lexer": "ipython3",
358-
"version": "3.11.9"
363+
"version": "3.11.14"
359364
}
360365
},
361366
"nbformat": 4,
362367
"nbformat_minor": 0
363-
}
368+
}

notebooks/train_Cellpose-SAM.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
{
44
"cell_type": "markdown",
55
"metadata": {
6-
"id": "view-in-github",
7-
"colab_type": "text"
6+
"colab_type": "text",
7+
"id": "view-in-github"
88
},
99
"source": [
1010
"<a href=\"https://colab.research.google.com/github/MouseLand/cellpose/blob/main/notebooks/train_Cellpose-SAM.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
@@ -113,7 +113,7 @@
113113
},
114114
"outputs": [],
115115
"source": [
116-
"!pip install git+https://www.github.com/mouseland/cellpose.git"
116+
"!uv pip install git+https://www.github.com/mouseland/cellpose.git"
117117
]
118118
},
119119
{
@@ -346,8 +346,8 @@
346346
"metadata": {
347347
"accelerator": "GPU",
348348
"colab": {
349-
"provenance": [],
350-
"include_colab_link": true
349+
"include_colab_link": true,
350+
"provenance": []
351351
},
352352
"kernelspec": {
353353
"display_name": "cellpose",

0 commit comments

Comments
 (0)