MouseLand / cellpose

a generalist algorithm for cellular segmentation with human-in-the-loop capabilities
https://www.cellpose.org/
BSD 3-Clause "New" or "Revised" License
1.36k stars 390 forks source link

[BUG] Index error on grayscale image training #805

Closed shoyip closed 12 months ago

shoyip commented 1 year ago

Describe the bug I am running a CellPose workflow for the nuclei model fine-tuning. I have greyscale images with 0/255 mask tiff.

image

To Reproduce Steps to reproduce the behavior:

  1. Run python -m cellpose --train --train_size --use_gpu --dir data/train/ --pretrained_model nuclei --verbose --mask_filter _masks --chan 0 --n_epochs 10

This is what I get.

2023-10-17 18:30:10,222 [INFO] WRITING LOG OUTPUT TO /home/artswarms/.cellpose/run.log
2023-10-17 18:30:10,222 [INFO] 
cellpose version:   2.2.3 
platform:           linux 
python version:     3.8.18 
torch version:      2.1.0+cu121
2023-10-17 18:30:10,289 [INFO] ** TORCH CUDA version installed and working. **
2023-10-17 18:30:10,289 [INFO] >>>> using GPU
2023-10-17 18:30:10,311 [INFO] not all flows are present, running flow generation for all images
2023-10-17 18:30:11,646 [INFO] 551 / 551 images in data/train/ folder have labels
2023-10-17 18:30:11,646 [INFO] >>>> during training rescaling images to fixed diameter of 30.0 pixels
2023-10-17 18:30:11,646 [INFO] >> nuclei << model set to be used
2023-10-17 18:30:11,713 [INFO] >>>> model diam_mean =  17.000 (ROIs rescaled to this size during training)
2023-10-17 18:30:16,855 [INFO] computing flows for labels
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 551/551 [00:16<00:00, 32.62it/s]
2023-10-17 18:30:35,872 [WARNING] 551 train images with number of masks less than min_train_masks (5), removing from train set
/home/artswarms/anaconda3/envs/cellpose/lib/python3.8/site-packages/cellpose/core.py:831: RuntimeWarning: Mean of empty slice.
  diam_train_mean = diam_train[diam_train > 0].mean()
/home/artswarms/anaconda3/envs/cellpose/lib/python3.8/site-packages/numpy/core/_methods.py:192: RuntimeWarning: invalid value encountered in scalar divide
  ret = ret.dtype.type(ret / rcount)
2023-10-17 18:30:36,052 [INFO] >>>> median diameter set to = 17
2023-10-17 18:30:36,052 [INFO] >>>> mean of training label mask diameters (saved to model) nan
Traceback (most recent call last):
  File "/home/artswarms/anaconda3/envs/cellpose/lib/python3.8/runpy.py", line 194, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/artswarms/anaconda3/envs/cellpose/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/artswarms/anaconda3/envs/cellpose/lib/python3.8/site-packages/cellpose/__main__.py", line 276, in <module>
    main()
  File "/home/artswarms/anaconda3/envs/cellpose/lib/python3.8/site-packages/cellpose/__main__.py", line 242, in main
    cpmodel_path = model.train(images, labels, train_files=image_names,
  File "/home/artswarms/anaconda3/envs/cellpose/lib/python3.8/site-packages/cellpose/models.py", line 789, in train
    model_path = self._train_net(train_data, train_flows, 
  File "/home/artswarms/anaconda3/envs/cellpose/lib/python3.8/site-packages/cellpose/core.py", line 846, in _train_net
    nchan = train_data[0].shape[0]
IndexError: list index out of range

What could be the cause of this? The error doesn't seem to be too explanatory by itself. Thanks in advance

Liuqian181 commented 1 year ago

Note Cellpose expects the labelled masks (0=no mask, 1,2…=masks) in a separate file, that is, in the mask images, different objects have different values 1,2,3…

shoyip commented 12 months ago

Yes, I have seen. I used opencv to perform the relabelling. Is this something that would be meaningful to include (at least the correct error message) in Cellpose? I think it would be useful. I can make the request in case.

img = cv2.imread(filename_in, cv2.IMREAD_GRAYSCALE)
labeled = cv2.connectedComponents(img)[1]
mrariden commented 11 months ago

@shoyip that would work for datasets that don't have touching cells. For datasets that have touching cells, connected components mask reconstruction would fail.

At this level, you (users in general) will know your data better than we can predict, so it makes the most sense to leave it as is.

More explicit documentation is warranted, however, and we'll remedy that.

CristiSoitu commented 2 months ago

did anyone figure this out? I'm having the same issue