diff --git a/monai/deploy/operators/__init__.py b/monai/deploy/operators/__init__.py index 79733bdb..17bf3ac0 100644 --- a/monai/deploy/operators/__init__.py +++ b/monai/deploy/operators/__init__.py @@ -1,4 +1,4 @@ -# Copyright 2021 MONAI Consortium +# Copyright 2021-2022 MONAI Consortium # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -12,6 +12,7 @@ .. autosummary:: :toctree: _autosummary + ClaraVizOperator DICOMDataLoaderOperator DICOMSegmentationWriterOperator DICOMSeriesSelectorOperator @@ -22,6 +23,7 @@ PublisherOperator """ +from .clara_viz_operator import ClaraVizOperator from .dicom_data_loader_operator import DICOMDataLoaderOperator from .dicom_seg_writer_operator import DICOMSegmentationWriterOperator from .dicom_series_selector_operator import DICOMSeriesSelectorOperator diff --git a/monai/deploy/operators/clara_viz_operator.py b/monai/deploy/operators/clara_viz_operator.py new file mode 100644 index 00000000..450dec07 --- /dev/null +++ b/monai/deploy/operators/clara_viz_operator.py @@ -0,0 +1,134 @@ +# Copyright 2022 MONAI Consortium +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import numpy as np + +import monai.deploy.core as md +from monai.deploy.core import ExecutionContext, Image, InputContext, IOType, Operator, OutputContext +from monai.deploy.utils.importutil import optional_import + +DataDefinition, _ = optional_import("clara.viz.core", name="DataDefinition") +Widget, _ = optional_import("clara.viz.widgets", name="Widget") +display, _ = optional_import("IPython.display", name="display") +interactive, _ = optional_import("ipywidgets", name="interactive") +Dropdown, _ = optional_import("ipywidgets", name="Dropdown") +Box, _ = optional_import("ipywidgets", name="Box") +VBox, _ = optional_import("ipywidgets", name="VBox") + + +@md.input("image", Image, IOType.IN_MEMORY) +@md.input("seg_image", Image, IOType.IN_MEMORY) +@md.env(pip_packages=["clara.viz.core", "clara.viz.widgets", "IPython"]) +class ClaraVizOperator(Operator): + """ + This operator uses Clara Viz to provide interactive view of a 3D volume including segmentation mask. + """ + + def __init__(self): + """Constructor of the operator.""" + super().__init__() + + @staticmethod + def _build_array(image, order): + numpy_array = image.asnumpy() + + array = DataDefinition.Array(array=numpy_array, order=order) + array.element_size = [1.0] + array.element_size.append(image.metadata().get("col_pixel_spacing", 1.0)) + array.element_size.append(image.metadata().get("row_pixel_spacing", 1.0)) + array.element_size.append(image.metadata().get("depth_pixel_spacing", 1.0)) + + # the renderer is expecting data in RIP order (Right Inferior Posterior) which results in + # this matrix + target_affine_transform = [ + [-1.0, 0.0, 0.0, 0.0], + [0.0, 0.0, 1.0, 0.0], + [0.0, -1.0, 0.0, 0.0], + [0.0, 0.0, 0.0, 1.0], + ] + + dicom_affine_transform = image.metadata().get("dicom_affine_transform", np.identity(4)) + + affine_transform = np.matmul(target_affine_transform, dicom_affine_transform) + + array.permute_axes = [ + 0, + max(range(3), key=lambda k: abs(affine_transform[0][k])) + 1, + max(range(3), key=lambda k: abs(affine_transform[1][k])) + 1, + max(range(3), key=lambda k: abs(affine_transform[2][k])) + 1, + ] + + array.flip_axes = [ + False, + affine_transform[0][array.permute_axes[1] - 1] < 0.0, + affine_transform[1][array.permute_axes[2] - 1] < 0.0, + affine_transform[2][array.permute_axes[3] - 1] < 0.0, + ] + + return array + + def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext): + """Displays the input image and segmentation mask + + Args: + op_input (InputContext): An input context for the operator. + op_output (OutputContext): An output context for the operator. + context (ExecutionContext): An execution context for the operator. + """ + input_image = op_input.get("image") + if not input_image: + raise ValueError("Input image is not found.") + input_seg_image = op_input.get("seg_image") + if not input_seg_image: + raise ValueError("Input segmentation image is not found.") + + # build the data definition + data_definition = DataDefinition() + + data_definition.arrays.append(self._build_array(input_image, "DXYZ")) + + data_definition.arrays.append(self._build_array(input_seg_image, "MXYZ")) + + widget = Widget() + widget.select_data_definition(data_definition) + # default view mode is 'CINEMATIC' switch to 'SLICE_SEGMENTATION' since we have no transfer functions defined + widget.settings["Views"][0]["mode"] = "SLICE_SEGMENTATION" + widget.settings["Views"][0]["cameraName"] = "Top" + widget.set_settings() + + # add controls + def set_view_mode(view_mode): + widget.settings["Views"][0]["mode"] = view_mode + if view_mode == "CINEMATIC": + widget.settings["Views"][0]["cameraName"] = "Perspective" + elif widget.settings["Views"][0]["cameraName"] == "Perspective": + widget.settings["Views"][0]["cameraName"] = "Top" + widget.set_settings() + + widget_view_mode = interactive( + set_view_mode, + view_mode=Dropdown( + options=[("Cinematic", "CINEMATIC"), ("Slice", "SLICE"), ("Slice Segmentation", "SLICE_SEGMENTATION")], + value="SLICE_SEGMENTATION", + description="View mode", + ), + ) + + def set_camera(camera): + if widget.settings["Views"][0]["mode"] != "CINEMATIC": + widget.settings["Views"][0]["cameraName"] = camera + widget.set_settings() + + widget_camera = interactive( + set_camera, camera=Dropdown(options=["Top", "Right", "Front"], value="Top", description="Camera") + ) + + display(Box([widget, VBox([widget_view_mode, widget_camera])])) diff --git a/notebooks/tutorials/03_segmentation_viz_app.ipynb b/notebooks/tutorials/03_segmentation_viz_app.ipynb new file mode 100644 index 00000000..42bd78bd --- /dev/null +++ b/notebooks/tutorials/03_segmentation_viz_app.ipynb @@ -0,0 +1,1733 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Creating a Segmentation App with MONAI Deploy App SDK\n", + "\n", + "This tutorial shows how to create an organ segmentation application for a PyTorch model that has been trained with MONAI.\n", + "\n", + "Deploying AI models requires the integration with clinical imaging network, even if in a for-research-use setting. This means that the AI deploy application will need to support standards-based imaging protocols, and specifically for Radiological imaging, DICOM protocol.\n", + "\n", + "Typically, DICOM network communication, either in DICOM TCP/IP network protocol or DICOMWeb, would be handled by DICOM devices or services, e.g. MONAI Deploy Informatics Gateway, so the deploy application itself would only need to use DICOM Part 10 files as input and save the AI result in DICOM Part10 file(s). For segmentation use cases, the DICOM instance file could be a DICOM Segmentation object or a DICOM RT Structure Set, and for classification, DICOM Structure Report and/or DICOM Encapsulated PDF.\n", + "\n", + "During model training, input and label images are typically in non-DICOM volumetric image format, e.g., NIfTI and PNG, converted from a specific DICOM study series. Furthermore, the voxel spacings most likely have been re-sampled to be uniform for all images. When integrated with imaging networks and receiving DICOM instances from modalities and Picture Archiving and Communications System, PACS, an AI deploy application may have to deal with a whole DICOM study with multiple series, whose images' spacing may not be the same as expected by the trained model. To address these cases consistently and efficiently, MONAI Deploy Application SDK provides classes, called operators, to parse DICOM studies, select specific series with application-defined rules, and convert the selected DICOM series into domain-specific image format along with meta-data representing the pertinent DICOM attributes.\n", + "\n", + "In the following sections, we will demonstrate how to create a MONAI Deploy application package using the MONAI Deploy App SDK.\n", + "\n", + ":::{note}\n", + "For local testing, if there is a lack of DICOM Part 10 files, one can use open source programs, e.g. 3D Slicer, to convert NIfTI to DICOM files.\n", + "\n", + ":::\n", + "\n", + "## Creating Operators and connecting them in Application class\n", + "\n", + "We will implement an application that consists of five Operators:\n", + "\n", + "- **DICOMDataLoaderOperator**:\n", + " - **Input(dicom_files)**: a folder path ([`DataPath`](/modules/_autosummary/monai.deploy.core.domain.DataPath))\n", + " - **Output(dicom_study_list)**: a list of DICOM studies in memory (List[[`DICOMStudy`](/modules/_autosummary/monai.deploy.core.domain.DICOMStudy)])\n", + "- **DICOMSeriesSelectorOperator**:\n", + " - **Input(dicom_study_list)**: a list of DICOM studies in memory (List[[`DICOMStudy`](/modules/_autosummary/monai.deploy.core.domain.DICOMStudy)])\n", + " - **Input(selection_rules)**: a selection rule (Dict)\n", + " - **Output(study_selected_series_list)**: a DICOM series object in memory ([`StudySelectedSeries`](/modules/_autosummary/monai.deploy.core.domain.StudySelectedSeries))\n", + "- **DICOMSeriesToVolumeOperator**:\n", + " - **Input(study_selected_series_list)**: a DICOM series object in memory ([`StudySelectedSeries`](/modules/_autosummary/monai.deploy.core.domain.StudySelectedSeries))\n", + " - **Output(image)**: an image object in memory ([`Image`](/modules/_autosummary/monai.deploy.core.domain.Image))\n", + "- **SpleenSegOperator**:\n", + " - **Input(image)**: an image object in memory ([`Image`](/modules/_autosummary/monai.deploy.core.domain.Image))\n", + " - **Output(seg_image)**: an image object in memory ([`Image`](/modules/_autosummary/monai.deploy.core.domain.Image))\n", + "- **DICOMSegmentationWriterOperator**:\n", + " - **Input(seg_image)**: a segmentation image object in memory ([`Image`](/modules/_autosummary/monai.deploy.core.domain.Image))\n", + " - **Input(study_selected_series_list)**: a DICOM series object in memory ([`StudySelectedSeries`](/modules/_autosummary/monai.deploy.core.domain.StudySelectedSeries))\n", + " - **Output(dicom_seg_instance)**: a file path ([`DataPath`](/modules/_autosummary/monai.deploy.core.domain.DataPath))\n", + "\n", + "\n", + ":::{note}\n", + "The `DICOMSegmentationWriterOperator` needs both the segmentation image as well as the original DICOM series meta-data in order to use the patient demographics and the DICOM Study level attributes.\n", + ":::\n", + "\n", + "The workflow of the application would look like this.\n", + "\n", + "```{mermaid}\n", + "%%{init: {\"theme\": \"base\", \"themeVariables\": { \"fontSize\": \"16px\"}} }%%\n", + "\n", + "classDiagram\n", + " direction TB\n", + "\n", + " DICOMDataLoaderOperator --|> DICOMSeriesSelectorOperator : dicom_study_list...dicom_study_list\n", + " DICOMSeriesSelectorOperator --|> DICOMSeriesToVolumeOperator : study_selected_series_list...study_selected_series_list\n", + " DICOMSeriesToVolumeOperator --|> SpleenSegOperator : image...image\n", + " DICOMSeriesSelectorOperator --|> DICOMSegmentationWriterOperator : study_selected_series_list...study_selected_series_list\n", + " SpleenSegOperator --|> DICOMSegmentationWriterOperator : seg_image...seg_image\n", + "\n", + "\n", + " class DICOMDataLoaderOperator {\n", + " dicom_files : DISK\n", + " dicom_study_list(out) IN_MEMORY\n", + " }\n", + " class DICOMSeriesSelectorOperator {\n", + " dicom_study_list : IN_MEMORY\n", + " selection_rules : IN_MEMORY\n", + " study_selected_series_list(out) IN_MEMORY\n", + " }\n", + " class DICOMSeriesToVolumeOperator {\n", + " study_selected_series_list : IN_MEMORY\n", + " image(out) IN_MEMORY\n", + " }\n", + " class SpleenSegOperator {\n", + " image : IN_MEMORY\n", + " seg_image(out) IN_MEMORY\n", + " }\n", + " class DICOMSegmentationWriterOperator {\n", + " seg_image : IN_MEMORY\n", + " study_selected_series_list : IN_MEMORY\n", + " dicom_seg_instance(out) DISK\n", + " }\n", + "```\n", + "\n", + "### Setup environment\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# Install MONAI and other necessary image processing packages for the application\n", + "!python -c \"import monai\" || pip install -q \"monai\"\n", + "!python -c \"import torch\" || pip install -q \"torch>=1.5\"\n", + "!python -c \"import numpy\" || pip install -q \"numpy>=1.20\"\n", + "!python -c \"import nibabel\" || pip install -q \"nibabel>=3.2.1\"\n", + "!python -c \"import pydicom\" || pip install -q \"pydicom>=1.4.2\"\n", + "!python -c \"import SimpleITK\" || pip install -q \"SimpleITK>=2.0.0\"\n", + "!python -c \"import typeguard\" || pip install -q \"typeguard>=2.12.1\"\n", + "\n", + "# Install MONAI Deploy App SDK package\n", + "!python -c \"import monai.deploy\" || pip install --upgrade -q \"monai-deploy-app-sdk\"\n", + "\n", + "# Install Clara Viz package\n", + "!python -c \"import clara.viz\" || pip install --upgrade -q \"clara-viz\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Download/Extract ai_spleen_seg_data from Google Drive" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Defaulting to user installation because normal site-packages is not writeable\n", + "Requirement already satisfied: gdown in /home/aheumann/.local/lib/python3.8/site-packages (4.0.1)\n", + "Requirement already satisfied: filelock in /home/aheumann/.local/lib/python3.8/site-packages (from gdown) (3.3.0)\n", + "Requirement already satisfied: six in /home/aheumann/.local/lib/python3.8/site-packages (from gdown) (1.16.0)\n", + "Requirement already satisfied: requests[socks]>=2.12.0 in /usr/lib/python3/dist-packages (from gdown) (2.22.0)\n", + "Requirement already satisfied: tqdm in /home/aheumann/.local/lib/python3.8/site-packages (from gdown) (4.62.3)\n", + "Requirement already satisfied: PySocks!=1.5.7,>=1.5.6 in /home/aheumann/.local/lib/python3.8/site-packages (from requests[socks]>=2.12.0->gdown) (1.7.1)\n", + "Downloading...\n", + "From: https://drive.google.com/uc?id=1GC_N8YQk_mOWN02oOzAU_2YDmNRWk--n\n", + "To: /home/aheumann/projects/monai-deploy-app-sdk/notebooks/tutorials/ai_spleen_seg_data_updated_1203.zip\n", + "100%|████████████████████████████████████████| 104M/104M [00:16<00:00, 6.35MB/s]\n", + "Archive: ai_spleen_seg_data_updated_1203.zip\n", + " inflating: dcm/IMG0001.dcm \n", + " inflating: dcm/IMG0002.dcm \n", + " inflating: dcm/IMG0003.dcm \n", + " inflating: dcm/IMG0004.dcm \n", + " inflating: dcm/IMG0005.dcm \n", + " inflating: dcm/IMG0006.dcm \n", + " inflating: dcm/IMG0007.dcm \n", + " inflating: dcm/IMG0008.dcm \n", + " inflating: dcm/IMG0009.dcm \n", + " inflating: dcm/IMG0010.dcm \n", + " inflating: dcm/IMG0011.dcm \n", + " inflating: dcm/IMG0012.dcm \n", + " inflating: dcm/IMG0013.dcm \n", + " inflating: dcm/IMG0014.dcm \n", + " inflating: dcm/IMG0015.dcm \n", + " inflating: dcm/IMG0016.dcm \n", + " inflating: dcm/IMG0017.dcm \n", + " inflating: dcm/IMG0018.dcm \n", + " inflating: dcm/IMG0019.dcm \n", + " inflating: dcm/IMG0020.dcm \n", + " inflating: dcm/IMG0021.dcm \n", + " inflating: dcm/IMG0022.dcm \n", + " inflating: dcm/IMG0023.dcm \n", + " inflating: dcm/IMG0024.dcm \n", + " inflating: dcm/IMG0025.dcm \n", + " inflating: dcm/IMG0026.dcm \n", + " inflating: dcm/IMG0027.dcm \n", + " inflating: dcm/IMG0028.dcm \n", + " inflating: dcm/IMG0029.dcm \n", + " inflating: dcm/IMG0030.dcm \n", + " inflating: dcm/IMG0031.dcm \n", + " inflating: dcm/IMG0032.dcm \n", + " inflating: dcm/IMG0033.dcm \n", + " inflating: dcm/IMG0034.dcm \n", + " inflating: dcm/IMG0035.dcm \n", + " inflating: dcm/IMG0036.dcm \n", + " inflating: dcm/IMG0037.dcm \n", + " inflating: dcm/IMG0038.dcm \n", + " inflating: dcm/IMG0039.dcm \n", + " inflating: dcm/IMG0040.dcm \n", + " inflating: dcm/IMG0041.dcm \n", + " inflating: dcm/IMG0042.dcm \n", + " inflating: dcm/IMG0043.dcm \n", + " inflating: dcm/IMG0044.dcm \n", + " inflating: dcm/IMG0045.dcm \n", + " inflating: dcm/IMG0046.dcm \n", + " inflating: dcm/IMG0047.dcm \n", + " inflating: dcm/IMG0048.dcm \n", + " inflating: dcm/IMG0049.dcm \n", + " inflating: dcm/IMG0050.dcm \n", + " inflating: dcm/IMG0051.dcm \n", + " inflating: dcm/IMG0052.dcm \n", + " inflating: dcm/IMG0053.dcm \n", + " inflating: dcm/IMG0054.dcm \n", + " inflating: dcm/IMG0055.dcm \n", + " inflating: dcm/IMG0056.dcm \n", + " inflating: dcm/IMG0057.dcm \n", + " inflating: dcm/IMG0058.dcm \n", + " inflating: dcm/IMG0059.dcm \n", + " inflating: dcm/IMG0060.dcm \n", + " inflating: dcm/IMG0061.dcm \n", + " inflating: dcm/IMG0062.dcm \n", + " inflating: dcm/IMG0063.dcm \n", + " inflating: dcm/IMG0064.dcm \n", + " inflating: dcm/IMG0065.dcm \n", + " inflating: dcm/IMG0066.dcm \n", + " inflating: dcm/IMG0067.dcm \n", + " inflating: dcm/IMG0068.dcm \n", + " inflating: dcm/IMG0069.dcm \n", + " inflating: dcm/IMG0070.dcm \n", + " inflating: dcm/IMG0071.dcm \n", + " inflating: dcm/IMG0072.dcm \n", + " inflating: dcm/IMG0073.dcm \n", + " inflating: dcm/IMG0074.dcm \n", + " inflating: dcm/IMG0075.dcm \n", + " inflating: dcm/IMG0076.dcm \n", + " inflating: dcm/IMG0077.dcm \n", + " inflating: dcm/IMG0078.dcm \n", + " inflating: dcm/IMG0079.dcm \n", + " inflating: dcm/IMG0080.dcm \n", + " inflating: dcm/IMG0081.dcm \n", + " inflating: dcm/IMG0082.dcm \n", + " inflating: dcm/IMG0083.dcm \n", + " inflating: dcm/IMG0084.dcm \n", + " inflating: dcm/IMG0085.dcm \n", + " inflating: dcm/IMG0086.dcm \n", + " inflating: dcm/IMG0087.dcm \n", + " inflating: dcm/IMG0088.dcm \n", + " inflating: dcm/IMG0089.dcm \n", + " inflating: dcm/IMG0090.dcm \n", + " inflating: dcm/IMG0091.dcm \n", + " inflating: dcm/IMG0092.dcm \n", + " inflating: dcm/IMG0093.dcm \n", + " inflating: dcm/IMG0094.dcm \n", + " inflating: dcm/IMG0095.dcm \n", + " inflating: dcm/IMG0096.dcm \n", + " inflating: dcm/IMG0097.dcm \n", + " inflating: dcm/IMG0098.dcm \n", + " inflating: dcm/IMG0099.dcm \n", + " inflating: dcm/IMG0100.dcm \n", + " inflating: dcm/IMG0101.dcm \n", + " inflating: dcm/IMG0102.dcm \n", + " inflating: dcm/IMG0103.dcm \n", + " inflating: dcm/IMG0104.dcm \n", + " inflating: dcm/IMG0105.dcm \n", + " inflating: dcm/IMG0106.dcm \n", + " inflating: dcm/IMG0107.dcm \n", + " inflating: dcm/IMG0108.dcm \n", + " inflating: dcm/IMG0109.dcm \n", + " inflating: dcm/IMG0110.dcm \n", + " inflating: dcm/IMG0111.dcm \n", + " inflating: dcm/IMG0112.dcm \n", + " inflating: dcm/IMG0113.dcm \n", + " inflating: dcm/IMG0114.dcm \n", + " inflating: dcm/IMG0115.dcm \n", + " inflating: dcm/IMG0116.dcm \n", + " inflating: dcm/IMG0117.dcm \n", + " inflating: dcm/IMG0118.dcm \n", + " inflating: dcm/IMG0119.dcm \n", + " inflating: dcm/IMG0120.dcm \n", + " inflating: dcm/IMG0121.dcm \n", + " inflating: dcm/IMG0122.dcm \n", + " inflating: dcm/IMG0123.dcm \n", + " inflating: dcm/IMG0124.dcm \n", + " inflating: dcm/IMG0125.dcm \n", + " inflating: dcm/IMG0126.dcm \n", + " inflating: dcm/IMG0127.dcm \n", + " inflating: dcm/IMG0128.dcm \n", + " inflating: dcm/IMG0129.dcm \n", + " inflating: dcm/IMG0130.dcm \n", + " inflating: dcm/IMG0131.dcm \n", + " inflating: dcm/IMG0132.dcm \n", + " inflating: dcm/IMG0133.dcm \n", + " inflating: dcm/IMG0134.dcm \n", + " inflating: dcm/IMG0135.dcm \n", + " inflating: dcm/IMG0136.dcm \n", + " inflating: dcm/IMG0137.dcm \n", + " inflating: dcm/IMG0138.dcm \n", + " inflating: dcm/IMG0139.dcm \n", + " inflating: dcm/IMG0140.dcm \n", + " inflating: dcm/IMG0141.dcm \n", + " inflating: dcm/IMG0142.dcm \n", + " inflating: dcm/IMG0143.dcm \n", + " inflating: dcm/IMG0144.dcm \n", + " inflating: dcm/IMG0145.dcm \n", + " inflating: dcm/IMG0146.dcm \n", + " inflating: dcm/IMG0147.dcm \n", + " inflating: dcm/IMG0148.dcm \n", + " inflating: dcm/IMG0149.dcm \n", + " inflating: dcm/IMG0150.dcm \n", + " inflating: dcm/IMG0151.dcm \n", + " inflating: dcm/IMG0152.dcm \n", + " inflating: dcm/IMG0153.dcm \n", + " inflating: dcm/IMG0154.dcm \n", + " inflating: dcm/IMG0155.dcm \n", + " inflating: dcm/IMG0156.dcm \n", + " inflating: dcm/IMG0157.dcm \n", + " inflating: dcm/IMG0158.dcm \n", + " inflating: dcm/IMG0159.dcm \n", + " inflating: dcm/IMG0160.dcm \n", + " inflating: dcm/IMG0161.dcm \n", + " inflating: dcm/IMG0162.dcm \n", + " inflating: dcm/IMG0163.dcm \n", + " inflating: dcm/IMG0164.dcm \n", + " inflating: dcm/IMG0165.dcm \n", + " inflating: dcm/IMG0166.dcm \n", + " inflating: dcm/IMG0167.dcm \n", + " inflating: dcm/IMG0168.dcm \n", + " inflating: dcm/IMG0169.dcm \n", + " inflating: dcm/IMG0170.dcm \n", + " inflating: dcm/IMG0171.dcm \n", + " inflating: dcm/IMG0172.dcm \n", + " inflating: dcm/IMG0173.dcm \n", + " inflating: dcm/IMG0174.dcm \n", + " inflating: dcm/IMG0175.dcm \n", + " inflating: dcm/IMG0176.dcm \n", + " inflating: dcm/IMG0177.dcm \n", + " inflating: dcm/IMG0178.dcm \n", + " inflating: dcm/IMG0179.dcm \n", + " inflating: dcm/IMG0180.dcm \n", + " inflating: dcm/IMG0181.dcm \n", + " inflating: dcm/IMG0182.dcm \n", + " inflating: dcm/IMG0183.dcm \n", + " inflating: dcm/IMG0184.dcm \n", + " inflating: dcm/IMG0185.dcm \n", + " inflating: dcm/IMG0186.dcm \n", + " inflating: dcm/IMG0187.dcm \n", + " inflating: dcm/IMG0188.dcm \n", + " inflating: dcm/IMG0189.dcm \n", + " inflating: dcm/IMG0190.dcm \n", + " inflating: dcm/IMG0191.dcm \n", + " inflating: dcm/IMG0192.dcm \n", + " inflating: dcm/IMG0193.dcm \n", + " inflating: dcm/IMG0194.dcm \n", + " inflating: dcm/IMG0195.dcm \n", + " inflating: dcm/IMG0196.dcm \n", + " inflating: dcm/IMG0197.dcm \n", + " inflating: dcm/IMG0198.dcm \n", + " inflating: dcm/IMG0199.dcm \n", + " inflating: dcm/IMG0200.dcm \n", + " inflating: dcm/IMG0201.dcm \n", + " inflating: dcm/IMG0202.dcm \n", + " inflating: dcm/IMG0203.dcm \n", + " inflating: dcm/IMG0204.dcm \n", + " inflating: dcm/IMG0205.dcm \n", + " inflating: dcm/IMG0206.dcm \n", + " inflating: dcm/IMG0207.dcm \n", + " inflating: dcm/IMG0208.dcm \n", + " inflating: dcm/IMG0209.dcm \n", + " inflating: dcm/IMG0210.dcm \n", + " inflating: dcm/IMG0211.dcm \n", + " inflating: dcm/IMG0212.dcm \n", + " inflating: dcm/IMG0213.dcm \n", + " inflating: dcm/IMG0214.dcm \n", + " inflating: dcm/IMG0215.dcm \n", + " inflating: dcm/IMG0216.dcm \n", + " inflating: dcm/IMG0217.dcm \n", + " inflating: dcm/IMG0218.dcm \n", + " inflating: dcm/IMG0219.dcm \n", + " inflating: dcm/IMG0220.dcm \n", + " inflating: dcm/IMG0221.dcm \n", + " inflating: dcm/IMG0222.dcm \n", + " inflating: dcm/IMG0223.dcm \n", + " inflating: dcm/IMG0224.dcm \n", + " inflating: dcm/IMG0225.dcm \n", + " inflating: dcm/IMG0226.dcm \n", + " inflating: dcm/IMG0227.dcm \n", + " inflating: dcm/IMG0228.dcm \n", + " inflating: dcm/IMG0229.dcm \n", + " inflating: dcm/IMG0230.dcm \n", + " inflating: dcm/IMG0231.dcm \n", + " inflating: dcm/IMG0232.dcm \n", + " inflating: dcm/IMG0233.dcm \n", + " inflating: dcm/IMG0234.dcm \n", + " inflating: dcm/IMG0235.dcm \n", + " inflating: dcm/IMG0236.dcm \n", + " inflating: dcm/IMG0237.dcm \n", + " inflating: dcm/IMG0238.dcm \n", + " inflating: dcm/IMG0239.dcm \n", + " inflating: dcm/IMG0240.dcm \n", + " inflating: dcm/IMG0241.dcm \n", + " inflating: dcm/IMG0242.dcm \n", + " inflating: dcm/IMG0243.dcm \n", + " inflating: dcm/IMG0244.dcm \n", + " inflating: dcm/IMG0245.dcm \n", + " inflating: dcm/IMG0246.dcm \n", + " inflating: dcm/IMG0247.dcm \n", + " inflating: dcm/IMG0248.dcm \n", + " inflating: dcm/IMG0249.dcm \n", + " inflating: dcm/IMG0250.dcm \n", + " inflating: dcm/IMG0251.dcm \n", + " inflating: dcm/IMG0252.dcm \n", + " inflating: dcm/IMG0253.dcm \n", + " inflating: dcm/IMG0254.dcm \n", + " inflating: dcm/IMG0255.dcm \n", + " inflating: dcm/IMG0256.dcm \n", + " inflating: dcm/IMG0257.dcm \n", + " inflating: dcm/IMG0258.dcm \n", + " inflating: dcm/IMG0259.dcm \n", + " inflating: dcm/IMG0260.dcm \n", + " inflating: dcm/IMG0261.dcm \n", + " inflating: dcm/IMG0262.dcm \n", + " inflating: dcm/IMG0263.dcm \n", + " inflating: dcm/IMG0264.dcm \n", + " inflating: dcm/IMG0265.dcm \n", + " inflating: dcm/IMG0266.dcm \n", + " inflating: dcm/IMG0267.dcm \n", + " inflating: dcm/IMG0268.dcm \n", + " inflating: dcm/IMG0269.dcm \n", + " inflating: dcm/IMG0270.dcm \n", + " inflating: dcm/IMG0271.dcm \n", + " inflating: dcm/IMG0272.dcm \n", + " inflating: dcm/IMG0273.dcm \n", + " inflating: dcm/IMG0274.dcm \n", + " inflating: dcm/IMG0275.dcm \n", + " inflating: dcm/IMG0276.dcm \n", + " inflating: dcm/IMG0277.dcm \n", + " inflating: dcm/IMG0278.dcm \n", + " inflating: dcm/IMG0279.dcm \n", + " inflating: dcm/IMG0280.dcm \n", + " inflating: dcm/IMG0281.dcm \n", + " inflating: dcm/IMG0282.dcm \n", + " inflating: dcm/IMG0283.dcm \n", + " inflating: dcm/IMG0284.dcm \n", + " inflating: dcm/IMG0285.dcm \n", + " inflating: dcm/IMG0286.dcm \n", + " inflating: dcm/IMG0287.dcm \n", + " inflating: dcm/IMG0288.dcm \n", + " inflating: dcm/IMG0289.dcm \n", + " inflating: dcm/IMG0290.dcm \n", + " inflating: dcm/IMG0291.dcm \n", + " inflating: dcm/IMG0292.dcm \n", + " inflating: dcm/IMG0293.dcm \n", + " inflating: dcm/IMG0294.dcm \n", + " inflating: dcm/IMG0295.dcm \n", + " inflating: dcm/IMG0296.dcm \n", + " inflating: dcm/IMG0297.dcm \n", + " inflating: dcm/IMG0298.dcm \n", + " inflating: dcm/IMG0299.dcm \n", + " inflating: dcm/IMG0300.dcm \n", + " inflating: dcm/IMG0301.dcm \n", + " inflating: dcm/IMG0302.dcm \n", + " inflating: dcm/IMG0303.dcm \n", + " inflating: dcm/IMG0304.dcm \n", + " inflating: dcm/IMG0305.dcm \n", + " inflating: dcm/IMG0306.dcm \n", + " inflating: dcm/IMG0307.dcm \n", + " inflating: dcm/IMG0308.dcm \n", + " inflating: dcm/IMG0309.dcm \n", + " inflating: dcm/IMG0310.dcm \n", + " inflating: dcm/IMG0311.dcm \n", + " inflating: dcm/IMG0312.dcm \n", + " inflating: dcm/IMG0313.dcm \n", + " inflating: dcm/IMG0314.dcm \n", + " inflating: dcm/IMG0315.dcm \n", + " inflating: dcm/IMG0316.dcm \n", + " inflating: dcm/IMG0317.dcm \n", + " inflating: dcm/IMG0318.dcm \n", + " inflating: dcm/IMG0319.dcm \n", + " inflating: dcm/IMG0320.dcm \n", + " inflating: dcm/IMG0321.dcm \n", + " inflating: dcm/IMG0322.dcm \n", + " inflating: dcm/IMG0323.dcm \n", + " inflating: dcm/IMG0324.dcm \n", + " inflating: dcm/IMG0325.dcm \n", + " inflating: dcm/IMG0326.dcm \n", + " inflating: dcm/IMG0327.dcm \n", + " inflating: dcm/IMG0328.dcm \n", + " inflating: dcm/IMG0329.dcm \n", + " inflating: dcm/IMG0330.dcm \n", + " inflating: dcm/IMG0331.dcm \n", + " inflating: dcm/IMG0332.dcm \n", + " inflating: dcm/IMG0333.dcm \n", + " inflating: dcm/IMG0334.dcm \n", + " inflating: dcm/IMG0335.dcm \n", + " inflating: dcm/IMG0336.dcm \n", + " inflating: dcm/IMG0337.dcm \n", + " inflating: dcm/IMG0338.dcm \n", + " inflating: dcm/IMG0339.dcm \n", + " inflating: dcm/IMG0340.dcm \n", + " inflating: dcm/IMG0341.dcm \n", + " inflating: dcm/IMG0342.dcm \n", + " inflating: dcm/IMG0343.dcm \n", + " inflating: dcm/IMG0344.dcm \n", + " inflating: dcm/IMG0345.dcm \n", + " inflating: dcm/IMG0346.dcm \n", + " inflating: dcm/IMG0347.dcm \n", + " inflating: dcm/IMG0348.dcm \n", + " inflating: dcm/IMG0349.dcm \n", + " inflating: dcm/IMG0350.dcm \n", + " inflating: dcm/IMG0351.dcm \n", + " inflating: dcm/IMG0352.dcm \n", + " inflating: dcm/IMG0353.dcm \n", + " inflating: dcm/IMG0354.dcm \n", + " inflating: dcm/IMG0355.dcm \n", + " inflating: dcm/IMG0356.dcm \n", + " inflating: dcm/IMG0357.dcm \n", + " inflating: dcm/IMG0358.dcm \n", + " inflating: dcm/IMG0359.dcm \n", + " inflating: dcm/IMG0360.dcm \n", + " inflating: dcm/IMG0361.dcm \n", + " inflating: dcm/IMG0362.dcm \n", + " inflating: dcm/IMG0363.dcm \n", + " inflating: dcm/IMG0364.dcm \n", + " inflating: dcm/IMG0365.dcm \n", + " inflating: dcm/IMG0366.dcm \n", + " inflating: dcm/IMG0367.dcm \n", + " inflating: dcm/IMG0368.dcm \n", + " inflating: dcm/IMG0369.dcm \n", + " inflating: dcm/IMG0370.dcm \n", + " inflating: dcm/IMG0371.dcm \n", + " inflating: dcm/IMG0372.dcm \n", + " inflating: dcm/IMG0373.dcm \n", + " inflating: dcm/IMG0374.dcm \n", + " inflating: dcm/IMG0375.dcm \n", + " inflating: dcm/IMG0376.dcm \n", + " inflating: dcm/IMG0377.dcm \n", + " inflating: dcm/IMG0378.dcm \n", + " inflating: dcm/IMG0379.dcm \n", + " inflating: dcm/IMG0380.dcm \n", + " inflating: dcm/IMG0381.dcm \n", + " inflating: dcm/IMG0382.dcm \n", + " inflating: dcm/IMG0383.dcm \n", + " inflating: dcm/IMG0384.dcm \n", + " inflating: dcm/IMG0385.dcm \n", + " inflating: dcm/IMG0386.dcm \n", + " inflating: dcm/IMG0387.dcm \n", + " inflating: dcm/IMG0388.dcm \n", + " inflating: dcm/IMG0389.dcm \n", + " inflating: dcm/IMG0390.dcm \n", + " inflating: dcm/IMG0391.dcm \n", + " inflating: dcm/IMG0392.dcm \n", + " inflating: dcm/IMG0393.dcm \n", + " inflating: dcm/IMG0394.dcm \n", + " inflating: dcm/IMG0395.dcm \n", + " inflating: dcm/IMG0396.dcm \n", + " inflating: dcm/IMG0397.dcm \n", + " inflating: dcm/IMG0398.dcm \n", + " inflating: dcm/IMG0399.dcm \n", + " inflating: dcm/IMG0400.dcm \n", + " inflating: dcm/IMG0401.dcm \n", + " inflating: dcm/IMG0402.dcm \n", + " inflating: dcm/IMG0403.dcm \n", + " inflating: dcm/IMG0404.dcm \n", + " inflating: dcm/IMG0405.dcm \n", + " inflating: dcm/IMG0406.dcm \n", + " inflating: dcm/IMG0407.dcm \n", + " inflating: dcm/IMG0408.dcm \n", + " inflating: dcm/IMG0409.dcm \n", + " inflating: dcm/IMG0410.dcm \n", + " inflating: dcm/IMG0411.dcm \n", + " inflating: dcm/IMG0412.dcm \n", + " inflating: dcm/IMG0413.dcm \n", + " inflating: dcm/IMG0414.dcm \n", + " inflating: dcm/IMG0415.dcm \n", + " inflating: dcm/IMG0416.dcm \n", + " inflating: dcm/IMG0417.dcm \n", + " inflating: dcm/IMG0418.dcm \n", + " inflating: dcm/IMG0419.dcm \n", + " inflating: dcm/IMG0420.dcm \n", + " inflating: dcm/IMG0421.dcm \n", + " inflating: dcm/IMG0422.dcm \n", + " inflating: dcm/IMG0423.dcm \n", + " inflating: dcm/IMG0424.dcm \n", + " inflating: dcm/IMG0425.dcm \n", + " inflating: dcm/IMG0426.dcm \n", + " inflating: dcm/IMG0427.dcm \n", + " inflating: dcm/IMG0428.dcm \n", + " inflating: dcm/IMG0429.dcm \n", + " inflating: dcm/IMG0430.dcm \n", + " inflating: dcm/IMG0431.dcm \n", + " inflating: dcm/IMG0432.dcm \n", + " inflating: dcm/IMG0433.dcm \n", + " inflating: dcm/IMG0434.dcm \n", + " inflating: dcm/IMG0435.dcm \n", + " inflating: dcm/IMG0436.dcm \n", + " inflating: dcm/IMG0437.dcm \n", + " inflating: dcm/IMG0438.dcm \n", + " inflating: dcm/IMG0439.dcm \n", + " inflating: dcm/IMG0440.dcm \n", + " inflating: dcm/IMG0441.dcm \n", + " inflating: dcm/IMG0442.dcm \n", + " inflating: dcm/IMG0443.dcm \n", + " inflating: dcm/IMG0444.dcm \n", + " inflating: dcm/IMG0445.dcm \n", + " inflating: dcm/IMG0446.dcm \n", + " inflating: dcm/IMG0447.dcm \n", + " inflating: dcm/IMG0448.dcm \n", + " inflating: dcm/IMG0449.dcm \n", + " inflating: dcm/IMG0450.dcm \n", + " inflating: dcm/IMG0451.dcm \n", + " inflating: dcm/IMG0452.dcm \n", + " inflating: dcm/IMG0453.dcm \n", + " inflating: dcm/IMG0454.dcm \n", + " inflating: dcm/IMG0455.dcm \n", + " inflating: dcm/IMG0456.dcm \n", + " inflating: dcm/IMG0457.dcm \n", + " inflating: dcm/IMG0458.dcm \n", + " inflating: dcm/IMG0459.dcm \n", + " inflating: dcm/IMG0460.dcm \n", + " inflating: dcm/IMG0461.dcm \n", + " inflating: dcm/IMG0462.dcm \n", + " inflating: dcm/IMG0463.dcm \n", + " inflating: dcm/IMG0464.dcm \n", + " inflating: dcm/IMG0465.dcm \n", + " inflating: dcm/IMG0466.dcm \n", + " inflating: dcm/IMG0467.dcm \n", + " inflating: dcm/IMG0468.dcm \n", + " inflating: dcm/IMG0469.dcm \n", + " inflating: dcm/IMG0470.dcm \n", + " inflating: dcm/IMG0471.dcm \n", + " inflating: dcm/IMG0472.dcm \n", + " inflating: dcm/IMG0473.dcm \n", + " inflating: dcm/IMG0474.dcm \n", + " inflating: dcm/IMG0475.dcm \n", + " inflating: dcm/IMG0476.dcm \n", + " inflating: dcm/IMG0477.dcm \n", + " inflating: dcm/IMG0478.dcm \n", + " inflating: dcm/IMG0479.dcm \n", + " inflating: dcm/IMG0480.dcm \n", + " inflating: dcm/IMG0481.dcm \n", + " inflating: dcm/IMG0482.dcm \n", + " inflating: dcm/IMG0483.dcm \n", + " inflating: dcm/IMG0484.dcm \n", + " inflating: dcm/IMG0485.dcm \n", + " inflating: dcm/IMG0486.dcm \n", + " inflating: dcm/IMG0487.dcm \n", + " inflating: dcm/IMG0488.dcm \n", + " inflating: dcm/IMG0489.dcm \n", + " inflating: dcm/IMG0490.dcm \n", + " inflating: dcm/IMG0491.dcm \n", + " inflating: dcm/IMG0492.dcm \n", + " inflating: dcm/IMG0493.dcm \n", + " inflating: dcm/IMG0494.dcm \n", + " inflating: dcm/IMG0495.dcm \n", + " inflating: dcm/IMG0496.dcm \n", + " inflating: dcm/IMG0497.dcm \n", + " inflating: dcm/IMG0498.dcm \n", + " inflating: dcm/IMG0499.dcm \n", + " inflating: dcm/IMG0500.dcm \n", + " inflating: dcm/IMG0501.dcm \n", + " inflating: dcm/IMG0502.dcm \n", + " inflating: dcm/IMG0503.dcm \n", + " inflating: dcm/IMG0504.dcm \n", + " inflating: dcm/IMG0505.dcm \n", + " inflating: dcm/IMG0506.dcm \n", + " inflating: dcm/IMG0507.dcm \n", + " inflating: dcm/IMG0508.dcm \n", + " inflating: dcm/IMG0509.dcm \n", + " inflating: dcm/IMG0510.dcm \n", + " inflating: dcm/IMG0511.dcm \n", + " inflating: dcm/IMG0512.dcm \n", + " inflating: dcm/IMG0513.dcm \n", + " inflating: dcm/IMG0514.dcm \n", + " inflating: dcm/IMG0515.dcm \n", + " inflating: model.ts \n" + ] + } + ], + "source": [ + "# Download ai_spleen_seg_data test data zip file\n", + "!pip install gdown \n", + "!gdown https://drive.google.com/uc?id=1GC_N8YQk_mOWN02oOzAU_2YDmNRWk--n\n", + "\n", + "# After downloading ai_spleen_seg_data zip file from the web browser or using gdown,\n", + "!unzip -o \"ai_spleen_seg_data_updated_1203.zip\"" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Setup imports\n", + "\n", + "Let's import necessary classes/decorators to define Application and Operator." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import logging\n", + "from os import path\n", + "\n", + "from numpy import uint8\n", + "\n", + "import monai.deploy.core as md\n", + "from monai.deploy.core import ExecutionContext, Image, InputContext, IOType, Operator, OutputContext\n", + "from monai.deploy.operators.monai_seg_inference_operator import InMemImageReader, MonaiSegInferenceOperator\n", + "from monai.transforms import (\n", + " Activationsd,\n", + " AsDiscreted,\n", + " Compose,\n", + " CropForegroundd,\n", + " EnsureChannelFirstd,\n", + " Invertd,\n", + " LoadImaged,\n", + " SaveImaged,\n", + " ScaleIntensityRanged,\n", + " Spacingd,\n", + " ToTensord,\n", + ")\n", + "\n", + "from monai.deploy.core import Application, resource\n", + "from monai.deploy.operators.dicom_data_loader_operator import DICOMDataLoaderOperator\n", + "from monai.deploy.operators.dicom_seg_writer_operator import DICOMSegmentationWriterOperator\n", + "from monai.deploy.operators.dicom_series_selector_operator import DICOMSeriesSelectorOperator\n", + "from monai.deploy.operators.dicom_series_to_volume_operator import DICOMSeriesToVolumeOperator\n", + "from monai.deploy.operators.clara_viz_operator import ClaraVizOperator" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating Model Specific Inference Operator classes\n", + "\n", + "Each Operator class inherits [Operator](/modules/_autosummary/monai.deploy.core.Operator) class and input/output properties are specified by using [@input](/modules/_autosummary/monai.deploy.core.input)/[@output](/modules/_autosummary/monai.deploy.core.output) decorators.\n", + "\n", + "Business logic would be implemented in the compute() method.\n", + "\n", + "The App SDK provides a `MonaiSegInferenceOperator` class to perform segmentation prediction with a Torch Script model. For consistency, this class uses MONAI dictionary-based transforms, as `Compose` object, for pre and post transforms. The model-specific inference operator will then only need to create the pre and post transform `Compose` based on what has been used in the model training and validation. Note that for deploy application, `ignite` is not needed nor supported.\n", + "\n", + "#### SpleenSegOperator\n", + "\n", + "The `SpleenSegOperator` gets as input an in-memory [Image](/modules/_autosummary/monai.deploy.core.domain.Image) object that has been converted from a DICOM CT series by the preceding `DICOMSeriesToVolumeOperator`, and as output in-memory segmentation [Image](/modules/_autosummary/monai.deploy.core.domain.Image) object.\n", + "\n", + "The `pre_process` function creates the pre-transforms `Compose` object. For `LoadImage`, a specialized `InMemImageReader`, derived from MONAI `ImageReader`, is used to convert the in-memory pixel data and return the `numpy` array as well as the meta-data. Also, the DICOM input pixel spacings are often not the same as expected by the model, so the `Spacingd` transform must be used to re-sample the image with the expected spacing.\n", + "\n", + "The `post_process` function creates the post-transform `Compose` object. The `SaveImageD` transform class is used to save the segmentation mask as NIfTI image file, which is optional as the in-memory mask image will be passed down to the DICOM Segmentation writer for creating a DICOM Segmentation instance. The `Invertd` must also be used to revert the segmentation image's orientation and spacing to be the same as the input.\n", + "\n", + "When the `MonaiSegInferenceOperator` object is created, the `ROI` size is specified, as well as the transform `Compose` objects. Furthermore, the dataset image key names are set accordingly.\n", + "\n", + "Loading of the model and performing the prediction are encapsulated in the `MonaiSegInferenceOperator` and other SDK classes. Once the inference is completed, the segmentation [Image](/modules/_autosummary/monai.deploy.core.domain.Image) object is created and set to the output (op_output.set(value, label)), by the `MonaiSegInferenceOperator`." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "@md.input(\"image\", Image, IOType.IN_MEMORY)\n", + "@md.output(\"seg_image\", Image, IOType.IN_MEMORY)\n", + "@md.env(pip_packages=[\"monai==0.6.0\", \"torch>=1.5\", \"numpy>=1.20\", \"nibabel\"])\n", + "class SpleenSegOperator(Operator):\n", + " \"\"\"Performs Spleen segmentation with a 3D image converted from a DICOM CT series.\n", + " \"\"\"\n", + "\n", + " def __init__(self):\n", + "\n", + " self.logger = logging.getLogger(\"{}.{}\".format(__name__, type(self).__name__))\n", + " super().__init__()\n", + " self._input_dataset_key = \"image\"\n", + " self._pred_dataset_key = \"pred\"\n", + "\n", + " def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext):\n", + "\n", + " input_image = op_input.get(\"image\")\n", + " if not input_image:\n", + " raise ValueError(\"Input image is not found.\")\n", + "\n", + " output_path = context.output.get().path\n", + "\n", + " # This operator gets an in-memory Image object, so a specialized ImageReader is needed.\n", + " _reader = InMemImageReader(input_image)\n", + " pre_transforms = self.pre_process(_reader)\n", + " post_transforms = self.post_process(pre_transforms, path.join(output_path, \"prediction_output\"))\n", + "\n", + " # Delegates inference and saving output to the built-in operator.\n", + " infer_operator = MonaiSegInferenceOperator(\n", + " (\n", + " 160,\n", + " 160,\n", + " 160,\n", + " ),\n", + " pre_transforms,\n", + " post_transforms,\n", + " )\n", + "\n", + " # Setting the keys used in the dictironary based transforms may change.\n", + " infer_operator.input_dataset_key = self._input_dataset_key\n", + " infer_operator.pred_dataset_key = self._pred_dataset_key\n", + "\n", + " # Now let the built-in operator handles the work with the I/O spec and execution context.\n", + " infer_operator.compute(op_input, op_output, context)\n", + "\n", + " def pre_process(self, img_reader) -> Compose:\n", + " \"\"\"Composes transforms for preprocessing input before predicting on a model.\"\"\"\n", + "\n", + " my_key = self._input_dataset_key\n", + " return Compose(\n", + " [\n", + " LoadImaged(keys=my_key, reader=img_reader),\n", + " EnsureChannelFirstd(keys=my_key),\n", + " Spacingd(keys=my_key, pixdim=[1.0, 1.0, 1.0], mode=[\"blinear\"], align_corners=True),\n", + " ScaleIntensityRanged(keys=my_key, a_min=-57, a_max=164, b_min=0.0, b_max=1.0, clip=True),\n", + " CropForegroundd(keys=my_key, source_key=my_key),\n", + " ToTensord(keys=my_key),\n", + " ]\n", + " )\n", + "\n", + " def post_process(self, pre_transforms: Compose, out_dir: str = \"./prediction_output\") -> Compose:\n", + " \"\"\"Composes transforms for postprocessing the prediction results.\"\"\"\n", + "\n", + " pred_key = self._pred_dataset_key\n", + " return Compose(\n", + " [\n", + " Activationsd(keys=pred_key, softmax=True),\n", + " AsDiscreted(keys=pred_key, argmax=True),\n", + " Invertd(\n", + " keys=pred_key, transform=pre_transforms, orig_keys=self._input_dataset_key, nearest_interp=True\n", + " ),\n", + " SaveImaged(keys=pred_key, output_dir=out_dir, output_postfix=\"seg\", output_dtype=uint8, resample=False),\n", + " ]\n", + " )" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Creating Application class\n", + "\n", + "Our application class would look like below.\n", + "\n", + "It defines `App` class, inheriting [Application](/modules/_autosummary/monai.deploy.core.Application) class.\n", + "\n", + "The requirements (resource and package dependency) for the App can be specified by using [@resource](/modules/_autosummary/monai.deploy.core.resource) and [@env](/modules/_autosummary/monai.deploy.core.env) decorators.\n", + "\n", + "The base class method, `compose`, is overridden. Objects required for DICOM parsing, series selection (selecting the first series for the current release), pixel data conversion to volume image, and segmentation instance creation are created, so is the model-specific `SpleenSegOperator`. The execution pipeline, as a Directed Acyclic Graph, is created by connecting these objects through self.add_flow()." + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "@resource(cpu=1, gpu=1, memory=\"7Gi\")\n", + "class AISpleenSegApp(Application):\n", + " def __init__(self, *args, **kwargs):\n", + " super().__init__(*args, **kwargs)\n", + "\n", + " def compose(self):\n", + "\n", + " study_loader_op = DICOMDataLoaderOperator()\n", + " series_selector_op = DICOMSeriesSelectorOperator()\n", + " series_to_vol_op = DICOMSeriesToVolumeOperator()\n", + " # Creates DICOM Seg writer with segment label name in a string list\n", + " dicom_seg_writer = DICOMSegmentationWriterOperator(seg_labels=[\"Spleen\"])\n", + "\n", + " # Creates the model specific segmentation operator\n", + " spleen_seg_op = SpleenSegOperator()\n", + "\n", + " # Creates the DAG by linking the operators\n", + " self.add_flow(study_loader_op, series_selector_op, {\"dicom_study_list\": \"dicom_study_list\"})\n", + " self.add_flow(series_selector_op, series_to_vol_op, {\"study_selected_series_list\": \"study_selected_series_list\"})\n", + " self.add_flow(series_to_vol_op, spleen_seg_op, {\"image\": \"image\"})\n", + "\n", + " self.add_flow(series_selector_op, dicom_seg_writer, {\"study_selected_series_list\": \"study_selected_series_list\"})\n", + " self.add_flow(spleen_seg_op, dicom_seg_writer, {\"seg_image\": \"seg_image\"})\n", + " \n", + " viz_op = ClaraVizOperator()\n", + " self.add_flow(series_to_vol_op, viz_op, {\"image\": \"image\"})\n", + " self.add_flow(spleen_seg_op, viz_op, {\"seg_image\": \"seg_image\"})\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Executing app locally\n", + "\n", + "We can execute the app in the Jupyter notebook. Note that the DICOM files of the CT Abdomen series must be present in the `dcm` and the Torch Script model at `model.ts`. Please use the actual path in your environment.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[34mGoing to initiate execution of operator DICOMDataLoaderOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMDataLoaderOperator \u001b[33m(Process ID: 98097, Operator ID: 7ac5e018-fe4d-4416-bb36-2fef5c19f77f)\u001b[39m\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[2022-01-28 11:27:16,671] [WARNING] (root) - No selection rules given; select all series.\n", + "[2022-01-28 11:27:16,671] [INFO] (root) - Working on study, instance UID: 1.2.826.0.1.3680043.2.1125.1.67295333199898911264201812221946213\n", + "[2022-01-28 11:27:16,672] [INFO] (root) - Working on series, instance UID: 1.2.826.0.1.3680043.2.1125.1.68102559796966796813942775094416763\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[34mDone performing execution of operator DICOMDataLoaderOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator DICOMSeriesSelectorOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMSeriesSelectorOperator \u001b[33m(Process ID: 98097, Operator ID: 5b8cc54b-23ee-4909-9409-8544ed42bedd)\u001b[39m\n", + "Working on study, instance UID: 1.2.826.0.1.3680043.2.1125.1.67295333199898911264201812221946213\n", + "Working on series, instance UID: 1.2.826.0.1.3680043.2.1125.1.68102559796966796813942775094416763\n", + "\u001b[34mDone performing execution of operator DICOMSeriesSelectorOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator DICOMSeriesToVolumeOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMSeriesToVolumeOperator \u001b[33m(Process ID: 98097, Operator ID: f248722e-d87a-4c5b-b398-dbf895180761)\u001b[39m\n", + "\u001b[34mDone performing execution of operator DICOMSeriesToVolumeOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator SpleenSegOperator\u001b[39m\n", + "\u001b[32mExecuting operator SpleenSegOperator \u001b[33m(Process ID: 98097, Operator ID: c3592b6b-7cc7-4818-b94e-f078752aa7b0)\u001b[39m\n", + "Converted Image object metadata:\n", + "SeriesInstanceUID: 1.2.826.0.1.3680043.2.1125.1.68102559796966796813942775094416763, type \n", + "Modality: CT, type \n", + "SeriesDescription: No series description, type \n", + "PatientPosition: HFS, type \n", + "SeriesNumber: 1, type \n", + "row_pixel_spacing: 1.0, type \n", + "col_pixel_spacing: 1.0, type \n", + "depth_pixel_spacing: 1.0, type \n", + "row_direction_cosine: [-1.0, 0.0, 0.0], type \n", + "col_direction_cosine: [0.0, -1.0, 0.0], type \n", + "depth_direction_cosine: [0.0, 0.0, 1.0], type \n", + "dicom_affine_transform: [[-1. 0. 0. 0.]\n", + " [ 0. -1. 0. 0.]\n", + " [ 0. 0. 1. 0.]\n", + " [ 0. 0. 0. 1.]], type \n", + "nifti_affine_transform: [[ 1. -0. -0. -0.]\n", + " [-0. 1. -0. -0.]\n", + " [ 0. 0. 1. 0.]\n", + " [ 0. 0. 0. 1.]], type \n", + "StudyInstanceUID: 1.2.826.0.1.3680043.2.1125.1.67295333199898911264201812221946213, type \n", + "StudyID: SLICER10001, type \n", + "StudyDate: 2019-09-16, type \n", + "StudyTime: 010100.000000, type \n", + "StudyDescription: spleen, type \n", + "AccessionNumber: 1, type \n", + "selection_name: 1.2.826.0.1.3680043.2.1125.1.68102559796966796813942775094416763, type \n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[2022-01-28 11:27:23,189] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Number of DICOM instance datasets in the list: 515\n", + "[2022-01-28 11:27:23,190] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Number of slices in the numpy image: 515\n", + "[2022-01-28 11:27:23,190] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Labels of the segments: ['Spleen']\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "file written: /home/aheumann/projects/monai-deploy-app-sdk/notebooks/tutorials/output/prediction_output/1.2.826.0.1.3680043.2.1125.1/1.2.826.0.1.3680043.2.1125.1_seg.nii.gz.\n", + "Output Seg image numpy array shaped: (515, 440, 440)\n", + "Output Seg image pixel max value: 1\n", + "\u001b[34mDone performing execution of operator SpleenSegOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator DICOMSegmentationWriterOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMSegmentationWriterOperator \u001b[33m(Process ID: 98097, Operator ID: 1f316176-3f15-41af-bbac-4114d3693cd7)\u001b[39m\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "[2022-01-28 11:27:24,546] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Unique values in seg image: [0 1]\n", + "[2022-01-28 11:27:25,248] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Saving output file /home/aheumann/projects/monai-deploy-app-sdk/notebooks/tutorials/output/dicom_seg-DICOMSEG.dcm\n", + "[2022-01-28 11:27:25,358] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - File saved.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[34mDone performing execution of operator DICOMSegmentationWriterOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator ClaraVizOperator\u001b[39m\n", + "\u001b[32mExecuting operator ClaraVizOperator \u001b[33m(Process ID: 98097, Operator ID: 67a92a8d-da44-44a0-b418-b28e82ff701a)\u001b[39m\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "3d9986b677e04a128042983e7236c008", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Box(children=(Widget(), VBox(children=(interactive(children=(Dropdown(description='View mode', index=2, option…" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[34mDone performing execution of operator ClaraVizOperator\n", + "\u001b[39m\n" + ] + } + ], + "source": [ + "app = AISpleenSegApp()\n", + "\n", + "app.run(input=\"dcm\", output=\"output\", model=\"model.ts\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Once the application is verified inside Jupyter notebook, we can write the above Python code into Python files in an application folder.\n", + "\n", + "The application folder structure would look like below:\n", + "\n", + "```bash\n", + "my_app\n", + "├── __main__.py\n", + "├── app.py\n", + "└── spleen_seg_operator.py\n", + "```\n", + "\n", + ":::{note}\n", + "We can create a single application Python file (such as `spleen_app.py`) that includes the content of the files, instead of creating multiple files.\n", + "You will see such an example in MedNist Classifier Tutorial.\n", + ":::" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "# Create an application folder\n", + "!mkdir -p my_app" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### spleen_seg_operator.py" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting my_app/spleen_seg_operator.py\n" + ] + } + ], + "source": [ + "%%writefile my_app/spleen_seg_operator.py\n", + "import logging\n", + "from os import path\n", + "\n", + "from numpy import uint8\n", + "\n", + "import monai.deploy.core as md\n", + "from monai.deploy.core import ExecutionContext, Image, InputContext, IOType, Operator, OutputContext\n", + "from monai.deploy.operators.monai_seg_inference_operator import InMemImageReader, MonaiSegInferenceOperator\n", + "from monai.transforms import (\n", + " Activationsd,\n", + " AsDiscreted,\n", + " Compose,\n", + " CropForegroundd,\n", + " EnsureChannelFirstd,\n", + " Invertd,\n", + " LoadImaged,\n", + " SaveImaged,\n", + " ScaleIntensityRanged,\n", + " Spacingd,\n", + " ToTensord,\n", + ")\n", + "\n", + "\n", + "@md.input(\"image\", Image, IOType.IN_MEMORY)\n", + "@md.output(\"seg_image\", Image, IOType.IN_MEMORY)\n", + "@md.env(pip_packages=[\"monai==0.6.0\", \"torch>=1.5\", \"numpy>=1.20\", \"nibabel\", \"typeguard\"])\n", + "class SpleenSegOperator(Operator):\n", + " \"\"\"Performs Spleen segmentation with a 3D image converted from a DICOM CT series.\n", + " \"\"\"\n", + "\n", + " def __init__(self):\n", + "\n", + " self.logger = logging.getLogger(\"{}.{}\".format(__name__, type(self).__name__))\n", + " super().__init__()\n", + " self._input_dataset_key = \"image\"\n", + " self._pred_dataset_key = \"pred\"\n", + "\n", + " def compute(self, op_input: InputContext, op_output: OutputContext, context: ExecutionContext):\n", + "\n", + " input_image = op_input.get(\"image\")\n", + " if not input_image:\n", + " raise ValueError(\"Input image is not found.\")\n", + "\n", + " output_path = context.output.get().path\n", + "\n", + " # This operator gets an in-memory Image object, so a specialized ImageReader is needed.\n", + " _reader = InMemImageReader(input_image)\n", + " pre_transforms = self.pre_process(_reader)\n", + " post_transforms = self.post_process(pre_transforms, path.join(output_path, \"prediction_output\"))\n", + "\n", + " # Delegates inference and saving output to the built-in operator.\n", + " infer_operator = MonaiSegInferenceOperator(\n", + " (\n", + " 160,\n", + " 160,\n", + " 160,\n", + " ),\n", + " pre_transforms,\n", + " post_transforms,\n", + " )\n", + "\n", + " # Setting the keys used in the dictironary based transforms may change.\n", + " infer_operator.input_dataset_key = self._input_dataset_key\n", + " infer_operator.pred_dataset_key = self._pred_dataset_key\n", + "\n", + " # Now let the built-in operator handles the work with the I/O spec and execution context.\n", + " infer_operator.compute(op_input, op_output, context)\n", + "\n", + " def pre_process(self, img_reader) -> Compose:\n", + " \"\"\"Composes transforms for preprocessing input before predicting on a model.\"\"\"\n", + "\n", + " my_key = self._input_dataset_key\n", + " return Compose(\n", + " [\n", + " LoadImaged(keys=my_key, reader=img_reader),\n", + " EnsureChannelFirstd(keys=my_key),\n", + " Spacingd(keys=my_key, pixdim=[1.0, 1.0, 1.0], mode=[\"blinear\"], align_corners=True),\n", + " ScaleIntensityRanged(keys=my_key, a_min=-57, a_max=164, b_min=0.0, b_max=1.0, clip=True),\n", + " CropForegroundd(keys=my_key, source_key=my_key),\n", + " ToTensord(keys=my_key),\n", + " ]\n", + " )\n", + "\n", + " def post_process(self, pre_transforms: Compose, out_dir: str = \"./prediction_output\") -> Compose:\n", + " \"\"\"Composes transforms for postprocessing the prediction results.\"\"\"\n", + "\n", + " pred_key = self._pred_dataset_key\n", + " return Compose(\n", + " [\n", + " Activationsd(keys=pred_key, softmax=True),\n", + " AsDiscreted(keys=pred_key, argmax=True),\n", + " Invertd(\n", + " keys=pred_key, transform=pre_transforms, orig_keys=self._input_dataset_key, nearest_interp=True\n", + " ),\n", + " SaveImaged(keys=pred_key, output_dir=out_dir, output_postfix=\"seg\", output_dtype=uint8, resample=False),\n", + " ]\n", + " )\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### app.py" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting my_app/app.py\n" + ] + } + ], + "source": [ + "%%writefile my_app/app.py\n", + "import logging\n", + "\n", + "from spleen_seg_operator import SpleenSegOperator\n", + "\n", + "from monai.deploy.core import Application, resource\n", + "from monai.deploy.operators.dicom_data_loader_operator import DICOMDataLoaderOperator\n", + "from monai.deploy.operators.dicom_seg_writer_operator import DICOMSegmentationWriterOperator\n", + "from monai.deploy.operators.dicom_series_selector_operator import DICOMSeriesSelectorOperator\n", + "from monai.deploy.operators.dicom_series_to_volume_operator import DICOMSeriesToVolumeOperator\n", + "\n", + "@resource(cpu=1, gpu=1, memory=\"7Gi\")\n", + "class AISpleenSegApp(Application):\n", + " def __init__(self, *args, **kwargs):\n", + " super().__init__(*args, **kwargs)\n", + "\n", + " def compose(self):\n", + "\n", + " study_loader_op = DICOMDataLoaderOperator()\n", + " series_selector_op = DICOMSeriesSelectorOperator(Sample_Rules_Text)\n", + " series_to_vol_op = DICOMSeriesToVolumeOperator()\n", + " # Creates DICOM Seg writer with segment label name in a string list\n", + " dicom_seg_writer = DICOMSegmentationWriterOperator(seg_labels=[\"Spleen\"])\n", + " # Creates the model specific segmentation operator\n", + " spleen_seg_op = SpleenSegOperator()\n", + "\n", + " # Creates the DAG by link the operators\n", + " self.add_flow(study_loader_op, series_selector_op, {\"dicom_study_list\": \"dicom_study_list\"})\n", + " self.add_flow(series_selector_op, series_to_vol_op, {\"study_selected_series_list\": \"study_selected_series_list\"})\n", + " self.add_flow(series_to_vol_op, spleen_seg_op, {\"image\": \"image\"})\n", + " self.add_flow(series_selector_op, dicom_seg_writer, {\"study_selected_series_list\": \"study_selected_series_list\"})\n", + " self.add_flow(spleen_seg_op, dicom_seg_writer, {\"seg_image\": \"seg_image\"})\n", + "\n", + "# This is a sample series selection rule in JSON, simply selecting CT series.\n", + "# If the study has more than 1 CT series, then all of them will be selected.\n", + "# Please see more detail in DICOMSeriesSelectorOperator.\n", + "Sample_Rules_Text = \"\"\"\n", + "{\n", + " \"selections\": [\n", + " {\n", + " \"name\": \"CT Series\",\n", + " \"conditions\": {\n", + " \"StudyDescription\": \"(.*?)\",\n", + " \"Modality\": \"(?i)CT\",\n", + " \"SeriesDescription\": \"(.*?)\"\n", + " }\n", + " }\n", + " ]\n", + "}\n", + "\"\"\"\n", + "\n", + "if __name__ == \"__main__\":\n", + " # Creates the app and test it standalone. When running is this mode, please note the following:\n", + " # -i , for input DICOM CT series folder\n", + " # -o , for the output folder, default $PWD/output\n", + " # -m , for model file path\n", + " # e.g.\n", + " # python3 app.py -i input -m model.ts\n", + " #\n", + " AISpleenSegApp(do_run=True)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```python\n", + "if __name__ == \"__main__\":\n", + " AISpleenSegApp(do_run=True)\n", + "```\n", + "\n", + "The above lines are needed to execute the application code by using `python` interpreter.\n", + "\n", + "### \\_\\_main\\_\\_.py\n", + "\n", + "\\_\\_main\\_\\_.py is needed for MONAI Application Packager to detect the main application code (`app.py`) when the application is executed with the application folder path (e.g., `python simple_imaging_app`)." + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Overwriting my_app/__main__.py\n" + ] + } + ], + "source": [ + "%%writefile my_app/__main__.py\n", + "from app import AISpleenSegApp\n", + "\n", + "if __name__ == \"__main__\":\n", + " AISpleenSegApp(do_run=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "app.py\t__main__.py spleen_seg_operator.py\n" + ] + } + ], + "source": [ + "!ls my_app" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In this time, let's execute the app in the command line." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[34mGoing to initiate execution of operator DICOMDataLoaderOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMDataLoaderOperator \u001b[33m(Process ID: 99004, Operator ID: 7115f2a4-8785-4cb8-a355-bae80217a0be)\u001b[39m\n", + "\u001b[34mDone performing execution of operator DICOMDataLoaderOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator DICOMSeriesSelectorOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMSeriesSelectorOperator \u001b[33m(Process ID: 99004, Operator ID: fdfd5a52-cbbd-47b6-b346-e9814e333c5e)\u001b[39m\n", + "[2022-01-28 11:27:27,862] [INFO] (root) - Finding series for Selection named: CT Series\n", + "[2022-01-28 11:27:27,862] [INFO] (root) - Searching study, : 1.2.826.0.1.3680043.2.1125.1.67295333199898911264201812221946213\n", + " # of series: 1\n", + "[2022-01-28 11:27:27,862] [INFO] (root) - Working on series, instance UID: 1.2.826.0.1.3680043.2.1125.1.68102559796966796813942775094416763\n", + "[2022-01-28 11:27:27,862] [INFO] (root) - On attribute: 'StudyDescription' to match value: '(.*?)'\n", + "[2022-01-28 11:27:27,862] [INFO] (root) - Series attribute value: spleen\n", + "[2022-01-28 11:27:27,862] [INFO] (root) - Series attribute string value did not match. Try regEx.\n", + "[2022-01-28 11:27:27,862] [INFO] (root) - On attribute: 'Modality' to match value: '(?i)CT'\n", + "[2022-01-28 11:27:27,862] [INFO] (root) - Series attribute value: CT\n", + "[2022-01-28 11:27:27,862] [INFO] (root) - Series attribute string value did not match. Try regEx.\n", + "[2022-01-28 11:27:27,862] [INFO] (root) - On attribute: 'SeriesDescription' to match value: '(.*?)'\n", + "[2022-01-28 11:27:27,862] [INFO] (root) - Series attribute value: No series description\n", + "[2022-01-28 11:27:27,862] [INFO] (root) - Series attribute string value did not match. Try regEx.\n", + "[2022-01-28 11:27:27,862] [INFO] (root) - Selected Series, UID: 1.2.826.0.1.3680043.2.1125.1.68102559796966796813942775094416763\n", + "\u001b[34mDone performing execution of operator DICOMSeriesSelectorOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator DICOMSeriesToVolumeOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMSeriesToVolumeOperator \u001b[33m(Process ID: 99004, Operator ID: 70a886ab-9f0b-4280-b521-992f56a9020b)\u001b[39m\n", + "\u001b[34mDone performing execution of operator DICOMSeriesToVolumeOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator SpleenSegOperator\u001b[39m\n", + "\u001b[32mExecuting operator SpleenSegOperator \u001b[33m(Process ID: 99004, Operator ID: 25cce913-6d32-4215-8fb2-358dd0d18702)\u001b[39m\n", + "Converted Image object metadata:\n", + "SeriesInstanceUID: 1.2.826.0.1.3680043.2.1125.1.68102559796966796813942775094416763, type \n", + "Modality: CT, type \n", + "SeriesDescription: No series description, type \n", + "PatientPosition: HFS, type \n", + "SeriesNumber: 1, type \n", + "row_pixel_spacing: 1.0, type \n", + "col_pixel_spacing: 1.0, type \n", + "depth_pixel_spacing: 1.0, type \n", + "row_direction_cosine: [-1.0, 0.0, 0.0], type \n", + "col_direction_cosine: [0.0, -1.0, 0.0], type \n", + "depth_direction_cosine: [0.0, 0.0, 1.0], type \n", + "dicom_affine_transform: [[-1. 0. 0. 0.]\n", + " [ 0. -1. 0. 0.]\n", + " [ 0. 0. 1. 0.]\n", + " [ 0. 0. 0. 1.]], type \n", + "nifti_affine_transform: [[ 1. -0. -0. -0.]\n", + " [-0. 1. -0. -0.]\n", + " [ 0. 0. 1. 0.]\n", + " [ 0. 0. 0. 1.]], type \n", + "StudyInstanceUID: 1.2.826.0.1.3680043.2.1125.1.67295333199898911264201812221946213, type \n", + "StudyID: SLICER10001, type \n", + "StudyDate: 2019-09-16, type \n", + "StudyTime: 010100.000000, type \n", + "StudyDescription: spleen, type \n", + "AccessionNumber: 1, type \n", + "selection_name: CT Series, type \n", + "file written: /home/aheumann/projects/monai-deploy-app-sdk/notebooks/tutorials/output/prediction_output/1.2.826.0.1.3680043.2.1125.1/1.2.826.0.1.3680043.2.1125.1_seg.nii.gz.\n", + "Output Seg image numpy array shaped: (515, 440, 440)\n", + "Output Seg image pixel max value: 1\n", + "\u001b[34mDone performing execution of operator SpleenSegOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator DICOMSegmentationWriterOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMSegmentationWriterOperator \u001b[33m(Process ID: 99004, Operator ID: dba1cc01-1dda-49e0-9c95-f91100ef4fbe)\u001b[39m\n", + "[2022-01-28 11:27:34,283] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Number of DICOM instance datasets in the list: 515\n", + "[2022-01-28 11:27:34,283] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Number of slices in the numpy image: 515\n", + "[2022-01-28 11:27:34,283] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Labels of the segments: ['Spleen']\n", + "[2022-01-28 11:27:35,656] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Unique values in seg image: [0 1]\n", + "[2022-01-28 11:27:36,287] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Saving output file /home/aheumann/projects/monai-deploy-app-sdk/notebooks/tutorials/output/dicom_seg-DICOMSEG.dcm\n", + "[2022-01-28 11:27:36,468] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - File saved.\n", + "\u001b[34mDone performing execution of operator DICOMSegmentationWriterOperator\n", + "\u001b[39m\n" + ] + } + ], + "source": [ + "!python my_app -i dcm -o output -m model.ts" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Above command is same with the following command line:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[34mGoing to initiate execution of operator DICOMDataLoaderOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMDataLoaderOperator \u001b[33m(Process ID: 99279, Operator ID: 2ef7b49a-4f37-40b7-a819-cadb1df4a990)\u001b[39m\n", + "\u001b[34mDone performing execution of operator DICOMDataLoaderOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator DICOMSeriesSelectorOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMSeriesSelectorOperator \u001b[33m(Process ID: 99279, Operator ID: f33c3d86-6eef-4802-ae5d-e9dfd0318f45)\u001b[39m\n", + "[2022-01-28 11:27:38,401] [INFO] (root) - Finding series for Selection named: CT Series\n", + "[2022-01-28 11:27:38,401] [INFO] (root) - Searching study, : 1.2.826.0.1.3680043.2.1125.1.67295333199898911264201812221946213\n", + " # of series: 1\n", + "[2022-01-28 11:27:38,401] [INFO] (root) - Working on series, instance UID: 1.2.826.0.1.3680043.2.1125.1.68102559796966796813942775094416763\n", + "[2022-01-28 11:27:38,401] [INFO] (root) - On attribute: 'StudyDescription' to match value: '(.*?)'\n", + "[2022-01-28 11:27:38,401] [INFO] (root) - Series attribute value: spleen\n", + "[2022-01-28 11:27:38,401] [INFO] (root) - Series attribute string value did not match. Try regEx.\n", + "[2022-01-28 11:27:38,401] [INFO] (root) - On attribute: 'Modality' to match value: '(?i)CT'\n", + "[2022-01-28 11:27:38,401] [INFO] (root) - Series attribute value: CT\n", + "[2022-01-28 11:27:38,401] [INFO] (root) - Series attribute string value did not match. Try regEx.\n", + "[2022-01-28 11:27:38,401] [INFO] (root) - On attribute: 'SeriesDescription' to match value: '(.*?)'\n", + "[2022-01-28 11:27:38,401] [INFO] (root) - Series attribute value: No series description\n", + "[2022-01-28 11:27:38,401] [INFO] (root) - Series attribute string value did not match. Try regEx.\n", + "[2022-01-28 11:27:38,401] [INFO] (root) - Selected Series, UID: 1.2.826.0.1.3680043.2.1125.1.68102559796966796813942775094416763\n", + "\u001b[34mDone performing execution of operator DICOMSeriesSelectorOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator DICOMSeriesToVolumeOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMSeriesToVolumeOperator \u001b[33m(Process ID: 99279, Operator ID: 3a6ab6a4-5a97-4301-939f-c24f5f83de95)\u001b[39m\n", + "\u001b[34mDone performing execution of operator DICOMSeriesToVolumeOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator SpleenSegOperator\u001b[39m\n", + "\u001b[32mExecuting operator SpleenSegOperator \u001b[33m(Process ID: 99279, Operator ID: 96b1a47c-4628-48f7-a26a-10e498e1ce8a)\u001b[39m\n", + "Converted Image object metadata:\n", + "SeriesInstanceUID: 1.2.826.0.1.3680043.2.1125.1.68102559796966796813942775094416763, type \n", + "Modality: CT, type \n", + "SeriesDescription: No series description, type \n", + "PatientPosition: HFS, type \n", + "SeriesNumber: 1, type \n", + "row_pixel_spacing: 1.0, type \n", + "col_pixel_spacing: 1.0, type \n", + "depth_pixel_spacing: 1.0, type \n", + "row_direction_cosine: [-1.0, 0.0, 0.0], type \n", + "col_direction_cosine: [0.0, -1.0, 0.0], type \n", + "depth_direction_cosine: [0.0, 0.0, 1.0], type \n", + "dicom_affine_transform: [[-1. 0. 0. 0.]\n", + " [ 0. -1. 0. 0.]\n", + " [ 0. 0. 1. 0.]\n", + " [ 0. 0. 0. 1.]], type \n", + "nifti_affine_transform: [[ 1. -0. -0. -0.]\n", + " [-0. 1. -0. -0.]\n", + " [ 0. 0. 1. 0.]\n", + " [ 0. 0. 0. 1.]], type \n", + "StudyInstanceUID: 1.2.826.0.1.3680043.2.1125.1.67295333199898911264201812221946213, type \n", + "StudyID: SLICER10001, type \n", + "StudyDate: 2019-09-16, type \n", + "StudyTime: 010100.000000, type \n", + "StudyDescription: spleen, type \n", + "AccessionNumber: 1, type \n", + "selection_name: CT Series, type \n", + "file written: /home/aheumann/projects/monai-deploy-app-sdk/notebooks/tutorials/output/prediction_output/1.2.826.0.1.3680043.2.1125.1/1.2.826.0.1.3680043.2.1125.1_seg.nii.gz.\n", + "Output Seg image numpy array shaped: (515, 440, 440)\n", + "Output Seg image pixel max value: 1\n", + "\u001b[34mDone performing execution of operator SpleenSegOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator DICOMSegmentationWriterOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMSegmentationWriterOperator \u001b[33m(Process ID: 99279, Operator ID: 50985a8f-832a-48b7-9eee-0c4a105d67f6)\u001b[39m\n", + "[2022-01-28 11:27:44,788] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Number of DICOM instance datasets in the list: 515\n", + "[2022-01-28 11:27:44,788] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Number of slices in the numpy image: 515\n", + "[2022-01-28 11:27:44,788] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Labels of the segments: ['Spleen']\n", + "[2022-01-28 11:27:46,146] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Unique values in seg image: [0 1]\n", + "[2022-01-28 11:27:46,775] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Saving output file /home/aheumann/projects/monai-deploy-app-sdk/notebooks/tutorials/output/dicom_seg-DICOMSEG.dcm\n", + "[2022-01-28 11:27:46,959] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - File saved.\n", + "\u001b[34mDone performing execution of operator DICOMSegmentationWriterOperator\n", + "\u001b[39m\n" + ] + } + ], + "source": [ + "import os\n", + "os.environ['MKL_THREADING_LAYER'] = 'GNU'\n", + "!monai-deploy exec my_app -i dcm -o output -m model.ts" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dicom_seg-DICOMSEG.dcm\tprediction_output\n" + ] + } + ], + "source": [ + "!ls output" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Packaging app" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's package the app with [MONAI Application Packager](/developing_with_sdk/packaging_app)." + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Building MONAI Application Package... Done\n", + "[2022-01-28 11:29:10,930] [INFO] (app_packager) - Successfully built my_app:latest\n" + ] + } + ], + "source": [ + "!monai-deploy package -b nvcr.io/nvidia/pytorch:21.11-py3 my_app --tag my_app:latest -m model.ts" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + ":::{note}\n", + "Building a MONAI Application Package (Docker image) can take time. Use `-l DEBUG` option if you want to see the progress.\n", + ":::\n", + "\n", + "We can see that the Docker image is created." + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "my_app latest 8430fb7497f4 1 second ago 15GB\n" + ] + } + ], + "source": [ + "!docker image ls | grep my_app" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Executing packaged app locally\n", + "\n", + "The packaged app can be run locally through [MONAI Application Runner](/developing_with_sdk/executing_packaged_app_locally)." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Checking dependencies...\n", + "--> Verifying if \"docker\" is installed...\n", + "\n", + "--> Verifying if \"my_app:latest\" is available...\n", + "\n", + "Checking for MAP \"my_app:latest\" locally\n", + "\"my_app:latest\" found.\n", + "\n", + "Reading MONAI App Package manifest...\n", + "--> Verifying if \"nvidia-docker\" is installed...\n", + "\n", + "\u001b[34mGoing to initiate execution of operator DICOMDataLoaderOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMDataLoaderOperator \u001b[33m(Process ID: 1, Operator ID: 13bce6e6-4fe4-4fdd-acd9-8b7b26fabd4d)\u001b[39m\n", + "\u001b[34mDone performing execution of operator DICOMDataLoaderOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator DICOMSeriesSelectorOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMSeriesSelectorOperator \u001b[33m(Process ID: 1, Operator ID: 3fc4155d-2951-4ece-9f76-fed9572abe12)\u001b[39m\n", + "[2022-01-28 10:29:15,923] [INFO] (root) - Finding series for Selection named: CT Series\n", + "[2022-01-28 10:29:15,923] [INFO] (root) - Searching study, : 1.2.826.0.1.3680043.2.1125.1.67295333199898911264201812221946213\n", + " # of series: 1\n", + "[2022-01-28 10:29:15,923] [INFO] (root) - Working on series, instance UID: 1.2.826.0.1.3680043.2.1125.1.68102559796966796813942775094416763\n", + "[2022-01-28 10:29:15,923] [INFO] (root) - On attribute: 'StudyDescription' to match value: '(.*?)'\n", + "[2022-01-28 10:29:15,923] [INFO] (root) - Series attribute value: spleen\n", + "[2022-01-28 10:29:15,923] [INFO] (root) - Series attribute string value did not match. Try regEx.\n", + "[2022-01-28 10:29:15,923] [INFO] (root) - On attribute: 'Modality' to match value: '(?i)CT'\n", + "[2022-01-28 10:29:15,923] [INFO] (root) - Series attribute value: CT\n", + "[2022-01-28 10:29:15,923] [INFO] (root) - Series attribute string value did not match. Try regEx.\n", + "[2022-01-28 10:29:15,923] [INFO] (root) - On attribute: 'SeriesDescription' to match value: '(.*?)'\n", + "[2022-01-28 10:29:15,923] [INFO] (root) - Series attribute value: No series description\n", + "[2022-01-28 10:29:15,923] [INFO] (root) - Series attribute string value did not match. Try regEx.\n", + "[2022-01-28 10:29:15,923] [INFO] (root) - Selected Series, UID: 1.2.826.0.1.3680043.2.1125.1.68102559796966796813942775094416763\n", + "\u001b[34mDone performing execution of operator DICOMSeriesSelectorOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator DICOMSeriesToVolumeOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMSeriesToVolumeOperator \u001b[33m(Process ID: 1, Operator ID: 1037ab83-1a52-4a94-a3bd-ad70f85ad186)\u001b[39m\n", + "\u001b[34mDone performing execution of operator DICOMSeriesToVolumeOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator SpleenSegOperator\u001b[39m\n", + "\u001b[32mExecuting operator SpleenSegOperator \u001b[33m(Process ID: 1, Operator ID: 5435a948-5ae6-441a-8a73-2c418d2b434d)\u001b[39m\n", + "Converted Image object metadata:\n", + "SeriesInstanceUID: 1.2.826.0.1.3680043.2.1125.1.68102559796966796813942775094416763, type \n", + "Modality: CT, type \n", + "SeriesDescription: No series description, type \n", + "PatientPosition: HFS, type \n", + "SeriesNumber: 1, type \n", + "row_pixel_spacing: 1.0, type \n", + "col_pixel_spacing: 1.0, type \n", + "depth_pixel_spacing: 1.0, type \n", + "row_direction_cosine: [-1.0, 0.0, 0.0], type \n", + "col_direction_cosine: [0.0, -1.0, 0.0], type \n", + "depth_direction_cosine: [0.0, 0.0, 1.0], type \n", + "dicom_affine_transform: [[-1. 0. 0. 0.]\n", + " [ 0. -1. 0. 0.]\n", + " [ 0. 0. 1. 0.]\n", + " [ 0. 0. 0. 1.]], type \n", + "nifti_affine_transform: [[ 1. -0. -0. -0.]\n", + " [-0. 1. -0. -0.]\n", + " [ 0. 0. 1. 0.]\n", + " [ 0. 0. 0. 1.]], type \n", + "StudyInstanceUID: 1.2.826.0.1.3680043.2.1125.1.67295333199898911264201812221946213, type \n", + "StudyID: SLICER10001, type \n", + "StudyDate: 2019-09-16, type \n", + "StudyTime: 010100.000000, type \n", + "StudyDescription: spleen, type \n", + "AccessionNumber: 1, type \n", + "selection_name: CT Series, type \n", + "file written: /var/monai/output/prediction_output/1.2.826.0.1.3680043.2.1125.1/1.2.826.0.1.3680043.2.1125.1_seg.nii.gz.\n", + "Output Seg image numpy array shaped: (515, 440, 440)\n", + "Output Seg image pixel max value: 1\n", + "\u001b[34mDone performing execution of operator SpleenSegOperator\n", + "\u001b[39m\n", + "\u001b[34mGoing to initiate execution of operator DICOMSegmentationWriterOperator\u001b[39m\n", + "\u001b[32mExecuting operator DICOMSegmentationWriterOperator \u001b[33m(Process ID: 1, Operator ID: 31cb5ca9-1fee-43a7-a14a-afef8ef757fe)\u001b[39m\n", + "[2022-01-28 10:29:21,681] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Number of DICOM instance datasets in the list: 515\n", + "[2022-01-28 10:29:21,681] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Number of slices in the numpy image: 515\n", + "[2022-01-28 10:29:21,681] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Labels of the segments: ['Spleen']\n", + "[2022-01-28 10:29:23,026] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Unique values in seg image: [0 1]\n", + "[2022-01-28 10:29:23,862] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Saving output file /var/monai/output/dicom_seg-DICOMSEG.dcm\n", + "[2022-01-28 10:29:24,013] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - File saved.\n", + "\u001b[34mDone performing execution of operator DICOMSegmentationWriterOperator\n", + "\u001b[39m\n" + ] + } + ], + "source": [ + "# Copy DICOM files are in 'dcm' folder\n", + "\n", + "# Launch the app\n", + "!monai-deploy run my_app:latest dcm output" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dicom_seg-DICOMSEG.dcm\tprediction_output\n" + ] + } + ], + "source": [ + "!ls output" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "interpreter": { + "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.10" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}