Skip to content

Commit 5e3fe2e

Browse files
cellpose training
1 parent 4808f10 commit 5e3fe2e

3 files changed

Lines changed: 68 additions & 3 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ models/denoising_5slices_n2v/original-denoised-difference.png
9494
We extract individual timepoints from the denoised TIFF stack (done manually or with Fiji).
9595
We then open selected frames in the **Cellpose GUI**, annotate them, and save masks as `_seg.npy`.
9696

97+
To annotate 3D images open cellpose GUI as:
98+
```
99+
python -m cellpose --Zstack
100+
```
101+
For more information on how to annotate images read [this docs](https://cellpose.readthedocs.io/en/latest/gui.html) and [this blog post](https://focalplane.biologists.com/2025/06/05/annotating-images-in-cellpose/).
102+
97103
This produces training pairs like:
98104

99105
```

scripts/saved-2d-to-cellpose.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import numpy as np
2+
import tifffile
3+
from pathlib import Path
4+
from shutil import rmtree
5+
import pandas as pd
6+
# import ace_tools as tools
7+
8+
# Define input/output paths
9+
input_dir = Path("D:/Data/Spheroids-Data-OCProject/Individual_Images/zprojection-denoised/")
10+
output_dir = Path("D:/Data/Spheroids-Data-OCProject/Individual_Images/zprojection-denoised-cellpose-training/")
11+
output_dir.mkdir(exist_ok=True)
12+
13+
# Clean old outputs if they exist
14+
if output_dir.exists():
15+
rmtree(output_dir)
16+
output_dir.mkdir()
17+
18+
# Find all .tif volumes
19+
tif_files = sorted(input_dir.glob("*.tif"))
20+
21+
# Process each volume and extract 2D labeled slices
22+
saved_files = []
23+
24+
for tif_file in tif_files:
25+
base_name = tif_file.stem
26+
seg_file = input_dir / f"{base_name}_seg.npy"
27+
28+
if not seg_file.exists():
29+
continue
30+
31+
volume = tifffile.imread(tif_file)
32+
mask_dict = np.load(seg_file, allow_pickle=True).item()
33+
mask = mask_dict["masks"]
34+
35+
for z in range(mask.shape[0]):
36+
if np.any(mask[z]):
37+
img_slice = volume[z]
38+
mask_slice = mask[z]
39+
40+
slice_name = f"{base_name}_z{z:02d}"
41+
img_out = output_dir / f"{slice_name}.tif"
42+
mask_out = output_dir / f"{slice_name}_seg.npy"
43+
44+
# Compose dictionary in Cellpose format
45+
dummy_flows = [np.zeros_like(mask_slice, dtype=np.float32) for _ in range(3)]
46+
dummy_outlines = np.zeros_like(mask_slice, dtype=np.uint8)
47+
48+
cellpose_mask = {
49+
"masks": mask_slice.astype(np.uint16),
50+
"flows": dummy_flows,
51+
"outlines": dummy_outlines,
52+
}
53+
54+
# Save image and corresponding full mask dictionary
55+
tifffile.imwrite(img_out, img_slice.astype(np.uint16))
56+
np.save(mask_out, cellpose_mask)
57+
58+
saved_files.append({"Slice Name": img_out.name, "Type": "Image"})
59+
saved_files.append({"Slice Name": mask_out.name, "Type": "Mask"})

scripts/train_cellpose.bat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ echo Starting Cellpose training...
33
call conda activate C:\Users\Caterina\anaconda3\envs\cellpose
44

55
cellpose --train ^
6-
--dir D:/Data/Spheroids-Data-OCProject/Individual_Images/2d-denoised/training/ ^
6+
--dir D:/Data/Spheroids-Data-OCProject/Individual_Images/zprojection-denoised-cellpose-training/ ^
77
--pretrained_model nuclei ^
88
--chan 0 --chan2 0 ^
99
--train_size ^
1010
--n_epochs 50 ^
1111
--batch_size 8 ^
1212
--learning_rate 0.2 ^
1313
--weight_decay 1e-5 ^
14-
--save_every 5 ^
15-
--model_name_out nuclei_custom ^
14+
--save_every 10 ^
15+
--model_name_out cellpose-zprojection-denoised ^
1616
--mask_filter _seg.npy ^
1717
--verbose
1818

0 commit comments

Comments
 (0)