Project-MONAI / model-zoo

MONAI Model Zoo that hosts models in the MONAI Bundle format.
Apache License 2.0
179 stars 67 forks source link

wholeBrainSeg_Large_UNEST_segmentation fails to be instantiated. #466

Closed lemuelatQSA closed 1 year ago

lemuelatQSA commented 1 year ago

Describe the bug

Model wholeBrainSeg_Large_UNEST_segmentation fails to be instantiated.

Reporting the error below:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
File ~/anaconda3/envs/monai/lib/python3.9/site-packages/monai/bundle/config_item.py:292, in ConfigComponent.instantiate(self, **kwargs)
    291 try:
--> 292     return instantiate(modname, mode, **args)
    293 except Exception as e:

File ~/anaconda3/envs/monai/lib/python3.9/site-packages/monai/utils/module.py:247, in instantiate(__path, __mode, **kwargs)
    246 if component is None:
--> 247     raise ModuleNotFoundError(f"Cannot locate class or function path: '{__path}'.")
    248 m = look_up_option(__mode, CompInitMode)

ModuleNotFoundError: Cannot locate class or function path: 'scripts.networks.unest_base_patch_4.UNesT'.

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
Cell In[6], line 2
      1 preprocessing  = model_config.get_parsed_content('preprocessing')
----> 2 model          = model_config.get_parsed_content('network')
      3 inferer        = model_config.get_parsed_content('inferer')
      4 postprocessing = model_config.get_parsed_content('postprocessing')

File ~/anaconda3/envs/monai/lib/python3.9/site-packages/monai/bundle/config_parser.py:291, in ConfigParser.get_parsed_content(self, id, **kwargs)
    289 elif not kwargs.get("lazy", True):
    290     self.parse(reset=not kwargs.get("lazy", True))
--> 291 return self.ref_resolver.get_resolved_content(id=id, **kwargs)

File ~/anaconda3/envs/monai/lib/python3.9/site-packages/monai/bundle/reference_resolver.py:190, in ReferenceResolver.get_resolved_content(self, id, **kwargs)
    178 def get_resolved_content(self, id: str, **kwargs: Any) -> ConfigExpression | str | Any | None:
    179     """
    180     Get the resolved ``ConfigItem`` by id.
    181 
   (...)
    188 
    189     """
--> 190     return self._resolve_one_item(id=id, **kwargs)

File ~/anaconda3/envs/monai/lib/python3.9/site-packages/monai/bundle/reference_resolver.py:160, in ReferenceResolver._resolve_one_item(self, id, waiting_list, **kwargs)
    158                 raise ValueError(msg) from err
    159         # recursively resolve the reference first
--> 160         self._resolve_one_item(id=d, waiting_list=waiting_list, **kwargs)
    161         waiting_list.discard(d)
    163 # all references are resolved, then try to resolve current config item

File ~/anaconda3/envs/monai/lib/python3.9/site-packages/monai/bundle/reference_resolver.py:168, in ReferenceResolver._resolve_one_item(self, id, waiting_list, **kwargs)
    166 # save the resolved result into `resolved_content` to recursively resolve others
    167 if isinstance(item, ConfigComponent):
--> 168     self.resolved_content[id] = item.instantiate() if kwargs.get("instantiate", True) else item
    169 elif isinstance(item, ConfigExpression):
    170     run_eval = kwargs.get("eval_expr", True)

File ~/anaconda3/envs/monai/lib/python3.9/site-packages/monai/bundle/config_item.py:294, in ConfigComponent.instantiate(self, **kwargs)
    292     return instantiate(modname, mode, **args)
    293 except Exception as e:
--> 294     raise RuntimeError(f"Failed to instantiate {self}.") from e

RuntimeError: Failed to instantiate {'_target_': 'scripts.networks.unest_base_patch_4.UNesT', 'in_channels': 1, 'out_channels': 133, 'patch_size': 4, 'depths': [2, 2, 8], 'embed_dim': [128, 256, 512], 'num_heads': [4, 8, 16]}.

To Reproduce

Code:

import os
import torch
import monai
from monai.bundle import ConfigParser, download

BUNDLES_DIR = os.path.abspath('models/')
OUTPUTS_DIR = os.path.abspath('monai_results/')
MODEL_NAME = 'wholeBrainSeg_Large_UNEST_segmentation'
MODEL_VERS = '0.2.2'

download(name=MODEL_NAME, version=MODEL_VERS, bundle_dir=BUNDLES_DIR)

model_config = ConfigParser()
model_config.read_config(os.path.join(BUNDLES_DIR, MODEL_NAME, 'configs', 'inference.json'))
model_config['bundle_root'] = BUNDLES_DIR
model_config['output_dir']  = OUTPUTS_DIR

preprocessing  = model_config.get_parsed_content('preprocessing')
model          = model_config.get_parsed_content('network') # error here!
inferer        = model_config.get_parsed_content('inferer')
postprocessing = model_config.get_parsed_content('postprocessing')

Expected behavior

To instantiate the model correctly.

Environment

================================
Printing MONAI config...
================================
MONAI version: 1.2.0
Numpy version: 1.25.0
Pytorch version: 1.13.1
MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False
MONAI rev id: c33f1ba588ee00229a309000e888f9817b4f1934
MONAI __file__: /home/charlemgn/anaconda3/envs/monai/lib/python3.9/site-packages/monai/__init__.py

Optional dependencies:
Pytorch Ignite version: NOT INSTALLED or UNKNOWN VERSION.
ITK version: NOT INSTALLED or UNKNOWN VERSION.
Nibabel version: 5.1.0
scikit-image version: NOT INSTALLED or UNKNOWN VERSION.
Pillow version: 9.4.0
Tensorboard version: NOT INSTALLED or UNKNOWN VERSION.
gdown version: NOT INSTALLED or UNKNOWN VERSION.
TorchVision version: 0.14.1
tqdm version: 4.65.0
lmdb version: NOT INSTALLED or UNKNOWN VERSION.
psutil version: 5.9.0
pandas version: NOT INSTALLED or UNKNOWN VERSION.
einops version: NOT INSTALLED or UNKNOWN VERSION.
transformers version: NOT INSTALLED or UNKNOWN VERSION.
mlflow version: NOT INSTALLED or UNKNOWN VERSION.
pynrrd version: NOT INSTALLED or UNKNOWN VERSION.

For details about installing the optional dependencies, please visit:
    https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies

================================
Printing system config...
================================
System: Linux
Linux version: Ubuntu 20.10
Platform: Linux-5.15.0-46-generic-x86_64-with-glibc2.32
Processor: x86_64
Machine: x86_64
Python version: 3.9.17
Process name: python
Command: ['python', '-c', 'import monai; monai.config.print_debug_info()']
Open files: []
Num physical CPUs: 4
Num logical CPUs: 4
Num usable CPUs: 4
CPU usage (%): [10.9, 8.7, 8.7, 100.0]
CPU freq. (MHz): 4
Load avg. in last 1, 5, 15 mins (%): [21.2, 45.0, 51.5]
Disk usage (%): 93.6
Avg. sensor temp. (Celsius): UNKNOWN for given OS
Total physical memory (GB): 31.2
Available memory (GB): 24.9
Used memory (GB): 5.7

================================
Printing GPU config...
================================
Num GPUs: 1
Has CUDA: True
CUDA version: 11.7
cuDNN enabled: True
cuDNN version: 8500
Current device: 0
Library compiled for CUDA architectures: ['sm_37', 'sm_50', 'sm_60', 'sm_61', 'sm_70', 'sm_75', 'sm_80', 'sm_86', 'compute_37']
GPU 0 Name: NVIDIA GeForce GTX 1050 Ti
GPU 0 Is integrated: False
GPU 0 Is multi GPU board: False
GPU 0 Multi processor count: 6
GPU 0 Total memory (GB): 3.9
GPU 0 CUDA capability (maj.min): 6.1
yiheng-wang-nv commented 1 year ago

Hi @lemuelatQSA , according to the error message:

ModuleNotFoundError: Cannot locate class or function path: 'scripts.networks.unest_base_patch_4.UNesT'.

could you refer to the readme and export the pythonpath, and then retry the code?

export PYTHONPATH=$PYTHONPATH: '<path to the bundle root dir>/scripts'
lemuelatQSA commented 1 year ago

Hi @yiheng-wang-nv and thank you for the prompt response.

The error persists after adding the scripts folder to the python path:

export PYTHONPATH=$PYTHONPATH:/data/projects/brain-segmentation-monai/models/wholeBrainSeg_Large_UNEST_segmentation/scripts; python app.py

Output:

/home/charlemgn/anaconda3/envs/monai/lib/python3.9/site-packages/monai/utils/deprecate_utils.py:321: FutureWarning: monai.transforms.io.dictionary LoadImaged.__init__:image_only: Current default value of argument `image_only=False` has been deprecated since version 1.1. It will be changed to `image_only=True` in version 1.3.
  warn_deprecated(argname, msg, warning_category)
Traceback (most recent call last):
  File "/home/charlemgn/anaconda3/envs/monai/lib/python3.9/site-packages/monai/bundle/config_item.py", line 292, in instantiate
    return instantiate(modname, mode, **args)
  File "/home/charlemgn/anaconda3/envs/monai/lib/python3.9/site-packages/monai/utils/module.py", line 247, in instantiate
    raise ModuleNotFoundError(f"Cannot locate class or function path: '{__path}'.")
ModuleNotFoundError: Cannot locate class or function path: 'scripts.networks.unest_base_patch_4.UNesT'.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/data/Progetti/brain-segmentation-monai/test1.py", line 19, in <module>
    model          = model_config.get_parsed_content('network') # error here!
  File "/home/charlemgn/anaconda3/envs/monai/lib/python3.9/site-packages/monai/bundle/config_parser.py", line 291, in get_parsed_content
    return self.ref_resolver.get_resolved_content(id=id, **kwargs)
  File "/home/charlemgn/anaconda3/envs/monai/lib/python3.9/site-packages/monai/bundle/reference_resolver.py", line 190, in get_resolved_content
    return self._resolve_one_item(id=id, **kwargs)
  File "/home/charlemgn/anaconda3/envs/monai/lib/python3.9/site-packages/monai/bundle/reference_resolver.py", line 160, in _resolve_one_item
    self._resolve_one_item(id=d, waiting_list=waiting_list, **kwargs)
  File "/home/charlemgn/anaconda3/envs/monai/lib/python3.9/site-packages/monai/bundle/reference_resolver.py", line 168, in _resolve_one_item
    self.resolved_content[id] = item.instantiate() if kwargs.get("instantiate", True) else item
  File "/home/charlemgn/anaconda3/envs/monai/lib/python3.9/site-packages/monai/bundle/config_item.py", line 294, in instantiate
    raise RuntimeError(f"Failed to instantiate {self}.") from e
RuntimeError: Failed to instantiate {'_target_': 'scripts.networks.unest_base_patch_4.UNesT', 'in_channels': 1, 'out_channels': 133, 'patch_size': 4, 'depths': [2, 2, 8], 'embed_dim': [128, 256, 512], 'num_heads': [4, 8, 16]}.
yiheng-wang-nv commented 1 year ago

Hi @lemuelatQSA , can you try:

export PYTHONPATH=$PYTHONPATH:/data/projects/brain-segmentation-monai/models/wholeBrainSeg_Large_UNEST_segmentation; python app.py

Hi @tangy5 , I think the readme has a mistake, in the bundle config it uses scripts., thus the python path should not include scripts, could you please help to double confirm it and fix the issue of this bundle if needed? Thanks in advance!

lemuelatQSA commented 1 year ago

Thank you @yiheng-wang-nv, removing scripts made it work :)