# Improve TotalSegmentator pre-segmentation speed

**URL:** <https://discourse.slicer.org/t/improve-totalsegmentator-pre-segmentation-speed/27238>\
**Category:** Development\
**Tags:** segmentation, ai\
**Created:** [January 14, 2023, 10:05am UTC](https://discourse.slicer.org/t/improve-totalsegmentator-pre-segmentation-speed/27238 "2023-01-14T10:05:25Z")\
**Posts on this page:** 7\
**Page:** 1

<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:** [January 14, 2023, 10:05am UTC](https://discourse.slicer.org/t/improve-totalsegmentator-pre-segmentation-speed/27238/1 "2023-01-14T10:05:25Z")

</div>

Interesting to see that Jakob has implemented a “roi\_subset” option in the latest version of the TotalSegmentator program, which allows for generating only the organs/structures that you would want to see or save.

Tested

python TotalSegmentator -i “C:\input.nii.gz” -o “C:\segmentation” --roi\_subset lung\_upper\_lobe\_right lung\_middle\_lobe\_right lung\_lower\_lobe\_right

from the command line and it works great, reducing the time needed to save the structures from 34 s (104 segmentations) to 6.6 s (3 segmentations).

---

<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:** [January 14, 2023, 4:19pm UTC](https://discourse.slicer.org/t/improve-totalsegmentator-pre-segmentation-speed/27238/2 "2023-01-14T16:19:43Z")

</div>

> [@rbumm](#):
>
> Interesting to see that Jakob has implemented a “roi\_subset” option in the latest version of the TotalSegmentator program

I’ve asked him to implement this feature because because fast pre-segmentation saving took 1.5 minutes on my computer, because all structures were saved. He implemented it within a day!

We now just need to update the Slicer extension to use this new parameter. @rbumm it would be nice if you could give it a try. It could be implemented by changing the `requiresPreSegmentation` property in the [tasks descriptions](https://github.com/lassoan/SlicerTotalSegmentator/blob/main/TotalSegmentator/TotalSegmentator.py#L308) from a Boolean flag to a list of segment names that are required and pass that to the pre-segmentation.

---

<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:** [January 14, 2023, 4:54pm UTC](https://discourse.slicer.org/t/improve-totalsegmentator-pre-segmentation-speed/27238/3 "2023-01-14T16:54:49Z")

</div>

Sure, I will give it a try.

` --roi-subset` can actually be set and has benefits during any TotalSegmentation and would not only be useful for tasks in which requirePreSegmentation is True. Should we better add it as another, independent self.tasks entry in the [extension class logic](https://github.com/lassoan/SlicerTotalSegmentator/blob/b27e1d65949d83bca4c9ee4e41fe353ab2a0ff3d/TotalSegmentator/TotalSegmentator.py#L283)?

---

<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:** [January 14, 2023, 5:13pm UTC](https://discourse.slicer.org/t/improve-totalsegmentator-pre-segmentation-speed/27238/4 "2023-01-14T17:13:26Z")

</div>

The next question is how we would implement the selection of organs/structures in the TotalSegmentator extension UI. Maybe a ListWidget with MultiSelect?

---

<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:** [January 15, 2023, 5:30pm UTC](https://discourse.slicer.org/t/improve-totalsegmentator-pre-segmentation-speed/27238/5 "2023-01-15T17:30:55Z")

</div>

Will be up for testing on TotelSegmentator extension GitHub in some minutes (new branch).

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/7/87b5c28ae71e3dfccfd6720c172606903048d0bb.png)

---

<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:** [January 15, 2023, 8:37pm UTC](https://discourse.slicer.org/t/improve-totalsegmentator-pre-segmentation-speed/27238/6 "2023-01-15T20:37:37Z")

</div>

I don’t think we need (and therefore we probably shouldn’t) add GUI for segment selection, because we know exactly what segments are needed from the pre-segmentation step; and for the full segmentation we don’t need segment selection (as it would complicate the GUI and in most cases it would slow down segment saving, because loading a few segments one-by-one takes longer than loading 100 segments all at once).

Let’s continue the discussion in the comment section of your pull request:

> <https://github.com/lassoan/SlicerTotalSegmentator/pull/11>
>
> \- choose organ/structure from a sorted QListWidget
> \- multilselect is possible

---

<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:** [January 16, 2023, 10:59am UTC](https://discourse.slicer.org/t/improve-totalsegmentator-pre-segmentation-speed/27238/7 "2023-01-16T10:59:49Z")

</div>

Agreed, but let me just my response from GitHub

_Thank you for your comments. I think implementing this in the GUI would be helpful because many users would just focus on 3-5 organs/structures of interest when using TS. Ideally, you would want to be able to save your selection of choice (f.e. liver only) for future use. It is very helpful to have the roi\_subset option in the logic when I call the logic from Lung CT Segmenter, because it saves nearly a minute of saving time (I could just ask for lobes and airways)._

here to enable a good discussion.
