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

14 monai thorax 3d pipeline #15

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<p align="center">
<h1 align="center">aide-svrtk</h1>
<p align="center">
AI-driven, automated <a href="https://github.com/SVRTK/SVRTK">SVR reconstruction</a> packaged as an AIDE Application, based on
AI-driven, <a href="https://github.com/SVRTK/auto-proc-svrtk">automated</a> version of <a href="https://github.com/SVRTK/SVRTK">SVR reconstruction</a> packaged as an AIDE Application, based on
the open-source <a href="https://github.com/Project-MONAI/monai-deploy/blob/main/guidelines/monai-application-package.md">
MONAI Application Package (MAP)</a> standard.
<br />
Expand All @@ -20,14 +20,14 @@

The slice-to-volume reconstruction toolkit ([SVRTK](https://github.com/SVRTK/SVRTK)) is an image-based registration
framework for reconstruction of 3D volumes from multiple 2D image slices. SVRTK is used extensively for performing
motion-corrected reconstruction of MRI data.
motion-corrected reconstruction of MRI data. The [automated SVR](https://github.com/SVRTK/auto-proc-svrtk) version employs deep learning [MONAI](https://github.com/Project-MONAI/MONAI) networks for localisation and reorientation of the thorax to the standard radiological space.

Currently, this MAP implements SVRTK for reconstruction of 2D **fetal brain MRI** data into motion-corrected 3D volumes.
Currently, this MAP implements SVRTK for reconstruction of 2D **fetal thorax MRI** data into motion-corrected 3D volumes.
Further MAPs for reconstruction of other fetal organs are in development and coming soon.

[AIDE](https://www.aicentre.co.uk/platforms#view1) is an open-source platform for the deployment of AI applications in
healthcare settings. This repo, created and led by [Tom Roberts](https://github.com/tomaroberts), packages automated
3D fetal brain MRI reconstruction into a [MONAI Application Package (MAP)](https://github.com/Project-MONAI/monai-deploy)
healthcare settings. This repo, created and led by [Tom Roberts](https://github.com/tomaroberts), packages [automated
3D fetal thorax MRI reconstruction](https://github.com/SVRTK/auto-proc-svrtk) into a [MONAI Application Package (MAP)](https://github.com/Project-MONAI/monai-deploy)
for execution on AIDE, and other platforms compatible with the MAP standard.

## aide-svrtk MAP workflow
Expand All @@ -38,10 +38,10 @@ MRI DICOM Series contained the motion-corrected reconstruction.
The aide-svrtk MAP consists of three operators:
1. `dcm2nii_operator.py` – converts the input 2D MRI DICOM Series into NIfTI format required by SVRTK, using
[dcm2niix](https://github.com/rordenlab/dcm2niix)
2. `fetal_mri_3d_brain_recon_operator.py` – runs automated fetal brain SVRTK reconstruction, in three main steps:
a. AI-driven brain masking
b. Slice-to-volume registration reconstruction
c. AI-driven brain reorientation
2. `fetal_mri_3d_thorax_recon_operator.py` – runs [automated fetal thorax SVRTK reconstruction](https://github.com/SVRTK/auto-proc-svrtk), in three main steps:
a. AI-driven thorax masking
b. Deformable slice-to-volume registration reconstruction
c. AI-driven thorax reorientation
3. `nii2dcm_operator.py` – converts the SVRTK output NIfTI into a 3D DICOM Series, using
[nii2dcm](https://github.com/tomaroberts/nii2dcm)

Expand All @@ -56,7 +56,7 @@ The aide-svrtk MAP consists of three operators:

1. Download
```shell
git clone https://github.com/SVRTK/aide-svrtk.git
git clone -b 14-monai-thorax-3d-pipeline https://github.com/SVRTK/aide-svrtk.git
```

2. Setup virtual env
Expand Down Expand Up @@ -98,37 +98,37 @@ The final MAP is called `map`

```shell
# Initial packaging of MAP
monai-deploy package app -t ghcr.io/svrtk/aide-svrtk/map-init:0.2.0 -r requirements.txt -l DEBUG
monai-deploy package app -t ghcr.io/svrtk/aide-svrtk/map-init:0.2.2 -r requirements.txt -l DEBUG

# Build 3rd-party software on top of MAP
docker build -t ghcr.io/svrtk/aide-svrtk/map:0.2.0 app/
docker build -t ghcr.io/svrtk/aide-svrtk/map:0.2.2 app/

# Test MAP with MONAI Deploy
monai-deploy run ghcr.io/svrtk/aide-svrtk/map:0.2.0 input/ output/
monai-deploy run ghcr.io/svrtk/aide-svrtk/map:0.2.2 input/ output/

# Push initial MAP and final MAP to GHCR
docker push ghcr.io/svrtk/aide-svrtk/map-init:0.2.0
docker push ghcr.io/svrtk/aide-svrtk/map:0.2.0
docker push ghcr.io/svrtk/aide-svrtk/map-init:0.2.2
docker push ghcr.io/svrtk/aide-svrtk/map:0.2.2
```

## Optional

Enter Docker container for testing

```shell
docker run --gpus all -it --rm -v local/path/to/aide-svrtk/input:/var/monai/input/ --entrypoint /bin/bash ghcr.io/svrtk/aide-svrtk/map:0.2.0
docker run --gpus all -it --rm -v local/path/to/aide-svrtk/input:/var/monai/input/ --entrypoint /bin/bash ghcr.io/svrtk/aide-svrtk/map:0.2.2
```

Run on specified GPU if machine has >1 available

```shell
CUDA_VISIBLE_DEVICES=2 monai-deploy run ghcr.io/svrtk/aide-svrtk/map:0.2.0 input/ output/
CUDA_VISIBLE_DEVICES=2 monai-deploy run ghcr.io/svrtk/aide-svrtk/map:0.2.2 input/ output/
```

## Running on AIDE
To run aide-svrtk MAP on AIDE, two files from the `app/workflows` directory are required, namely:
- `fetal-3d-brain-mri.json` – AIDE Clinical Workflow file.
- `fetal-3d-thorax-mri.json` – AIDE Clinical Workflow file.
- Effectively a sequence of tasks run on AIDE
- `fetal-brain-3d-recon-argo-template.yaml` – Argo Workflow Template file.
- `fetal-thorax-3d-recon-argo-template.yaml` – Argo Workflow Template file.
- Called by the AIDE Clinical Workflow file. Executes the aide-svrtk MAP using Argo. This is the central task within
the AIDE Clinical Workflow file.
46 changes: 28 additions & 18 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ghcr.io/svrtk/aide-svrtk/map-init:0.2.0 AS build
FROM ghcr.io/svrtk/aide-svrtk/map-init:0.2.2 AS build

# Add dcm2niix to MAP
WORKDIR /bin
Expand All @@ -19,7 +19,7 @@ WORKDIR /var/monai

# Add SVRTK to MAP

WORKDIR /home
WORKDIR /bin

# Install general libraries
RUN apt-get update && apt-get install --no-install-recommends --no-install-suggests -y \
Expand All @@ -37,42 +37,52 @@ RUN apt-get install -y \

# Install MIRTK/SVRTK
RUN git clone https://github.com/SVRTK/MIRTK.git \
&& mkdir -p /home/MIRTK/Packages/SVRTK
RUN git clone https://github.com/SVRTK/SVRTK.git /home/MIRTK/Packages/SVRTK \
&& mkdir -p /home/MIRTK/build \
&& cd /home/MIRTK/build \
&& mkdir -p /bin/MIRTK/Packages/SVRTK
RUN git clone https://github.com/SVRTK/SVRTK.git /bin/MIRTK/Packages/SVRTK \
&& mkdir -p /bin/MIRTK/build \
&& cd /bin/MIRTK/build \
&& cmake -D WITH_TBB="ON" -D MODULE_SVRTK="ON" .. \
&& make -j

ENV PATH="$PATH:/home/MIRTK/build/bin:/home/MIRTK/build/lib/tools"
ENV PATH="$PATH:/bin/MIRTK/build/bin:/bin/MIRTK/build/lib/tools"

WORKDIR /var/monai


# Add 3D UNet model weights & execution script
# Add MONAI 3D UNet model weights & execution script

WORKDIR /home

# Setup 3D UNet models
RUN git clone https://github.com/SVRTK/Segmentation_FetalMRI.git --branch svrtk-docker-gpu-0.10 --single-branch /home/Segmentation_FetalMRI
RUN git clone https://github.com/SVRTK/auto-proc-svrtk.git /home/auto-proc-svrtk

RUN wget https://gin.g-node.org/SVRTK/fetal_mri_network_weights/raw/master/monai-checkpoints-atunet-brain_bet_all_degree_raw_stacks-1-lab/best_metric_model.pth -P /home/auto-proc-svrtk/trained_models/monai-checkpoints-atunet-brain_bet_all_degree_raw_stacks-1-lab \
&& wget https://gin.g-node.org/SVRTK/fetal_mri_network_weights/raw/master/monai-checkpoints-unet-global-loc-2-lab/best_metric_model.pth -P /home/auto-proc-svrtk/trained_models/monai-checkpoints-unet-global-loc-2-lab \
&& wget https://gin.g-node.org/SVRTK/fetal_mri_network_weights/raw/master/monai-checkpoints-unet-svr-brain-reo-5-lab/best_metric_model.pth -P /home/auto-proc-svrtk/trained_models/monai-checkpoints-unet-svr-brain-reo-5-lab \
&& wget https://gin.g-node.org/SVRTK/fetal_mri_network_weights/raw/master/monai-checkpoints-unet-svr-brain-reo-raw-stacks-5-lab/best_metric_model.pth -P /home/auto-proc-svrtk/trained_models/monai-checkpoints-unet-svr-brain-reo-raw-stacks-5-lab \
&& wget https://gin.g-node.org/SVRTK/fetal_mri_network_weights/raw/master/monai-checkpoints-unet-stack-body-reo-4-lab/best_metric_model.pth -P /home/auto-proc-svrtk/trained_models/monai-checkpoints-unet-stack-body-reo-4-lab \
&& wget https://gin.g-node.org/SVRTK/fetal_mri_network_weights/raw/master/monai-checkpoints-unet-thorax-reo-4-lab-cmr/best_metric_model.pth -P /home/auto-proc-svrtk/trained_models/monai-checkpoints-unet-thorax-reo-4-lab-cmr




RUN wget https://gin.g-node.org/SVRTK/fetal_mri_network_weights/raw/master/checkpoints-brain-loc-2-labels/latest.ckpt -P /home/Segmentation_FetalMRI/trained-models/checkpoints-brain-loc-labels \
&& wget https://gin.g-node.org/SVRTK/fetal_mri_network_weights/raw/master/checkpoints-brain-loc-2-labels-cropped/latest.ckpt -P /home/Segmentation_FetalMRI/trained-models/checkpoints-brain-loc-labels-cropped \
&& wget https://gin.g-node.org/SVRTK/fetal_mri_network_weights/raw/master/checkpoints-brain-reo-5-labels/latest.ckpt -P /home/Segmentation_FetalMRI/trained-models/checkpoints-brain-reorientation \
&& wget https://gin.g-node.org/SVRTK/fetal_mri_network_weights/raw/master/checkpoints-brain-reo-5-labels-raw-stacks/latest.ckpt -P /home/Segmentation_FetalMRI/trained-models/checkpoints-brain-reorientation-stacks

# Install Python packages
RUN python -m pip install -r /home/Segmentation_FetalMRI/requirements.txt
RUN python -m pip install -r /home/auto-proc-svrtk/config/requirements.txt

# Bugfix: without below, cannot import torch within Python
# Error: OSError: /opt/hpcx/ompi/lib/libmpi.so.40: undefined symbol: opal_hwloc201_hwloc_get_type_depth
# Fix: https://forums.developer.nvidia.com/t/issues-building-docker-image-from-ngc-container-nvcr-io-nvidia-pytorch-22-py3/209034/5
ENV PATH="${PATH}:/opt/hpcx/ompi/bin"
ENV LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/opt/hpcx/ompi/lib"

COPY docker-recon-brain-auto.bash /home/scripts/docker-recon-brain-auto.bash
RUN mkdir -p /home/recon \
&& mkdir -p /home/output \
&& chmod +x /home/scripts/*
#COPY /home/auto-proc-svrtk/scripts/auto-thorax-reconstruction-aide.sh /home/scripts/auto-thorax-reconstruction.sh


RUN mkdir -p /home/recon
RUN mkdir -p /home/output
RUN mkdir -p /home/scripts
RUN chmod +x /home/auto-proc-svrtk/scripts/*


WORKDIR /var/monai
4 changes: 2 additions & 2 deletions app/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from app import FetalMri3dBrainApp
from app import FetalMri3dThoraxApp

if __name__ == "__main__":
FetalMri3dBrainApp(do_run=True)
FetalMri3dThoraxApp(do_run=True)
18 changes: 9 additions & 9 deletions app/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# AI-driven 3D fetal brain MRI reconstruction using SVRTK constructed as a MONAI Application Package (MAP)
# AI-driven 3D fetal thorax MRI reconstruction using SVRTK constructed as a MONAI Application Package (MAP)
#
# Tom Roberts ([email protected] / [email protected])
#
Expand All @@ -10,18 +10,18 @@
from monai.deploy.operators.dicom_series_selector_operator import DICOMSeriesSelectorOperator

from operators.dcm2nii_operator import Dcm2NiiOperator
from operators.fetal_mri_3d_brain_recon_operator import FetalMri3dBrainOperator
from operators.fetal_mri_3d_thorax_recon_operator import FetalMri3dThoraxOperator
from operators.nii2dcm_operator import NiftiToDicomWriterOperator


@resource(cpu=16, gpu=1, memory="32Gi")
class FetalMri3dBrainApp(Application):
class FetalMri3dThoraxApp(Application):
"""
Motion-corrected 3D fetal brain MRI Application class
Motion-corrected 3D fetal thorax MRI Application class
"""

name = "3d-fetal-brain-mri"
description = "Motion-corrected 3D fetal brain MRI application."
name = "3d-fetal-thorax-mri"
description = "Motion-corrected 3D fetal thorax MRI application."
version = "0.2.0"

def compose(self):
Expand All @@ -36,8 +36,8 @@ def compose(self):
# DICOM to NIfTI operator
dcm2nii_op = Dcm2NiiOperator()

# Fetal Brain 3D MRI reconstruction operator
fetal_mri_3d_recon_op = FetalMri3dBrainOperator()
# Fetal Thorax 3D MRI reconstruction operator
fetal_mri_3d_recon_op = FetalMri3dThoraxOperator()

# DICOM Writer operator
nii2dcm_op = NiftiToDicomWriterOperator()
Expand Down Expand Up @@ -68,4 +68,4 @@ def compose(self):
"""

if __name__ == "__main__":
FetalMri3dBrainApp(do_run=True)
FetalMri3dThoraxApp(do_run=True)
Loading