AI segmentation with lung CT analyzer error

Hi, I’ve been trying to use the Lung CT Analyzer extension with the AI segmentation ‘lung mask’, but I cannot get it to work. First, an error occurred that ‘torch was not present’, but I solved this by installing PyTorch extension in slicer (and then install pytorch via this module). However, afterwards I still get an error when I try to segment the lung.

First, when I press apply, I get a warning:

pytorch CUDA is not found on your system. The AI processing will last 3-10 minutes. Do you want to continue?

If I press OK, I will get the following error:

Failed to compute results: Command ‘[‘C:/Users/mmdeboer/AppData/Local/NA-MIC/Slicer 5.0.3/bin/…/bin\PythonSlicer.EXE’, ‘-m’, ‘pip’, ‘install’, ‘git+https://github.com/JoHof/lungmask’]’ returned non-zero exit status 1.

What do I have to do to solve this error? Thanks in advance!

What GPU do you have in use?
Message 1 is normal if you have no GPU or a GPU that does not support CUDA. CUDA needs an Nvidia GPU.

This is strange.
Could you restart Slicer and paste

slicer.util.pip_install("git+https://github.com/JoHof/lungmask")

into the Python Interactor, press return, and post the result?
Thank you.

Hi, I am facing a similar problem. My laptop has a NVDIA GeForce RTX 3050 Ti.

When pasting the suggested line on the Python console:

slicer.util.pip_install(“git+https://github.com/JoHof/lungmask”)

I get the message:

Collecting git+https://github.com/JoHof/lungmask
Cloning GitHub - JoHof/lungmask: Automated lung segmentation in CT to c:\users\pagai\appdata\local\temp\pip-req-build-y_ut0tcq
ERROR: Error [WinError 2] The system cannot find the file specified while executing command git version
ERROR: Cannot find command ‘git’ - do you have ‘git’ installed and in your PATH?

[notice] A new release of pip available: 22.3 → 23.0
[notice] To update, run: python-real.exe -m pip install --upgrade pip

Thank you

@rbumm It is important to note that users don’t have git installed on their system. You can install git relatively easily on Windows (see complete implementation here), but I don’t think there is no easy/standard solution that works on all linux and macOS configurations. It is easier to install from the zip file that github provides (as shown here) because then you don’t need git at all.

Note that the disadvantage of using git+https or git .zip format is that if the URL does not change (e.g., because you use the latest main version) then pip will just use the latest cached version. Therefore, instead of using a branch name, I would recommend to use a specific git hash, as it is done here.

All these issues are solved if the maintainer uploads a package to PyPI. If the maintainer of this package does not do it then you may enter an issue in the repository’s bugtracker to request this.

1 Like

Thank you @lassoan, very helpful.

How are these issues solved then? Will slicer.util.pip_install() recognize the latest version?

Sidenote: slicer.util.pip_uninstall() only works in 5.2.1 if you do a slicer.util.restart() afterward.

pip handles download, install, versioning, resolving version requirements, etc. pip_install will install the package according to the requirements that you specify. If you don’t specify any requirement then it just makes sure that some version of the package is installed. If you specify --upgrade then it’ll install the latest one. If you specify a version range then it’ll choose a version in that range.

Yes, if a Python package is already loaded then installing a new version will not change anything, as the Python scripts from the previous version are already loaded.

Note that pip_uninstall may fail if the Python package is in use and it contains shared libraries (DLLs). If you want to make uninstallation work, you would need to quit Slicer and run PythonSlicer -m pip uninstall ..., but often uninstalling while Slicer is running and then restarting is enough (and it is much easier to do than running some Python scripts outside Slicer). If Python package uninstallation was commonly needed then we could implement some mechanism to do it at Slicer application startup (before initializing Python, by launching the external PythonSlicer interpreter).

1 Like

Hello everyone, I recently updated 3D slicer to the version 5.60 and when I’m trying to use the Lung CT Segmenter extension (which it was working before) I’m getting this error:

Failed to compute results: module ‘torch.backends’ has no attribute ‘mps’

I already checked the updates for PyTorch and the lungmask library, I even installed the version where it was working before (5.40) but even doing that it is not working and giving me the same error, what do you thinkg I can do…

I’m using 3D slicer in a Mac

Please report this error on the lungmask GitHub under issues. Can not reproduce that on a PC. The last version of lungmask obviously “adds support for mps (apple metal)”. If the problem can not be solved by the developer we will switch to an earlier version of lungmask.

I reported there, thanks!

The version of Lungmask currently being used in my 3D slicer is the 0.2.18

Can you try to use the Pytorch extension and install Pytorch in CPU mode ? Do you still get the error then ?

You could always install an earlier version of lungmask from the Python console:

slicer.util.pip_uninstall("lungmask")
slicer.util.pip_install("https://github.com/JoHof/lungmask/archive/18cfc8967501dc84c844d17594b711c0a8e4711b.zip")
# restart slicer

This will install lungmask 0.2.17

Yes, I tried this before, it was set as automatic, I used the CPU mode same error

Thank you so much!
It defenitly solved the error, going back to the last version works