From 234f6409d4e787f735884664647e8f02462ebb96 Mon Sep 17 00:00:00 2001 From: M Q Date: Tue, 4 Jun 2024 15:33:07 -0700 Subject: [PATCH 1/2] Fix this example application to make it work with SDK v0.6+ Signed-off-by: M Q --- .../breast_density_classifer_app/README.md | 26 +++++++----------- .../apps/breast_density_classifer_app/app.py | 4 ++- .../breast_density_classifer_app/app.yaml | 27 +++++++++++++++++++ .../breast_density_classifier_operator.py | 9 ++++--- .../requirements.txt | 4 +++ 5 files changed, 49 insertions(+), 21 deletions(-) create mode 100644 examples/apps/breast_density_classifer_app/app.yaml create mode 100644 examples/apps/breast_density_classifer_app/requirements.txt diff --git a/examples/apps/breast_density_classifer_app/README.md b/examples/apps/breast_density_classifer_app/README.md index b488908b..c1256c49 100644 --- a/examples/apps/breast_density_classifer_app/README.md +++ b/examples/apps/breast_density_classifer_app/README.md @@ -1,29 +1,23 @@ -## A MONAI Application Package to deploy breast density classification algorithm -This MAP is based on the Breast Density Model in MONAI [Model-Zoo](https://github.com/Project-MONAI/model-zoo). This model is developed at the Center for Augmented Intelligence in Imaging at the Mayo Clinic, Florida. +## A MONAI Application Package to deploy breast density classification algorithm +This MAP is based on the Breast Density Model in MONAI [Model-Zoo](https://github.com/Project-MONAI/model-zoo). This model is developed at the Center for Augmented Intelligence in Imaging at the Mayo Clinic, Florida. For any questions, feel free to contact Vikash Gupta (gupta.vikash@mayo.edu) Sample data and a torchscript model can be downloaded from https://drive.google.com/drive/folders/1Dryozl2MwNunpsGaFPVoaKBLkNbVM3Hu?usp=sharing -## Run the application package -### Python CLI +## Run the application code with Python interpreter ``` -python app.py -i -o -m +python app.py -i -o -m ``` -### MONAI Deploy CLI +## Package the application as a MONAI Application Package (contianer image) +In order to build the MONAI App Package, go a level up and execute the following command. ``` -monai-deploy exec app.py -i -o -m -``` -Alternatively, you can go a level higher and execute -``` -monai-deploy exec breast_density_classification_app -i -o -m +monai-deploy package breast_density_classification_app -m -c breast_density_classifer_app/app.yaml --tag breast_density:0.1.0 --platform x64-workstation -l DEBUG ``` - -### Packaging the monai app -In order to build the monai app, Go a level up and execute the following command. +## Run the MONAI Application Package using MONAI Deploy CLI ``` -monai-deploy package -b nvcr.io/nvidia/pytorch:21.12-py3 breast_density_classification_app --tag breast_density:0.1.0 -m $breast_density_model +monai-deploy run breast_density-x64-workstation-dgpu-linux-amd64:0.1.0 -i -o ``` - +Once the container exits successfully, check the results in the output directory. There should be a newly creeated DICOM instance file and a `output.json` file containing the classification results. \ No newline at end of file diff --git a/examples/apps/breast_density_classifer_app/app.py b/examples/apps/breast_density_classifer_app/app.py index 13cbdd2e..950c8eaf 100644 --- a/examples/apps/breast_density_classifer_app/app.py +++ b/examples/apps/breast_density_classifer_app/app.py @@ -26,7 +26,7 @@ def __init__(self, *args, **kwargs): def compose(self): """Creates the app specific operators and chain them up in the processing DAG.""" - logging.info(f"Begin {self.compose.__name__}") + self._logger.info(f"Begin {self.compose.__name__}") # Use command line options over environment variables to init context. app_context: AppContext = Application.init_app_context(self.argv) @@ -34,6 +34,8 @@ def compose(self): app_output_path = Path(app_context.output_path) model_path = Path(app_context.model_path) + self._logger.info(f"App input, output path, & model path: {app_input_path}, {app_output_path}, {model_path}") + model_info = ModelInfo( "MONAI Model for Breast Density", "BreastDensity", diff --git a/examples/apps/breast_density_classifer_app/app.yaml b/examples/apps/breast_density_classifer_app/app.yaml new file mode 100644 index 00000000..5c2e6649 --- /dev/null +++ b/examples/apps/breast_density_classifer_app/app.yaml @@ -0,0 +1,27 @@ +%YAML 1.2 +# SPDX-FileCopyrightText: Copyright (c) 2022-2023 MONAI. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# +# 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. +--- +application: + title: MONAI Deploy App Package - Spleen Seg Inference + version: 1.0 + inputFormats: ["file"] + outputFormats: ["file"] + +resources: + cpu: 1 + gpu: 1 + memory: 1Gi + gpuMemory: 2Gi diff --git a/examples/apps/breast_density_classifer_app/breast_density_classifier_operator.py b/examples/apps/breast_density_classifer_app/breast_density_classifier_operator.py index b74ec5a4..4a442088 100644 --- a/examples/apps/breast_density_classifer_app/breast_density_classifier_operator.py +++ b/examples/apps/breast_density_classifer_app/breast_density_classifier_operator.py @@ -1,4 +1,5 @@ import json +import logging import os from pathlib import Path from typing import Dict, Optional @@ -94,7 +95,9 @@ def _get_model(self, app_context: AppContext, model_path: Path, model_name: str) # `app_context.models.get(model_name)` returns a model instance if exists. # If model_name is not specified and only one model exists, it returns that model. model = app_context.models.get(model_name) + logging.info("Got the model network from the app context.") else: + logging.info("Model network not in context. JIT loading from file...") model = torch.jit.load( ClassifierOperator.MODEL_LOCAL_PATH, map_location=torch.device("cuda" if torch.cuda.is_available() else "cpu"), @@ -149,9 +152,7 @@ def compute(self, op_input, op_output, context): device = torch.device("cuda" if torch.cuda.is_available() else "cpu") - # Need to get the model from context, when it is re-implemented, and for now, load it directly here. - # model = context.models.get() - model = torch.jit.load(self.model_path, map_location=device) + # Model network loading has been handled during init. pre_transforms = self.pre_process(_reader) post_transforms = self.post_process() @@ -162,7 +163,7 @@ def compute(self, op_input, op_output, context): with torch.no_grad(): for d in dataloader: image = d[0].to(device) - outputs = model(image) + outputs = self.model(image) out = post_transforms(outputs).data.cpu().numpy()[0] print(out) diff --git a/examples/apps/breast_density_classifer_app/requirements.txt b/examples/apps/breast_density_classifer_app/requirements.txt new file mode 100644 index 00000000..973cfe06 --- /dev/null +++ b/examples/apps/breast_density_classifer_app/requirements.txt @@ -0,0 +1,4 @@ +highdicom>=0.18.2 +monai>=1.2.0 +pydicom>=2.3.0 +torch>=1.12.0 \ No newline at end of file From 7fd7a690a5ee8bf302514d1b931ac65090a81793 Mon Sep 17 00:00:00 2001 From: M Q Date: Tue, 4 Jun 2024 15:55:23 -0700 Subject: [PATCH 2/2] Fix the example app errors and corrected folder name Signed-off-by: M Q --- .../README.md | 0 .../__init__.py | 0 .../__main__.py | 0 .../app.py | 0 .../app.yaml | 0 .../breast_density_classifier_operator.py | 0 .../requirements.txt | 0 7 files changed, 0 insertions(+), 0 deletions(-) rename examples/apps/{breast_density_classifer_app => breast_density_classifier_app}/README.md (100%) rename examples/apps/{breast_density_classifer_app => breast_density_classifier_app}/__init__.py (100%) rename examples/apps/{breast_density_classifer_app => breast_density_classifier_app}/__main__.py (100%) rename examples/apps/{breast_density_classifer_app => breast_density_classifier_app}/app.py (100%) rename examples/apps/{breast_density_classifer_app => breast_density_classifier_app}/app.yaml (100%) rename examples/apps/{breast_density_classifer_app => breast_density_classifier_app}/breast_density_classifier_operator.py (100%) rename examples/apps/{breast_density_classifer_app => breast_density_classifier_app}/requirements.txt (100%) diff --git a/examples/apps/breast_density_classifer_app/README.md b/examples/apps/breast_density_classifier_app/README.md similarity index 100% rename from examples/apps/breast_density_classifer_app/README.md rename to examples/apps/breast_density_classifier_app/README.md diff --git a/examples/apps/breast_density_classifer_app/__init__.py b/examples/apps/breast_density_classifier_app/__init__.py similarity index 100% rename from examples/apps/breast_density_classifer_app/__init__.py rename to examples/apps/breast_density_classifier_app/__init__.py diff --git a/examples/apps/breast_density_classifer_app/__main__.py b/examples/apps/breast_density_classifier_app/__main__.py similarity index 100% rename from examples/apps/breast_density_classifer_app/__main__.py rename to examples/apps/breast_density_classifier_app/__main__.py diff --git a/examples/apps/breast_density_classifer_app/app.py b/examples/apps/breast_density_classifier_app/app.py similarity index 100% rename from examples/apps/breast_density_classifer_app/app.py rename to examples/apps/breast_density_classifier_app/app.py diff --git a/examples/apps/breast_density_classifer_app/app.yaml b/examples/apps/breast_density_classifier_app/app.yaml similarity index 100% rename from examples/apps/breast_density_classifer_app/app.yaml rename to examples/apps/breast_density_classifier_app/app.yaml diff --git a/examples/apps/breast_density_classifer_app/breast_density_classifier_operator.py b/examples/apps/breast_density_classifier_app/breast_density_classifier_operator.py similarity index 100% rename from examples/apps/breast_density_classifer_app/breast_density_classifier_operator.py rename to examples/apps/breast_density_classifier_app/breast_density_classifier_operator.py diff --git a/examples/apps/breast_density_classifer_app/requirements.txt b/examples/apps/breast_density_classifier_app/requirements.txt similarity index 100% rename from examples/apps/breast_density_classifer_app/requirements.txt rename to examples/apps/breast_density_classifier_app/requirements.txt