YangLabHKUST / UCS

Code for "UCS: a unified approach to cell segmentation for subcellular spatial transcriptomics"
2 stars 0 forks source link

Incorrect result getting from Stereo-seq data #2

Closed Renpeng007 closed 1 month ago

Renpeng007 commented 2 months ago

Thank you for the excellent work!

I tried running the script on your demo stereo-seq data, but I did not get the expected results. I am concerned that some hyperparameters might need adjustment, but I am unsure which ones should be changed.

#!/bin/bash python run.py \ --gene_map /mnt/e/datasets/stereo-seq_testing_dataset_UCS/gene_map.tif \ --nuclei_mask /mnt/e/datasets/stereo-seq_testing_dataset_UCS/cellpose_segmentation.tif \

The log goes like this:

Log dir exists: ./Log, please choose an option: d (delete) / n (new) / q (quit): n Log dir: ./Log_new_1722279489 Checkpoint dir: ./Log_new_1722279489/checkpoint 2024-07-29 20:58:09,241 INFO Opts: Namespace(cell_net_epoch=1, cell_net_lr=0.0001, cell_net_nuclei_weight=1, dilation_iter_num=4, dilation_kernel_size=10, fg_net_batch_size=256, fg_net_epoch=1, fg_net_lr=0.0001, fg_net_nuclei_weight=1, gene_map='/mnt/e/datasets/stereo-seq_testing_dataset_UCS/gene_map.tif', gpu='0', log_dir='./Log', nuclei_mask='/mnt/e/datasets/stereo-seq_testing_dataset_UCS/cellpose_segmentation.tif', patch_size=48, seed=0, tau=5, using_filtered_background=True) Initializing train dataset 2024-07-29 20:58:09,243 INFO Loading gene map from /mnt/e/datasets/stereo-seq_testing_dataset_UCS/gene_map.tif 2024-07-29 20:58:17,995 INFO Gene map shape: (1200, 1200, 2000) 2024-07-29 20:58:17,995 INFO Loading nuclei mask from /mnt/e/datasets/stereo-seq_testing_dataset_UCS/cellpose_segmentation.tif 2024-07-29 20:58:18,154 INFO Nuclei mask unique values: 1090, Shape: (1200, 1200) 2024-07-29 20:58:18,155 INFO Total 625 patches with shift 0 2024-07-29 20:58:18,629 INFO Initialize weights Length of train dataset: 625 2024-07-29 20:58:19,139 INFO Start training foreground net 2024-07-29 20:58:19,139 INFO Using filtered background 2024-07-29 20:58:30,297 INFO Epoch 0, iter 0, loss 0.6893935799598694 2024-07-29 20:58:34,754 INFO Epoch 0, iter 1, loss 0.6519399285316467 2024-07-29 20:58:38,708 INFO Epoch 0, iter 2, loss 0.6921696066856384 2024-07-29 20:58:38,789 INFO Model saved to ./Log_new_1722279489/checkpoint/model.pth 2024-07-29 20:58:38,790 INFO Start training cell net 2024-07-29 20:58:39,400 INFO Epoch 0, iter 0, loss 0.6933246850967407 2024-07-29 20:58:39,881 INFO Epoch 0, iter 10, loss 0.703301191329956 2024-07-29 20:58:40,993 INFO Epoch 0, iter 30, loss 0.6927601099014282 2024-07-29 20:58:41,543 INFO Epoch 0, iter 40, loss 0.6889930963516235 2024-07-29 20:58:41,979 INFO Epoch 0, iter 50, loss 0.6927635669708252 2024-07-29 20:58:42,403 INFO Epoch 0, iter 60, loss 0.6899683475494385 2024-07-29 20:58:42,843 INFO Epoch 0, iter 70, loss 0.6927381753921509 2024-07-29 20:58:43,020 INFO Epoch 0, iter 80, loss 0.6913470029830933 2024-07-29 20:58:43,726 INFO Epoch 0, iter 100, loss 0.6897174715995789 2024-07-29 20:58:44,076 INFO Epoch 0, iter 110, loss 0.6859836578369141 2024-07-29 20:58:44,362 INFO Epoch 0, iter 120, loss 0.667649507522583 2024-07-29 20:58:44,884 INFO Epoch 0, iter 140, loss 0.6849753856658936 2024-07-29 20:58:45,155 INFO Epoch 0, iter 150, loss 0.685003936290741 2024-07-29 20:58:45,497 INFO Epoch 0, iter 160, loss 0.6948559284210205 2024-07-29 20:58:46,370 INFO Epoch 0, iter 180, loss 0.6417354345321655 2024-07-29 20:58:46,632 INFO Epoch 0, iter 190, loss 0.6930499076843262 2024-07-29 20:58:46,874 INFO Epoch 0, iter 200, loss 0.6825616359710693 2024-07-29 20:58:47,146 INFO Epoch 0, iter 210, loss 0.6672583222389221 2024-07-29 20:58:47,448 INFO Epoch 0, iter 220, loss 0.6237497329711914 2024-07-29 20:58:47,710 INFO Epoch 0, iter 230, loss 0.6880190968513489 2024-07-29 20:58:47,945 INFO Epoch 0, iter 240, loss 0.6949312090873718 2024-07-29 20:58:48,225 INFO Epoch 0, iter 250, loss 0.6862005591392517 2024-07-29 20:58:48,680 INFO Epoch 0, iter 270, loss 0.6438055038452148 2024-07-29 20:58:48,953 INFO Epoch 0, iter 280, loss 0.6326137185096741 2024-07-29 20:58:49,263 INFO Epoch 0, iter 290, loss 0.6591458320617676 2024-07-29 20:58:49,507 INFO Epoch 0, iter 300, loss 0.6755223274230957 2024-07-29 20:58:49,806 INFO Epoch 0, iter 310, loss 0.6142774820327759 2024-07-29 20:58:50,447 INFO Epoch 0, iter 330, loss 0.6572760343551636 2024-07-29 20:58:50,747 INFO Epoch 0, iter 340, loss 0.6507608294487 2024-07-29 20:58:51,033 INFO Epoch 0, iter 350, loss 0.5828614234924316 2024-07-29 20:58:51,321 INFO Epoch 0, iter 360, loss 0.6879668235778809 2024-07-29 20:58:51,593 INFO Epoch 0, iter 370, loss 0.6865547895431519 2024-07-29 20:58:51,987 INFO Epoch 0, iter 380, loss 0.5545623302459717 2024-07-29 20:58:52,372 INFO Epoch 0, iter 390, loss 0.6140924692153931 2024-07-29 20:58:52,748 INFO Epoch 0, iter 400, loss 0.6714704632759094 2024-07-29 20:58:53,154 INFO Epoch 0, iter 410, loss 0.5050652027130127 2024-07-29 20:58:53,537 INFO Epoch 0, iter 420, loss 0.6577508449554443 2024-07-29 20:58:53,969 INFO Epoch 0, iter 430, loss 0.552645206451416 2024-07-29 20:58:54,385 INFO Epoch 0, iter 440, loss 0.5540516972541809 2024-07-29 20:58:55,282 INFO Epoch 0, iter 450, loss 0.6268185377120972 2024-07-29 20:58:55,557 INFO Epoch 0, iter 460, loss 0.5897736549377441 2024-07-29 20:58:55,844 INFO Epoch 0, iter 470, loss 0.5530616641044617 2024-07-29 20:58:56,143 INFO Epoch 0, iter 480, loss 0.48683005571365356 2024-07-29 20:58:56,425 INFO Epoch 0, iter 490, loss 0.6583248972892761 2024-07-29 20:58:56,690 INFO Epoch 0, iter 500, loss 0.6400178670883179 2024-07-29 20:58:56,955 INFO Epoch 0, iter 510, loss 0.6743050813674927 2024-07-29 20:58:57,251 INFO Epoch 0, iter 520, loss 0.4267827868461609 2024-07-29 20:58:57,546 INFO Epoch 0, iter 530, loss 0.64091557264328 2024-07-29 20:58:57,830 INFO Epoch 0, iter 540, loss 0.5979130268096924 2024-07-29 20:58:58,077 INFO Epoch 0, iter 550, loss 0.6523219347000122 2024-07-29 20:58:58,424 INFO Epoch 0, iter 560, loss 0.4326769709587097 2024-07-29 20:58:58,767 INFO Epoch 0, iter 570, loss 0.5862611532211304 2024-07-29 20:58:59,055 INFO Epoch 0, iter 580, loss 0.32469338178634644 2024-07-29 20:58:59,348 INFO Epoch 0, iter 590, loss 0.6243890523910522 2024-07-29 20:58:59,700 INFO Epoch 0, iter 600, loss 0.669928789138794 2024-07-29 20:59:00,085 INFO Epoch 0, iter 610, loss 0.37075579166412354 2024-07-29 20:59:00,451 INFO Epoch 0, iter 620, loss 0.6807065010070801 2024-07-29 20:59:00,645 INFO Model saved to ./Log_new_1722279489/checkpoint/model.pth Initializing pred dataset with shift 0 and shift patch size//2 2024-07-29 20:59:00,645 INFO Loading gene map from /mnt/e/datasets/stereo-seq_testing_dataset_UCS/gene_map.tif 2024-07-29 20:59:09,699 INFO Gene map shape: (1200, 1200, 2000) 2024-07-29 20:59:09,700 INFO Loading nuclei mask from /mnt/e/datasets/stereo-seq_testing_dataset_UCS/cellpose_segmentation.tif 2024-07-29 20:59:09,863 INFO Nuclei mask unique values: 1090, Shape: (1200, 1200) 2024-07-29 20:59:09,864 INFO Total 625 patches with shift 0 2024-07-29 20:59:09,864 INFO Loading gene map from /mnt/e/datasets/stereo-seq_testing_dataset_UCS/gene_map.tif 2024-07-29 20:59:19,539 INFO Gene map shape: (1200, 1200, 2000) 2024-07-29 20:59:19,540 INFO Loading nuclei mask from /mnt/e/datasets/stereo-seq_testing_dataset_UCS/cellpose_segmentation.tif 2024-07-29 20:59:19,682 INFO Nuclei mask unique values: 1090, Shape: (1200, 1200) 2024-07-29 20:59:19,683 INFO Total 576 patches with shift 24 2024-07-29 20:59:51,625 INFO Prediction finished 2024-07-29 20:59:51,625 INFO Filling grid 2024-07-29 20:59:51,736 INFO 625 patches available 2024-07-29 20:59:51,736 INFO Num multiprocessing splits: 16 2024-07-29 20:59:52,448 INFO Combining results 2024-07-29 20:59:56,774 INFO Saved final segmentation to ./Log_new_1722279489/pred/segmentation_mask.tif

and I also attached the overlapping of segmentation results with the nuclei staining image. segmentation_mask_visual

chenyhvvvv commented 1 month ago

Hello, thank you for your questions.

Yes, some hyperparameters should be changed due to small size of the stereoseq patch. Here is the script: python run.py --gene_map /import/macyang_home2/ychenlp/Code/ucs-main/Raw_data/stereoseq/gene_map.tif --nuclei_mask /import/macyang_home2/ychenlp/Code/ucs-main/Raw_data/stereoseq/cellpose_segmentation.tif --log_dir ./log/stereoseq_new --gpu 1 --fg_net_epoch 5 --cell_net_epoch 2 --fg_net_lr 1e-3 --cell_net_lr 1e-3

And than you can visualize the result using the following code: `import tifffile import matplotlib.pyplot as plt from skimage.segmentation import find_boundaries import numpy as np gene_map = tifffile.imread("/import/macyang_home2/ychenlp/Code/ucs-main/Raw_data/stereoseq/gene_map.tif") dapi = tifffile.imread("/import/macyang_home2/ychenlp/Code/ucs-main/Raw_data/stereoseq/Mouse_brain_Adult_sub.tif") seg_mask = tifffile.imread("/import/macyang_home2/ychenlp/Code/ucs-main/log/stereoseq_new/pred/segmentation_mask.tif") from skimage.segmentation import find_boundaries

gene_sum = gene_map.sum(axis=2) boundary = find_boundaries(seg_mask, mode='inner')

fig, ax = plt.subplots(1, 1, figsize=(10, 10)) ax.axis('off') ax.imshow(gene_sum > 0, aspect='auto', cmap='Greys') ax.imshow(boundary, aspect='auto', cmap='Blues', alpha=0.5)`

111