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”