Loading .NRRD file

Hello,

I have a @MONAI UNETR model that I trained and am using for inference in a 3D Slicer module. I’ve been using this with .nii.gz format files and would now like to add .nrrd filetypes. I installed the optional itk dependencies and was able to load the .nrrd filetypes with the monai.data.image_reader.ITKReader() on Windows OS\CPU. However, when I run the same code on Linux\CPU or GPU, it crashes the application with no error message. The second of the following two lines produces the crash when the filetype is nrrd:

file='/home/sara/MONAI/ModuleTest/testModuleVolume/undeterminedSex_AAPN_K1026-1-e15.5_Cbx4.nrrd'
myReader = monai.data.image_reader.ITKReader()
img_obj = myReader.read(file)

My model was trained using MONAI 0.8.0. I have tried updating to 0.9.0 and using the NRRDReader(), which does work, but I believe the UNETR model was updated between these and so I’m getting the following error when I try to load the model:

  File "/home2/sara/Slicer_Preview/MEMOS/MEMOS/MEMOS.py", line 251, in run
    net.load_state_dict(torch.load(modelPath, map_location='cpu'))
  File "/home/sara/Slicer_stable/Slicer-5.0.2-linux-amd64/lib/Python/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1482, in load_state_dict
    raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
RuntimeError: Error(s) in loading state_dict for UNETR:
	Unexpected key(s) in state_dict: "vit.patch_embedding.cls_token", "encoder2.blocks.0.1.conv3.conv.weight", "encoder2.blocks.1.1.conv3.conv.weight", "encoder3.blocks.0.1.conv3.conv.weight". 

I would appreciate some advice on the best way to proceed. Is it be possible to convert my model to be compatible with MONAI 0.9.0, or is there a fix to get the ITKReader working in Linux?

Thanks,
Sara

Do you see the same error using a publicly available .nrrd file which we could use to reproduce the issue?
If so, could you post the steps you’ve used to install the optional dependencies so that we could try to debug the issue?

Thanks, I posted a publicly available .nrrd file here: SampleData/undeterminedSex_AAPN_K1026-1-e15.5_Cbx4.nrrd at master · smrolfe/SampleData · GitHub

My installation steps are:

slicer.util.pip_install('monai[itk]==0.8.0')
slicer.util.pip_install('pillow')
slicer.util.pip_install('nibabel')
slicer.util.pip_install('einops')
slicer.util.pip_install(‘torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113’)

Thanks for the file!
I’m currently unsure how to use slicer and it seems it has an own (pip-)package manager?

I’m able to load the uploaded .nrrd file by installing the current stable torch==1.12.0 release and by building MONAI will all image backends via running:

pip install -e '.[all]'

in the MONAI source folder.

Code:

import torch
import monai

file='./undeterminedSex_AAPN_K1026-1-e15.5_Cbx4.nrrd'
myReader = monai.data.image_reader.ITKReader()
img_obj = myReader.read(file)
print(img_obj.dtype)
# <class 'numpy.uint8'>

I’ve also tested the current monai==0.9.0 release via

pip install monai[all]

as well as monai[all]==0.8.0 as it seems you would need this version, and all are able to load the image.

Could you run your code with gdb and post the stacktrace here?

gdb --args python script.py
...
run
...
bt

When instalingl monai[all], I ran into an error on linux when installing openslide-python. I get the same error when running:
pip_install('openslide-python')

This appears to be OS specific, since the install command works from my Windows application. I am using 3D Slicer’s built tin pip install function. Is it possible that this dependency could cause the crash when reading .nrrd files?

What is the error you are seeing when installing openslide-python?

I am getting the following errors:

>>> pip_install('openslide-python')
Collecting openslide-python
  Using cached openslide-python-1.2.0.tar.gz (338 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Requirement already satisfied: Pillow in ./lib/Python/lib/python3.9/site-packages (from openslide-python) (9.0.1)
Building wheels for collected packages: openslide-python
  Building wheel for openslide-python (setup.py): started
  Building wheel for openslide-python (setup.py): finished with status 'error'
  error: subprocess-exited-with-error

  � python setup.py bdist_wheel did not run successfully.
  ? exit code: 1
  ??> [16 lines of output]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.linux-x86_64-3.9
      creating build/lib.linux-x86_64-3.9/openslide
      copying openslide/__init__.py -> build/lib.linux-x86_64-3.9/openslide
      copying openslide/_version.py -> build/lib.linux-x86_64-3.9/openslide
      copying openslide/lowlevel.py -> build/lib.linux-x86_64-3.9/openslide
      copying openslide/deepzoom.py -> build/lib.linux-x86_64-3.9/openslide
      running build_ext
      building 'openslide._convert' extension
      creating build/temp.linux-x86_64-3.9
      creating build/temp.linux-x86_64-3.9/openslide
      /opt/rh/devtoolset-7/root/usr/bin/gcc -pthread -std=c99 -Wall -Wstrict-prototypes -fno-strict-aliasing -fwrapv -g -fPIC -I/home/sara/Slicer_stable/Slicer-5.0.2-linux-amd64/lib/Python/include/python3.9 -c openslide/_convert.c -o build/temp.linux-x86_64-3.9/openslide/_convert.o
      error: command '/opt/rh/devtoolset-7/root/usr/bin/gcc' failed: No such file or directory
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for openslide-python
  Running setup.py clean for openslide-python
Failed to build openslide-python
Installing collected packages: openslide-python
  Running setup.py install for openslide-python: started
  Running setup.py install for openslide-python: finished with status 'error'
  error: subprocess-exited-with-error

  � Running setup.py install for openslide-python did not run successfully.
  ? exit code: 1
  ??> [18 lines of output]
      running install
      /home/sara/Slicer_stable/Slicer-5.0.2-linux-amd64/lib/Python/lib/python3.9/site-packages/setuptools/command/install.py:34: SetuptoolsDeprecationWarning: setup.py install is deprecated. Use build and pip and other standards-based tools.
        warnings.warn(
      running build
      running build_py
      creating build
      creating build/lib.linux-x86_64-3.9
      creating build/lib.linux-x86_64-3.9/openslide
      copying openslide/__init__.py -> build/lib.linux-x86_64-3.9/openslide
      copying openslide/_version.py -> build/lib.linux-x86_64-3.9/openslide
      copying openslide/lowlevel.py -> build/lib.linux-x86_64-3.9/openslide
      copying openslide/deepzoom.py -> build/lib.linux-x86_64-3.9/openslide
      running build_ext
      building 'openslide._convert' extension
      creating build/temp.linux-x86_64-3.9
      creating build/temp.linux-x86_64-3.9/openslide
      /opt/rh/devtoolset-7/root/usr/bin/gcc -pthread -std=c99 -Wall -Wstrict-prototypes -fno-strict-aliasing -fwrapv -g -fPIC -I/home/sara/Slicer_stable/Slicer-5.0.2-linux-amd64/lib/Python/include/python3.9 -c openslide/_convert.c -o build/temp.linux-x86_64-3.9/openslide/_convert.o
      error: command '/opt/rh/devtoolset-7/root/usr/bin/gcc' failed: No such file or directory
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: legacy-install-failure

� Encountered error while trying to install package.
??> openslide-python

note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.
WARNING: You are using pip version 22.0.3; however, version 22.1.2 is available.
You should consider upgrading via the '/home/sara/Slicer_stable/Slicer-5.0.2-linux-amd64/bin/./python-real -m pip install --upgrade pip' command.
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/home/sara/Slicer_stable/Slicer-5.0.2-linux-amd64/bin/Python/slicer/util.py", line 3431, in pip_install
    _executePythonModule('pip', args)
  File "/home/sara/Slicer_stable/Slicer-5.0.2-linux-amd64/bin/Python/slicer/util.py", line 3394, in _executePythonModule
    logProcessOutput(proc)
  File "/home/sara/Slicer_stable/Slicer-5.0.2-linux-amd64/bin/Python/slicer/util.py", line 3363, in logProcessOutput
    raise CalledProcessError(retcode, proc.args, output=proc.stdout, stderr=proc.stderr)
subprocess.CalledProcessError: Command '['/home/sara/Slicer_stable/Slicer-5.0.2-linux-amd64/bin/../bin/PythonSlicer', '-m', 'pip', 'install', 'openslide-python']' returned non-zero exit status 1.

It seems the pip install command cannot find a gcc binary:

error: command '/opt/rh/devtoolset-7/root/usr/bin/gcc' failed: No such file or directory

What do you see if you run gcc --version or which gcc?
If it doesn’t find any gcc installation you might want to install it via:

sudo apt update
sudo apt install build-essential

(or the equivalent call for your distro).