Loading .NRRD file

username: smrolfe
cooked: <p>Hello,</p>
<p>I have a <a class="mention" href="/u/monai">@MONAI</a> 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:</p>
<pre><code class="lang-auto">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)
</code></pre>
<p>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:</p>
<pre><code class="lang-auto">  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". 
</code></pre>
<p>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?</p>
<p>Thanks,<br>
Sara</p>

username: ptrblck
cooked: <p>Do you see the same error using a publicly available .nrrd file which we could use to reproduce the issue?<br>
If so, could you post the steps you’ve used to install the optional dependencies so that we could try to debug the issue?</p>

username: smrolfe
cooked: <p>Thanks, I posted a publicly available .nrrd file here: <a href="https://github.com/smrolfe/SampleData/blob/master/undeterminedSex_AAPN_K1026-1-e15.5_Cbx4.nrrd" class="inline-onebox" rel="noopener nofollow ugc">SampleData/undeterminedSex_AAPN_K1026-1-e15.5_Cbx4.nrrd at master · smrolfe/SampleData · GitHub</a></p>
<p>My installation steps are:</p>
<pre><code class="lang-auto">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’)
</code></pre>

username: ptrblck
cooked: <p>Thanks for the file!<br>
I’m currently unsure how to use <code>slicer</code> and it seems it has an own (pip-)package manager?</p>
<p>I’m able to load the uploaded <code>.nrrd</code> file by installing the current stable <code>torch==1.12.0</code> release and by building <code>MONAI</code> will all image backends via running:</p>
<pre><code class="lang-auto">pip install -e '.[all]'
</code></pre>
<p>in the <code>MONAI</code> source folder.</p>
<p>Code:</p>
<pre><code class="lang-python">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)
# &lt;class 'numpy.uint8'&gt;
</code></pre>
<p>I’ve also tested the current <code>monai==0.9.0</code> release via</p>
<pre><code class="lang-auto">pip install monai[all]
</code></pre>
<p>as well as <code>monai[all]==0.8.0</code> as it seems you would need this version, and all are able to load the image.</p>
<p>Could you run your code with <code>gdb</code> and post the stacktrace here?</p>
<pre><code class="lang-auto">gdb --args python script.py
...
run
...
bt
</code></pre>

username: smrolfe
cooked: <p>When instalingl monai[all], I ran into an error on linux when installing openslide-python.  I get the same error when running:<br>
<code>pip_install('openslide-python')</code></p>
<p>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?</p>

username: ptrblck
cooked: <p>What is the error you are seeing when installing <code>openslide-python</code>?</p>

username: smrolfe
cooked: <p>I am getting the following errors:</p>
<pre><code class="lang-auto">&gt;&gt;&gt; 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
  ??&gt; [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 -&gt; build/lib.linux-x86_64-3.9/openslide
      copying openslide/_version.py -&gt; build/lib.linux-x86_64-3.9/openslide
      copying openslide/lowlevel.py -&gt; build/lib.linux-x86_64-3.9/openslide
      copying openslide/deepzoom.py -&gt; 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
  ??&gt; [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 -&gt; build/lib.linux-x86_64-3.9/openslide
      copying openslide/_version.py -&gt; build/lib.linux-x86_64-3.9/openslide
      copying openslide/lowlevel.py -&gt; build/lib.linux-x86_64-3.9/openslide
      copying openslide/deepzoom.py -&gt; 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.
??&gt; 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 "&lt;console&gt;", line 1, in &lt;module&gt;
  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.
</code></pre>

username: ptrblck
cooked: <p>It seems the <code>pip install</code> command cannot find a <code>gcc</code> binary:</p>
<pre><code class="lang-auto">error: command '/opt/rh/devtoolset-7/root/usr/bin/gcc' failed: No such file or directory
</code></pre>
<p>What do you see if you run <code>gcc --version</code> or <code>which gcc</code>?<br>
If it doesn’t find any <code>gcc</code> installation you might want to install it via:</p>
<pre><code class="lang-auto">sudo apt update
sudo apt install build-essential
</code></pre>
<p>(or the equivalent call for your distro).</p>