Skip to content

Commit 807603c

Browse files
committed
Get CellposeStarDist working on windows
1 parent fd958bb commit 807603c

File tree

1 file changed

+45
-6
lines changed

1 file changed

+45
-6
lines changed

src/main/resources/script_templates/PyImageJ/CellposeStarDistSegmentation.py

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
'''
44
Note that this script requires a Python environment that includes StarDist and Cellpose
55
StarDist currently only supports NumPy 1.x, which necessitates using TensorFlow 2.15 or earlier
6-
TensorFlow 2.15 itself requires python 3.11 or earlier
6+
TensorFlow 2.15 itself requires python 3.11 or earlier.
7+
8+
We also use cellpose 3.x because cellpose 4.x is heavily biased towards using their `cpsam`,
9+
"segment anything" model. This is a very cool model, but it is also huge and performance
10+
with a CPU is not great. It is also overkill for this example.
11+
12+
Using cellpose 3.x allows us to stick with the light and focused `ctyo` model for segmentation.
713
814
You can rebuild your Python environment by using:
915
Edit > Options > Python…
@@ -12,15 +18,48 @@
1218
1319
--Conda dependencies--
1420
python=3.11
15-
numpy=1.26.4
1621
1722
--Pip dependencies--
23+
numpy=1.26.4
24+
csbdeep==0.8.0
1825
tensorflow==2.15
19-
cellpose==4.0.6
26+
cellpose==3.1.1.1
2027
stardist==0.9.0
21-
csbdeep==0.8.0
2228
'''
2329

30+
# Although cellpose 3.x "recognizes" the `ctyo` model, all models must be downloaded
31+
# once before use (to USER_HOME/.cellpose/models).
32+
# Unfortunately the built-in logic for downloading models in cellpose is completely
33+
# tied to tqdm, which breaks when running in Fiji on Windows.
34+
# Attempts to disable tqdm with environment variables and "monkey patching" failed
35+
# Thus, the following code is necessary to download the cyto model if not already available.
36+
37+
import os
38+
from pathlib import Path
39+
import urllib.request
40+
41+
def ensure_cyto_model():
42+
"""Ensure the Cellpose 'cyto' model files exist in ~/.cellpose/models/."""
43+
model_dir = Path.home() / ".cellpose" / "models"
44+
model_dir.mkdir(parents=True, exist_ok=True)
45+
46+
files = {
47+
"cytotorch_0": "https://www.cellpose.org/models/cytotorch_0",
48+
"size_cytotorch_0.npy": "https://www.cellpose.org/models/size_cytotorch_0.npy"
49+
}
50+
51+
for filename, url in files.items():
52+
target = model_dir / filename
53+
if not target.exists():
54+
print(f"Downloading {filename}...")
55+
urllib.request.urlretrieve(url, target)
56+
print(f"{filename} downloaded to {target}")
57+
else:
58+
print(f"Skipping {filename} - already cached at {target}")
59+
60+
# Download and cache the cyto model
61+
ensure_cyto_model()
62+
2463
import sys
2564
import imagej.convert as convert
2665
import numpy as np
@@ -81,7 +120,7 @@ def get_bounding_box(indices: np.ndarray):
81120
nuc_labels, _ = model.predict_instances(normalize(xdata[:, :, 0]))
82121

83122
# run Cellpose on cytoplasm (grayscale)
84-
model = models.CellposeModel(gpu=False, model_type='cyto')
123+
model = models.Cellpose(gpu=False, model_type='cyto')
85124
ch = [0, 0]
86125
cyto_labels = model.eval(xdata[:, :, 1].data, channels=ch, diameter=72.1)
87126

@@ -136,4 +175,4 @@ def get_bounding_box(indices: np.ndarray):
136175

137176
#TODO this pops an unnecessary display at the end but if I don't make it the last line the ROIs don't show
138177
rm.moveRoisToOverlay(imp)
139-
rm.runCommand(imp, "Show All")
178+
rm.runCommand(imp, "Show All")

0 commit comments

Comments
 (0)