|
1 | 1 | # 🐭 Lungs segmentation in mice CT scans |
2 | 2 |
|
3 | | -We provide a neural network model for segmenting the lungs of the mice. The model is based on the [U-Net](https://lmb.informatik.uni-freiburg.de/people/ronneber/u-net/) architecture. |
| 3 | +Neural network for segmenting mouse lungs in CT scans, based on the classic [U-Net](https://lmb.informatik.uni-freiburg.de/people/ronneber/u-net/) architecture. |
4 | 4 |
|
5 | 5 | <p align="center"> |
6 | | - <img src="https://raw.githubusercontent.com/qchapp/lungs-segmentation/refs/heads/master/images/main_fig.png" height="500"> |
| 6 | + <img src="https://raw.githubusercontent.com/qchapp/lungs-segmentation/refs/heads/master/images/main_fig.png" height="500" alt="Model overview"> |
7 | 7 | </p> |
8 | 8 |
|
9 | | -The goal of our tool is to provid a reliable way to segment the lungs in mouse CT scans. The U-net model produces a binary mask representing the segmentation of the lungs. |
| 9 | +The goal is to provide a reliable, easy-to-use tool to obtain **binary lung masks** from mouse CT scans. |
10 | 10 |
|
11 | | -## Try the model |
| 11 | +--- |
12 | 12 |
|
13 | | -- [Install the package](#installation) |
14 | | -- [Follow the usage instructions](#usage-in-napari) |
| 13 | +## Quick links |
| 14 | + |
| 15 | +- [Try in your browser (Hugging Face Space)](#try-in-your-browser-hugging-face-space) |
| 16 | +- [Installation](#installation) |
| 17 | +- [Use in napari (GUI)](#usage-in-napari) |
| 18 | +- [Use as a Python library](#usage-as-a-library) |
| 19 | +- [Use from the command line](#usage-as-a-cli) |
| 20 | +- [Run with Docker (GHCR)](#run-with-docker-ghcr) |
| 21 | +- [Models & weights](#models) |
| 22 | +- [Dataset](#dataset) |
| 23 | +- [Issues](#issues) |
| 24 | +- [License](#license) |
| 25 | +- [Acknowledgments](#acknowledgments) |
| 26 | +- [Carbon footprint](#carbon-footprint-of-this-project) |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## Try in your browser (Hugging Face Space) |
| 31 | + |
| 32 | +No local installation needed — test the model directly in your browser: |
| 33 | + |
| 34 | +➡️ **https://huggingface.co/spaces/qchapp/3d-lungs-segmentation** |
| 35 | + |
| 36 | +Upload a mouse CT scan, run the segmentation, visualize and download the resulting lung mask. |
| 37 | + |
| 38 | +--- |
15 | 39 |
|
16 | 40 | ## Installation |
17 | 41 |
|
18 | | -We recommend performing the installation in a clean Python environment. |
| 42 | +We recommend using a fresh Python environment. |
19 | 43 |
|
20 | | -The code requires `python>=3.9`, as well as `pytorch>=2.0`. Please install Pytorch first and separately following the instructions for your platform on [pytorch.org](https://pytorch.org/get-started/locally/). |
| 44 | +- **Requirements:** `python>=3.9`, `pytorch>=2.0` |
| 45 | + Please install PyTorch first for your platform following the instructions on [pytorch.org](https://pytorch.org/get-started/locally/). |
21 | 46 |
|
22 | | -Install `unet_lungs_segmentation` using *pip* after you've installed Pytorch: |
| 47 | +Install the package from PyPI: |
23 | 48 |
|
24 | | -```sh |
| 49 | +```bash |
25 | 50 | pip install unet_lungs_segmentation |
26 | 51 | ``` |
27 | 52 |
|
28 | | -or clone the repository and install with: |
| 53 | +Or install from source: |
29 | 54 |
|
30 | | -```sh |
| 55 | +```bash |
31 | 56 | git clone https://github.com/qchapp/lungs-segmentation.git |
| 57 | +cd lungs-segmentation |
32 | 58 | pip install -e . |
33 | 59 | ``` |
34 | 60 |
|
35 | | -## Models |
| 61 | +--- |
36 | 62 |
|
37 | | -The model weights (~1 GB) will be automatically downloaded from [Hugging Face](https://huggingface.co/qchapp/unet-lungs-segmentation-weights). |
| 63 | +## Usage in napari |
38 | 64 |
|
| 65 | +[Napari](https://napari.org/stable/) is a multidimensional image viewer for Python. |
39 | 66 |
|
40 | | -## Usage in Napari |
| 67 | +1. Launch napari: |
41 | 68 |
|
42 | | -[Napari](https://napari.org/stable/) is a multi-dimensional image viewer for python. To use our model in Napari, start the viewer with |
43 | | - |
44 | | -```sh |
45 | | -napari |
46 | | -``` |
| 69 | + ```bash |
| 70 | + napari |
| 71 | + ``` |
47 | 72 |
|
48 | | -To open an image, use `File > Open files` or drag-and-drop an image into the viewer window. If you want to open medical image formats such as NIFTI directly, consider installing the [napari-medical-image-formats](https://pypi.org/project/napari-medical-image-formats/) plugin. |
| 73 | +2. Open an image via **File → Open files…** or drag & drop into the viewer. |
| 74 | + To open formats like **NIfTI** directly, consider installing the plugin |
| 75 | + [`napari-medical-image-formats`](https://pypi.org/project/napari-medical-image-formats/). |
49 | 76 |
|
50 | | -**Sample data**: To test the model, you can run it on our provided sample image. In Napari, open the image from `File > Open Sample > Mouse lung CT scan`. |
| 77 | +3. **Sample data:** In napari, go to **File → Open Sample → Mouse lung CT scan** to try the model. |
51 | 78 |
|
52 | | -Next, in the menu bar select `Plugins > Lungs segmentation (unet_lungs_segmentation)`. Select an image and run it by pressing the "Segment lungs" button. |
| 79 | +4. Run the plugin via **Plugins → Lungs segmentation (unet_lungs_segmentation)**. |
| 80 | + Select the image layer and click **Segment lungs**. |
53 | 81 |
|
54 | 82 | <p align="center"> |
55 | | - <img src="https://raw.githubusercontent.com/qchapp/lungs-segmentation/refs/heads/master/images/napari-screenshot.png" height="500"> |
| 83 | + <img src="https://raw.githubusercontent.com/qchapp/lungs-segmentation/refs/heads/master/images/napari-screenshot.png" height="500" alt="napari plugin screenshot"> |
56 | 84 | </p> |
57 | 85 |
|
| 86 | +--- |
| 87 | + |
58 | 88 | ## Usage as a library |
59 | 89 |
|
60 | | -You can run a model in just a few lines of code to produce a segmentation mask from an image (represented as a numpy array). |
| 90 | +Run the model in a few lines to obtain a binary mask (NumPy array): |
61 | 91 |
|
62 | | -```py |
| 92 | +```python |
63 | 93 | from unet_lungs_segmentation import LungsPredict |
64 | 94 |
|
65 | 95 | lungs_predict = LungsPredict() |
66 | | -mask = lungs_predict.segment_lungs(your_image) |
| 96 | +mask = lungs_predict.segment_lungs(your_image) # your_image: NumPy array |
67 | 97 | ``` |
68 | | -or if you want to apply a specific `threshold` (`float` between 0 and 1): |
69 | | -```py |
70 | | -mask = lungs_predict.segment_lungs(your_image, threshold) |
| 98 | + |
| 99 | +Specify a custom probability `threshold` (float in `[0, 1]`) if desired: |
| 100 | + |
| 101 | +```python |
| 102 | +mask = lungs_predict.segment_lungs(your_image, threshold=0.5) |
71 | 103 | ``` |
72 | 104 |
|
| 105 | +--- |
| 106 | + |
73 | 107 | ## Usage as a CLI |
74 | 108 |
|
75 | | -Run inference on an image from the command-line. For example: |
| 109 | +Run inference on a single image: |
76 | 110 |
|
77 | | -```sh |
| 111 | +```bash |
78 | 112 | uls_predict_image -i /path/to/folder/image_001.tif [-t <threshold>] |
79 | 113 | ``` |
80 | 114 |
|
81 | | -The `<threshold>` will be applied to the predicted image in order to have a binary mask. A default threshold of 0.5 will be applied if none is given. Should be a `float` between 0 and 1. |
| 115 | +- If `-t`/`--threshold` is provided, it binarizes the prediction at that value (default: `0.5` in `[0,1]`). |
| 116 | +- The output mask is saved next to the input image: |
82 | 117 |
|
83 | | -The command will save the segmentation next to the image: |
84 | 118 | ``` |
85 | 119 | folder/ |
86 | | - ├── image_001.tif |
87 | | - ├── image_001_mask.tif |
| 120 | +├── image_001.tif |
| 121 | +└── image_001_mask.tif |
88 | 122 | ``` |
89 | 123 |
|
90 | | -Run inference in batch on all images in a folder: |
| 124 | +Batch-process a folder: |
91 | 125 |
|
92 | | -```sh |
| 126 | +```bash |
93 | 127 | uls_predict_folder -i /path/to/folder/ [-t <threshold>] |
94 | 128 | ``` |
95 | | -Will produce: |
| 129 | + |
| 130 | +Produces, e.g.: |
| 131 | + |
96 | 132 | ``` |
97 | 133 | folder/ |
98 | | - ├── image_001.tif |
99 | | - ├── image_001_mask.tif |
100 | | - ├── image_002.tif |
101 | | - ├── image_002_mask.tif |
| 134 | +├── image_001.tif |
| 135 | +├── image_001_mask.tif |
| 136 | +├── image_002.tif |
| 137 | +└── image_002_mask.tif |
| 138 | +``` |
| 139 | + |
| 140 | +--- |
| 141 | + |
| 142 | +## Run with Docker (GHCR) |
| 143 | + |
| 144 | +You can run the tool without a local Python setup using our container on **GitHub Container Registry**. |
| 145 | + |
| 146 | +1. **Pull the image:** |
| 147 | + |
| 148 | +```bash |
| 149 | +docker pull ghcr.io/qchapp/unet-lungs-segmentation:latest |
| 150 | +``` |
| 151 | + |
| 152 | +2. **Run on a single image** (mount a data folder into the container): |
| 153 | + |
| 154 | +```bash |
| 155 | +docker run --rm -v /path/to/data:/data ghcr.io/qchapp/unet-lungs-segmentation:latest \ |
| 156 | + -i /data/image_001.tif -t 0.5 |
102 | 157 | ``` |
103 | 158 |
|
| 159 | +3. **Run on a folder:** |
| 160 | + |
| 161 | +```bash |
| 162 | +docker run --rm -v /path/to/data:/data ghcr.io/qchapp/unet-lungs-segmentation:latest \ |
| 163 | + uls_predict_folder -i /data |
| 164 | +``` |
| 165 | + |
| 166 | +> **Note:** The container’s entrypoint is `uls_predict_image`. |
| 167 | +> For folder mode, prefix the command explicitly as shown above. |
| 168 | +
|
| 169 | +--- |
| 170 | + |
| 171 | +## Models |
| 172 | + |
| 173 | +The model weights (~1 GB) are downloaded automatically on first use from |
| 174 | +[Hugging Face](https://huggingface.co/qchapp/unet-lungs-segmentation-weights). |
| 175 | + |
| 176 | +--- |
| 177 | + |
104 | 178 | ## Dataset |
105 | 179 |
|
106 | | -Our model was trained using a dataset of `355` images coming from 17 different experiments, 2 different scanners and validated on `62` images. |
| 180 | +The model was trained on **355** images from 17 different experiments and 2 scanners, and validated on **62** images. |
| 181 | + |
| 182 | +--- |
107 | 183 |
|
108 | 184 | ## Issues |
109 | 185 |
|
110 | | -If you encounter any problems, please fill an issue along with a detailed description. |
| 186 | +If you encounter problems, please open an issue with a clear description and, if possible, a minimal reproducible example. |
| 187 | + |
| 188 | +--- |
111 | 189 |
|
112 | 190 | ## Acknowledgments |
113 | 191 |
|
114 | 192 | This project was developed as part of a **Bachelor’s project** at the *EPFL Center for Imaging*. |
115 | 193 | It was carried out under the supervision of **Mallory Wittwer** and **Edward Andò**, whom we sincerely thank for their guidance and support. |
116 | 194 |
|
| 195 | +--- |
| 196 | + |
117 | 197 | ## License |
118 | 198 |
|
119 | | -This model is licensed under the [BSD-3](LICENSE.txt) license. |
| 199 | +This model is released under the [BSD-3](LICENSE.txt) license. |
| 200 | + |
| 201 | +--- |
120 | 202 |
|
121 | 203 | ## Carbon footprint of this project |
122 | 204 |
|
123 | | -As per the online tool [*Green algorithms*](http://calculator.green-algorithms.org/), the footprint of training this model was estimated to be around 584 g CO2e. |
| 205 | +As estimated by [Green Algorithms](http://calculator.green-algorithms.org/), training this model emitted approximately **584 g CO₂e**. |
0 commit comments