iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📘

[Image Analysis] Cell Segmentation from Micrographs with Cellpose Part 2: Batch Processing

に公開

In the previous article, I introduced how to use the Cellpose GUI and how to create custom models.
https://zenn.dev/rchiji/articles/92690f26968e9b#インストール

However, since the Cellpose GUI does not support batch processing, you cannot repeat the process on other images even if you have developed a versatile model.
In this article, I will introduce examples of applying Cellpose models to multiple images using Python command-line processing.

We will use the cellpose virtual environment created last time. Start the virtual environment from the Anaconda Prompt.

conda activate cellpose

There are two ways to proceed: processing directly from the command line or starting and running Jupyter Notebook/Lab.

【 Execution from the Command Line 】

https://cellpose.readthedocs.io/en/latest/command.html

Use the python -m cellpose command.
There are many arguments, but the minimum ones needed are listed below:

  • --dir:
    Specify the path to the folder containing the images.
  • --pretrained_model:
    Specify the model to be used for prediction. Provide the model name if using one provided by Cellpose, or the path to the model file if using one you created yourself.
  • --diameter:
    The pixel diameter of the structures/objects you want to detect. If nothing is specified, 30 is used. Specifying --diameter 0 will use the value saved in the model. If using a model provided by Cellpose, it might estimate it for each image??
  • --chan/--chan2:
    Channel numbers to use. The default is 0, assuming grayscale. Depending on the model, you may need to specify the cytoplasm and nuclei channels.
  • --savedir:
    The destination for saving predicted label images. If not specified, the results will be output to the folder specified by --dir. *Note: However, the Numpy data file for the predicted labels, "_seg.npy", will be saved in the --dir folder.
  • --save_png/--save_tif:
    Instructs whether to save the predicted label images as png or tif in addition to the "_seg.npy" file. The save destination is the one specified by --savedir. If not specified, they are saved in the --dir path.
  • --save_rois:
    A zip file containing ROI outline coordinate information. This is the same as what is obtained via File > Save outlines as .zip archive of ROI files for ImageJ in the Cellpose GUI and can be handled from the ImageJ ROI Manager.
  • --use_gpu:
    Instructs to use the GPU. Include this if a GPU-enabled Cellpose environment is available.
help
usage: __main__.py [-h] [--version] [--verbose] [--use_gpu] [--gpu_device GPU_DEVICE] [--check_mkl] [--dir DIR]
                   [--image_path IMAGE_PATH] [--look_one_level_down] [--img_filter IMG_FILTER]
                   [--channel_axis CHANNEL_AXIS] [--z_axis Z_AXIS] [--chan CHAN] [--chan2 CHAN2] [--invert]
                   [--all_channels] [--pretrained_model PRETRAINED_MODEL] [--add_model ADD_MODEL] [--unet]
                   [--nclasses NCLASSES] [--no_resample] [--net_avg] [--no_interp] [--no_norm] [--do_3D]
                   [--diameter DIAMETER] [--stitch_threshold STITCH_THRESHOLD] [--min_size MIN_SIZE] [--fast_mode]
                   [--flow_threshold FLOW_THRESHOLD] [--cellprob_threshold CELLPROB_THRESHOLD]
                   [--anisotropy ANISOTROPY] [--exclude_on_edges] [--augment] [--save_png] [--save_tif] [--no_npy]
                   [--savedir SAVEDIR] [--dir_above] [--in_folders] [--save_flows] [--save_outlines] [--save_rois]
                   [--save_ncolor] [--save_txt] [--train] [--train_size] [--test_dir TEST_DIR]
                   [--mask_filter MASK_FILTER] [--diam_mean DIAM_MEAN] [--learning_rate LEARNING_RATE]
                   [--weight_decay WEIGHT_DECAY] [--n_epochs N_EPOCHS] [--batch_size BATCH_SIZE]
                   [--min_train_masks MIN_TRAIN_MASKS] [--residual_on RESIDUAL_ON] [--style_on STYLE_ON]
                   [--concatenation CONCATENATION] [--save_every SAVE_EVERY] [--save_each]
                   [--model_name_out MODEL_NAME_OUT]

Cellpose Command Line Parameters

optional arguments:
  -h, --help            show this help message and exit
  --version             show cellpose version info
  --verbose             show information about running and settings and save to log

Hardware Arguments:
  --use_gpu             use gpu if torch with cuda installed
  --gpu_device GPU_DEVICE
                        which gpu device to use, use an integer for torch, or mps for M1
  --check_mkl           check if mkl working

Input Image Arguments:
  --dir DIR             folder containing data to run or train on.
  --image_path IMAGE_PATH
                        if given and --dir not given, run on single image instead of folder (cannot train with this
                        option)
  --look_one_level_down
                        run processing on all subdirectories of current folder
  --img_filter IMG_FILTER
                        end string for images to run on
  --channel_axis CHANNEL_AXIS
                        axis of image which corresponds to image channels
  --z_axis Z_AXIS       axis of image which corresponds to Z dimension
  --chan CHAN           channel to segment; 0: GRAY, 1: RED, 2: GREEN, 3: BLUE. Default: 0
  --chan2 CHAN2         nuclear channel (if cyto, optional); 0: NONE, 1: RED, 2: GREEN, 3: BLUE. Default: 0
  --invert              invert grayscale channel
  --all_channels        use all channels in image if using own model and images with special channels

Model Arguments:
  --pretrained_model PRETRAINED_MODEL
                        model to use for running or starting training
  --add_model ADD_MODEL
                        model path to copy model to hidden .cellpose folder for using in GUI/CLI
  --unet                run standard unet instead of cellpose flow output
  --nclasses NCLASSES   if running unet, choose 2 or 3; cellpose always uses 3

Algorithm Arguments:
  --no_resample         disable dynamics on full image (makes algorithm faster for images with large diameters)
  --net_avg             run 4 networks instead of 1 and average results
  --no_interp           do not interpolate when running dynamics (was default)
  --no_norm             do not normalize images (normalize=False)
  --do_3D               process images as 3D stacks of images (nplanes x nchan x Ly x Lx
  --diameter DIAMETER   cell diameter, if 0 will use the diameter of the training labels used in the model, or with
                        built-in model will estimate diameter for each image
  --stitch_threshold STITCH_THRESHOLD
                        compute masks in 2D then stitch together masks with IoU>0.9 across planes
  --min_size MIN_SIZE   minimum number of pixels per mask, can turn off with -1
  --fast_mode           now equivalent to --no_resample; make code run faster by turning off resampling
  --flow_threshold FLOW_THRESHOLD
                        flow error threshold, 0 turns off this optional QC step. Default: 0.4
  --cellprob_threshold CELLPROB_THRESHOLD
                        cellprob threshold, default is 0, decrease to find more and larger masks
  --anisotropy ANISOTROPY
                        anisotropy of volume in 3D
  --exclude_on_edges    discard masks which touch edges of image
  --augment             tiles image with overlapping tiles and flips overlapped regions to augment

Output Arguments:
  --save_png            save masks as png and outlines as text file for ImageJ
  --save_tif            save masks as tif and outlines as text file for ImageJ
  --no_npy              suppress saving of npy
  --savedir SAVEDIR     folder to which segmentation results will be saved (defaults to input image directory)
  --dir_above           save output folders adjacent to image folder instead of inside it (off by default)
  --in_folders          flag to save output in folders (off by default)
  --save_flows          whether or not to save RGB images of flows when masks are saved (disabled by default)
  --save_outlines       whether or not to save RGB outline images when masks are saved (disabled by default)
  --save_rois           whether or not to save ImageJ compatible ROI archive (disabled by default)
  --save_ncolor         whether or not to save minimal "n-color" masks (disabled by default
  --save_txt            flag to enable txt outlines for ImageJ (disabled by default)

Training Arguments:
  --train               train network using images in dir
  --train_size          train size network at end of training
  --test_dir TEST_DIR   folder containing test data (optional)
  --mask_filter MASK_FILTER
                        end string for masks to run on. use "_seg.npy" for manual annotations from the GUI. Default:
                        _masks
  --diam_mean DIAM_MEAN
                        mean diameter to resize cells to during training -- if starting from pretrained models it
                        cannot be changed from 30.0
  --learning_rate LEARNING_RATE
                        learning rate. Default: 0.2
  --weight_decay WEIGHT_DECAY
                        weight decay. Default: 1e-05
  --n_epochs N_EPOCHS   number of epochs. Default: 500
  --batch_size BATCH_SIZE
                        batch size. Default: 8
  --min_train_masks MIN_TRAIN_MASKS
                        minimum number of masks a training image must have to be used. Default: 5
  --residual_on RESIDUAL_ON
                        use residual connections
  --style_on STYLE_ON   use style vector
  --concatenation CONCATENATION
                        concatenate downsampled layers with upsampled layers (off by default which means they are
                        added)
  --save_every SAVE_EVERY
                        number of epochs to skip between saves. Default: 100
  --save_each           save the model under a different filename per --save_every epoch for later comparsion
  --model_name_out MODEL_NAME_OUT
                        Name of model to save as, defaults to name describing model architecture. Model is saved in
                        the folder specified by --dir in models subfolder.


1. Preparing the Model

Continuing from last time, we will use brightfield images from a phase-contrast microscope and use the custom-made model. In this article, it is assumed that the model is placed on the desktop as "models/model_filename".
If you use a pretrained model, specifying the model name will download the model file for you.


2. Preparing the Prediction Image Folder

First, prepare a folder containing the images you want to predict. For clarity, the folder is placed on the desktop.


3. Running the Prediction

Run the following command. Specify the path to the folder containing the images for prediction in the --dir argument, and the path to the model file in the --pretrained_model argument. (It seems that absolute paths are preferred.)

python -m cellpose --dir "C:\Users\Ryota_Chijimatsu\Desktop\Prediction" --pretrained_model C:\Users\Ryota_Chijimatsu\Desktop\models\CP_20230728_122230 --chan 0 --chan2 0 --diameter 0 --save_png --save_rois


Execution screen


4. Checking the Results

Once finished, you will get output like the following.

Files ending in "_cp_masks.png" are label images that, when opened in ImageJ, have different intensities for each detected cell.

Files ending in "_cp_output.png" show summary images of the predictions side-by-side.

"_rois.zip" files contain ROI information to be used in ImageJ. (Open the original image in ImageJ -> Analyze > Tools > ROI Manager -> Open the Roi zip file from More > Open in the ROI Manager.)

【 Execution from a Notebook 】

https://cellpose.readthedocs.io/en/latest/notebook.html
https://nbviewer.org/github/MouseLand/cellpose/blob/master/notebooks/run_cellpose.ipynb

This is also introduced in this book, so please refer to it.
https://zenn.dev/rchiji/books/201199bf94a67e/viewer/a2649c

First, start Jupyter Notebook or Jupyter Lab.

jupyter lab

1. Loading Libraries

The models module within cellpose is used for loading prediction models and executing predictions.
The io module is used for reading images for prediction and writing out prediction results.

from cellpose import models, io

2. Loading the Model

Load the model using the .CellposeModel() method. When using a model provided by cellpose, specify it like model_type='cyto'.
When using a custom model, specify the path to the model file using the pretrained_model= argument.

model_path = r"C:\Users\Ryota_Chijimatsu\Desktop\models\CP_20230728_122230"
model = models.CellposeModel(pretrained_model=model_path)

3. Loading Images for Prediction

Load the images to be used for prediction. We use cellpose's io.imread, but other tools can be used as long as they provide image data as a NumPy array.

For prediction, use either a single image or a list of multiple images.

For a single image
img_path = r"C:\Users\Ryota_Chijimatsu\Desktop\Prediction\BF_1_XY01_00001_CH1.tif"
img = io.imread(filename=img_path)
For multiple images
from glob import glob

# Create a list of image file paths
img_list = glob(r"C:\Users\Ryota_Chijimatsu\Desktop\Prediction\*.tif")

# Convert to a list of images
img_list = [ io.imread(img) for img in img_list]

4. Executing Predictions

Use the .eval() method. Arguments include a single image or a list of images, and parameters like diameter= and channels=.
Setting diameter=None allows the model to estimate the object diameter automatically.
In the channels= argument, specify a list such as [channel1_number, channel2_number]. The number 0 represents grayscale or None. This varies depending on the model, so you need to know which channels the model was trained on.



Let's execute the prediction and check its output.

For a single image
result1 = model.eval(x=img,
                    diameter=None,
                    channels=[0,0])
For a list of images
result2 = model.eval(x=img_list,
                    diameter=None,
                    channels=[0,0])


5. Checking the Results

The results are returned as multiple values.

Among them, the first return value is the predicted label image.

plt.imshow(result1[0])
plt.colorbar()

When predicting with an image list, the first return value is also a list of label images.

fig, axs = plt.subplots(1,3, figsize=(10,10))
axs[0].imshow(result2[0][0])
axs[1].imshow(result2[0][1])
axs[2].imshow(result2[0][2])

Summary of Prediction Results

If necessary, you can use the prediction result information to check a summary image as follows.

from cellpose import plot
import matplotlib.pyplot as plt # pip install matplotlib

fig = plt.figure(figsize=(12,5))
plot.show_segmentation(fig, img=img, maski=result1[0], flowi=result1[1][0])
plt.tight_layout()
plt.show()

6. Exporting the Results

Use the io.masks_flows_to_seg() function to save the "_seg.npy" file locally.

io.masks_flows_to_seg(images=img, masks=result1[0], flows=result1[1], diams=None, file_names="tmp")

Use the io.save_to_png() function to export the predicted label images.

io.save_to_png(images=img, masks=result1[0], flows=result1[1], file_names="label.png")


【 Cellpose in QuPath 】

https://github.com/BIOP/qupath-extension-cellpose

A Cellpose extension for the free image analysis software QuPath has been released.

Details are introduced in my book.
https://zenn.dev/rchiji/books/51fbb88213582d



Not only can you perform predictions on multiple images/regions using published Cellpose models, but you can also use models created with the Cellpose GUI in QuPath.

Furthermore, it is also possible to create prediction models within QuPath. QuPath has more annotation tools than the Cellpose GUI, making labeling easier.
Even for large data such as whole slide images, you can train models by labeling arbitrary areas within the image.

Example of ground truth labels



While grayscale images are used when applying Cellpose to brightfield images, you can also perform color deconvolution in QuPath to use Hematoxylin or DAB channels with Cellpose.

Discussion