Adding extra weights/subtasks to TotalSegmentator

Hello,

I would like to be able to implement the additional license-dependent weights/subtasks for TotalSegmentator in 3D Slicer.

I received the weights files from Jakob Wassterthal and have added them to the 3d_fullres weights folder (as per this post: TotalSegmentator error at first run: Command ...Python\Scripts\TotalSegmentator... returned non-zero exit status 120 - #22 by zhang_ming)

However, since they are new weights, I think I need to adjust the TotalSegmentator.py code in “…AppData\Local\NA-MIC\Slicer 5.2.2\NA-MIC\Extensions-31382\TotalSegmentator\lib\Slicer-5.2\qt-scripted-modules”.
Is this correct?

I see where I can add new tasks to the GUI (lines 307-314) but I do not see where I can link the weights file for that task.

Thank you,
Eva

I had a closer look at the code and noticed the following:

The new subtasks I am trying to run (“bones_tissue_test” and “aortic_branches_test”) are present as arguments in the TotalSegmentator file in the TotalSegmentator repository (https://github.com/wasserth/TotalSegmentator/blob/master/bin/TotalSegmentator).

However, they are not listed in the TotalSegmentator file in the Python>scripts folder in Slicer3D. I just tried reinstalling Slicer but that also did not update the subtasks.

If I replace the TotalSegmentator file in the Python>scripts folder with the updated version from the github repo of Jakob Wasserthal, the 3D Slicer installation directory, I get an error that there are too many arguments.

Furthermore, I assume I need to not only add the new tasks in lines 207-315 in the TotalSegmentator.py script in the Slicer Total Segmentator folder, but also make sure all the label terminologies are included.

Could you advise me in

  1. how to add the new subtask arguments in the TotalSegmentator file in the Slicer Python scrips folder
  2. how to adjust TotalSegmentator.py in the Slicer Total Segmentator extensions folder to add these subtasks and their corresponding labels?

Thank you very much,
Eva

Do you want to run these tests in 3D Slicer and are you in for a complicated procedure?

:slight_smile:

Here you go:

To run the new test tasks from the extension, you must “git install” the latest TotalSegmentator tool in the 3D Slicer directory.

In 3D Slicers Python console run

slicer.util.pip_uninstall("TotalSegmentator")
slicer.util.pip_install("git+https://github.com/wasserth/TotalSegmentator.git")

Then, find your 3D Slicer Python script directory.

On my Windows system, this is:
C:\Users\Yourname\AppData\Local\NA-MIC\Slicer 5.2.2\lib\Python\Scripts

Open a PowerShell in this directory.
Copy your weights into this directory.

In order to update the TotalSegmentator weights (here: aortic branches, they are licensed and may not be shared), execute

python ./totalseg_import_weights -i ./Task435_Heart_vessels_118subj.zip

The next problem is that the TotelSegmentator 3D Slicer extension does not know yet about the newly installed tasks.

See the next post. @lassoan and I will find a way to integrate new test tasks as well as implement a way to add new or licensed weights in the extension. Until then, you can use my workaround.

Find new tasks
“aortic_branches_test” and “bones_tissue_test” implemented in my development version of
Totalsegmentator.py

You could use this Python code file in your local TotalSegmentator extension folder until the standard extension gets updated.

Restart Slicer to see the new tasks.

@rbumm Wow, thank you so much for this solution!

I am having trouble reinstalling TotalSegmentator due to a permission error:

Could not install packages due to an OSError: [WinError 5] Access is denied: ‘C:\Users\[myname]\AppData\Local\NA-MIC\Slicer 5.2.2\lib\Python\Lib\site-packages\~harset_normalizer\md.cp39-win_amd64.pyd’

Consider using the --user option or check the permissions.

I guess this is because of not having admin rights for our work computers.
I’ll try to get temp admin access (not sure how to implement the --user option).

Thanks again, I will keep you updated whether it works!

Hi again.
False alarm, it works perfectly now.

Thank you so so much @rbumm !

EDIT: I do get a warning now when I open Slicer that says:

C:\Users.…\AppData\Local\NA-MIC\Slicer 5.2.2\lib\Python\Lib\site-packages\requests_init_.py:102: RequestsDependencyWarning: urllib3 (1.26.14) or chardet (5.1.0)/charset_normalizer (2.0.12) doesn’t match a supported version!
warnings.warn("urllib3 ({}) or chardet ({})/charset_normalizer ({}) doesn’t match a supported "

Is this ok?

Best,
Eva

I get the same warning and no negative effects yet …

Great, thanks for the quick reply!

No issues on my end yet either.

Best,
Eva

Hi!
I’m trying to do the same thing as Eva, and I’m slightly confused.
Should I do everything that is in the above post, and replace the Totalsegmentator.py?
Or is it suffice to just repalce Totalsegmentator.py?
Or is it now in the official extension, and should I wait for any update schedules?
Thanks in advance.

HJ

In the latest TotalSegmentator extension, we have implemented a checkbox “use latest development version” and a button “Import weights”.

image

When you add additional weights, you should make a "“Force reinstall” with “use latest development version” checked. Then press the “Import weights” button and search the zipped weight file on your hard drive. Select it and it will be automatically installed.

Please update the TotalsSegmentator extension or uninstall/reinstall it.
@lassoan For some reason, this does not work in 5.3.0 yet (I see the older extension after a reinstall)

1 Like

Extensions are only updated for the latest Slicer Stable Release and the latest Slicer Preview Release. If you want to get the latest extension version for a Slicer Preview Release then you need to download and install the latest Slicer Preview Release.

1 Like

It would be good to have a warning message for such cases. Not very obvious …

The extension update process is described here. Where would you show a warning?

The best way would be to display a message box as soon as the user tries to load an extension from the extension manager:
“You are loading a possibly outdated extension because you are not using the latest stable (x.x.x) or the preview (y.y.y) version of 3D Slicer”

Hi @rbumm

I recently downloaded Slicer 5.4 (stable release) and followed your instructions for adding the extra weights. It works well - after importing each weight, the restart/update was suggested automatically.

Thanks!
Eva

Thank you for the feedback, Eva!

A new version of TotalSegmentator is on the horizon. Stay tuned.

I am also running into issue using manually installed weights. Using Mac 12.6.1 with slicer 5.4/5.5. Mac intel i5, intel iris 6100

I received the zip file from Jakob for bone_tissue_test. I had previously (i believe with slicer 5.4) installed total segmentator, and manually installed the weights by either using the import function, or copying into the 3d full res folder, I can’t recall now. It was workign great for awhile, selecting bones_tissue_test as the segmentation task in drop down menu.

At some point I installed slicer 5.5 and now I’ve run into issues. I downgraded to 5.4 and have the same issues. It basically error-ed and said I couldn’t select bones_tissue_test anymore. I then fooled around, trying to reinstall segmentator, reinstall pytorch, reinstall weights, and cannot get it to work anymore.

Here is the python log:

/Applications/Slicer.app/Contents/Extensions-31938/TotalSegmentator/lib/Slicer-5.4/qt-scripted-modules/TotalSegmentator.py:783: UserWarning: ‘has_cuda’ is deprecated, please use ‘torch.backends.cuda.is_built()’
cuda = torch.cuda if torch.has_cuda and torch.cuda.is_available() else None
[Python] Failed to compute results.
[Python] Command ‘[’/Applications/Slicer.app/Contents/bin/…/bin/PythonSlicer’, ‘/Applications/Slicer.app/Contents/lib/Python/bin/TotalSegmentator’, ‘-i’, ‘/private/var/folders/nv/5xxpc59s7rxbzpmwslgc90v00000gp/T/Slicer-allenm/__SlicerTemp__2023-10-11_19+15+50.643/total-segmentator-input.nii’, ‘-o’, ‘/private/var/folders/nv/5xxpc59s7rxbzpmwslgc90v00000gp/T/Slicer-allenm/__SlicerTemp__2023-10-11_19+15+50.643/segmentation’, ‘–ml’, ‘–task’, ‘bones_tissue_test’]’ returned non-zero exit status 2.
Traceback (most recent call last):
File “/Applications/Slicer.app/Contents/Extensions-31938/TotalSegmentator/lib/Slicer-5.4/qt-scripted-modules/TotalSegmentator.py”, line 271, in onApplyButton
self.logic.process(self.ui.inputVolumeSelector.currentNode(), self.ui.outputSegmentationSelector.currentNode(),
File “/Applications/Slicer.app/Contents/Extensions-31938/TotalSegmentator/lib/Slicer-5.4/qt-scripted-modules/TotalSegmentator.py”, line 868, in process
self.logProcessOutput(proc)
File “/Applications/Slicer.app/Contents/Extensions-31938/TotalSegmentator/lib/Slicer-5.4/qt-scripted-modules/TotalSegmentator.py”, line 701, in logProcessOutput
raise CalledProcessError(retcode, proc.args, output=proc.stdout, stderr=proc.stderr)
subprocess.CalledProcessError: Command ‘[’/Applications/Slicer.app/Contents/bin/…/bin/PythonSlicer’, ‘/Applications/Slicer.app/Contents/lib/Python/bin/TotalSegmentator’, ‘-i’, ‘/private/var/folders/nv/5xxpc59s7rxbzpmwslgc90v00000gp/T/Slicer-allenm/__SlicerTemp__2023-10-11_19+15+50.643/total-segmentator-input.nii’, ‘-o’, ‘/private/var/folders/nv/5xxpc59s7rxbzpmwslgc90v00000gp/T/Slicer-allenm/__SlicerTemp__2023-10-11_19+15+50.643/segmentation’, ‘–ml’, ‘–task’, ‘bones_tissue_test’]’ returned non-zero exit status 2.

Inside the total segmentator window, this is the output:

Creating segmentations with TotalSegmentator AI…
Total Segmentator arguments: [‘-i’, ‘/private/var/folders/nv/5xxpc59s7rxbzpmwslgc90v00000gp/T/Slicer-allenm/__SlicerTemp__2023-10-11_19+15+50.643/total-segmentator-input.nii’, ‘-o’, ‘/private/var/folders/nv/5xxpc59s7rxbzpmwslgc90v00000gp/T/Slicer-allenm/__SlicerTemp__2023-10-11_19+15+50.643/segmentation’, ‘–ml’, ‘–task’, ‘bones_tissue_test’]
/Applications/Slicer.app/Contents/lib/Python/bin/TotalSegmentator:5: DeprecationWarning: pkg_resources is deprecated as an API. See Package Discovery and Resource Access using pkg_resources - setuptools 68.2.2.post20231008 documentation
from pkg_resources import require
usage: TotalSegmentator [-h] -i filepath -o directory [-ml]
[-nr NR_THR_RESAMP] [-ns NR_THR_SAVING] [-f]
[-t NORA_TAG] [-p]
[-ta {total,lung_vessels,cerebral_bleed,hip_implant,coronary_arteries,body,pleural_pericard_effusion,liver_vessels,bones_extremities,tissue_types,heartchambers_highres,head,aortic_branches,heartchambers_test,test}]
[-rs ROI_SUBSET [ROI_SUBSET …]] [-s] [-r]
[-cp CROP_PATH] [-bs] [-fs] [-q] [-v] [–test 0|1|2|3]
[–version]
TotalSegmentator: error: argument -ta/–task: invalid choice: ‘bones_tissue_test’ (choose from ‘total’, ‘lung_vessels’, ‘cerebral_bleed’, ‘hip_implant’, ‘coronary_arteries’, ‘body’, ‘pleural_pericard_effusion’, ‘liver_vessels’, ‘bones_extremities’, ‘tissue_types’, ‘heartchambers_highres’, ‘head’, ‘aortic_branches’, ‘heartchambers_test’, ‘test’)

Why is bones_tissue_test an invalid choice? In the totalsegmentator.py file it is listed in the self.tasks area?

Current pytorch util shows torch 2.1.0 and torchvision 0.16.0

Thanks for any help!

The TotalSegmenter extension has not yet been upgraded to Totalsegmentator 2.0 yet.
There are issues with the SimpleITK, which may be incompatible with what Slicer uses, at least what lungmask uses.

Name: SimpleITK
Version: 2.3.0
Summary: SimpleITK is a simplified interface to the Insight Toolkit (ITK) for image registration and segmentation
Home-page: http://simpleitk.org/
Author: Insight Software Consortium
Author-email: insight-users@itk.org
License: Apache
Location: c:\users\rudolf\appdata\local\slicer.org\slicer 5.4.0_2\lib\python\lib\site-packages
Requires: 
Required-by: acvl-utils, lungmask, nnunetv2, TotalSegmentator

See a PR here

Any idea how to go back to what I had working before? I rolled back to slicer 5.4 but that didn’t allow me to get it working again using the bone_tissue_test weight

I would suggest deleting the complete 5.4 folders, reinstalling Slicer, reinstalling Pytorch, restarting Slicer, reinstalling TotalSegmentator, restarting Slicer, and then doing what you did to install the weights previously.