Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 35 additions & 37 deletions panoptica/README.md
Original file line number Diff line number Diff line change
@@ -1,61 +1,59 @@
# Panoptica Tutorials

This folder contains several Jupyter notebooks to showcase different possible use cases of the [panoptica package](https://github.com/BrainLesion/panoptica).
The package allows to compute instance-wise segmentation quality metrics for 2D and 3D semantic- and instance segmentation maps by providing 3 core modules:
This folder contains several Jupyter notebooks to showcase different possible use cases of the [panoptica package](https://github.com/BrainLesion/panoptica).
The package allows to compute instance-wise segmentation quality metrics for 2D and 3D semantic- and instance segmentation maps.

**1. Instance Approximator:** instance approximation algorithms in panoptic segmentation evaluation. Available now: connected components algorithm.
## Notebooks
### Semantic: Spine example

**2. Instance Matcher:** instance matching algorithm in panoptic segmentation evaluation, to align and compare predicted instances with reference instances.

**3. Instance Evaluator:** Evaluation of panoptic segmentation performance by evaluating matched instance pairs and calculating various metrics like true positives, Dice score, IoU, and ASSD for each instance.
<img src="https://github.com/BrainLesion/panoptica/blob/main/examples/figures/semantic.png?raw=true" alt="semantic_figure" height="300"/>

![workflow_figure](https://github.com/BrainLesion/panoptica/blob/main/examples/figures/workflow.png)
`example_spine_semantic.ipynb`

## Use Cases
[![nbviewer](https://raw.githubusercontent.com/jupyter/design/master/logos/Badges/nbviewer_badge.svg)](https://nbviewer.jupyter.org/github/BrainLesion/tutorials/blob/main/panoptica/example_spine_semantic.ipynb)
<a target="_blank" href="https://colab.research.google.com/github/BrainLesion/tutorials/blob/main/panoptica/example_spine_semantic.ipynb">
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
</a>

### Semantic Segmentation Input
### Matched Instance: Spine example

<img src="https://github.com/BrainLesion/panoptica/blob/main/examples/figures/semantic.png?raw=true" alt="semantic_figure" height="300"/>
<img src="https://github.com/BrainLesion/panoptica/blob/main/examples/figures/matched_instance.png?raw=true" alt="matched_instance_figure" height="300"/>

Although for many biomedical segmentation problems, an instance-wise evaluation is highly relevant and desirable, they are still addressed as semantic segmentation problems due to lack of appropriate instance labels.

**Modules [1-3]** can be used to obtain panoptic metrics of matched instances based on a semantic segmentation input.
`example_spine_matched_instance.ipynb`

[Jupyter Notebook Example](https://github.com/BrainLesion/tutorials/tree/main/panoptica/example_spine_semantic.ipynb)
[![nbviewer](https://raw.githubusercontent.com/jupyter/design/master/logos/Badges/nbviewer_badge.svg)](https://nbviewer.jupyter.org/github/BrainLesion/tutorials/blob/main/panoptica/example_spine_matched_instance.ipynb)
<a target="_blank" href="https://colab.research.google.com/github/BrainLesion/tutorials/blob/main/panoptica/example_spine_matched_instance.ipynb">
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
</a>

### Unmatched Instances Input
### Unmatched Instance: Spine example

<img src="https://github.com/BrainLesion/panoptica/blob/main/examples/figures/unmatched_instance.png?raw=true" alt="unmatched_instance_figure" height="300"/>

It is a common issue that instance segementation outputs have good segmentations with mismatched labels.
`example_spine_unmatched_instance.ipynb`

For this case **modules [2-3]** can be utilized to match the instances and report panoptic metrics.
[![nbviewer](https://raw.githubusercontent.com/jupyter/design/master/logos/Badges/nbviewer_badge.svg)](https://nbviewer.jupyter.org/github/BrainLesion/tutorials/blob/main/panoptica/example_spine_unmatched_instance.ipynb)
<a target="_blank" href="https://colab.research.google.com/github/BrainLesion/tutorials/blob/main/panoptica/example_spine_unmatched_instance.ipynb">
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
</a>

[Jupyter Notebook Example](https://github.com/BrainLesion/tutorials/tree/main/panoptica/example_spine_unmatched_instance.ipynb)

### Matched Instances Input

<img src="https://github.com/BrainLesion/panoptica/blob/main/examples/figures/matched_instance.png?raw=true" alt="matched_instance_figure" height="300"/>

Ideally the input data already provides matched instances.

In this case **module 3** can be used to directly report panoptic metrics without requiring any internal preprocessing.
### Matching: Spine example
`example_spine_matching_algorithm.ipynb`

[Jupyter Notebook Example](https://github.com/BrainLesion/tutorials/tree/main/panoptica/example_spine_matched_instance.ipynb)
[![nbviewer](https://raw.githubusercontent.com/jupyter/design/master/logos/Badges/nbviewer_badge.svg)](https://nbviewer.jupyter.org/github/BrainLesion/tutorials/blob/main/panoptica/example_spine_matching_algorithm.ipynb)
<a target="_blank" href="https://colab.research.google.com/github/BrainLesion/tutorials/blob/main/panoptica/example_spine_matching_algorithm.ipynb">
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
</a>

## Citation

If you use panoptica in your research, please cite it to support the development!
### Save/Load Configs example:
`example_config.ipynb`

Kofler, F., Möller, H., Buchner, J. A., de la Rosa, E., Ezhov, I., Rosier, M., Mekki, I., Shit, S., Negwer, M., Al-Maskari, R., Ertürk, A., Vinayahalingam, S., Isensee, F., Pati, S., Rueckert, D., Kirschke, J. S., Ehrlich, S. K., Reinke, A., Menze, B., Wiestler, B., & Piraud, M. (2023). _Panoptica -- instance-wise evaluation of 3D semantic and instance segmentation maps._ [arXiv preprint arXiv:2312.02608](https://arxiv.org/abs/2312.02608).
[![nbviewer](https://raw.githubusercontent.com/jupyter/design/master/logos/Badges/nbviewer_badge.svg)](https://nbviewer.jupyter.org/github/BrainLesion/tutorials/blob/main/panoptica/example_config.ipynb)
<a target="_blank" href="https://colab.research.google.com/github/BrainLesion/tutorials/blob/main/panoptica/example_config.ipynb">
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>
</a>

```
@misc{kofler2023panoptica,
title={Panoptica -- instance-wise evaluation of 3D semantic and instance segmentation maps},
author={Florian Kofler and Hendrik Möller and Josef A. Buchner and Ezequiel de la Rosa and Ivan Ezhov and Marcel Rosier and Isra Mekki and Suprosanna Shit and Moritz Negwer and Rami Al-Maskari and Ali Ertürk and Shankeeth Vinayahalingam and Fabian Isensee and Sarthak Pati and Daniel Rueckert and Jan S. Kirschke and Stefan K. Ehrlich and Annika Reinke and Bjoern Menze and Benedikt Wiestler and Marie Piraud},
year={2023},
eprint={2312.02608},
archivePrefix={arXiv},
primaryClass={cs.CV}
}
```
143 changes: 83 additions & 60 deletions panoptica/example_config.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,69 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"!pip install panoptica auxiliary rich numpy > /dev/null"
]
},
{
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: panoptica in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 1)) (1.0.0.post2.dev0+2f7d01f)\n",
"Requirement already satisfied: auxiliary in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 2)) (0.0.42)\n",
"Requirement already satisfied: rich in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 3)) (13.6.0)\n",
"Requirement already satisfied: numpy in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 4)) (1.25.2)\n",
"Requirement already satisfied: connected-components-3d<4.0.0,>=3.12.3 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (3.12.3)\n",
"Requirement already satisfied: ruamel.yaml<0.19.0,>=0.18.6 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (0.18.6)\n",
"Requirement already satisfied: scikit-image<0.23.0,>=0.22.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (0.22.0)\n",
"Requirement already satisfied: scipy<2.0.0,>=1.7.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (1.11.2)\n",
"Requirement already satisfied: nibabel>=3.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (5.1.0)\n",
"Requirement already satisfied: path>=16.10.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (17.0.0)\n",
"Requirement already satisfied: pathlib>=1.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (1.0.1)\n",
"Requirement already satisfied: pillow>=10.0.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (10.0.0)\n",
"Requirement already satisfied: tifffile>=2023.8.25 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (2023.8.30)\n",
"Requirement already satisfied: markdown-it-py>=2.2.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from rich->-r requirements.txt (line 3)) (3.0.0)\n",
"Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from rich->-r requirements.txt (line 3)) (2.17.2)\n",
"Requirement already satisfied: mdurl~=0.1 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from markdown-it-py>=2.2.0->rich->-r requirements.txt (line 3)) (0.1.2)\n",
"Requirement already satisfied: packaging>=17 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from nibabel>=3.0->auxiliary->-r requirements.txt (line 2)) (23.1)\n",
"Requirement already satisfied: ruamel.yaml.clib>=0.2.7 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from ruamel.yaml<0.19.0,>=0.18.6->panoptica->-r requirements.txt (line 1)) (0.2.8)\n",
"Requirement already satisfied: networkx>=2.8 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (3.1)\n",
"Requirement already satisfied: imageio>=2.27 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (2.31.3)\n",
"Requirement already satisfied: lazy_loader>=0.3 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (0.3)\n"
]
}
],
"source": [
"!pip install -r requirements.txt"
"If you installed the packages and requirements on your own machine, you can skip this section and start from the import section.\n",
"\n",
"### Setup Colab environment (optional) \n",
"Otherwise you can follow and execute the tutorial on your browser.\n",
"In order to start working on the notebook, click on the following button, this will open this page in the Colab environment and you will be able to execute the code on your own (*Google account required*).\n",
"\n",
"<a target=\"_blank\" href=\"https://colab.research.google.com/github/BrainLesion/tutorials/blob/main/panoptica/example_config.ipynb\">\n",
" <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
"</a>\n",
"\n",
"Now that you are visualizing the notebook in Colab, run the next cell to install the packages we will use. There are few things you should follow in order to properly set the notebook up:\n",
"1. Warning: This notebook was not authored by Google. Click on 'Run anyway'.\n",
"1. When the installation commands are done, there might be \"Restart runtime\" button at the end of the output. Please, click it.\n",
"If you run the next cell in a Google Colab environment, it will **clone the 'tutorials' repository** in your google drive. This will create a **new folder** called \"tutorials\" in **your Google Drive**.\n",
"All generated file will be created/uploaded to your Google Drive respectively.\n",
"\n",
"After the first execution of the next cell, you might receive some warnings and notifications, please follow these instructions:\n",
" - 'Permit this notebook to access your Google Drive files?' Click on 'Yes', and select your account.\n",
" - Google Drive for desktop wants to access your Google Account. Click on 'Allow'.\n",
"\n",
"Afterwards the \"tutorials\" folder has been created. You can navigate it through the lefthand panel in Colab. You might also have received an email that informs you about the access on your Google Drive."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"\n",
"# Check if we are in google colab currently\n",
"try:\n",
" import google.colab\n",
"\n",
" colabFlag = True\n",
"except ImportError as r:\n",
" colabFlag = False\n",
"\n",
"# Execute certain steps only if we are in a colab environment\n",
"if colabFlag:\n",
" # Create a folder in your Google Drive\n",
" from google.colab import drive\n",
"\n",
" drive.mount(\"/content/drive\")\n",
" # clone repository and set path\n",
" !git clone https://github.com/BrainLesion/tutorials.git /content/drive/MyDrive/tutorials\n",
" BASE_PATH = \"/content/drive/MyDrive/tutorials/panoptica\"\n",
" sys.path.insert(0, BASE_PATH)\n",
"\n",
"else: # normal jupyter notebook environment\n",
" BASE_PATH = \".\" # current working directory would be BraTs-Toolkit anyways if you are not in colab"
]
},
{
Expand All @@ -60,7 +90,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -90,7 +120,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 13,
"metadata": {},
"outputs": [
{
Expand All @@ -102,14 +132,14 @@
" 106, 107, 108, 202, 203, 204, 205, 206, 207, 208], dtype=uint8))"
]
},
"execution_count": 11,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ref_masks = read_nifti(\"./spine_seg/matched_instance/ref.nii.gz\")\n",
"pred_masks = read_nifti(\"./spine_seg/matched_instance/pred.nii.gz\")\n",
"ref_masks = read_nifti(f\"{BASE_PATH}/spine_seg/matched_instance/ref.nii.gz\")\n",
"pred_masks = read_nifti(f\"{BASE_PATH}/spine_seg/matched_instance/pred.nii.gz\")\n",
"\n",
"# labels are matching\n",
"np.unique(ref_masks), np.unique(pred_masks)"
Expand All @@ -124,7 +154,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 14,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -156,7 +186,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 15,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -202,14 +232,14 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saved config into /DATA/NAS/ongoing_projects/hendrik/panoptica/repo/panoptica/test_config.yaml\n"
"Saved config into /home/marcelrosier/miniconda3/envs/tutorials/lib/python3.10/site-packages/panoptica/test_config.yaml\n"
]
}
],
Expand Down Expand Up @@ -238,14 +268,14 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<panoptica.panoptica_evaluator.Panoptica_Evaluator object at 0x7f05ac7b80d0>\n"
"<panoptica.panoptica_evaluator.Panoptica_Evaluator object at 0x7002f42ccd60>\n"
]
}
],
Expand All @@ -256,7 +286,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 18,
"metadata": {},
"outputs": [
{
Expand All @@ -267,9 +297,9 @@
"-- Got MatchedInstancePair, will evaluate instances\n",
"\n",
"+++ MATCHING +++\n",
"Number of instances in reference (num_ref_instances): 1\n",
"Number of instances in prediction (num_pred_instances): 1\n",
"True Positives (tp): 1\n",
"Number of instances in reference (num_ref_instances): 22\n",
"Number of instances in prediction (num_pred_instances): 22\n",
"True Positives (tp): 22\n",
"False Positives (fp): 0\n",
"False Negatives (fn): 0\n",
"Recognition Quality / F1-Score (rq): 1.0\n",
Expand All @@ -278,12 +308,12 @@
"Global Binary Dice (global_bin_dsc): 0.9744370224078394\n",
"\n",
"+++ INSTANCE +++\n",
"Segmentation Quality IoU (sq): 0.9501484001456879 +- 0.0\n",
"Panoptic Quality IoU (pq): 0.9501484001456879\n",
"Segmentation Quality Dsc (sq_dsc): 0.9744370224078394 +- 0.0\n",
"Panoptic Quality Dsc (pq_dsc): 0.9744370224078394\n",
"Segmentation Quality ASSD (sq_assd): 0.1499152780072207 +- 0.0\n",
"Segmentation Quality Relative Volume Difference (sq_rvd): -0.0006011763989268869 +- 0.0\n",
"Segmentation Quality IoU (sq): 0.8328184295330796 +- 0.15186064004517466\n",
"Panoptic Quality IoU (pq): 0.8328184295330796\n",
"Segmentation Quality Dsc (sq_dsc): 0.900292616009954 +- 0.10253566174957332\n",
"Panoptic Quality Dsc (pq_dsc): 0.900292616009954\n",
"Segmentation Quality ASSD (sq_assd): 0.250331887879225 +- 0.07696680402317076\n",
"Segmentation Quality Relative Volume Difference (sq_rvd): 0.0028133049062930553 +- 0.034518928495505724\n",
"\n"
]
}
Expand All @@ -293,13 +323,6 @@
"result, debug_data = evaluator.evaluate(pred_masks, ref_masks)[\"ungrouped\"]\n",
"print(result) # yields same results as the evaluator object manually constructed"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -318,7 +341,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
"version": "3.10.14"
}
},
"nbformat": 4,
Expand Down
Loading