Project-MONAI / monai-deploy-app-sdk

MONAI Deploy App SDK offers a framework and associated tools to design, develop and verify AI-driven applications in the healthcare imaging domain.
Apache License 2.0
94 stars 48 forks source link

[BUG] The DICOMSegmentationWriterOperator fails with a specific input DICOM Series #146

Closed MMelQin closed 2 years ago

MMelQin commented 3 years ago

Describe the bug The DICOMSegmentationWriterOperator fails with a specific input DICOM Series, when this operator is included in the UNETR segmentation sample app with a DICOM series converted from the BTCV_ts dataset, img0066.nii.gz.

Steps/Code to reproduce bug Make sure the input image, img0066.nii.gz in the BTCV dataset is available and converted to DICOM as input. Run the UNETR sample app, ai_unetr_seg_app, with the aforementioned DICOM series as input. The DICOM segmentation writing fails due to a failure in reshaping the seg mask binary array in a [x, 8] array, even though the shape is compatible, and the seg writer succeeds with larger seg mask array from other test inputs, e.g. the CT series used in the Spleen seg example. Note; the BTCV dataset is not publicly available.

The exception details are included below:

Executing operator DICOMSegmentationWriterOperator (Process ID: 1, Operator ID: b6b2e4ca-c12a-42a2-8c44-ca829cc94362)
[2021-09-21 23:18:35,351] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Number of DICOM instance datasets in the list: 313
[2021-09-21 23:18:35,351] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Number of slices in the numpy image: 313
[2021-09-21 23:18:35,351] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Labels of the segments: ['spleen', 'rkid', 'lkid', 'gall', 'eso', 'liver', 'sto', 'aorta', 'IVC', 'veins', 'pancreas', 'rad', 'lad']
[2021-09-21 23:18:36,436] [INFO] (monai.deploy.operators.dicom_seg_writer_operator.DICOMSegWriter) - Unique values in seg image: [ 0  1  2  3  4  5  6  7  8  9 10 11 12 13]
DICOMSeg creation failed. Error:
cannot reshape array of size 292257108 into shape (8)
Traceback (most recent call last):
  File "/opt/monai/app/app.py", line 90, in <module>
    app_instance.run()
  File "/opt/monai/app/app.py", line 36, in run
    super().run()
  File "/home/monai/.local/lib/python3.8/site-packages/monai/deploy/core/application.py", line 429, in run
    executor_obj.run()
  File "/home/monai/.local/lib/python3.8/site-packages/monai/deploy/core/executors/single_process_executor.py", line 125, in run
    op.compute(op_exec_context.input_context, op_exec_context.output_context, op_exec_context)
  File "/home/monai/.local/lib/python3.8/site-packages/monai/deploy/operators/dicom_seg_writer_operator.py", line 116, in compute
    self.create_dicom_seg(seg_image_numpy, dicom_series, output_path)
  File "/home/monai/.local/lib/python3.8/site-packages/monai/deploy/operators/dicom_seg_writer_operator.py", line 124, in create_dicom_seg
    self._seg_writer.write(image, dicom_dataset_list, file_path, self._seg_labels)
  File "/home/monai/.local/lib/python3.8/site-packages/monai/deploy/operators/dicom_seg_writer_operator.py", line 248, in write
    segslice_from_mhd(dcm_out, seg_img, input_ds, len(seg_labels))
  File "/home/monai/.local/lib/python3.8/site-packages/monai/deploy/operators/dicom_seg_writer_operator.py", line 587, in segslice_from_mhd
    dcm_output.PixelData = np.packbits(np.flip(np.reshape(out_pixels.astype(np.bool), (-1, 8)), 1)).tostring()
  File "<__array_function__ internals>", line 5, in reshape
  File "/home/monai/.local/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 298, in reshape
    return _wrapfunc(a, 'reshape', newshape, order=order)
  File "/home/monai/.local/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 57, in _wrapfunc
    return bound(*args, **kwds)
ValueError: cannot reshape array of size 292257108 into shape (8)

ERROR: MONAI Application "unetr_seg_app:0.0.1" failed.

Expected behavior It is expected that the DICOM Seg write to succeed, as failure on reshape array of size 292257108 into shape (8) cannot be explained, and is unexpeced. Environment details (please complete the following information)

Additional context Running the app.py directly also fails with the same error for the same input.

MMelQin commented 2 years ago

The UNETR has 13 labels, while the DICOM Seg Writer can only support upto 8. Updated the example UNETR App to stop using the DICOM Seg Writer, and save surface mesh in STL.