New extension: HDBrainExtraction for AI-based skull stripping

A new AI-based tool - HDBrainExtracion extension - is added to 3D Slicer for skull stripping (blanking out region outside the brain in MRI images). The model is trained on a large set of images and proven to be more robust than many other similar tools.

The extension is built by packaging the HD-BET model provided by DKFZ (Heidelberg, Germany). This extension can serve as a good example for others who intend to distribute their AI models via 3D Slicer.

4 Likes

This is a very instructive and well-written extension, thank you @lassoan. Do you have any information on how HD-BET trained their skull stripping model in detail? The cited paper remains a bit unclear here. It would be interesting to have similar tissue stripping AI-based modules for other organs.

1 Like

I don’t have any more information other than what they described in their paper. You may contact the authors at the email provided in the paper or by submitting an issue in their GitHub repository for more details.

Short feedback: Works great on a Windows 11 GTX 1060 laptop, but I needed to start Slicer in Admin mode to install the PyTorch extension upon the first run. Processing took about 5 min to complete (Device=auto), so it is probably not using the GPU yet.

image

1 Like

Thanks for the feedback. What indicated that installing as admin user was necessary?

I’ve noticed that failed Pytorch installs may prevent proper installation. You may consider trying to reinstall Slicer from scratch (deleting the Slicer install folder completely) to see if it makes Slicer’s pytorch find the GPU.

Also make sure that in your NVIDIA settings Slicer is configured to use the GPU.

I install HDBrainExtraction, it asks to install PyTorch too, I agree, then I restart and run the extension the first time and get:

1 Like

This exception was solved by applying this commit

to the PyTorch utility module. Then the install runs normally, but unfortunately, it does not detect CUDA yet on my two systems. Using Slicer in Admin mode was mandatory, otherwise, the PyTorch Util was throwing exceptions.

2 Likes

Very good performance on my desktop (RTX 3070 Ti): 20 s rendering time

CUDA seems to be available on that system.

image

2 Likes

Thanks for the additional information.

If you encounter any issues then always try to update the extensions in the latest Slicer Stable Release, or install the latest Slicer Preview Release (extension updates are not available for preview releases).

1 Like

HDBrainExtraction is useful for non-volumetric T1 images?

I would expect that the module only works for images that contains many slices and cover the full brain, but you can try it on sĂ­ngle-slice images, too.

If you find that it does not work then you can train a new network specifically for sĂ­ngle-slice images. You should be able to automatically generate training data from full-brain data sets.

This is great… do you know if there is a way to hack the Pytorch installation to swap it to use the Metal-backend that is now available for M1/M2 processors?

Wet use light-the-torch to install pytorch. Check out its documentation for ARM architecture compatibility. Note that Slicer runs on ARM macs through Rosetta emulation, which might complicate things a bit further.

1 Like

Hello Guys,
I installed CUDA, PyTorch and HDBrainExtraction. However when I try to run the extension I get an error message. I tried reinstalling several timers and also running slicer in admin mode.
I basically don’t know what else to do. I installed everything today, so I’m assuming I have the most recent versions of everything. It would be great if somebody could help me out.

The error message in full detail: Traceback (most recent call last):
File “C:\Users\Rickli\AppData\Local\NA-MIC\Slicer 5.2.1\bin\Python\slicer\util.py”, line 2961, in tryWithErrorDisplay
yield
File “C:/Users/Rickli/AppData/Local/NA-MIC/Slicer 5.2.1/NA-MIC/Extensions-31317/HDBrainExtraction/lib/Slicer-5.2/qt-scripted-modules/HDBrainExtractionTool.py”, line 227, in onApplyButton
self.logic.setupPythonRequirements()
File “C:/Users/Rickli/AppData/Local/NA-MIC/Slicer 5.2.1/NA-MIC/Extensions-31317/HDBrainExtraction/lib/Slicer-5.2/qt-scripted-modules/HDBrainExtractionTool.py”, line 301, in setupPythonRequirements
slicer.util.pip_install(‘git+https://github.com/MIC-DKFZ/batchgenerators#egg=batchgenerators’)
File “C:\Users\Rickli\AppData\Local\NA-MIC\Slicer 5.2.1\bin\Python\slicer\util.py”, line 3571, in pip_install
_executePythonModule(‘pip’, args)
File “C:\Users\Rickli\AppData\Local\NA-MIC\Slicer 5.2.1\bin\Python\slicer\util.py”, line 3533, in _executePythonModule
logProcessOutput(proc)
File “C:\Users\Rickli\AppData\Local\NA-MIC\Slicer 5.2.1\bin\Python\slicer\util.py”, line 3502, in logProcessOutput
raise CalledProcessError(retcode, proc.args, output=proc.stdout, stderr=proc.stderr)
subprocess.CalledProcessError: Command ‘[‘C:/Users/Rickli/AppData/Local/NA-MIC/Slicer 5.2.1/bin/…/bin\PythonSlicer.EXE’, ‘-m’, ‘pip’, ‘install’, ‘git+https://github.com/MIC-DKFZ/batchgenerators#egg=batchgenerators’]’ returned non-zero exit status 1.

Let’s see if you get more information if you run the installation from the Windows terminal. Exit Slicer and run this command in the Command Prompt:

“%localappdata%\NA-MIC\Slicer 5.2.1\bin\PythonSlicer.exe” -m pip install git+https://github.com/MIC-DKFZ/batchgenerators#egg=batchgenerators

Thank you for your help! I didn’t have “git” installed. After installing and quickly restarting my PC it worked just fine. I think it also used the GPU to extract, as it was quite fast. The extraction turned out wonderful!

2 Likes

This works very well! I was just wondering if it is possible to generate a mask with higher resolution?

You can get higher-resolution segmentation if you use a GPU. In CPU mode the resolution is automatically reduced to keep the computation time lower.

1 Like