STOmics / Stereopy

A toolkit of spatial transcriptomic analysis.
MIT License
187 stars 64 forks source link

ModuleNotFound error when importing cell_seg_v3 from stereo.image #141

Closed playgamemy closed 1 year ago

playgamemy commented 1 year ago

Tried to import and use the deep learning model v3 as listed in here https://stereopy.readthedocs.io/en/latest/Tutorials/Cell_Segmentation.html which claims no install of tensorflow and torch is required,

However, since image module mport the deep learning v1 module on initiation, ModuleNotFound error occurs when following code runs

Maybe convert "raise" to "warning" to allow importing cell_seg_v3 without error raised, eventuallly these should be moved to init of individual method instead of the image module.

from stereo.image import cell_seg_v3 as cell_seg_v3

model_path = './cell_segmetation_v3.0.onnx'
img_path = './SS200000135TL_D1_regist.tif'
out_path = './SS200000135TL_D1_regist.cell_cut.tif'

cell_seg_v3(model_path, img_path, out_path, need_tissue_cut=False)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
File ~\miniconda3\envs\st\lib\site-packages\stereo\image\__init__.py:5
      4 from .pyramid import merge_pyramid, create_pyramid
----> 5 from .segmentation.segment import cell_seg
      6 from .cellbin.modules.cell_segmentation import cell_seg_v3

File ~\miniconda3\envs\st\lib\site-packages\stereo\image\segmentation\segment.py:5
      4 import os
----> 5 import seg_utils.cell_seg_pipeline as pipeline
      8 def cell_seg(
      9         model_path: str, 
     10         img_path: str, 
   (...)
     17         post_processing_workers: int=10
     18     ):

File ~\miniconda3\envs\st\lib\site-packages\stereo\image\segmentation\seg_utils\cell_seg_pipeline.py:16
     15 from . import tissue_seg
---> 16 import seg_utils.cell_infer as cell_infer
     17 from stereo.image.tissue_cut import SingleStrandDNATissueCut, DEEP, INTENSITY

File ~\miniconda3\envs\st\lib\site-packages\stereo\image\segmentation\seg_utils\cell_infer.py:4
      3 import logging
----> 4 import torch
      5 from seg_utils.utils import normalize, cell_watershed, resize, tile_image, untile_image, split, merge, outline, view_bar

ModuleNotFoundError: No module named 'torch'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
Cell In[3], line 3
      1 import warnings
      2 warnings.filterwarnings('ignore')
----> 3 from stereo.image import cell_seg_v3 as cell_seg_v3
      5 model_path = './cell_segmetation_v3.0.onnx'
      6 img_path = './SS200000135TL_D1_regist.tif'

File ~\miniconda3\envs\st\lib\site-packages\stereo\image\__init__.py:22
     10 except ImportError as e:
     11     errmsg = """function `merge_pyramid`, `create_pyramid`, `cell_seg`, `cell_seg_v3`, `tissue_cut`, 
     12 `cell_seg_deepcell` is not import at `stereo.image` module.
     13 ************************************************
   (...)
     20 ************************************************
     21     """
---> 22     raise ImportError(errmsg)

ImportError: function `merge_pyramid`, `create_pyramid`, `cell_seg`, `cell_seg_v3`, `tissue_cut`, 
`cell_seg_deepcell` is not import at `stereo.image` module.
************************************************
* Some necessary modules may not be installed. *
* Please install them by:                      *
*   pip install tensorflow==2.7.0              *
*   pip install torch==1.10.0                  *
*   pip install torchvision==0.11.1            *
*   pip install albumentations==0.4.6          *
************************************************
Tripfantasy commented 1 year ago

Hello! I have a similar error. When following the guide for v3 cell segmentation model:

AttributeError: module 'stereo.image' has no attribute 'cell_seg_v3'

Checked module attributes to see if it exists via print(dir(im)): ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'cell_seg', 'cell_seg_deepcell', 'create_pyramid', 'merge_pyramid', 'pyramid', 'segmentation', 'segmentation_deepcell', 'tissue_cut']

Confirms` that cell_seg_v3 does not exist. Out of curiosity, I tried running im.cell_seg() with the model_path arg pointing to the v3 model. This results in a pickling error:

File "/home/dm2763/micromamba/envs/segmentation/lib/python3.8/site-packages/torch/serialization.py", line 920, in _legacy_load magic_number = pickle_module.load(f, **pickle_load_args) _pickle.UnpicklingError: invalid load key, '\x08'.

After outputting: [2023-07-06 15:50:54][Stereo][1107028][140495023171392][cell_seg_pipeline][45][INFO]: Transform 16bit to 8bit : 0.00

[2023-07-06 15:50:54][Stereo][1107028][140495023171392][cell_seg_pipeline][100][INFO]: get tissue mask, process 1

[2023-07-06 15:50:59][Stereo][1107028][140495023171392][cell_seg_pipeline][126][INFO]: get roi

Checked v1 model to see if it still functions properly (to see if my environment is bugged) and seems to work fine.

tanliwei-coder commented 1 year ago

Hi! @playgamemy

As mentioned in the bottom of the Exception, you need to install some necessary modules.

tanliwei-coder commented 1 year ago

Hi!! @Tripfantasy , what is the version of your stereopy? the cell_seg_v3 is in stereopy from 0.12.0 and the cell_seg can not support the v3 model.

Tripfantasy commented 1 year ago

Hello @tanliwei-coder I've updated to 0.12.1 and now receive a similar error to @playgamemy above, despite installing the necessary modules and versions. Running pip/conda list shows them existing at the preferred versions. I have even tried a new conda environment with dev install using git repo + installing requirements.txt. Same error.

image

playgamemy commented 1 year ago

Hi! @playgamemy

As mentioned in the bottom of the Exception, you need to install some necessary modules. image

@tanliwei-coder But as the tutorial indicated, the cell_seg_v3 has converted to .onnx model and should require only the onnx runtime library. If that is true the error raised cannot be ignored during import of cell_seg_v3.

FYI, i am using Stereopy 0.12.1, and I can run cell_seg_v3 after installing the "necessary" modules, so my issue is different from that of @Tripfantasy

tanliwei-coder commented 1 year ago

@playgamemy

Because the cell_seg_v3 is in stereo.image module which is the same as cell_seg and cell_seg_deepcell, and those necessary modules mentioned in the Excepion is necessary for cell_seg and cell_seg_deepcell, so it will check if installed while importing stereo.image.

Tripfantasy commented 1 year ago

@tanliwei-coder I've updated to the proper version and still have all dependencies installed at proper versions. I was able to eliminate the error from running 'sudo apt-get install -y libgl1-mesa-dev'.

tanliwei-coder commented 1 year ago

@Tripfantasy OK, I have installed this library(libgl1-mesa-dev) when I created my docker image based on my past experience, despite I can't remember which python module dependent on it (seems to be opencv), so I never encounter this error, thanks for your feedback.