# Adding extra weights/subtasks to TotalSegmentator

**URL:** <https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032>\
**Category:** Support\
**Created:** [August 7, 2023, 5:20pm UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032 "2023-08-07T17:20:01Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![evaherbst](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/evaherbst/32/65595_2.png) [@evaherbst](https://discourse.slicer.org/u/evaherbst)\
**Post date:** [August 7, 2023, 5:20pm UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/1 "2023-08-07T17:20:01Z")

</div>

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](https://discourse.slicer.org/t/totalsegmentator-error-at-first-run-command-python-scripts-totalsegmentator-returned-non-zero-exit-status-120/26755/22))

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

---

<div class="post-metadata">

**Author:** ![evaherbst](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/evaherbst/32/65595_2.png) [@evaherbst](https://discourse.slicer.org/u/evaherbst)\
**Post date:** [August 8, 2023, 10:23am UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/2 "2023-08-08T10:23:15Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [August 8, 2023, 11:50am UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/3 "2023-08-08T11:50:14Z")

</div>

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

🙂

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

```auto
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

```auto
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.

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [August 8, 2023, 12:11pm UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/4 "2023-08-08T12:11:06Z")

</div>

Find new tasks  
“aortic\_branches\_test” and “bones\_tissue\_test” implemented in my development version of  
[Totalsegmentator.py](https://github.com/rbumm/SlicerTotalSegmentator/blob/subset_dev_branch/TotalSegmentator/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.

---

<div class="post-metadata">

**Author:** ![evaherbst](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/evaherbst/32/65595_2.png) [@evaherbst](https://discourse.slicer.org/u/evaherbst)\
**Post date:** [August 8, 2023, 12:36pm UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/5 "2023-08-08T12:36:52Z")

</div>

@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!

---

<div class="post-metadata">

**Author:** ![evaherbst](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/evaherbst/32/65595_2.png) [@evaherbst](https://discourse.slicer.org/u/evaherbst)\
**Post date:** [August 8, 2023, 1:36pm UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/6 "2023-08-08T13:36:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [August 8, 2023, 2:32pm UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/7 "2023-08-08T14:32:53Z")

</div>

I get the same warning and no negative effects yet …

---

<div class="post-metadata">

**Author:** ![evaherbst](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/evaherbst/32/65595_2.png) [@evaherbst](https://discourse.slicer.org/u/evaherbst)\
**Post date:** [August 8, 2023, 2:53pm UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/8 "2023-08-08T14:53:09Z")

</div>

Great, thanks for the quick reply!

No issues on my end yet either.

Best,  
Eva

---

<div class="post-metadata">

**Author:** ![Lee\_Ho\_Jun](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lee_ho_jun/32/4519_2.png) [@Lee\_Ho\_Jun](https://discourse.slicer.org/u/Lee_Ho_Jun)\
**Post date:** [August 17, 2023, 10:14am UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/9 "2023-08-17T10:14:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [August 17, 2023, 10:49am UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/10 "2023-08-17T10:49:36Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/6/5/65c0f53b8d546ee3efb9fd9906fc0753255de3cc.png)

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)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 17, 2023, 11:08am UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/11 "2023-08-17T11:08:58Z")

</div>

> [@rbumm](#):
>
> For some reason, this does not work in 5.3.0 yet (I see the older extension after a reinstall)

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.

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [August 17, 2023, 11:14am UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/12 "2023-08-17T11:14:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 17, 2023, 11:42am UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/13 "2023-08-17T11:42:57Z")

</div>

The extension update process is described [here](https://slicer.readthedocs.io/en/latest/user_guide/extensions_manager.html#update-extensions-for-slicer-preview-releases). Where would you show a warning?

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [August 17, 2023, 1:00pm UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/14 "2023-08-17T13:00:32Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![evaherbst](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/evaherbst/32/65595_2.png) [@evaherbst](https://discourse.slicer.org/u/evaherbst)\
**Post date:** [September 25, 2023, 12:02pm UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/16 "2023-09-25T12:02:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [September 25, 2023, 12:41pm UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/17 "2023-09-25T12:41:43Z")

</div>

Thank you for the feedback, Eva!

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

---

<div class="post-metadata">

**Author:** ![Michael\_Allen](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/michael_allen/32/66371_2.png) [@Michael\_Allen](https://discourse.slicer.org/u/Michael_Allen)\
**Post date:** [October 12, 2023, 3:24am UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/18 "2023-10-12T03:24:44Z")

</div>

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](https://setuptools.pypa.io/en/latest/pkg_resources.html)  
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!

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [October 12, 2023, 1:04pm UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/19 "2023-10-12T13:04:59Z")

</div>

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.

```auto
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](https://github.com/lassoan/SlicerTotalSegmentator/pull/32)

---

<div class="post-metadata">

**Author:** ![Michael\_Allen](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/michael_allen/32/66371_2.png) [@Michael\_Allen](https://discourse.slicer.org/u/Michael_Allen)\
**Post date:** [October 13, 2023, 2:14am UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/20 "2023-10-13T02:14:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rbumm](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/rbumm/32/9404_2.png) [@rbumm](https://discourse.slicer.org/u/rbumm)\
**Post date:** [October 13, 2023, 7:49am UTC](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032/21 "2023-10-13T07:49:37Z")

</div>

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.

[Next page](https://discourse.slicer.org/t/adding-extra-weights-subtasks-to-totalsegmentator/31032.md?page=2)
