Skip to content

Correct the place to save input image file as well as correct application class name #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/apps/ai_livertumor_seg_app/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from app import AIUnetrSegApp
from app import AILiverTumorApp

if __name__ == "__main__":
AIUnetrSegApp(do_run=True)
AILiverTumorApp(do_run=True)
10 changes: 5 additions & 5 deletions examples/apps/ai_livertumor_seg_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
@resource(cpu=1, gpu=1, memory="7Gi")
# pip_packages can be a string that is a path(str) to requirements.txt file or a list of packages.
# The MONAI pkg is not required by this class, instead by the included operators.
class AIUnetrSegApp(Application):
class AILiverTumorApp(Application):
def __init__(self, *args, **kwargs):
"""Creates an application instance."""

Expand Down Expand Up @@ -63,14 +63,14 @@ def compose(self):
self.add_flow(study_loader_op, series_selector_op, {"dicom_study_list": "dicom_study_list"})
self.add_flow(series_selector_op, series_to_vol_op, {"dicom_series": "dicom_series"})
self.add_flow(series_to_vol_op, unetr_seg_op, {"image": "image"})
# Add the publishing operator to save the input and seg images for Render Server.
# Note the PublisherOperator has temp impl till a proper rendering module is created.
self.add_flow(unetr_seg_op, publisher_op, {"saved_images_folder": "saved_images_folder"})
# Note below the dicom_seg_writer requires two inputs, each coming from a upstream operator.
# Also note that the DICOMSegmentationWriterOperator may throw exception with some inputs.
# Bug has been created to track the issue.
self.add_flow(series_selector_op, dicom_seg_writer, {"dicom_series": "dicom_series"})
self.add_flow(unetr_seg_op, dicom_seg_writer, {"seg_image": "seg_image"})
# Add the publishing operator to save the input and seg images for Render Server.
# Note the PublisherOperator has temp impl till a proper rendering module is created.
self.add_flow(unetr_seg_op, publisher_op, {"saved_images_folder": "saved_images_folder"})

self._logger.debug(f"End {self.compose.__name__}")

Expand All @@ -84,5 +84,5 @@ def compose(self):
# python3 app.py -i input -m model/model.ts
#
logging.basicConfig(level=logging.DEBUG)
app_instance = AIUnetrSegApp() # Optional params' defaults are fine.
app_instance = AILiverTumorApp() # Optional params' defaults are fine.
app_instance.run()
22 changes: 13 additions & 9 deletions examples/apps/ai_livertumor_seg_app/livertumor_seg_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ def compute(self, op_input: InputContext, op_output: OutputContext, context: Exe

# This operator gets an in-memory Image object, so a specialized ImageReader is needed.
_reader = InMemImageReader(input_image)
pre_transforms = self.pre_process(_reader)
# In this example, the input image, once loaded at the beginning of the pre-transforms, is
# saved on disk, so is the segmentation prediction image at the end of the post-transform.
# They are both saved in the same subfolder of the application output folder, with names
# distinguished by postfix. They can also be save in different subfolder if need be.
# These images files can then be packaged for rendering.
pre_transforms = self.pre_process(_reader, op_output_folder_path)
post_transforms = self.post_process(pre_transforms, op_output_folder_path)

# Delegates inference and saving output to the built-in operator.
Expand All @@ -98,14 +103,20 @@ def compute(self, op_input: InputContext, op_output: OutputContext, context: Exe
# Now let the built-in operator handles the work with the I/O spec and execution context.
infer_operator.compute(op_input, op_output, context)

def pre_process(self, img_reader) -> Compose:
def pre_process(self, img_reader, out_dir: str = "./input_images") -> Compose:
"""Composes transforms for preprocessing input before predicting on a model."""

my_key = self._input_dataset_key
return Compose(
[
LoadImaged(keys=my_key, reader=img_reader),
EnsureChannelFirstd(keys=my_key),
SaveImaged(
keys=my_key,
output_dir=out_dir,
output_postfix="",
resample=False,
),
Spacingd(keys=my_key, pixdim=(1.0, 1.0, 1.0), mode=("bilinear"), align_corners=True),
ScaleIntensityRanged(my_key, a_min=-21, a_max=189, b_min=0.0, b_max=1.0, clip=True),
CropForegroundd(my_key, source_key=my_key),
Expand All @@ -125,12 +136,5 @@ def post_process(self, pre_transforms: Compose, out_dir: str = "./prediction_out
keys=pred_key, transform=pre_transforms, orig_keys=self._input_dataset_key, nearest_interp=True
),
SaveImaged(keys=pred_key, output_dir=out_dir, output_postfix="seg", output_dtype=uint8, resample=False),
SaveImaged(
keys=self._input_dataset_key,
output_dir=out_dir,
output_postfix="",
output_dtype=uint8,
resample=False,
),
]
)
6 changes: 3 additions & 3 deletions examples/apps/ai_unetr_seg_app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ def compose(self):
self.add_flow(study_loader_op, series_selector_op, {"dicom_study_list": "dicom_study_list"})
self.add_flow(series_selector_op, series_to_vol_op, {"dicom_series": "dicom_series"})
self.add_flow(series_to_vol_op, unetr_seg_op, {"image": "image"})
# Add the publishing operator to save the input and seg images for Render Server.
# Note the PublisherOperator has temp impl till a proper rendering module is created.
self.add_flow(unetr_seg_op, publisher_op, {"saved_images_folder": "saved_images_folder"})
# Note below the dicom_seg_writer requires two inputs, each coming from a upstream operator.
# Also note that the DICOMSegmentationWriterOperator may throw exception with some inputs.
# Bug has been created to track the issue.
self.add_flow(series_selector_op, dicom_seg_writer, {"dicom_series": "dicom_series"})
self.add_flow(unetr_seg_op, dicom_seg_writer, {"seg_image": "seg_image"})
# Add the publishing operator to save the input and seg images for Render Server.
# Note the PublisherOperator has temp impl till a proper rendering module is created.
self.add_flow(unetr_seg_op, publisher_op, {"saved_images_folder": "saved_images_folder"})

self._logger.debug(f"End {self.compose.__name__}")

Expand Down
22 changes: 13 additions & 9 deletions examples/apps/ai_unetr_seg_app/unetr_seg_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ def compute(self, op_input: InputContext, op_output: OutputContext, context: Exe

# This operator gets an in-memory Image object, so a specialized ImageReader is needed.
_reader = InMemImageReader(input_image)
pre_transforms = self.pre_process(_reader)
# In this example, the input image, once loaded at the beginning of the pre-transforms, is
# saved on disk, so is the segmentation prediction image at the end of the post-transform.
# They are both saved in the same subfolder of the application output folder, with names
# distinguished by postfix. They can also be save in different subfolder if need be.
# These images files can then be packaged for rendering.
pre_transforms = self.pre_process(_reader, op_output_folder_path)
post_transforms = self.post_process(pre_transforms, op_output_folder_path)

# Delegates inference and saving output to the built-in operator.
Expand All @@ -91,14 +96,20 @@ def compute(self, op_input: InputContext, op_output: OutputContext, context: Exe
# Now let the built-in operator handles the work with the I/O spec and execution context.
infer_operator.compute(op_input, op_output, context)

def pre_process(self, img_reader) -> Compose:
def pre_process(self, img_reader, out_dir: str = "./input_images") -> Compose:
"""Composes transforms for preprocessing input before predicting on a model."""

my_key = self._input_dataset_key
return Compose(
[
LoadImaged(keys=my_key, reader=img_reader),
AddChanneld(keys=my_key),
SaveImaged(
keys=my_key,
output_dir=out_dir,
output_postfix="",
resample=False,
),
Spacingd(keys=my_key, pixdim=(1.5, 1.5, 2.0), mode=("bilinear")),
Orientationd(keys=my_key, axcodes="RAS"),
ScaleIntensityRanged(my_key, a_min=-175, a_max=250, b_min=0.0, b_max=1.0, clip=True),
Expand All @@ -119,12 +130,5 @@ def post_process(self, pre_transforms: Compose, out_dir: str = "./prediction_out
keys=pred_key, transform=pre_transforms, orig_keys=self._input_dataset_key, nearest_interp=True
),
SaveImaged(keys=pred_key, output_dir=out_dir, output_postfix="seg", output_dtype=uint8, resample=False),
SaveImaged(
keys=self._input_dataset_key,
output_dir=out_dir,
output_postfix="",
output_dtype=uint8,
resample=False,
),
]
)