Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic GitHub action for testing nnunet inference #2647

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
49 changes: 49 additions & 0 deletions .github/workflows/run_tests_nnunet.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Run nnunet tests on all OS
on:
push:
paths-ignore:
- '**.md'
jobs:

run-tests:
strategy:
matrix:
# os: [ubuntu-latest, windows-latest, macos-latest] # fails on windows until https://github.com/MIC-DKFZ/nnUNet/issues/2396 is resolved
os: [ubuntu-latest, macos-latest]
python-version: ["3.10"]
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}

- name: Download and extract weights and download example file
# use python instead of curl to avoid the need to install curl (more difficult for macos)
run: |
mkdir -p $HOME/github_actions_nnunet/results
python -c "import urllib.request; urllib.request.urlretrieve('https://github.com/wasserth/TotalSegmentator/releases/download/v2.0.0-weights/Dataset300_body_6mm_1559subj.zip', '$HOME/github_actions_nnunet/results/tmp_download_file.zip')"
unzip -o $HOME/github_actions_nnunet/results/tmp_download_file.zip -d $HOME/github_actions_nnunet/results
rm $HOME/github_actions_nnunet/results/tmp_download_file.zip

- name: Install dependencies on Ubuntu
if: runner.os == 'Linux'
run: |
python -m pip install --upgrade pip
pip install pytest Cython
pip install torch==2.4.0 -f https://download.pytorch.org/whl/cpu
pip install .

- name: Install dependencies on Windows / MacOS
if: runner.os == 'Windows' || runner.os == 'macOS'
run: |
python -m pip install --upgrade pip
pip install pytest Cython
pip install torch==2.4.0
pip install .

- name: Run test script
run: python nnunetv2/tests/integration_tests/run_nnunet_inference.py
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ ENV/
*.txt
.idea/*
*.png
*.nii.gz
# *.nii.gz # nifti files needed for example_data for github actions tests
*.nii
*.tif
*.bmp
Expand Down
Binary file added nnunetv2/tests/example_data/example_ct_sm.nii.gz
Binary file not shown.
Binary file not shown.
46 changes: 46 additions & 0 deletions nnunetv2/tests/integration_tests/run_nnunet_inference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import os
import shutil
import subprocess
from pathlib import Path

import nibabel as nib
import numpy as np


def dice_score(y_true, y_pred):
intersect = np.sum(y_true * y_pred)
denominator = np.sum(y_true) + np.sum(y_pred)
f1 = (2 * intersect) / (denominator + 1e-6)
return f1


def run_tests_and_exit_on_failure():
"""
Runs inference of a simple nnU-Net for CT body segmentation on a small example CT image
and checks if the output is correct.
"""
# Set nnUNet_results env var
weights_dir = Path.home() / "github_actions_nnunet" / "results"
os.environ["nnUNet_results"] = str(weights_dir)

# Copy example file
os.makedirs("nnunetv2/tests/github_actions_output", exist_ok=True)
shutil.copy("nnunetv2/tests/example_data/example_ct_sm.nii.gz", "nnunetv2/tests/github_actions_output/example_ct_sm_0000.nii.gz")

# Run nnunet
subprocess.call(f"nnUNetv2_predict -i nnunetv2/tests/github_actions_output -o nnunetv2/tests/github_actions_output -d 300 -tr nnUNetTrainer -c 3d_fullres -f 0 -device cpu", shell=True)

# Check if the nnunet segmentation is correct
img_gt = nib.load(f"nnunetv2/tests/example_data/example_ct_sm_T300_output.nii.gz").get_fdata()
img_pred = nib.load(f"nnunetv2/tests/github_actions_output/example_ct_sm.nii.gz").get_fdata()
dice = dice_score(img_gt, img_pred)
images_equal = dice > 0.99 # allow for a small difference in the segmentation, otherwise the test will fail often
assert images_equal, f"The nnunet segmentation is not correct (dice: {dice:.5f})."

# Clean up
shutil.rmtree("nnunetv2/tests/github_actions_output")
shutil.rmtree(Path.home() / "github_actions_nnunet")


if __name__ == "__main__":
run_tests_and_exit_on_failure()
Loading