Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reloading trained model #1088

Closed
Jiadalee opened this issue Jan 14, 2025 · 13 comments
Closed

reloading trained model #1088

Jiadalee opened this issue Jan 14, 2025 · 13 comments

Comments

@Jiadalee
Copy link

Hello, I have a question about the run_cellpose_2.ipynb. For the section of Train new model, the initial model was trained and saved to model_path = train_dir + 'models/'. And then, for the following codes from the section of Evaluation on test data below,

masks = model.eval(test_data, channels=[chan, chan2], diameter=diam_labels)[0]

When you run the model on test images, why don'tyou load the trained model "CP_tissuenet" from the path and then pass the trained model for evaluation? why do you still directly call the model.eval()? It seems themodel.eval()` still calls the initial cyto default model, not the trained model' for evaluation. I would think of replacing this code block by below:

trained_model_path = "content/human_in_the_loop/train/models/CP_tissuenet"
model = models.CellposeModel(gpu=True, pretrained_model= trained_model_path )
masks = model.eval(test_data,
channels=[chan, chan2],
diameter=diam_labels)[0]

Could you pls check?

@carsen-stringer
Copy link
Member

the model.net has been modified in the train function and should be equivalent to the call with the trained model path above. are you receiving an error otherwise? indeed if you want to use the model again after reloading the notebook then you need to load the model from the saved path

@Jiadalee
Copy link
Author

Jiadalee commented Feb 7, 2025

@carsen-stringer hi, it is not an error. It is a strange message saying

WARNING:cellpose.models:pretrained_model path does not exist, using default model

I comprehensively read through all your previous Issues. I found that If we want to load the saved fine-tuned/re-trained model for eval, we have to set the gpu = False. Otherwise, the locally saved model cannot detect any masks, and the function will use the default model and pop up the warning message above. You can find some posts discussing this issue. I'm not sure what the default model is. Cyto3 is the default model? if loading is not successful

I'm still a little doubted when you said they are equivalent. I tried some times. One instance to drive my doubt is that the evaluation results vary for these two model re-loading methods. If we fine-tuned the initial model with RGB images, and eval outputs by loading the fine-tuned model from path are RGB masks, since model architecture was modified during the training. However, the eval outputs by directly calling the model are still gray-scale masks (gray scale outputs are same as the initial model's outputs). This difference shows that calling the trained model from path is different from directly calling the model.

@carsen-stringer
Copy link
Member

carsen-stringer commented Feb 7, 2025

oh it means that the model didn't load - they are not equivalent because you are loading the default model. what I meant was that you don't have to load a model if you've just trained the model. I think the bug in the code above is that you're missing a slash in the path: "/content/human_in_the_loop/train/models/CP_tissuenet" but you can check your paths in the notebook and see if that's where it is.

Sorry can you clarify though, the model only works if gpu is False, and what other issue showed this?

@carsen-stringer carsen-stringer changed the title Question reloading trained model Feb 7, 2025
@Jiadalee
Copy link
Author

Jiadalee commented Feb 7, 2025

oh it means that the model didn't load - they are not equivalent because you are loading the default model. what I meant was that you don't have to load a model if you've just trained the model. I think the bug in the code above is that you're missing a slash in the path: "/content/human_in_the_loop/train/models/CP_tissuenet" but you can check your paths in the notebook and see if that's where it is.

Sorry can you clarify though, the model only works if gpu is False, and what other issue showed this?

@carsen-stringer, I just added the slah, and the reloading works now. So you meant, within a same workflow pipeline, if I just trained the model and then I can directly call the model, no need to re-load it again? If so, why do we still assign a new name to the trained model?

for the gpu issue: I trained the cyto3 model by setting the gpu is true
model_pretrained = models.CellposeModel(gpu=True, model_type="cyto3").
see below log info

2025-02-07 12:10:40,900 [INFO] >> cyto3 << model set to be used
2025-02-07 12:10:40,902 [INFO] ** TORCH CUDA version installed and working. **
2025-02-07 12:10:40,903 [INFO] >>>> using GPU (CUDA)
2025-02-07 12:10:40,978 [INFO] >>>> loading model /home/jiada_li/.cellpose/models/cyto3
2025-02-07 12:10:41,062 [INFO] >>>> model diam_mean = 30.000 (ROIs rescaled to this size during training)

Then, I run the gpu for training

model_name = "cyto3_finetuned_new"

# fine-tune cyto model (Note: cellpose model only accept image shape of [batch, channel, height, width] - channel first)
new_model_path, train_losses, val_losses = train.train_seg(model_pretrained.net,
                              train_data=train_images,
                              train_labels=train_masks,
                              test_data=val_images,
                              test_labels=val_masks,
                              channels=channels,
                              save_path=model_dir,
                              n_epochs=200,
                              learning_rate=0.01,
                              weight_decay=0.0001,
                              SGD=True,                  # set False to Use Adam optimizer
                              nimg_per_epoch=8,
                              model_name=model_name,
                              min_train_masks=0)         # Allow training with fewer masks

The training log info looks good as below:

2025-02-07 12:13:12,396 [INFO] WRITING LOG OUTPUT TO /home/jiada_li/.cellpose/run.log
2025-02-07 12:13:12,396 [INFO]
cellpose version: 3.1.1.dev3+g0d8d8f7
platform: linux
python version: 3.12.2
torch version: 2.6.0+cu124
2025-02-07 12:13:12,397 [INFO] flows precomputed
2025-02-07 12:13:12,409 [INFO] flows precomputed
2025-02-07 12:13:12,411 [INFO] >>> computing diameters
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 170/170 [00:00<00:00, 5373.95it/s]
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 18/18 [00:00<00:00, 5017.11it/s]
2025-02-07 12:13:12,450 [INFO] >>> using channels [2, 1]
2025-02-07 12:13:12,451 [INFO] >>> normalizing {'lowhigh': None, 'percentile': None, 'normalize': True, 'norm3D': True, 'sharpen_radius': 0, 'smooth_radius': 0, 'tile_norm_blocksize': 0, 'tile_norm_smooth3D': 1, 'invert': False}

2025-02-07 12:13:12,625 [INFO] >>> n_epochs=200, n_train=170, n_test=18
2025-02-07 12:13:12,625 [INFO] >>> SGD, learning_rate=0.01000, weight_decay=0.00010, momentum=0.900
2025-02-07 12:13:12,627 [INFO] >>> saving model to Microscopy/Image Analysis/trained_models/models/cyto3_finetuned_new
2025-02-07 12:13:12,793 [INFO] 0, train_loss=1.2104, test_loss=0.4869, LR=0.000000, time 0.17s
2025-02-07 12:13:13,202 [INFO] 5, train_loss=0.7089, test_loss=0.4542, LR=0.005556, time 0.57s
2025-02-07 12:13:13,600 [INFO] 10, train_loss=0.7476, test_loss=0.4317, LR=0.010000, time 0.97s
2025-02-07 12:13:14,329 [INFO] 20, train_loss=0.5997, test_loss=0.3685, LR=0.010000, time 1.70s
2025-02-07 12:13:15,058 [INFO] 30, train_loss=0.5385, test_loss=0.3386, LR=0.010000, time 2.43s
2025-02-07 12:13:15,788 [INFO] 40, train_loss=0.4372, test_loss=0.3259, LR=0.010000, time 3.16s
2025-02-07 12:13:16,528 [INFO] 50, train_loss=0.4548, test_loss=0.3278, LR=0.010000, time 3.90s
2025-02-07 12:13:17,266 [INFO] 60, train_loss=0.3597, test_loss=0.3108, LR=0.010000, time 4.64s
2025-02-07 12:13:17,992 [INFO] 70, train_loss=0.3344, test_loss=0.3142, LR=0.010000, time 5.37s
2025-02-07 12:13:18,721 [INFO] 80, train_loss=0.3080, test_loss=0.3183, LR=0.010000, time 6.09s
2025-02-07 12:13:19,449 [INFO] 90, train_loss=0.3582, test_loss=0.3206, LR=0.010000, time 6.82s
2025-02-07 12:13:20,178 [INFO] 100, train_loss=0.2838, test_loss=0.3080, LR=0.010000, time 7.55s
2025-02-07 12:13:20,179 [INFO] saving network parameters to Microscopy/Image Analysis/trained_models/models/cyto3_finetuned_new
2025-02-07 12:13:21,013 [INFO] 110, train_loss=0.3060, test_loss=0.3030, LR=0.010000, time 8.39s
2025-02-07 12:13:21,742 [INFO] 120, train_loss=0.4307, test_loss=0.3091, LR=0.010000, time 9.11s
2025-02-07 12:13:22,469 [INFO] 130, train_loss=0.3829, test_loss=0.2955, LR=0.010000, time 9.84s
2025-02-07 12:13:23,202 [INFO] 140, train_loss=0.3512, test_loss=0.2868, LR=0.010000, time 10.58s
2025-02-07 12:13:23,957 [INFO] 150, train_loss=0.3693, test_loss=0.2854, LR=0.005000, time 11.33s
2025-02-07 12:13:24,726 [INFO] 160, train_loss=0.4437, test_loss=0.2827, LR=0.001250, time 12.10s
2025-02-07 12:13:25,493 [INFO] 170, train_loss=0.3644, test_loss=0.2832, LR=0.000313, time 12.87s
2025-02-07 12:13:26,261 [INFO] 180, train_loss=0.3715, test_loss=0.2841, LR=0.000078, time 13.63s
2025-02-07 12:13:27,028 [INFO] 190, train_loss=0.3474, test_loss=0.2848, LR=0.000020, time 14.40s
2025-02-07 12:13:27,645 [INFO] saving network parameters to Microscopy/Image Analysis/trained_models/models/cyto3_finetuned_new
Diameter of labels in training images: 15.681035041809082

Aftewards, I reloaded the model for evaluation with the following codes.

model_path_finetuned = "/Microscopy/Image Analysis/trained_models/models/cyto3_finetuned_new"
model_finetuned = models.CellposeModel(gpu=False, pretrained_model=model_path_finetuned) 

# Run model evaluation separately for each channel configuration
pred_masks_list = []
for ch in channel_options:
    pred_masks = model_finetuned.eval(
    #pred_masks = model_pretrained.eval(
        test_images,                           # TIFF image has 4 planes and 3 channels
        channels=ch,                           # Pass one channel configuration at a time
        flow_threshold=flow_threshold,
        cellprob_threshold=cellprob_threshold,
        stitch_threshold=0.1,                  # help link segmented regions across frames.
        do_3D=True,                            # ensure Cellpose processes the image stack as a whole 3D volume instead of per plane separately.
        diameter=measured_diameter + 4         
    )[0]  # Extract masks

    pred_masks_list.append(pred_masks)

Unfortunately, the model cannot detect any masks. However, when I shift the gpu to False, then the model can generate predicted ROI masks. Similar issue can be found #1071 Custom model not able to detect ROIs after training on cyto3,

@carsen-stringer
Copy link
Member

carsen-stringer commented Feb 7, 2025

can you please post the output of the last block of code you posted?

regarding the model training, we always still save the model so you can use it later, but it is modified in the workspace you are currently in

(and thanks for all the detailed info!)

@Jiadalee
Copy link
Author

Jiadalee commented Feb 7, 2025

can you please post the output of the last block of code you posted?

regarding the model training, we always still save the model so you can use it later, but it is modified in the workspace you are currently in

(and thanks for all the detailed info!)

@carsen-stringer Pls see the output log info below. You can see no cell found message at the bottom

start running model evaluation...for 3D segmentation
2025-02-07 13:15:43,847 [INFO] 0%| | 0/4 [00:00<?, ?it/s]
2025-02-07 13:15:43,848 [INFO] multi-stack tiff read in as having 3 planes 1 channels
2025-02-07 13:15:43,851 [INFO] resizing 3D image with rescale=0.88 and anisotropy=None
2025-02-07 13:15:43,854 [INFO] running YX: 2 planes of size (112, 112)
2025-02-07 13:15:43,854 [INFO]
2025-02-07 13:15:43,855 [INFO] 0%| | 0/1 [00:00<?, ?it/s]
2025-02-07 13:15:43,856 [INFO]
2025-02-07 13:15:43,869 [INFO] 100%|##########| 1/1 [00:00<00:00, 75.39it/s]
2025-02-07 13:15:43,870 [INFO] running ZY: 112 planes of size (2, 112)
2025-02-07 13:15:43,871 [INFO]
2025-02-07 13:15:43,872 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:43,872 [INFO]
2025-02-07 13:15:44,041 [INFO] 100%|##########| 14/14 [00:00<00:00, 83.14it/s]
2025-02-07 13:15:44,043 [INFO] running ZX: 112 planes of size (2, 112)
2025-02-07 13:15:44,044 [INFO]
2025-02-07 13:15:44,045 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:44,045 [INFO]
2025-02-07 13:15:44,222 [INFO] 100%|##########| 14/14 [00:00<00:00, 79.51it/s]
2025-02-07 13:15:44,224 [INFO] resizing 3D flows and cellprob to original image size
2025-02-07 13:15:44,226 [INFO] network run in 0.37s
2025-02-07 13:15:44,598 [INFO] No cell pixels found.
2025-02-07 13:15:44,600 [INFO] masks created in 0.00s
2025-02-07 13:15:44,604 [INFO] multi-stack tiff read in as having 3 planes 1 channels
2025-02-07 13:15:44,607 [INFO] resizing 3D image with rescale=0.88 and anisotropy=None
2025-02-07 13:15:44,609 [INFO] running YX: 2 planes of size (112, 112)
2025-02-07 13:15:44,610 [INFO]
2025-02-07 13:15:44,611 [INFO] 0%| | 0/1 [00:00<?, ?it/s]
2025-02-07 13:15:44,611 [INFO]
2025-02-07 13:15:44,627 [INFO] 100%|##########| 1/1 [00:00<00:00, 64.96it/s]
2025-02-07 13:15:44,628 [INFO] running ZY: 112 planes of size (2, 112)
2025-02-07 13:15:44,630 [INFO]
2025-02-07 13:15:44,631 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:44,631 [INFO]
2025-02-07 13:15:44,820 [INFO] 100%|##########| 14/14 [00:00<00:00, 74.38it/s]
2025-02-07 13:15:44,821 [INFO] running ZX: 112 planes of size (2, 112)
2025-02-07 13:15:44,823 [INFO]
2025-02-07 13:15:44,824 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:44,824 [INFO]
2025-02-07 13:15:45,013 [INFO] 100%|##########| 14/14 [00:00<00:00, 74.28it/s]
2025-02-07 13:15:45,015 [INFO] resizing 3D flows and cellprob to original image size
2025-02-07 13:15:45,017 [INFO] network run in 0.41s
2025-02-07 13:15:45,325 [INFO] No cell pixels found.
2025-02-07 13:15:45,326 [INFO] masks created in 0.00s
2025-02-07 13:15:45,331 [INFO] multi-stack tiff read in as having 3 planes 1 channels
2025-02-07 13:15:45,334 [INFO] resizing 3D image with rescale=0.88 and anisotropy=None
2025-02-07 13:15:45,336 [INFO] running YX: 2 planes of size (112, 112)
2025-02-07 13:15:45,337 [INFO]
2025-02-07 13:15:45,337 [INFO] 0%| | 0/1 [00:00<?, ?it/s]
2025-02-07 13:15:45,338 [INFO]
2025-02-07 13:15:45,353 [INFO] 100%|##########| 1/1 [00:00<00:00, 67.59it/s]
2025-02-07 13:15:45,355 [INFO] running ZY: 112 planes of size (2, 112)
2025-02-07 13:15:45,356 [INFO]
2025-02-07 13:15:45,357 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:45,357 [INFO]
2025-02-07 13:15:45,548 [INFO] 100%|##########| 14/14 [00:00<00:00, 73.51it/s]
2025-02-07 13:15:45,549 [INFO] running ZX: 112 planes of size (2, 112)
2025-02-07 13:15:45,551 [INFO]
2025-02-07 13:15:45,552 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:45,552 [INFO]
2025-02-07 13:15:45,746 [INFO] 100%|##########| 14/14 [00:00<00:00, 72.42it/s]
2025-02-07 13:15:45,748 [INFO] resizing 3D flows and cellprob to original image size
2025-02-07 13:15:45,750 [INFO] network run in 0.42s
2025-02-07 13:15:46,100 [INFO] masks created in 0.05s
2025-02-07 13:15:46,104 [INFO] multi-stack tiff read in as having 3 planes 1 channels
2025-02-07 13:15:46,107 [INFO] resizing 3D image with rescale=0.88 and anisotropy=None
2025-02-07 13:15:46,110 [INFO] running YX: 2 planes of size (112, 112)
2025-02-07 13:15:46,110 [INFO]
2025-02-07 13:15:46,111 [INFO] 0%| | 0/1 [00:00<?, ?it/s]
2025-02-07 13:15:46,112 [INFO]
2025-02-07 13:15:46,128 [INFO] 100%|##########| 1/1 [00:00<00:00, 66.34it/s]
2025-02-07 13:15:46,129 [INFO] running ZY: 112 planes of size (2, 112)
2025-02-07 13:15:46,130 [INFO]
2025-02-07 13:15:46,131 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:46,132 [INFO]
2025-02-07 13:15:46,326 [INFO] 100%|##########| 14/14 [00:00<00:00, 72.18it/s]
2025-02-07 13:15:46,327 [INFO] running ZX: 112 planes of size (2, 112)
2025-02-07 13:15:46,329 [INFO]
2025-02-07 13:15:46,330 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:46,330 [INFO]
2025-02-07 13:15:46,527 [INFO] 100%|##########| 14/14 [00:00<00:00, 71.33it/s]
2025-02-07 13:15:46,529 [INFO] resizing 3D flows and cellprob to original image size
2025-02-07 13:15:46,531 [INFO] network run in 0.42s
2025-02-07 13:15:46,833 [INFO] No cell pixels found.
2025-02-07 13:15:46,834 [INFO] masks created in 0.00s
2025-02-07 13:15:46,839 [INFO] 100%|##########| 4/4 [00:02<00:00, 1.34it/s]
2025-02-07 13:15:46,839 [INFO] 0%| | 0/4 [00:00<?, ?it/s]
2025-02-07 13:15:46,840 [INFO] multi-stack tiff read in as having 3 planes 1 channels
2025-02-07 13:15:46,843 [INFO] resizing 3D image with rescale=0.88 and anisotropy=None
2025-02-07 13:15:46,846 [INFO] running YX: 2 planes of size (112, 112)
2025-02-07 13:15:46,847 [INFO]
2025-02-07 13:15:46,847 [INFO] 0%| | 0/1 [00:00<?, ?it/s]
2025-02-07 13:15:46,848 [INFO]
2025-02-07 13:15:46,863 [INFO] 100%|##########| 1/1 [00:00<00:00, 69.85it/s]
2025-02-07 13:15:46,864 [INFO] running ZY: 112 planes of size (2, 112)
2025-02-07 13:15:46,866 [INFO]
2025-02-07 13:15:46,866 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:46,867 [INFO]
2025-02-07 13:15:47,057 [INFO] 100%|##########| 14/14 [00:00<00:00, 73.66it/s]
2025-02-07 13:15:47,059 [INFO] running ZX: 112 planes of size (2, 112)
2025-02-07 13:15:47,060 [INFO]
2025-02-07 13:15:47,061 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:47,061 [INFO]
2025-02-07 13:15:47,250 [INFO] 100%|##########| 14/14 [00:00<00:00, 74.58it/s]
2025-02-07 13:15:47,251 [INFO] resizing 3D flows and cellprob to original image size
2025-02-07 13:15:47,253 [INFO] network run in 0.41s
2025-02-07 13:15:47,555 [INFO] No cell pixels found.
2025-02-07 13:15:47,556 [INFO] masks created in 0.00s
2025-02-07 13:15:47,560 [INFO] multi-stack tiff read in as having 3 planes 1 channels
2025-02-07 13:15:47,563 [INFO] resizing 3D image with rescale=0.88 and anisotropy=None
2025-02-07 13:15:47,565 [INFO] running YX: 2 planes of size (112, 112)
2025-02-07 13:15:47,566 [INFO]
2025-02-07 13:15:47,567 [INFO] 0%| | 0/1 [00:00<?, ?it/s]
2025-02-07 13:15:47,568 [INFO]
2025-02-07 13:15:47,583 [INFO] 100%|##########| 1/1 [00:00<00:00, 69.49it/s]
2025-02-07 13:15:47,584 [INFO] running ZY: 112 planes of size (2, 112)
2025-02-07 13:15:47,585 [INFO]
2025-02-07 13:15:47,586 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:47,586 [INFO]
2025-02-07 13:15:47,776 [INFO] 100%|##########| 14/14 [00:00<00:00, 74.01it/s]
2025-02-07 13:15:47,777 [INFO] running ZX: 112 planes of size (2, 112)
2025-02-07 13:15:47,779 [INFO]
2025-02-07 13:15:47,779 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:47,780 [INFO]
2025-02-07 13:15:47,971 [INFO] 100%|##########| 14/14 [00:00<00:00, 73.52it/s]
2025-02-07 13:15:47,973 [INFO] resizing 3D flows and cellprob to original image size
2025-02-07 13:15:47,975 [INFO] network run in 0.41s
2025-02-07 13:15:48,277 [INFO] No cell pixels found.
2025-02-07 13:15:48,279 [INFO] masks created in 0.00s
2025-02-07 13:15:48,283 [INFO] multi-stack tiff read in as having 3 planes 1 channels
2025-02-07 13:15:48,286 [INFO] resizing 3D image with rescale=0.88 and anisotropy=None
2025-02-07 13:15:48,288 [INFO] running YX: 2 planes of size (112, 112)
2025-02-07 13:15:48,289 [INFO]
2025-02-07 13:15:48,290 [INFO] 0%| | 0/1 [00:00<?, ?it/s]
2025-02-07 13:15:48,290 [INFO]
2025-02-07 13:15:48,305 [INFO] 100%|##########| 1/1 [00:00<00:00, 69.58it/s]
2025-02-07 13:15:48,306 [INFO] running ZY: 112 planes of size (2, 112)
2025-02-07 13:15:48,308 [INFO]
2025-02-07 13:15:48,308 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:48,309 [INFO]
2025-02-07 13:15:48,500 [INFO] 100%|##########| 14/14 [00:00<00:00, 73.64it/s]
2025-02-07 13:15:48,501 [INFO] running ZX: 112 planes of size (2, 112)
2025-02-07 13:15:48,502 [INFO]
2025-02-07 13:15:48,503 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:48,503 [INFO]
2025-02-07 13:15:48,697 [INFO] 100%|##########| 14/14 [00:00<00:00, 72.56it/s]
2025-02-07 13:15:48,699 [INFO] resizing 3D flows and cellprob to original image size
2025-02-07 13:15:48,701 [INFO] network run in 0.41s
2025-02-07 13:15:49,048 [INFO] masks created in 0.05s
2025-02-07 13:15:49,052 [INFO] multi-stack tiff read in as having 3 planes 1 channels
2025-02-07 13:15:49,055 [INFO] resizing 3D image with rescale=0.88 and anisotropy=None
2025-02-07 13:15:49,058 [INFO] running YX: 2 planes of size (112, 112)
2025-02-07 13:15:49,059 [INFO]
2025-02-07 13:15:49,059 [INFO] 0%| | 0/1 [00:00<?, ?it/s]
2025-02-07 13:15:49,060 [INFO]
2025-02-07 13:15:49,075 [INFO] 100%|##########| 1/1 [00:00<00:00, 68.48it/s]
2025-02-07 13:15:49,076 [INFO] running ZY: 112 planes of size (2, 112)
2025-02-07 13:15:49,078 [INFO]
2025-02-07 13:15:49,078 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:49,079 [INFO]
2025-02-07 13:15:49,269 [INFO] 100%|##########| 14/14 [00:00<00:00, 73.67it/s]
2025-02-07 13:15:49,271 [INFO] running ZX: 112 planes of size (2, 112)
2025-02-07 13:15:49,272 [INFO]
2025-02-07 13:15:49,273 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:49,273 [INFO]
2025-02-07 13:15:49,464 [INFO] 100%|##########| 14/14 [00:00<00:00, 73.64it/s]
2025-02-07 13:15:49,466 [INFO] resizing 3D flows and cellprob to original image size
2025-02-07 13:15:49,468 [INFO] network run in 0.41s
2025-02-07 13:15:49,769 [INFO] No cell pixels found.
2025-02-07 13:15:49,771 [INFO] masks created in 0.00s
2025-02-07 13:15:49,775 [INFO] 100%|##########| 4/4 [00:02<00:00, 1.36it/s]
2025-02-07 13:15:49,776 [INFO] 0%| | 0/4 [00:00<?, ?it/s]
2025-02-07 13:15:49,777 [INFO] multi-stack tiff read in as having 3 planes 1 channels
2025-02-07 13:15:49,780 [INFO] resizing 3D image with rescale=0.88 and anisotropy=None
2025-02-07 13:15:49,783 [INFO] running YX: 2 planes of size (112, 112)
2025-02-07 13:15:49,783 [INFO]
2025-02-07 13:15:49,784 [INFO] 0%| | 0/1 [00:00<?, ?it/s]
2025-02-07 13:15:49,785 [INFO]
2025-02-07 13:15:49,800 [INFO] 100%|##########| 1/1 [00:00<00:00, 67.96it/s]
2025-02-07 13:15:49,801 [INFO] running ZY: 112 planes of size (2, 112)
2025-02-07 13:15:49,802 [INFO]
2025-02-07 13:15:49,803 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:49,804 [INFO]
2025-02-07 13:15:49,995 [INFO] 100%|##########| 14/14 [00:00<00:00, 73.50it/s]
2025-02-07 13:15:49,996 [INFO] running ZX: 112 planes of size (2, 112)
2025-02-07 13:15:49,998 [INFO]
2025-02-07 13:15:49,998 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:49,999 [INFO]
2025-02-07 13:15:50,191 [INFO] 100%|##########| 14/14 [00:00<00:00, 73.05it/s]
2025-02-07 13:15:50,192 [INFO] resizing 3D flows and cellprob to original image size
2025-02-07 13:15:50,194 [INFO] network run in 0.41s
2025-02-07 13:15:50,496 [INFO] No cell pixels found.
2025-02-07 13:15:50,498 [INFO] masks created in 0.00s
2025-02-07 13:15:50,502 [INFO] multi-stack tiff read in as having 3 planes 1 channels
2025-02-07 13:15:50,505 [INFO] resizing 3D image with rescale=0.88 and anisotropy=None
2025-02-07 13:15:50,507 [INFO] running YX: 2 planes of size (112, 112)
2025-02-07 13:15:50,508 [INFO]
2025-02-07 13:15:50,509 [INFO] 0%| | 0/1 [00:00<?, ?it/s]
2025-02-07 13:15:50,509 [INFO]
2025-02-07 13:15:50,524 [INFO] 100%|##########| 1/1 [00:00<00:00, 69.23it/s]
2025-02-07 13:15:50,525 [INFO] running ZY: 112 planes of size (2, 112)
2025-02-07 13:15:50,527 [INFO]
2025-02-07 13:15:50,528 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:50,528 [INFO]
2025-02-07 13:15:50,720 [INFO] 100%|##########| 14/14 [00:00<00:00, 73.33it/s]
2025-02-07 13:15:50,721 [INFO] running ZX: 112 planes of size (2, 112)
2025-02-07 13:15:50,722 [INFO]
2025-02-07 13:15:50,723 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:50,723 [INFO]
2025-02-07 13:15:50,916 [INFO] 100%|##########| 14/14 [00:00<00:00, 73.08it/s]
2025-02-07 13:15:50,917 [INFO] resizing 3D flows and cellprob to original image size
2025-02-07 13:15:50,919 [INFO] network run in 0.41s
2025-02-07 13:15:51,220 [INFO] No cell pixels found.
2025-02-07 13:15:51,221 [INFO] masks created in 0.00s
2025-02-07 13:15:51,225 [INFO] multi-stack tiff read in as having 3 planes 1 channels
2025-02-07 13:15:51,228 [INFO] resizing 3D image with rescale=0.88 and anisotropy=None
2025-02-07 13:15:51,231 [INFO] running YX: 2 planes of size (112, 112)
2025-02-07 13:15:51,231 [INFO]
2025-02-07 13:15:51,232 [INFO] 0%| | 0/1 [00:00<?, ?it/s]
2025-02-07 13:15:51,233 [INFO]
2025-02-07 13:15:51,248 [INFO] 100%|##########| 1/1 [00:00<00:00, 69.08it/s]
2025-02-07 13:15:51,249 [INFO] running ZY: 112 planes of size (2, 112)
2025-02-07 13:15:51,251 [INFO]
2025-02-07 13:15:51,251 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:51,252 [INFO]
2025-02-07 13:15:51,445 [INFO] 100%|##########| 14/14 [00:00<00:00, 72.63it/s]
2025-02-07 13:15:51,446 [INFO] running ZX: 112 planes of size (2, 112)
2025-02-07 13:15:51,448 [INFO]
2025-02-07 13:15:51,448 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:51,449 [INFO]
2025-02-07 13:15:51,642 [INFO] 100%|##########| 14/14 [00:00<00:00, 72.64it/s]
2025-02-07 13:15:51,644 [INFO] resizing 3D flows and cellprob to original image size
2025-02-07 13:15:51,646 [INFO] network run in 0.42s
2025-02-07 13:15:51,991 [INFO] masks created in 0.05s
2025-02-07 13:15:51,996 [INFO] multi-stack tiff read in as having 3 planes 1 channels
2025-02-07 13:15:51,999 [INFO] resizing 3D image with rescale=0.88 and anisotropy=None
2025-02-07 13:15:52,001 [INFO] running YX: 2 planes of size (112, 112)
2025-02-07 13:15:52,002 [INFO]
2025-02-07 13:15:52,002 [INFO] 0%| | 0/1 [00:00<?, ?it/s]
2025-02-07 13:15:52,003 [INFO]
2025-02-07 13:15:52,018 [INFO] 100%|##########| 1/1 [00:00<00:00, 70.15it/s]
2025-02-07 13:15:52,019 [INFO] running ZY: 112 planes of size (2, 112)
2025-02-07 13:15:52,021 [INFO]
2025-02-07 13:15:52,021 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:52,022 [INFO]
2025-02-07 13:15:52,213 [INFO] 100%|##########| 14/14 [00:00<00:00, 73.37it/s]
2025-02-07 13:15:52,215 [INFO] running ZX: 112 planes of size (2, 112)
2025-02-07 13:15:52,216 [INFO]
2025-02-07 13:15:52,217 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:15:52,217 [INFO]
2025-02-07 13:15:52,409 [INFO] 100%|##########| 14/14 [00:00<00:00, 73.18it/s]
2025-02-07 13:15:52,411 [INFO] resizing 3D flows and cellprob to original image size
2025-02-07 13:15:52,413 [INFO] network run in 0.41s
2025-02-07 13:15:52,712 [INFO] No cell pixels found.
2025-02-07 13:15:52,713 [INFO] masks created in 0.00s
2025-02-07 13:15:52,717 [INFO] 100%|##########| 4/4 [00:02<00:00, 1.36it/s]

@Jiadalee
Copy link
Author

Jiadalee commented Feb 7, 2025

@carsen-stringer if I use gpu = false, then the eval outputs log info would be like below. Cee pixels can be found and masks are predicted and created

start running model evaluation...for 3D segmentation
2025-02-07 13:51:50,271 [INFO] multi-stack tiff read in as having 4 planes 3 channels
2025-02-07 13:51:50,275 [INFO] resizing 3D image with rescale=0.88 and anisotropy=None
2025-02-07 13:51:50,278 [INFO] running YX: 3 planes of size (112, 112)
2025-02-07 13:51:50,279 [INFO] 0%| | 0/1 [00:00<?, ?it/s]
2025-02-07 13:51:50,608 [INFO] 100%|##########| 1/1 [00:00<00:00, 3.05it/s]
2025-02-07 13:51:50,609 [INFO] running ZY: 112 planes of size (3, 112)
2025-02-07 13:51:50,611 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:51:51,600 [INFO] 100%|##########| 14/14 [00:00<00:00, 14.17it/s]
2025-02-07 13:51:51,601 [INFO] running ZX: 112 planes of size (3, 112)
2025-02-07 13:51:51,603 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:51:52,588 [INFO] 100%|##########| 14/14 [00:00<00:00, 14.22it/s]
2025-02-07 13:51:52,590 [INFO] resizing 3D flows and cellprob to original image size
2025-02-07 13:51:52,593 [INFO] network run in 2.32s
2025-02-07 13:51:53,000 [INFO] masks created in 0.08s
2025-02-07 13:51:53,006 [INFO] multi-stack tiff read in as having 4 planes 3 channels
2025-02-07 13:51:53,009 [INFO] resizing 3D image with rescale=0.88 and anisotropy=None
2025-02-07 13:51:53,012 [INFO] running YX: 3 planes of size (112, 112)
2025-02-07 13:51:53,013 [INFO] 0%| | 0/1 [00:00<?, ?it/s]
2025-02-07 13:51:53,111 [INFO] 100%|##########| 1/1 [00:00<00:00, 10.29it/s]
2025-02-07 13:51:53,112 [INFO] running ZY: 112 planes of size (3, 112)
2025-02-07 13:51:53,114 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:51:54,105 [INFO] 100%|##########| 14/14 [00:00<00:00, 14.14it/s]
2025-02-07 13:51:54,107 [INFO] running ZX: 112 planes of size (3, 112)
2025-02-07 13:51:54,108 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:51:55,109 [INFO] 100%|##########| 14/14 [00:01<00:00, 14.00it/s]
2025-02-07 13:51:55,111 [INFO] resizing 3D flows and cellprob to original image size
2025-02-07 13:51:55,113 [INFO] network run in 2.10s
2025-02-07 13:51:55,499 [INFO] masks created in 0.08s
2025-02-07 13:51:55,506 [INFO] multi-stack tiff read in as having 4 planes 3 channels
2025-02-07 13:51:55,510 [INFO] resizing 3D image with rescale=0.88 and anisotropy=None
2025-02-07 13:51:55,513 [INFO] running YX: 3 planes of size (112, 112)
2025-02-07 13:51:55,514 [INFO] 0%| | 0/1 [00:00<?, ?it/s]
2025-02-07 13:51:55,617 [INFO] 100%|##########| 1/1 [00:00<00:00, 9.75it/s]
2025-02-07 13:51:55,619 [INFO] running ZY: 112 planes of size (3, 112)
2025-02-07 13:51:55,621 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:51:56,609 [INFO] 100%|##########| 14/14 [00:00<00:00, 14.17it/s]
2025-02-07 13:51:56,611 [INFO] running ZX: 112 planes of size (3, 112)
2025-02-07 13:51:56,612 [INFO] 0%| | 0/14 [00:00<?, ?it/s]
2025-02-07 13:51:57,595 [INFO] 100%|##########| 14/14 [00:00<00:00, 14.25it/s]
2025-02-07 13:51:57,597 [INFO] resizing 3D flows and cellprob to original image size
2025-02-07 13:51:57,600 [INFO] network run in 2.09s
2025-02-07 13:51:57,980 [INFO] masks created in 0.08

@carsen-stringer
Copy link
Member

thanks, can you please include the log output when the model is made?

@carsen-stringer
Copy link
Member

btw I think you want to run with "do_3D=False" because you are using "stitch_threshold"

@Jiadalee
Copy link
Author

Jiadalee commented Feb 7, 2025

thanks, can you please include the log output when the model is made?

do you mean the log info of training the model? I set do_3D = True since I need to do segmentation over multiple-channel RGB images. Isn't stitch_threshold a parameter that I need to set for 3D segmentation?

@Jiadalee
Copy link
Author

Jiadalee commented Feb 10, 2025

@carsen-stringer Hello, just to check back this model re-loading issue. Setting GPU = Yes or No should not be an issue now, as long as your training and testing stages have consistent GPU setting, but I'm still quite confused about your reply of

the model.net has been modified in the train function and should be equivalent to the call with the trained model path above.

Below, I compared these two methods to reload the trained model.

Firstly, my model training is like below:

#Model train: 
try:
    model_path_pretrained = "Microscopy/Image Analysis/trained_models/models/cyto3"
    model_pretrained = models.CellposeModel(gpu=True, pretrained_model=model_path_pretrained)
except Exception as e:
    print(f"An error occurred while loading the model: {e}")

channels = [2,1]
model_name = "cyto3_finetuned_new"

# run training cyto3 model 
new_model_path, train_losses, val_losses = train.train_seg(model_pretrained.net,
                              train_data=train_images,
                              train_labels=train_masks,
                              test_data=val_images,
                              test_labels=val_masks,
                              channels=channels,
                              save_path=model_dir,
                              n_epochs=200,
                              learning_rate=0.01,
                              weight_decay=0.0001,
                              SGD=True,                        # set False to Use Adam optimizer
                              nimg_per_epoch=8,
                              model_name=model_name,
                              min_train_masks=0)         # Allow training with fewer masks	

Secondly, method No.1 to reload the trained model:

#Model reloading Method No.1
model_finetuned = models.CellposeModel(gpu=True, model_type="cyto3")

Then, method No.2 to reload the trained model:

#Model reloading Method No.2
model_path_finetuned = "Microscopy/Image Analysis/trained_models/models/cyto3_finetuned_new"
model_finetuned = models.CellposeModel(gpu=True, pretrained_model=model_path_finetuned)

If I understood your previous response correctly, model reloading method No.1 is the same as the method No.2, but actually they are not although I just run testing immediately after training the model within a workflow.

for testing the model, as you suggested, I just removed the do_3D = True and stitch_threshold=0.1, see below

#Run model testing
pred_masks_list = []
for ch in channel_options:
    pred_masks = model_finetuned.eval(
        test_images,                                     # TIFF image has 4 planes and 3 channels
        channels=ch,                                   # Pass one channel configuration at a time
        flow_threshold=flow_threshold,
        cellprob_threshold=cellprob_threshold,                           
        diameter=measured_diameter + 4         
    )[0]  # Extract masks

    pred_masks_list.append(pred_masks)

Unfortunately, the model testing performance based on these two reloading methods are significantly different. The first loading method has IOU of around 0.96 while the second loading method has IOU of around 0.54. Could you please help check if my codes and model implementation are somehow wrong? I would appreciate your help.

@carsen-stringer
Copy link
Member

what I mean is you do not have to load a model, since it has been trained in your notebook.

you are loading different models with those commands, you can see in the logger output which model is loaded. I would guess that means your finetuned model is bad, which would make sense if you keep retraining it over and over - you should not start training from an already finetuned model, please follow the cellpose2 notebook, which starts with a built-in cellpose model to retrain from.

@Jiadalee
Copy link
Author

what I mean is you do not have to load a model, since it has been trained in your notebook.

you are loading different models with those commands, you can see in the logger output which model is loaded. I would guess that means your finetuned model is bad, which would make sense if you keep retraining it over and over - you should not start training from an already finetuned model, please follow the cellpose2 notebook, which starts with a built-in cellpose model to retrain from.

@carsen-stringer thank you for the clarification. I will check out the cellpose2 notebook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants