9.16. Clara AI Prostate Segmentation

9.16.1. Overview

This example is a containerized AI inference application, developed for use as one of the operators in the Clara Deploy pipelines. The application is built on the base AI application container, which provides the application framework to deploy Clara Train TLT trained models. The same execution configuration file, set of transform functions, and scanning window inference logic are used; however, inference is performed on the TensorRT Inference Server.

9.16.2. Inputs

The application, in the form of a Docker container, expects an input folder (/input by default), which can be mapped to the host volume when the Docker container is started. This folder must contain a volume image file in the Nifti or MetaImage format. Furthermore, the volume image must be constructed from a single series of a DICOM study, typically an axial series with the data type of the original primary. In this case, the MRI T2 series is expected.

9.16.3. Outputs

The application saves the segmentation results to an output folder, /output by default, which can also be mapped to a folder on the host volume. After the successful completion of the application, a segmentation volume image of format MetaImage is saved in the output folder. The name of the output file is the same as that of the input file due to certain limitations of the downstream consumer.

The example container also publishes data for the Clara Deploy Render Server to the /publish folder by default. The original volume image and segmented volume image, along with a render configuration file, are saved in this folder.

9.16.4. AI Model

The application uses the segmentation_mri_prostate_wp_v1 model, which was developed by NIH, SUNY Syracuse, and NVIDIA for whole prostate segmentation. It is not published on ngc.nvidia.com as of now. The input tensor is of shape i128x128x32 with a single channel. The output is of the same shape with two channels.

The application also uses the same transform library and configuration file for the validation/inference pipeline during TLT model training. The key model attributes (e.g. the model name) are saved in the file config_inference.json.

9.16.4.1. NVIDIA TensorRT Inference Server (TRTIS)

The application performs inference on the NVIDIA TensorRT Inference Server (TRTIS), which provides a cloud inferencing solution optimized for NVIDIA GPUs. The server provides an inference service via an HTTP or gRPC endpoint, allowing remote clients to request inferencing for any model being managed by the server.

9.16.5. Directory Structure

The directories in the container are shown below. The application under /App is from the base container, except for the files in the config directory, which is model specific. The trtis_client directory contains the TRTIS API client library, while sdk_dist contains the Clara Train TLT transforms library. The medical directory contains compiled modules from Clara Train TLT, and the writer directory contains a specialized writer that saves the segmentation result to a volume image file as MetaImage.

/app
└── app_base_inference
    ├── app.py
    ├── config
    │   ├── config.pbtxt
    │   ├── config_render.json
    │   ├── config_validation.json
    │   ├── __init__.py
    │   └── model_config.json
    ├── Dockerfile
    ├── executor.py
    ├── logging_config.json
    ├── main.py
    ├── medical
    ├── Pipfile
    ├── public
    │   └── docs
    │       └── README.md
    ├── writers
    │   ├── __init__.py
    │   ├── __init__.py
    │   ├── mhd_writer.py
    │   └── writer.py
    ├── trtis_client/
    └── sdk_dist/
/input
/output
/publish

9.16.6. Executing Operator Locally

If you want to see the internals of the container and manually run the application, follow these steps.

  1. Start the container in interactive mode. See the next section on how to run the container, and replace the docker run command with the following:

    docker run --entrypoint /bin/bash
    
  2. Once in the the Docker terminal, ensure the current directory is /app.

  3. Execute the following command:

    pipenv run python ./app_base_inference/main.py
    
  4. When finished, type exit.

9.16.7. Executing Operator in Docker

9.16.7.1. Prerequisites

  1. Ensure the Docker image of TRTIS has been imported into the local Docker repository with the following command:

    docker images
    
  2. Look for the image name TRTIS and the correct tag for the release (e.g. 19.08-py3).

  3. Download both the input dataset and the trained model from the MODEL SCRIPTS section for this container on NGC, following the steps in the Setup section.

9.16.7.2. Step 1

Switch to your working directory (e.g. test_prostate_seg).

9.16.7.3. Step 2

Create, if they do not exist, the following directories under your working directory:

  • input containing the input image file

  • output for the segmentation output

  • publish for publishing data for the Render Server

  • logs for the log files

  • models containing models copied from the segmentation_mri_prostate_wp_v1 folder

9.16.7.4. Step 3

In your working directory, create a shell script (run_prostate.sh, or another name if you prefer), copy the sample content below, and save it.

#!/bin/bash

# Copyright (c) 2019, NVIDIA CORPORATION.  All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto.  Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.

# Define the name of the app (aka operator), assumed same as the project folder name
APP_NAME="app_prostate_wp"

# Define the TenseorRT Inference Server Docker image, which will be used for testing
# Use either local repo or NVIDIA repo
#TRTIS_IMAGE="clara/trtis"
TRTIS_IMAGE="nvcr.io/nvidia/tensorrtserver:19.08-py3"

# Launch the container with the following environment variables
# to provide runtime information.
export NVIDIA_CLARA_APPDIR="/app"
export NVIDIA_CLARA_TRTISURI="localhost:8000"

# Define the model name for use when launching TRTIS with only the specific model
MODEL_NAME="segmentation_mri_prostate_wp_v1"

# Create Docker network so that containers can communicate on this network
NETWORK_NAME="container-demo"

# Create network
docker network create ${NETWORK_NAME}

# Run TRTIS(name: trtis), maping ./models/${MODEL_NAME} to /models/${MODEL_NAME}
# (localhost:8000 will be used)
nvidia-docker run --name trtis --network ${NETWORK_NAME} -d --rm --shm-size=1g --ulimit memlock=-1 --ulimit stack=67108864 \
    -p 8000:8000 \
    -v $(pwd)/models/${MODEL_NAME}:/models/${MODEL_NAME} ${TRTIS_IMAGE} \
    trtserver --model-store=/models

# Wait until TRTIS is ready
trtis_local_uri=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' trtis)
echo -n "Wait until TRTIS ${trtis_local_uri} is ready..."
while [ $(curl -s ${trtis_local_uri}:8000/api/status | grep -c SERVER_READY) -eq 0 ]; do
    sleep 1
    echo -n "."
done
echo "done"

export NVIDIA_CLARA_TRTISURI="${trtis_local_uri}:8000"

# Run ${APP_NAME} container.
# Launch the app container with the following environment variables internally
# to provide input/output path information.
docker run --name ${APP_NAME} --network ${NETWORK_NAME} -it --rm \
    -v $(pwd)/input:/input \
    -v $(pwd)/output:/output \
    -v $(pwd)/logs:/logs \
    -v $(pwd)/publish:/publish \
    -e NVIDIA_CLARA_APPDIR \
    -e NVIDIA_CLARA_TRTISURI \
    -e DEBUG_VSCODE \
    -e DEBUG_VSCODE_PORT \
    -e NVIDIA_CLARA_NOSYNCLOCK=TRUE \
    ${APP_NAME}

echo "${APP_NAME} has finished."

# Stop TRTIS container
echo "Stopping TRTIS"
docker stop trtis > /dev/null

# Remove network
docker network remove ${NETWORK_NAME} > /dev/null

9.16.7.5. Step 4

Execute the script as shown below and wait for the application container to finish:

./run_prostate.sh

9.16.7.6. Step 5

Check for the following output files:

  1. Segmentation results in the output directory:

    • One file of the same name as your input file, with extension .mhd

    • One file of the same name, with extension .raw

  2. Published data in the publish directory:

    • Original volume image, in either MHD or NIfTI format

    • Segmentation volume image (<input file name only>.out.mhd and <input file name only>.out.raw)

    • Render Server config file (config_render.json)

    • Metadata file describing the above file (meta.config)

9.16.7.7. Step 6

To visualize the segmentation results, or the rendering on Clara Dashboard, please refer to sections on visualization.