介绍使用 cellpose 快速分割图像中的细胞。
cellpose安装和使用
前面介绍了SAM用于图像中的物体分割。SAM因为训练数据集非常广,所以能够分割的对象非常多,通用性估计是目前AI模型中最好的。这里要介绍的 cellpose 则是在细胞图像分割中的明星模型,在这个细分专业领域中,cellpose 1 用于常规的细胞图像分割会更加方便,基本上是「拿来即用」。
安装cellpose
在中文媒体中,cellpose 的安装和使用教程已经蛮丰富了。cellpose 在 github 中开源,其仓库中对于安装使用也有非常详细的说明。参考之前介绍 napari 环境的安装,cellpose 也可以通过 conda 新建一个虚拟环境后再进行安装,这也是官方推荐的做法。
更新cellpose
事实上我很早之前就安装了 cellpose,看着它从1.0变到3.0。如果要更新的话,可以使用 python -m pip install cellpose --upgrade
。安装之后,激活虚拟环境,在命令行可以输入 cellpose
打开软件界面。如果嫌麻烦,也可以写一个 bat 脚本作为快捷方式:
conda activate cellpose && cellpose
cellpose分割模型权重
cellpose 提供了许多训练好的模型权重,国内直接下载网速较慢。为了方便,我把这些权重文件打包放在百度网盘(详见文末链接),下载后解压到 ~\.cellpose\models
目录下即可。~
代表的是用户目录,例如用户cat,其在 windows 系统中默认用户根目录是 C:\Users\cat\
。
cellpose分割细胞明场图像
然后把图片拖拽进去进行分割。注意分割参数中 diameter 还是比较重要的(对分割结果影响比较大),这个是代表了感兴趣物体的大小,可以点击 calibrate
来自动估计,也可以自己手动测量一下。然后到了 cellpose3版本,已经默认并推荐使用 cyto3 进行细胞分割,这个相对来说具有更好的通用性。
注意,cellpose 对于非英文字符支持不友好,所以安装路径和数据存放路径中都不要有非英文字符,否则容易报错。
可以看到分割的结果相当不错。如果分割结果有少量错误,可以自行修改。如果有比较多的错误,可以根据纠错的结果重新迁移学习,训练得到一个新的模型。修改 mask 和 train 的说明和一些快捷键,cellpose 软件中也有 Help 可以方便查看。为了方便,这里摘抄一份,详见本文末尾附录。
保存分割结果为ImageJ支持的ROI选区
如果分割结果不错,可以保存结果。默认是存为 npy 格式。也可以保存为单纯的mask位图(tif格式)或者是 ImageJ 的 RoiSet。可以看到 cellpose 发展到 3.0 版本,其输出形式对于后续使用其它分析工具的兼容性大大改善,这个非常棒。
这里我们选择导出为 ImageJ 的 RoiSet,会在输入图像相同的目录下保存一个带后缀 _rois.zip
的文件:
使用 ImageJ 打开看看:
非常棒,就是ROI的命名比较乱。
附录
cellpose用户界面操作
Main GUI mouse controls:
- Pan = left-click + drag
- Zoom = scroll wheel (or +/= and - buttons)
- Full view = double left-click
- Select mask = left-click on mask
- Delete mask = Ctrl (or COMMAND on Mac) + left-click
- Merge masks = Alt + left-click (will merge last two)
- Start draw mask = right-click
- End draw mask = right-click, or return to circle at beginning
Overlaps in masks are NOT allowed. If you draw a mask on top of another mask, it is cropped so that it doesn’t overlap with the old mask. Masks in 2D should be single strokes (single stroke is checked). If you want to draw masks in 3D (experimental), then you can turn this option off and draw a stroke on each plane with the cell and then press ENTER. 3D labelling will fill in planes that you have not labelled so that you do not have to as densely label.
!NOTE!: The GUI automatically saves after you draw a mask in 2D but NOT after 3D mask drawing and NOT after segmentation. Save in the file menu or with Ctrl+S. The output file is in the same folder as the loaded image with _seg.npy
appended.
Bulk Mask Deletion Clicking the 'delete multiple' button will allow you to select and delete multiple masks at once. Masks can be deselected by clicking on them again. Once you have selected all the masks you want to delete, click the 'done' button to delete them.
Alternatively, you can create a rectangular region to delete a regions of masks by clicking the 'delete multiple' button, and then moving and/or resizing the region to select the masks you want to delete. Once you have selected the masks you want to delete, click the 'done' button to delete them.
At any point in the process, you can click the 'cancel' button to cancel the bulk deletion.
FYI there are tooltips throughout the GUI (hover over text to see)
Keyboard shortcuts | Description |
---|---|
=/+ button // - button | zoom in // zoom out |
CTRL+Z | undo previously drawn mask/stroke |
CTRL+Y | undo remove mask |
CTRL+0 | clear all masks |
CTRL+L | load image (can alternatively drag and drop image) |
CTRL+S | SAVE MASKS IN IMAGE to _seg.npy file |
CTRL+T | train model using _seg.npy files in folder |
CTRL+P | load _seg.npy file (note: it will load automatically with image if it exists) |
CTRL+M | load masks file (must be same size as image with 0 for NO mask, and 1,2,3… for masks) |
CTRL+N | save masks as PNG |
CTRL+R | save ROIs to native ImageJ ROI format |
CTRL+F | save flows to image file |
A/D or LEFT/RIGHT | cycle through images in current directory |
W/S or UP/DOWN | change color (RGB/gray/red/green/blue) |
R / G / B | toggle between RGB and Red or Green or Blue |
PAGE-UP / PAGE-DOWN | change to flows and cell prob views (if segmentation computed) |
X | turn masks ON or OFF |
Z | toggle outlines ON or OFF |
, / . | increase / decrease brush size for drawing masks |
Segmentation options \ (2D only)
SIZE: you can manually enter the \ approximate diameter for your cells, or press “calibrate” to let the model estimate it. The size is \ represented by a disk at the bottom of the view window (can turn this disk of by unchecking \ “scale disk on”).
use GPU: if you have specially \ installed the cuda version of mxnet, then you can activate this, but it won’t give huge speedups when \ running single 2D images in the GUI.
MODEL: there is a _cytoplasm_ \ model and a _nuclei_ model, choose what you want to segment
CHAN TO SEG: this is the channel in \ which the cytoplasm or nuclei exist
CHAN2 (OPT): if _cytoplasm_ model \ is chosen, then choose the nuclear channel for this option
cellpose迁移学习训练
Check out this video to learn the process.
- Drag and drop an image from a folder of images with a similar style (like similar cell types).
- Run the built-in models on one of the images using the "model zoo" and find the one that works best for your data. Make sure that if you have a nuclear channel you have selected it for CHAN2.
- Fix the labelling by drawing new ROIs (right-click) and deleting incorrect ones (CTRL+click). The GUI autosaves any manual changes (but does not autosave after running the model, for that click CTRL+S). The segmentation is saved in a "_seg.npy" file.
- Go to the "Models" menu in the File bar at the top and click "Train new model..." or use shortcut CTRL+T.
- Choose the pretrained model to start the training from (the model you used in #2), and type in the model name that you want to use. The other parameters should work well in general for most data types. Then click OK.
- The model will train (much faster if you have a GPU) and then auto-run on the next image in the folder. Next you can repeat step 3 to step 5 as many times as is necessary.
- The trained model is available to use in the future in the GUI in the "custom model" section and is saved in your image folder.
cellpose 分割模型权重文件
通过百度网盘分享的文件:cellpose模型权重文件
链接:https://pan.baidu.com/s/1EQ3gfc-OodzyylNUM9iMvw
提取码:
- Cellpose 2.0: how to train your own model, Nature Methods, 2022. https://doi.org/10.1038/s41592-022-01663-4 ↩
此处评论已关闭