Skip to content

Commit 493c771

Browse files
authored
Fix breast density example application to make it work with SDK v0.6+ (#491)
* Fix this example application to make it work with SDK v0.6+ Signed-off-by: M Q <[email protected]> * Fix the example app errors and corrected folder name Signed-off-by: M Q <[email protected]> --------- Signed-off-by: M Q <[email protected]>
1 parent 8f804c0 commit 493c771

File tree

8 files changed

+62
-34
lines changed

8 files changed

+62
-34
lines changed

examples/apps/breast_density_classifer_app/README.md

-29
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## A MONAI Application Package to deploy breast density classification algorithm
2+
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.
3+
For any questions, feel free to contact Vikash Gupta ([email protected])
4+
Sample data and a torchscript model can be downloaded from https://drive.google.com/drive/folders/1Dryozl2MwNunpsGaFPVoaKBLkNbVM3Hu?usp=sharing
5+
6+
7+
## Run the application code with Python interpreter
8+
```
9+
python app.py -i <input_dir> -o <out_dir> -m <breast_density_model>
10+
```
11+
12+
## Package the application as a MONAI Application Package (contianer image)
13+
In order to build the MONAI App Package, go a level up and execute the following command.
14+
```
15+
monai-deploy package breast_density_classification_app -m <breast_density_model> -c breast_density_classifer_app/app.yaml --tag breast_density:0.1.0 --platform x64-workstation -l DEBUG
16+
```
17+
18+
## Run the MONAI Application Package using MONAI Deploy CLI
19+
```
20+
monai-deploy run breast_density-x64-workstation-dgpu-linux-amd64:0.1.0 -i <input_dir> -o <output_dir>
21+
```
22+
23+
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.

examples/apps/breast_density_classifer_app/app.py renamed to examples/apps/breast_density_classifier_app/app.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ def __init__(self, *args, **kwargs):
2626

2727
def compose(self):
2828
"""Creates the app specific operators and chain them up in the processing DAG."""
29-
logging.info(f"Begin {self.compose.__name__}")
29+
self._logger.info(f"Begin {self.compose.__name__}")
3030

3131
# Use command line options over environment variables to init context.
3232
app_context: AppContext = Application.init_app_context(self.argv)
3333
app_input_path = Path(app_context.input_path)
3434
app_output_path = Path(app_context.output_path)
3535
model_path = Path(app_context.model_path)
3636

37+
self._logger.info(f"App input, output path, & model path: {app_input_path}, {app_output_path}, {model_path}")
38+
3739
model_info = ModelInfo(
3840
"MONAI Model for Breast Density",
3941
"BreastDensity",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
%YAML 1.2
2+
# SPDX-FileCopyrightText: Copyright (c) 2022-2023 MONAI. All rights reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
---
17+
application:
18+
title: MONAI Deploy App Package - Spleen Seg Inference
19+
version: 1.0
20+
inputFormats: ["file"]
21+
outputFormats: ["file"]
22+
23+
resources:
24+
cpu: 1
25+
gpu: 1
26+
memory: 1Gi
27+
gpuMemory: 2Gi

examples/apps/breast_density_classifer_app/breast_density_classifier_operator.py renamed to examples/apps/breast_density_classifier_app/breast_density_classifier_operator.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import logging
23
import os
34
from pathlib import Path
45
from typing import Dict, Optional
@@ -94,7 +95,9 @@ def _get_model(self, app_context: AppContext, model_path: Path, model_name: str)
9495
# `app_context.models.get(model_name)` returns a model instance if exists.
9596
# If model_name is not specified and only one model exists, it returns that model.
9697
model = app_context.models.get(model_name)
98+
logging.info("Got the model network from the app context.")
9799
else:
100+
logging.info("Model network not in context. JIT loading from file...")
98101
model = torch.jit.load(
99102
ClassifierOperator.MODEL_LOCAL_PATH,
100103
map_location=torch.device("cuda" if torch.cuda.is_available() else "cpu"),
@@ -149,9 +152,7 @@ def compute(self, op_input, op_output, context):
149152

150153
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
151154

152-
# Need to get the model from context, when it is re-implemented, and for now, load it directly here.
153-
# model = context.models.get()
154-
model = torch.jit.load(self.model_path, map_location=device)
155+
# Model network loading has been handled during init.
155156

156157
pre_transforms = self.pre_process(_reader)
157158
post_transforms = self.post_process()
@@ -162,7 +163,7 @@ def compute(self, op_input, op_output, context):
162163
with torch.no_grad():
163164
for d in dataloader:
164165
image = d[0].to(device)
165-
outputs = model(image)
166+
outputs = self.model(image)
166167
out = post_transforms(outputs).data.cpu().numpy()[0]
167168
print(out)
168169

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
highdicom>=0.18.2
2+
monai>=1.2.0
3+
pydicom>=2.3.0
4+
torch>=1.12.0

0 commit comments

Comments
 (0)