# Lung CT Segmenter now supports 'lungmask' AI for lung and lobe segmentation

**URL:** <https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081>\
**Category:** Support\
**Tags:** segmentation, lungctanalyzer, ai, lung, ml\
**Created:** [September 4, 2022, 5:10pm UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081 "2022-09-04T17:10:55Z")\
**Posts on this page:** 20\
**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:** [September 4, 2022, 5:10pm UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/1 "2022-09-04T17:10:55Z")

</div>

We have a new “experimental AI” option in Lung CT Segmenter for creating lung and lobe segmentations with minimal effort (one tracheal markup) on the fly.

This feature involves Johannes Hofmanningers [lungmask](https://github.com/JoHof/lungmask) with permission.

A short YouTube demo is here:

[![](https://img.youtube.com/vi/bw1IaxVkOM4/maxresdefault.jpg "Lung and lobe segmentation with "lungmask" AI in 3D Slicer") ](https://www.youtube.com/watch?v=bw1IaxVkOM4)

In this setting, an Nvidia GPU (1060 +), 6 GB GPU memory and “CUDA enabled” are strongly recommended.

Real processing time on a desktop PC with RTX 3070Ti GPU (without the in-video speedup): 97.23 s.  
Slicer extension: Lung CT Analyzer  
Dataset: From MONAILabel Task06\_lung with permission.

---

<div class="post-metadata">

**Author:** ![Mehran](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mehran/32/11080_2.png) [@Mehran](https://discourse.slicer.org/u/Mehran)\
**Post date:** [September 6, 2022, 2:05pm UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/2 "2022-09-06T14:05:16Z")

</div>

That is great. Is it possible to use cli and save labelmaps?

---

<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 6, 2022, 3:46pm UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/3 "2022-09-06T15:46:27Z")

</div>

Yes, you can.

The Lung CT segmenter [AI test function](https://github.com/rbumm/SlicerLungCTAnalyzer/blob/dcb9c94ba35fd9398558bedfa066da45094ed989/LungCTSegmenter/LungCTSegmenter.py#L1824) is a working example.

The only thing to note is the current necessity to define one tracheal markup in this code. If you want just lungs and lobes you could just freely define one, f.e. as in the test example, it is not really needed for AI.

---

<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 6, 2022, 3:47pm UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/4 "2022-09-06T15:47:54Z")

</div>

> [@Mehran](#):
>
> and save labelmaps

You would have to convert the output segmentation to a labelmap if you really needed to.

---

<div class="post-metadata">

**Author:** ![Mehran](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/mehran/32/11080_2.png) [@Mehran](https://discourse.slicer.org/u/Mehran)\
**Post date:** [September 7, 2022, 7:29am UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/5 "2022-09-07T07:29:56Z")

</div>

thanks, i will do that

---

<div class="post-metadata">

**Author:** ![yopi.simargi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/yopi.simargi/32/10249_2.png) [@yopi.simargi](https://discourse.slicer.org/u/yopi.simargi)\
**Post date:** [October 25, 2022, 3:38am UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/6 "2022-10-25T03:38:25Z")

</div>

Hi Rudolf. Mine is not working. It is said no module name torch. How to get it and use the module torch? Thank you.

---

<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 25, 2022, 6:11am UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/7 "2022-10-25T06:11:40Z")

</div>

Please install the “Pytorch Utils” extension and restart Slicer.  
Then it should typically install torch when you run the lung segmenter extension, but you could also install it from Pytorch Utils directly:

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/0/c0091e8cf796d3e3baf5f63c653f1aa769360446.png)

Please be patient during installation, it will take several minutes with no reaction or progress bar.

Which version of 3D Slicer and Lung CT Segmenter are you using?

---

<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:** [October 27, 2022, 5:41am UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/8 "2022-10-27T05:41:36Z")

</div>

Also note that you may need 20-30GB of free disk space for installing pytorch. If you run out of disk space then you may need to completely remove all remnants of pytorch and then install it again. I’ve found that in this case it is easier to reinstall Slicer than trying to clean up things manually.

---

<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 27, 2022, 9:44am UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/9 "2022-10-27T09:44:15Z")

</div>

All dependencies (SegmentEditorExtraEffects, SurfaceWrapSolidify, and PyTorch) are now included in Lung CT Analyzer’s [CMakeList.txt](https://github.com/rbumm/SlicerLungCTAnalyzer/blob/1d852c1bd7d8a7b2098ed95646a13b96e6058f2f/CMakeLists.txt#L13) for user convenience.

A fresh 3D Slicer 5.0.3 install on a Windows 11 system occupies 850 MB which grows to 7.4 GB after AI lung segmentations (mainly from Pytorch).

---

<div class="post-metadata">

**Author:** ![yopi.simargi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/yopi.simargi/32/10249_2.png) [@yopi.simargi](https://discourse.slicer.org/u/yopi.simargi)\
**Post date:** [October 28, 2022, 3:10am UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/10 "2022-10-28T03:10:37Z")

</div>

![Screen Shot 2022-10-28 at 10.06.31](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/9/296b69ae0296e0ee7d9a598918abc5cd521562cc.png)  
 ![Screen Shot 2022-10-28 at 10.08.08](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/6/36fc5fd6c2bbe6a441c0cf320351881d8898e4bb.jpeg)

Hi everyone, thank you for guiding me. I have installed the Pytorch and still got the same message. Is there any way to fix it? Thank you.

---

<div class="post-metadata">

**Author:** ![yopi.simargi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/yopi.simargi/32/10249_2.png) [@yopi.simargi](https://discourse.slicer.org/u/yopi.simargi)\
**Post date:** [October 28, 2022, 3:20am UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/11 "2022-10-28T03:20:57Z")

</div>

I am using Macbook Pro, macOS Big Sur, 2.8GHz Dual-Core Intel Core i7, Memory 8 GB. 3D Slicer version 5.03.  
I have installed PyTorch as I mentioned and the AI still not working. Thank you for your help Rudolf.

---

<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 28, 2022, 9:05am UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/12 "2022-10-28T09:05:51Z")

</div>

Unfortunately, I have no experience with using PyTorch in MacOS.

Have you been pressing this button in the PyTorch Utils extension?

![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/5/250a083cce816b51b6f44c021391c6e65e7c4b04.png)

If yes and you still got the error message you may want to refer [to this page](https://pytorch.org/get-started/locally/) on how to install PyTorch from Slicer´s Python Interactor. See “Installing on MacOS” section.

`import torch`

should not throw an exception and

```auto
import torch
x = torch.rand(5, 3)
print(x)

```

should show a result.

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [October 28, 2022, 1:28pm UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/13 "2022-10-28T13:28:07Z")

</div>

Following the steps to install and test pytorch that @rbumm suggested works for me on a mac (m2 macbook air) with Slicer 5.0.3.

---

<div class="post-metadata">

**Author:** ![mch114](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@mch114](https://discourse.slicer.org/u/mch114)\
**Post date:** [May 20, 2023, 8:36am UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/14 "2023-05-20T08:36:10Z")

</div>

Hi,

I am using the Lung CT analyser for a research project involving analysis of chest CTs of severe COVID patients. I would like to be able to automatically segment the lung regions using the lungmask AI feature/dataset, however I consistently get the following error each time I try it;

 ![Screenshot 2023-05-20 at 5.57.33 pm](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/6/9636d339540a779dab4c107735a33dc9621818d7.jpeg)

Details;

- Machine: MacBook Pro M1
- OS: Mac OS Ventura Version 13.2.1 (22D68)
- Slicer for Mac version 5.2.2

Python code for the error is;

 ![Screenshot 2023-05-20 at 5.58.29 pm](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/b/ab2a20a979dc95785866c5e5ccb8cddcf40de593.png)

I’m curious to know if this is an issue others have had with lungmask within 3D Slicer, and if so were there any solutions?

Thanks

---

<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:** [May 20, 2023, 11:13am UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/16 "2023-05-20T11:13:33Z")

</div>

I just tested your issue and can confirm it on my systems too. Will try to find a solution.

---

<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:** [May 20, 2023, 12:40pm UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/17 "2023-05-20T12:40:00Z")

</div>

Identified the error. The lungmask developers have introduced a different infer mode in their new program version 0.2.14. LungCTAnalyzer has been adopted (2.67) and has just been pushed to the project GitHub, so it will be available for updates tomorrow via the extension manager and no longer display the error message.

---

<div class="post-metadata">

**Author:** ![mch114](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@mch114](https://discourse.slicer.org/u/mch114)\
**Post date:** [May 23, 2023, 12:43pm UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/18 "2023-05-23T12:43:10Z")

</div>

Hi Dr. Bumm,

Thanks very much for looking into this. I can confirm the lungmask AI model is working well within 3D slicer now and producing very nice segmentations.

Thanks again for the solution,

Marcus

---

<div class="post-metadata">

**Author:** ![rsalkin](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@rsalkin](https://discourse.slicer.org/u/rsalkin)\
**Post date:** [January 17, 2024, 3:24pm UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/19 "2024-01-17T15:24:24Z")

</div>

Hello,

I recently updated slicer and had to re-install the Lung CT Segmenter. I am now getting an error stating the module ‘torch’ has no attribute ‘inference\_mode’ (see below). I tried to uninstall and re-install the module as well as pytorch.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/4/4/447025586451b64c4931fd7d175238dc4fc192df.jpeg)

Currently running windows 10 enterprise on a HP Z1 workstation, so using CUDA is not an option.

Any help would be appreciated, thanks!

---

<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 24, 2024, 11:25am UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/20 "2024-01-24T11:25:55Z")

</div>

Hello,

sorry for the late response.  
I can not reproduce the problem after the last 3 installations that I have done.  
What do you see in the Python console when you experience this error?

Best

Rudolf

---

<div class="post-metadata">

**Author:** ![rsalkin](https://avatars.discourse-cdn.com/v4/letter/r/b487fb/32.png) [@rsalkin](https://discourse.slicer.org/u/rsalkin)\
**Post date:** [January 24, 2024, 3:22pm UTC](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081/21 "2024-01-24T15:22:45Z")

</div>

Hello Dr. Bumm,

Thank you for the reply. I attached my python console output below. The problem persist even with a fresh re-install of everything.

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

[Next page](https://discourse.slicer.org/t/lung-ct-segmenter-now-supports-lungmask-ai-for-lung-and-lobe-segmentation/25081.md?page=2)
