Cell Segmentation#

This part shows how to use the function of cell segmentation. We provide two models here, deep cell model and the deep learning model developed by ourselves.

Preparation

Before Cell Segmentation, Tissue Segmentation, certain packages should be installed beforehand. Cell Segmentation version 3.0 does not need to install those python packages below, more details in Deep Learning Model.

Install necessary packages by running commands as below:

For CPU:

   pip install tensorflow==2.7.0
   pip install torch==1.10.0
   pip install torchvision==0.11.1
   pip install albumentations==0.4.6
   pip protobuf~=3.20.0

For GPU:

   pip install tensorflow==2.7.0
   pip install torch==1.10.0+cu113 –extra-index-url https://download.pytorch.org/whl/
   pip install torchvision==0.11.1+cu113 –extra-index-url https://download.pytorch.org/whl/
   pip install albumentations==0.4.6
   pip install onnxruntime-gpu
   pip protobuf~=3.20.0

About tissue segmentation#

All processes of cell segmentation will perform tissue segmentation beforehand. You could the path to tissue segmentation model file by tissue_seg_model_path.

Download the Tissue Segmentation Model.

More detail about Tissue Segmentation.

Deep learning model#

In order to keep compatibility, we support two versions of cell_seg methods in stereo.image module.

Deep Learning Model V3#

New V3 model is upload to our demo named cell_segmetation_v3.0.onnx. V3 method don’t need to install heavy packages like tensorflow and pytorch, just import cell_seg_v3 from stereo.image, ignoring the warning.

Note

If you don’t need to perform tissue segmentation, you can set the need_tissue_cut parameter to False in the cell_seg_v3 function.

[1]:
from stereo import image as im

model = '../data/seg_models/Demo_CellSegmentation/Deep_Learning_Model_v03/cell_segmetation_v3.0.onnx'
image = '../data/SS200000135TL_D1_regist.tif'
output = './cell_seg_result_v3'
tissue_seg_model = '../data/seg_models/tissueseg_bcdu_SDI_230523_tf.onnx'

im.cell_seg(
    model_path=model,
    img_path=image,
    out_path=output,
    method='v3',
    tissue_seg_model_path=tissue_seg_model,
    tissue_seg_staining_type='ssDNA',
    gpu='0' # set to -1 to run on cpu
)
/data/users/tanliwei/conda/envs/stereopy/lib/python3.8/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm
[INFO 20240322-15-22-59 p41436 __init__ tissue_segmentation.py:33] start loading model weight
INFO:cellbin.utils.clog:start loading model weight
[INFO 20240322-15-22-59 p41436 _f_load_model onnx_net.py:29] loading weight from ../data/seg_models/tissueseg_bcdu_SDI_230523_tf.onnx
INFO:cellbin.utils.clog:loading weight from ../data/seg_models/tissueseg_bcdu_SDI_230523_tf.onnx
[INFO 20240322-15-23-08 p41436 _f_load_model onnx_net.py:40] onnx work on gpu 0
2024-03-22 15:23:07.530111290 [W:onnxruntime:, session_state.cc:1162 VerifyEachNodeIsAssignedToAnEp] Some nodes were not assigned to the preferred execution providers which may or may not have an negative impact on performance. e.g. ORT explicitly assigns shape related ops to CPU to improve perf.
2024-03-22 15:23:07.530138976 [W:onnxruntime:, session_state.cc:1164 VerifyEachNodeIsAssignedToAnEp] Rerunning with verbose output on a non-minimal build will show node assignments.
INFO:cellbin.utils.clog:onnx work on gpu 0
[INFO 20240322-15-23-08 p41436 __init__ tissue_segmentation.py:35] end loading model weight
INFO:cellbin.utils.clog:end loading model weight
[INFO 20240322-15-23-10 p41436 run tissue_segmentation.py:46] start tissue seg
INFO:cellbin.utils.clog:start tissue seg
[INFO 20240322-15-23-23 p41436 run tissue_segmentation.py:48] end tissue seg
INFO:cellbin.utils.clog:end tissue seg
[2024-03-22 15:23:24][Stereo][41436][MainThread][140580916746048][cell_seg_pipeline][64][INFO]: Transform 16bit to 8bit : 0.00
[2024-03-22 15:23:24][Stereo][41436][MainThread][140580916746048][cell_seg_pipeline_v3][23][INFO]: Start do cell mask, the method is v3, this will take some minutes.
[INFO 20240322-15-23-24 p41436 __init__ cell_segmentation.py:38] start loading model weight
INFO:cellbin.utils.clog:start loading model weight
[INFO 20240322-15-23-25 p41436 _f_load_model onnx_net.py:29] loading weight from ../data/seg_models/Demo_CellSegmentation/Deep_Learning_Model_v03/cell_segmetation_v3.0.onnx
INFO:cellbin.utils.clog:loading weight from ../data/seg_models/Demo_CellSegmentation/Deep_Learning_Model_v03/cell_segmetation_v3.0.onnx
[INFO 20240322-15-23-26 p41436 _f_load_model onnx_net.py:40] onnx work on gpu 0
2024-03-22 15:23:26.164712908 [W:onnxruntime:, session_state.cc:1162 VerifyEachNodeIsAssignedToAnEp] Some nodes were not assigned to the preferred execution providers which may or may not have an negative impact on performance. e.g. ORT explicitly assigns shape related ops to CPU to improve perf.
2024-03-22 15:23:26.164731167 [W:onnxruntime:, session_state.cc:1164 VerifyEachNodeIsAssignedToAnEp] Rerunning with verbose output on a non-minimal build will show node assignments.
INFO:cellbin.utils.clog:onnx work on gpu 0
[INFO 20240322-15-23-26 p41436 __init__ cell_segmentation.py:40] end loading model weight
INFO:cellbin.utils.clog:end loading model weight
[2024-03-22 15:23:26][Stereo][41436][MainThread][140580916746048][cell_seg_pipeline_v3][33][INFO]: Load ../data/seg_models/Demo_CellSegmentation/Deep_Learning_Model_v03/cell_segmetation_v3.0.onnx) finished.
[INFO 20240322-15-23-27 p41436 run cell_segmentation.py:51] start cell segmentation
INFO:cellbin.utils.clog:start cell segmentation
[INFO 20240322-15-23-31 p41436 flush clog.py:115] 0%|          | 0/124 [00:00<?, ?it/s]
INFO:cellbin.utils.clog:0%|          | 0/124 [00:00<?, ?it/s]
[INFO 20240322-15-23-42 p41436 flush clog.py:115] 3%|3         | 4/124 [00:10<05:04,  2.54s/it]
INFO:cellbin.utils.clog:3%|3         | 4/124 [00:10<05:04,  2.54s/it]
[INFO 20240322-15-23-52 p41436 flush clog.py:115] 9%|8         | 11/124 [00:20<03:22,  1.79s/it]
INFO:cellbin.utils.clog:9%|8         | 11/124 [00:20<03:22,  1.79s/it]
[INFO 20240322-15-24-03 p41436 flush clog.py:115] 15%|#5        | 19/124 [00:32<02:47,  1.60s/it]
INFO:cellbin.utils.clog:15%|#5        | 19/124 [00:32<02:47,  1.60s/it]
[INFO 20240322-15-24-15 p41436 flush clog.py:115] 22%|##1       | 27/124 [00:43<02:28,  1.53s/it]
INFO:cellbin.utils.clog:22%|##1       | 27/124 [00:43<02:28,  1.53s/it]
[INFO 20240322-15-24-25 p41436 flush clog.py:115] 27%|##7       | 34/124 [00:54<02:16,  1.52s/it]
INFO:cellbin.utils.clog:27%|##7       | 34/124 [00:54<02:16,  1.52s/it]
[INFO 20240322-15-24-37 p41436 flush clog.py:115] 33%|###3      | 41/124 [01:05<02:08,  1.55s/it]
INFO:cellbin.utils.clog:33%|###3      | 41/124 [01:05<02:08,  1.55s/it]
[INFO 20240322-15-24-48 p41436 flush clog.py:115] 39%|###8      | 48/124 [01:17<02:00,  1.59s/it]
INFO:cellbin.utils.clog:39%|###8      | 48/124 [01:17<02:00,  1.59s/it]
[INFO 20240322-15-25-00 p41436 flush clog.py:115] 44%|####4     | 55/124 [01:28<01:52,  1.62s/it]
INFO:cellbin.utils.clog:44%|####4     | 55/124 [01:28<01:52,  1.62s/it]
[INFO 20240322-15-25-11 p41436 flush clog.py:115] 44%|####4     | 55/124 [01:40<01:52,  1.62s/it]
INFO:cellbin.utils.clog:44%|####4     | 55/124 [01:40<01:52,  1.62s/it]
[INFO 20240322-15-25-12 p41436 flush clog.py:115] 50%|#####     | 62/124 [01:40<01:41,  1.64s/it]
INFO:cellbin.utils.clog:50%|#####     | 62/124 [01:40<01:41,  1.64s/it]
[INFO 20240322-15-25-24 p41436 flush clog.py:115] 56%|#####5    | 69/124 [01:52<01:30,  1.65s/it]
INFO:cellbin.utils.clog:56%|#####5    | 69/124 [01:52<01:30,  1.65s/it]
[INFO 20240322-15-25-34 p41436 flush clog.py:115] 60%|######    | 75/124 [02:02<01:21,  1.66s/it]
INFO:cellbin.utils.clog:60%|######    | 75/124 [02:02<01:21,  1.66s/it]
[INFO 20240322-15-25-44 p41436 flush clog.py:115] 66%|######6   | 82/124 [02:12<01:07,  1.61s/it]
INFO:cellbin.utils.clog:66%|######6   | 82/124 [02:12<01:07,  1.61s/it]
[INFO 20240322-15-25-55 p41436 flush clog.py:115] 72%|#######1  | 89/124 [02:23<00:55,  1.57s/it]
INFO:cellbin.utils.clog:72%|#######1  | 89/124 [02:23<00:55,  1.57s/it]
[INFO 20240322-15-26-05 p41436 flush clog.py:115] 77%|#######7  | 96/124 [02:33<00:42,  1.53s/it]
INFO:cellbin.utils.clog:77%|#######7  | 96/124 [02:33<00:42,  1.53s/it]
[INFO 20240322-15-26-15 p41436 flush clog.py:115] 83%|########3 | 103/124 [02:43<00:31,  1.50s/it]
INFO:cellbin.utils.clog:83%|########3 | 103/124 [02:43<00:31,  1.50s/it]
[INFO 20240322-15-26-25 p41436 flush clog.py:115] 89%|########8 | 110/124 [02:53<00:20,  1.49s/it]
INFO:cellbin.utils.clog:89%|########8 | 110/124 [02:53<00:20,  1.49s/it]
[INFO 20240322-15-26-35 p41436 flush clog.py:115] 94%|#########4| 117/124 [03:03<00:10,  1.47s/it]
INFO:cellbin.utils.clog:94%|#########4| 117/124 [03:03<00:10,  1.47s/it]
[INFO 20240322-15-26-44 p41436 flush clog.py:115] 100%|##########| 124/124 [03:12<00:00,  1.55s/it]
INFO:cellbin.utils.clog:100%|##########| 124/124 [03:12<00:00,  1.55s/it]
[INFO 20240322-15-26-45 p41436 run cell_segmentation.py:53] end cell segmentation
INFO:cellbin.utils.clog:end cell segmentation
[2024-03-22 15:26:50][Stereo][41436][MainThread][140580916746048][cell_seg_pipeline_v3][63][INFO]: Result saved : ./cell_seg_result_v3/SS200000135TL_D1_regist_mask.tif

Deep Learning Model V1#

First, please download the deep learning model V1. It is assumed that the local storage path is '../data/seg_models/Demo_CellSegmentation/Deep_Learning_Model_v01/cell_segmetation_v1.0.pth'.

Besides, if you want to use GPU for prediction, set GPU id to the parameter gpu, if not, CPU will be used by default.

[2]:
from stereo import image as im

model = '../data/seg_models/Demo_CellSegmentation/Deep_Learning_Model_v01/cell_segmetation_v1.0.pth'
image = '../data/SS200000135TL_D1_regist.tif'
output = './cell_seg_result_v1'
tissue_seg_model = '../data/seg_models/tissueseg_bcdu_SDI_230523_tf.onnx'

im.cell_seg(
    model_path=model,
    img_path=image,
    out_path=output,
    method='v1',
    tissue_seg_model_path=tissue_seg_model,
    tissue_seg_staining_type='ssDNA',
    gpu='0' # set to -1 to run on cpu
)
[INFO 20240322-15-44-55 p41436 __init__ tissue_segmentation.py:33] start loading model weight
INFO:cellbin.utils.clog:start loading model weight
[INFO 20240322-15-44-55 p41436 _f_load_model onnx_net.py:29] loading weight from ../data/seg_models/tissueseg_bcdu_SDI_230523_tf.onnx
INFO:cellbin.utils.clog:loading weight from ../data/seg_models/tissueseg_bcdu_SDI_230523_tf.onnx
[INFO 20240322-15-44-56 p41436 _f_load_model onnx_net.py:40] onnx work on gpu 0
2024-03-22 15:44:56.074384182 [W:onnxruntime:, session_state.cc:1162 VerifyEachNodeIsAssignedToAnEp] Some nodes were not assigned to the preferred execution providers which may or may not have an negative impact on performance. e.g. ORT explicitly assigns shape related ops to CPU to improve perf.
2024-03-22 15:44:56.074406769 [W:onnxruntime:, session_state.cc:1164 VerifyEachNodeIsAssignedToAnEp] Rerunning with verbose output on a non-minimal build will show node assignments.
INFO:cellbin.utils.clog:onnx work on gpu 0
[INFO 20240322-15-44-56 p41436 __init__ tissue_segmentation.py:35] end loading model weight
INFO:cellbin.utils.clog:end loading model weight
[INFO 20240322-15-44-56 p41436 run tissue_segmentation.py:46] start tissue seg
INFO:cellbin.utils.clog:start tissue seg
[INFO 20240322-15-45-08 p41436 run tissue_segmentation.py:48] end tissue seg
INFO:cellbin.utils.clog:end tissue seg
[2024-03-22 15:45:09][Stereo][41436][MainThread][140580916746048][cell_seg_pipeline][64][INFO]: Transform 16bit to 8bit : 0.00
using device: cuda:0
【image 1/2】
100%|█████████████████████████████████████████| 177/177 [00:36<00:00,  4.91it/s]
【image 2/2】
100%|█████████████████████████████████████████████| 1/1 [00:00<00:00, 25.95it/s]
[2024-03-22 15:46:06][Stereo][41436][MainThread][140580916746048][cell_seg_pipeline_v1][117][INFO]: Cell inference : 39.13
[2024-03-22 15:46:07][Stereo][41436][MainThread][140580916746048][cell_seg_pipeline_v1][122][INFO]: Filter by tissue mask : 0.24
[2024-03-22 15:46:07][Stereo][41436][MainThread][140580916746048][cell_seg_pipeline_v1][127][INFO]: Mosaic tissue roi : 0.09
[2024-03-22 15:47:46][Stereo][41436][MainThread][140580916746048][cell_seg_pipeline_v1][132][INFO]: Post-processing : 99.00
[2024-03-22 15:48:21][Stereo][41436][MainThread][140580916746048][cell_seg_pipeline_v1][135][INFO]: Result saved : ./cell_seg_result_v1

Deep Learning Model V1_Pro#

Model v1_pro algorithm is optimized on the basis of v1.

Besides, if you want to use GPU for prediction, set GPU id to the parameter gpu, if not, CPU will be used by default.

[2]:
from stereo import image as im

model = '../data/seg_models/Demo_CellSegmentation/Deep_Learning_Model_v01_pro/cell_segmetation_v1.0_pro.pth'
image = '../data/SS200000135TL_D1_regist.tif'
output = './cell_seg_result_v1_pro'
tissue_seg_model = '../data/seg_models/tissueseg_bcdu_SDI_230523_tf.onnx'

im.cell_seg(
    model_path=model,
    img_path=image,
    out_path=output,
    method='v1_pro',
    tissue_seg_model_path=tissue_seg_model,
    tissue_seg_staining_type='ssDNA',
    gpu='0' # set to -1 to run on cpu
)
[INFO 20240321-18-10-28 p158802 __init__ tissue_segmentation.py:33] start loading model weight
INFO:cellbin.utils.clog:start loading model weight
[INFO 20240321-18-10-28 p158802 _f_load_model onnx_net.py:29] loading weight from ../data/seg_models/tissueseg_bcdu_SDI_230523_tf.onnx
INFO:cellbin.utils.clog:loading weight from ../data/seg_models/tissueseg_bcdu_SDI_230523_tf.onnx
[INFO 20240321-18-10-28 p158802 _f_load_model onnx_net.py:40] onnx work on gpu 0
2024-03-21 18:10:28.860106121 [W:onnxruntime:, session_state.cc:1162 VerifyEachNodeIsAssignedToAnEp] Some nodes were not assigned to the preferred execution providers which may or may not have an negative impact on performance. e.g. ORT explicitly assigns shape related ops to CPU to improve perf.
2024-03-21 18:10:28.860126462 [W:onnxruntime:, session_state.cc:1164 VerifyEachNodeIsAssignedToAnEp] Rerunning with verbose output on a non-minimal build will show node assignments.
INFO:cellbin.utils.clog:onnx work on gpu 0
[INFO 20240321-18-10-28 p158802 __init__ tissue_segmentation.py:35] end loading model weight
INFO:cellbin.utils.clog:end loading model weight
[INFO 20240321-18-10-29 p158802 run tissue_segmentation.py:46] start tissue seg
INFO:cellbin.utils.clog:start tissue seg
[INFO 20240321-18-10-41 p158802 run tissue_segmentation.py:48] end tissue seg
INFO:cellbin.utils.clog:end tissue seg
[2024-03-21 18:10:42][Stereo][158802][MainThread][140406620477248][cell_seg_pipeline][64][INFO]: Transform 16bit to 8bit : 0.00
[2024-03-21 18:10:55][Stereo][158802][MainThread][140406620477248][cell_seg_pipeline_v1_pro][50][INFO]: Start do cell mask, the method is v1_pro, this will take some minutes.
[2024-03-21 18:10:56][Stereo][158802][MainThread][140406620477248][cell_infer][43][INFO]: CellCut_model infer path ../data/seg_models/Demo_CellSegmentation/Deep_Learning_Model_v01_pro/cell_segmetation_v1.0_pro.pth...
[2024-03-21 18:10:57][Stereo][158802][MainThread][140406620477248][cell_infer][45][INFO]: Load model from: ../data/seg_models/Demo_CellSegmentation/Deep_Learning_Model_v01_pro/cell_segmetation_v1.0_pro.pth
[2024-03-21 18:10:57][Stereo][158802][MainThread][140406620477248][cell_infer][48][INFO]: Load model ok.
[2024-03-21 18:10:57][Stereo][158802][MainThread][140406620477248][cell_infer][52][INFO]: GPU type is NVIDIA GeForce RTX 3090
[2024-03-21 18:10:57][Stereo][158802][MainThread][140406620477248][cell_infer][53][INFO]: using device: cuda:0
[2024-03-21 18:10:57][Stereo][158802][MainThread][140406620477248][cell_infer][56][INFO]: image shape: (13539, 10206)
[2024-03-21 18:10:57][Stereo][158802][MainThread][140406620477248][cell_infer][59][INFO]: median filter using cpu
[2024-03-21 18:16:38][Stereo][158802][MainThread][140406620477248][cell_infer][67][INFO]: median filter: 341.33809781074524
100%|█████████████████████████████████████████| 288/288 [00:30<00:00,  9.39it/s]
[2024-03-21 18:17:09][Stereo][158802][MainThread][140406620477248][cell_infer][56][INFO]: image shape: (233, 310)
[2024-03-21 18:17:09][Stereo][158802][MainThread][140406620477248][cell_infer][59][INFO]: median filter using cpu
[2024-03-21 18:17:14][Stereo][158802][MainThread][140406620477248][cell_infer][67][INFO]: median filter: 4.6608827114105225
100%|█████████████████████████████████████████████| 1/1 [00:00<00:00, 20.87it/s]
[2024-03-21 18:17:14][Stereo][158802][MainThread][140406620477248][cell_seg_pipeline_v1_pro][57][INFO]: Cell inference : 377.42
[2024-03-21 18:17:14][Stereo][158802][MainThread][140406620477248][cell_seg_pipeline_v1_pro][62][INFO]: Filter by tissue mask : 0.43
[2024-03-21 18:17:14][Stereo][158802][MainThread][140406620477248][cell_seg_pipeline_v1_pro][68][INFO]: Mosaic tissue roi : 0.11
[2024-03-21 18:17:45][Stereo][158802][MainThread][140406620477248][cell_seg_pipeline_v1_pro][73][INFO]: Post-processing : 30.47
[2024-03-21 18:17:45][Stereo][158802][MainThread][140406620477248][cell_seg_pipeline_v1_pro][76][INFO]: Result saved : ./cell_seg_result_v1_pro

Deep cell model#

Similarly download the deep cell model from our cloud disk. It is assumed that the local storage path is '../data/seg_models/Demo_CellSegmentation/Deep_Cell_Model/'.

[4]:
from stereo import image as im

model_path = '../data/seg_models/Demo_CellSegmentation/Deep_Cell_Model/'
image = '../data/SS200000135TL_D1_regist.tif'
output = './cell_seg_result_deepcell/'
tissue_seg_model = '../data/seg_models/tissueseg_bcdu_SDI_230523_tf.onnx'

im.cell_seg_deepcell(
    model_path=model_path,
    img_path=image,
    out_path=output,
    tissue_seg_model_path=tissue_seg_model,
    tissue_seg_staining_type='ssDNA',
    gpu='0' # set to -1 to run on cpu
)
[INFO 20240322-09-30-56 p158802 __init__ tissue_segmentation.py:33] start loading model weight
INFO:cellbin.utils.clog:start loading model weight
[INFO 20240322-09-30-56 p158802 _f_load_model onnx_net.py:29] loading weight from ../data/seg_models/tissueseg_bcdu_SDI_230523_tf.onnx
INFO:cellbin.utils.clog:loading weight from ../data/seg_models/tissueseg_bcdu_SDI_230523_tf.onnx
[INFO 20240322-09-30-57 p158802 _f_load_model onnx_net.py:40] onnx work on gpu 0
2024-03-22 09:30:56.935226749 [W:onnxruntime:, session_state.cc:1162 VerifyEachNodeIsAssignedToAnEp] Some nodes were not assigned to the preferred execution providers which may or may not have an negative impact on performance. e.g. ORT explicitly assigns shape related ops to CPU to improve perf.
2024-03-22 09:30:56.935251324 [W:onnxruntime:, session_state.cc:1164 VerifyEachNodeIsAssignedToAnEp] Rerunning with verbose output on a non-minimal build will show node assignments.
INFO:cellbin.utils.clog:onnx work on gpu 0
[INFO 20240322-09-30-57 p158802 __init__ tissue_segmentation.py:35] end loading model weight
INFO:cellbin.utils.clog:end loading model weight
[INFO 20240322-09-30-57 p158802 run tissue_segmentation.py:46] start tissue seg
INFO:cellbin.utils.clog:start tissue seg
[INFO 20240322-09-31-11 p158802 run tissue_segmentation.py:48] end tissue seg
INFO:cellbin.utils.clog:end tissue seg
[2024-03-22 09:31:12][Stereo][158802][MainThread][140406620477248][cell_seg_pipeline][63][INFO]: Transform 16bit to 8bit : 0.00
[2024-03-22 09:31:12][Stereo][158802][MainThread][140406620477248][cell_seg_pipeline][71][INFO]: Get tissue mask : 0.00
WARNING:tensorflow:SavedModel saved prior to TF 2.5 detected when loading Keras model. Please ensure that you are saving the model with model.save() or tf.keras.models.save_model(), *NOT* tf.saved_model.save(). To confirm, there should be a file named "keras_metadata.pb" in the SavedModel directory.
2024-03-22 09:31:34.297937: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2024-03-22 09:31:34.353109: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 8933 MB memory:  -> device: 0, name: NVIDIA GeForce RTX 3090, pci bus id: 0000:d9:00.0, compute capability: 8.6
2024-03-22 09:31:50.652647: I tensorflow/stream_executor/cuda/cuda_dnn.cc:366] Loaded cuDNN version 8900
【image 1/2】 batch:[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]100%        1/1
WARNING:tensorflow:SavedModel saved prior to TF 2.5 detected when loading Keras model. Please ensure that you are saving the model with model.save() or tf.keras.models.save_model(), *NOT* tf.saved_model.save(). To confirm, there should be a file named "keras_metadata.pb" in the SavedModel directory.
[2024-03-22 09:36:15][Stereo][158802][MainThread][140406620477248][cell_seg_pipeline][310][INFO]: Cell inference : 281.94
【image 2/2】 batch:[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]100%        1/1
[2024-03-22 09:36:15][Stereo][158802][MainThread][140406620477248][cell_seg_pipeline][315][INFO]: Filter by tissue mask : 0.28
[2024-03-22 09:36:15][Stereo][158802][MainThread][140406620477248][cell_seg_pipeline][320][INFO]: Mosaic tissue roi : 0.10
[2024-03-22 09:37:38][Stereo][158802][MainThread][140406620477248][cell_seg_pipeline][325][INFO]: Post-processing : 82.73
[2024-03-22 09:37:54][Stereo][158802][MainThread][140406620477248][cell_seg_pipeline][328][INFO]: Result saved : ./cell_seg_result_deepcell/

After running the function, three result pictures will be generated in output directory.

mask.tif: cell segmentation result

outline.tif: cell boundary result

score.tif: cell fraction result

We compared different cell segmentation algorithm models:

cell_segmentation_comparision.png

Cellpose#

Cellpose uses an improved deep neural network architecture based on the classic Unet structure. This method introduces a residual block to predict the input image and outputs the three matrices of the horizontal and vertical gradients of the image and whether the pixel is in the cell. Three predictions synthesize a gradient vector field. Finally, a thermal field with a fixed point is constructed from this gradient vector field such that all pixels that converge to the same fixed point are grouped together to obtain the shape of a single cell. Please download ​demo image for Cellpose.

Preparation

Install necessary packages by running commands as below:

   pip install patchify
   pip install torch
   pip install fastremap
   pip install roifile

Here we recommend using model_type='cyto2' for segmentation based on cell cytoplasm, which is an optimized method compared to 'cyto'.

[ ]:
from stereo.algorithm import cell_pose as cp

image="./Cellpose_DemoImage.tif"
output="./Cellpose_DemoImage_mask.tif"

cp.Cellpose(
            img_path=image,
            out_path=output,
            model_type='cyto2',
            dmin=30,   # min cell diameter
            dmax=40,   # max cell diameter
            step=10
            )

CellposeResults.png

New cellbin GEF#

After obtaining mask results through cell segmentation function, you can generate a cell bin GEF from the mask as well as the corresponding BGEF (including SN.raw.gef, SN.gef, SN.tissue.gef).

First, you need a BGEF , then refer to following example. Remember to specify the path of directory to save cell bin GEF by cgef_out_dir, the path of BGEF by bgef_path and the path of mask by mask_path.

It will return the path of cell bin GEF.

[ ]:
from stereo.tools.cell_cut import CellCut

cgef_out_dir = "./cell_cut_out"
bgef_path = "./SS200000135TL_D1.raw.gef"
mask_path = "./SS200000135TL_D1_mask.tif"

cc = CellCut(cgef_out_dir=cgef_out_dir)
out_path = cc.cell_cut(bgef_path=bgef_path, mask_path=mask_path)

If only have GEM file, you could generate a new cell bin GEF based on it and the corresponding mask.

[ ]:
from stereo.tools.cell_cut import CellCut

cgef_out_dir = "./cell_cut_out"
gem_path = "./SS200000135TL_D1.gem.gz"
mask_path = "./SS200000135TL_D1_mask.tif"

cc = CellCut(cgef_out_dir=cgef_out_dir)
out_path = cc.cell_cut(gem_path=gem_path, mask_path=mask_path)