From 34570fab8ebac95e5d379ab564422e3d74238e94 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Wed, 16 Sep 2020 10:07:28 +0800 Subject: [PATCH 1/3] [DLMED] add DICOM format in the tutorial Signed-off-by: Nic Ma --- load_medical_images.ipynb | 83 +++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 20 deletions(-) diff --git a/load_medical_images.ipynb b/load_medical_images.ipynb index 2db2667b7a..52828af1da 100644 --- a/load_medical_images.ipynb +++ b/load_medical_images.ipynb @@ -75,17 +75,20 @@ "name": "stdout", "output_type": "stream", "text": [ - "MONAI version: 0.2.0\n", - "Python version: 3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 19:07:31) [GCC 7.3.0]\n", - "Numpy version: 1.18.1\n", - "Pytorch version: 1.6.0\n", + "MONAI version: 0.2.0+165.g72df264\n", + "Python version: 3.6.10 |Anaconda, Inc.| (default, May 8 2020, 02:54:21) [GCC 7.3.0]\n", + "Numpy version: 1.19.1\n", + "Pytorch version: 1.7.0a0+8deb4fe\n", "\n", "Optional dependencies:\n", - "Pytorch Ignite version: NOT INSTALLED or UNKNOWN VERSION.\n", + "Pytorch Ignite version: 0.3.0\n", "Nibabel version: 3.1.1\n", "scikit-image version: 0.15.0\n", "Pillow version: 7.2.0\n", - "Tensorboard version: 2.1.0\n", + "Tensorboard version: 1.15.0+nv\n", + "gdown version: 3.12.2\n", + "TorchVision version: 0.8.0a0\n", + "ITK version: 5.1.1\n", "\n", "For details about installing the optional dependencies, please visit:\n", " https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies\n", @@ -140,7 +143,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 2, "metadata": { "tags": [] }, @@ -158,7 +161,7 @@ " [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", " [0., 0., 1., 0.],\n", - " [0., 0., 0., 1.]]), 'spatial_shape': [128, 128, 128], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.nii.gz'}\n" + " [0., 0., 0., 1.]]), 'spatial_shape': [128, 128, 128], 'filename_or_obj': '/tmp/tmp8k6mrupv/test_image.nii.gz'}\n" ] } ], @@ -187,7 +190,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -203,7 +206,7 @@ " [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", " [0., 0., 1., 0.],\n", - " [0., 0., 0., 1.]]), 'spatial_shape': [128, 128, 128], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.nii.gz'}\n" + " [0., 0., 0., 1.]]), 'spatial_shape': [128, 128, 128], 'filename_or_obj': '/tmp/tmp8k6mrupv/test_image.nii.gz'}\n" ] } ], @@ -219,6 +222,46 @@ "print(f\"meta data:{meta}\")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Load 3D image in DICOM format" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "image data shape:(128, 128, 128)\n", + "meta data:{'0008|0016': '1.2.840.10008.5.1.4.1.1.7.2', '0008|0018': '1.2.826.0.1.3680043.2.1125.1.67521977900196267350223751193327363', '0008|0020': '20200916', '0008|0030': '020830.308835 ', '0008|0050': '', '0008|0060': 'OT', '0008|0090': '', '0010|0010': '', '0010|0020': '', '0010|0030': '', '0010|0040': '', '0020|000d': '1.2.826.0.1.3680043.2.1125.1.14047733743954145972234965692640011', '0020|000e': '1.2.826.0.1.3680043.2.1125.1.69821186495165346685618438462015632', '0020|0010': '', '0020|0011': '', '0020|0013': '', '0020|0052': '1.2.826.0.1.3680043.2.1125.1.59609996444569026233113858995717616', '0028|0002': '1', '0028|0004': 'MONOCHROME2 ', '0028|0008': '128 ', '0028|0009': '(5200,9230)', '0028|0010': '128', '0028|0011': '128', '0028|0100': '8', '0028|0101': '8', '0028|0102': '7', '0028|0103': '0', '0028|1052': '0 ', '0028|1053': '1 ', '0028|1054': 'US', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n", + " [0., 1., 0.],\n", + " [0., 0., 1.]]), 'original_affine': array([[1., 0., 0., 0.],\n", + " [0., 1., 0., 0.],\n", + " [0., 0., 1., 0.],\n", + " [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n", + " [0., 1., 0., 0.],\n", + " [0., 0., 1., 0.],\n", + " [0., 0., 0., 1.]]), 'spatial_shape': [128, 128, 128], 'filename_or_obj': '/tmp/tmp8k6mrupv/test_image.dcm'}\n" + ] + } + ], + "source": [ + "filename = os.path.join(tempdir, \"test_image.dcm\")\n", + "dcm_image = np.random.randint(256, size=(128, 128, 128)).astype(np.uint8())\n", + "itk_np_view = itk.image_view_from_array(dcm_image)\n", + "itk.imwrite(itk_np_view, filename)\n", + "data, meta = LoadImage()(filename)\n", + "\n", + "print(f\"image data shape:{data.shape}\")\n", + "print(f\"meta data:{meta}\")" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -228,7 +271,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 5, "metadata": { "tags": [] }, @@ -243,7 +286,7 @@ " [0., 1., 0.],\n", " [0., 0., 1.]]), 'affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", - " [0., 0., 1.]]), 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.png'}\n" + " [0., 0., 1.]]), 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmp8k6mrupv/test_image.png'}\n" ] } ], @@ -272,7 +315,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -285,13 +328,13 @@ " [0., 1., 0.],\n", " [0., 0., 1.]]), 'affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", - " [0., 0., 1.]]), 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.png'}\n" + " [0., 0., 1.]]), 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmp8k6mrupv/test_image.png'}\n" ] } ], "source": [ "loader = LoadImage()\n", - "loader.register(ITKReader(c_order_axis_indexing=True))\n", + "loader.register(ITKReader())\n", "data, meta = loader(filename)\n", "\n", "print(f\"image data shape:{data.shape}\")\n", @@ -310,7 +353,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -318,7 +361,7 @@ "output_type": "stream", "text": [ "image data shape:(256, 256, 2)\n", - "meta data:{'format': None, 'mode': 'LA', 'width': 256, 'height': 256, 'info': {}, 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.png'}\n" + "meta data:{'format': None, 'mode': 'LA', 'width': 256, 'height': 256, 'info': {}, 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmp8k6mrupv/test_image.png'}\n" ] } ], @@ -340,7 +383,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 9, "metadata": { "tags": [] }, @@ -355,7 +398,7 @@ " [0., 1., 0.],\n", " [0., 0., 1.]]), 'affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", - " [0., 0., 1.]]), 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmpg4lwxckh/test_image.png'}\n" + " [0., 0., 1.]]), 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmp8k6mrupv/test_image.png'}\n" ] } ], @@ -383,7 +426,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ From 20186d89019d8e6aa0c28321ab569cbe12decb7a Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Wed, 16 Sep 2020 23:33:27 +0800 Subject: [PATCH 2/3] [DLMED] update according to comments Signed-off-by: Nic Ma --- load_medical_images.ipynb | 98 ++++++++++++++++++++++++++++----------- 1 file changed, 71 insertions(+), 27 deletions(-) diff --git a/load_medical_images.ipynb b/load_medical_images.ipynb index 52828af1da..6df3781d92 100644 --- a/load_medical_images.ipynb +++ b/load_medical_images.ipynb @@ -75,7 +75,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "MONAI version: 0.2.0+165.g72df264\n", + "MONAI version: 0.1.0+397.g9e476ca.dirty\n", "Python version: 3.6.10 |Anaconda, Inc.| (default, May 8 2020, 02:54:21) [GCC 7.3.0]\n", "Numpy version: 1.19.1\n", "Pytorch version: 1.7.0a0+8deb4fe\n", @@ -136,7 +136,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Load image with default image reader\n", + "## Load Nifti image with default image reader\n", "MONAI leverages `ITK` as the default image reader, it can support most of the common medical image formats.\n", "More details, please check: https://github.com/InsightSoftwareConsortium/ITK/tree/master/Modules/IO" ] @@ -152,8 +152,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "image data shape:(128, 128, 128)\n", - "meta data:{'ITK_FileNotes': '', 'aux_file': '', 'bitpix': '64', 'cal_max': '0', 'cal_min': '0', 'datatype': '64', 'descrip': '', 'dim[0]': '3', 'dim[1]': '128', 'dim[2]': '128', 'dim[3]': '128', 'dim[4]': '1', 'dim[5]': '1', 'dim[6]': '1', 'dim[7]': '1', 'dim_info': '0', 'intent_code': '0', 'intent_name': '', 'intent_p1': '0', 'intent_p2': '0', 'intent_p3': '0', 'nifti_type': '1', 'pixdim[0]': '0', 'pixdim[1]': '1', 'pixdim[2]': '1', 'pixdim[3]': '1', 'pixdim[4]': '0', 'pixdim[5]': '0', 'pixdim[6]': '0', 'pixdim[7]': '0', 'qform_code': '1', 'qform_code_name': 'NIFTI_XFORM_SCANNER_ANAT', 'qoffset_x': '-0', 'qoffset_y': '-0', 'qoffset_z': '0', 'quatern_b': '0', 'quatern_c': '0', 'quatern_d': '1', 'scl_inter': '0', 'scl_slope': '1', 'sform_code': '0', 'sform_code_name': 'NIFTI_XFORM_UNKNOWN', 'slice_code': '0', 'slice_duration': '0', 'slice_end': '0', 'slice_start': '0', 'srow_x': '0 0 0 0', 'srow_y': '0 0 0 0', 'srow_z': '0 0 0 0', 'toffset': '0', 'vox_offset': '352', 'xyzt_units': '2', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n", + "image data shape:(64, 128, 96)\n", + "meta data:{'ITK_FileNotes': '', 'aux_file': '', 'bitpix': '64', 'cal_max': '0', 'cal_min': '0', 'datatype': '64', 'descrip': '', 'dim[0]': '3', 'dim[1]': '96', 'dim[2]': '128', 'dim[3]': '64', 'dim[4]': '1', 'dim[5]': '1', 'dim[6]': '1', 'dim[7]': '1', 'dim_info': '0', 'intent_code': '0', 'intent_name': '', 'intent_p1': '0', 'intent_p2': '0', 'intent_p3': '0', 'nifti_type': '1', 'pixdim[0]': '0', 'pixdim[1]': '1', 'pixdim[2]': '1', 'pixdim[3]': '1', 'pixdim[4]': '0', 'pixdim[5]': '0', 'pixdim[6]': '0', 'pixdim[7]': '0', 'qform_code': '1', 'qform_code_name': 'NIFTI_XFORM_SCANNER_ANAT', 'qoffset_x': '-0', 'qoffset_y': '-0', 'qoffset_z': '0', 'quatern_b': '0', 'quatern_c': '0', 'quatern_d': '1', 'scl_inter': '0', 'scl_slope': '1', 'sform_code': '0', 'sform_code_name': 'NIFTI_XFORM_UNKNOWN', 'slice_code': '0', 'slice_duration': '0', 'slice_end': '0', 'slice_start': '0', 'srow_x': '0 0 0 0', 'srow_y': '0 0 0 0', 'srow_z': '0 0 0 0', 'toffset': '0', 'vox_offset': '352', 'xyzt_units': '2', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", " [0., 0., 1.]]), 'original_affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", @@ -161,14 +161,14 @@ " [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", " [0., 0., 1., 0.],\n", - " [0., 0., 0., 1.]]), 'spatial_shape': [128, 128, 128], 'filename_or_obj': '/tmp/tmp8k6mrupv/test_image.nii.gz'}\n" + " [0., 0., 0., 1.]]), 'spatial_shape': [96, 128, 64], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.nii.gz'}\n" ] } ], "source": [ "# generate 3D test images\n", "tempdir = tempfile.mkdtemp()\n", - "test_image = np.random.rand(128, 128, 128)\n", + "test_image = np.random.rand(64, 128, 96)\n", "filename = os.path.join(tempdir, \"test_image.nii.gz\")\n", "itk_np_view = itk.image_view_from_array(test_image)\n", "itk.imwrite(itk_np_view, filename)\n", @@ -182,7 +182,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Load a list of images and stack as 1 training item\n", + "## Load a list of Nifti images and stack as 1 training item\n", "Loading a list of files, stack them together and add a new dimension as first dimension.\n", "\n", "And use the meta data of the first image to represent the stacked result." @@ -197,8 +197,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "image data shape:(3, 128, 128, 128)\n", - "meta data:{'ITK_FileNotes': '', 'aux_file': '', 'bitpix': '64', 'cal_max': '0', 'cal_min': '0', 'datatype': '64', 'descrip': '', 'dim[0]': '3', 'dim[1]': '128', 'dim[2]': '128', 'dim[3]': '128', 'dim[4]': '1', 'dim[5]': '1', 'dim[6]': '1', 'dim[7]': '1', 'dim_info': '0', 'intent_code': '0', 'intent_name': '', 'intent_p1': '0', 'intent_p2': '0', 'intent_p3': '0', 'nifti_type': '1', 'pixdim[0]': '0', 'pixdim[1]': '1', 'pixdim[2]': '1', 'pixdim[3]': '1', 'pixdim[4]': '0', 'pixdim[5]': '0', 'pixdim[6]': '0', 'pixdim[7]': '0', 'qform_code': '1', 'qform_code_name': 'NIFTI_XFORM_SCANNER_ANAT', 'qoffset_x': '-0', 'qoffset_y': '-0', 'qoffset_z': '0', 'quatern_b': '0', 'quatern_c': '0', 'quatern_d': '1', 'scl_inter': '0', 'scl_slope': '1', 'sform_code': '0', 'sform_code_name': 'NIFTI_XFORM_UNKNOWN', 'slice_code': '0', 'slice_duration': '0', 'slice_end': '0', 'slice_start': '0', 'srow_x': '0 0 0 0', 'srow_y': '0 0 0 0', 'srow_z': '0 0 0 0', 'toffset': '0', 'vox_offset': '352', 'xyzt_units': '2', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n", + "image data shape:(3, 64, 128, 96)\n", + "meta data:{'ITK_FileNotes': '', 'aux_file': '', 'bitpix': '64', 'cal_max': '0', 'cal_min': '0', 'datatype': '64', 'descrip': '', 'dim[0]': '3', 'dim[1]': '96', 'dim[2]': '128', 'dim[3]': '64', 'dim[4]': '1', 'dim[5]': '1', 'dim[6]': '1', 'dim[7]': '1', 'dim_info': '0', 'intent_code': '0', 'intent_name': '', 'intent_p1': '0', 'intent_p2': '0', 'intent_p3': '0', 'nifti_type': '1', 'pixdim[0]': '0', 'pixdim[1]': '1', 'pixdim[2]': '1', 'pixdim[3]': '1', 'pixdim[4]': '0', 'pixdim[5]': '0', 'pixdim[6]': '0', 'pixdim[7]': '0', 'qform_code': '1', 'qform_code_name': 'NIFTI_XFORM_SCANNER_ANAT', 'qoffset_x': '-0', 'qoffset_y': '-0', 'qoffset_z': '0', 'quatern_b': '0', 'quatern_c': '0', 'quatern_d': '1', 'scl_inter': '0', 'scl_slope': '1', 'sform_code': '0', 'sform_code_name': 'NIFTI_XFORM_UNKNOWN', 'slice_code': '0', 'slice_duration': '0', 'slice_end': '0', 'slice_start': '0', 'srow_x': '0 0 0 0', 'srow_y': '0 0 0 0', 'srow_z': '0 0 0 0', 'toffset': '0', 'vox_offset': '352', 'xyzt_units': '2', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", " [0., 0., 1.]]), 'original_affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", @@ -206,7 +206,7 @@ " [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", " [0., 0., 1., 0.],\n", - " [0., 0., 0., 1.]]), 'spatial_shape': [128, 128, 128], 'filename_or_obj': '/tmp/tmp8k6mrupv/test_image.nii.gz'}\n" + " [0., 0., 0., 1.]]), 'spatial_shape': [96, 128, 64], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.nii.gz'}\n" ] } ], @@ -238,8 +238,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "image data shape:(128, 128, 128)\n", - "meta data:{'0008|0016': '1.2.840.10008.5.1.4.1.1.7.2', '0008|0018': '1.2.826.0.1.3680043.2.1125.1.67521977900196267350223751193327363', '0008|0020': '20200916', '0008|0030': '020830.308835 ', '0008|0050': '', '0008|0060': 'OT', '0008|0090': '', '0010|0010': '', '0010|0020': '', '0010|0030': '', '0010|0040': '', '0020|000d': '1.2.826.0.1.3680043.2.1125.1.14047733743954145972234965692640011', '0020|000e': '1.2.826.0.1.3680043.2.1125.1.69821186495165346685618438462015632', '0020|0010': '', '0020|0011': '', '0020|0013': '', '0020|0052': '1.2.826.0.1.3680043.2.1125.1.59609996444569026233113858995717616', '0028|0002': '1', '0028|0004': 'MONOCHROME2 ', '0028|0008': '128 ', '0028|0009': '(5200,9230)', '0028|0010': '128', '0028|0011': '128', '0028|0100': '8', '0028|0101': '8', '0028|0102': '7', '0028|0103': '0', '0028|1052': '0 ', '0028|1053': '1 ', '0028|1054': 'US', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n", + "image data shape:(64, 128, 96)\n", + "meta data:{'0008|0016': '1.2.840.10008.5.1.4.1.1.7.2', '0008|0018': '1.2.826.0.1.3680043.2.1125.1.80122288036627477213837978683890917', '0008|0020': '20200916', '0008|0030': '144614.579926 ', '0008|0050': '', '0008|0060': 'OT', '0008|0090': '', '0010|0010': '', '0010|0020': '', '0010|0030': '', '0010|0040': '', '0020|000d': '1.2.826.0.1.3680043.2.1125.1.86756564143559857384328393610373279', '0020|000e': '1.2.826.0.1.3680043.2.1125.1.18075541727410629229137138485058946', '0020|0010': '', '0020|0011': '', '0020|0013': '', '0020|0052': '1.2.826.0.1.3680043.2.1125.1.35779275716963140613846603007887561', '0028|0002': '1', '0028|0004': 'MONOCHROME2 ', '0028|0008': '64', '0028|0009': '(5200,9230)', '0028|0010': '128', '0028|0011': '96', '0028|0100': '8', '0028|0101': '8', '0028|0102': '7', '0028|0103': '0', '0028|1052': '0 ', '0028|1053': '1 ', '0028|1054': 'US', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", " [0., 0., 1.]]), 'original_affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", @@ -247,13 +247,13 @@ " [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", " [0., 0., 1., 0.],\n", - " [0., 0., 0., 1.]]), 'spatial_shape': [128, 128, 128], 'filename_or_obj': '/tmp/tmp8k6mrupv/test_image.dcm'}\n" + " [0., 0., 0., 1.]]), 'spatial_shape': [96, 128, 64], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.dcm'}\n" ] } ], "source": [ "filename = os.path.join(tempdir, \"test_image.dcm\")\n", - "dcm_image = np.random.randint(256, size=(128, 128, 128)).astype(np.uint8())\n", + "dcm_image = np.random.randint(256, size=(64, 128, 96)).astype(np.uint8())\n", "itk_np_view = itk.image_view_from_array(dcm_image)\n", "itk.imwrite(itk_np_view, filename)\n", "data, meta = LoadImage()(filename)\n", @@ -266,12 +266,56 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## Load 2D image in PNG format" + "## Load a list of DICOM images and stack as 1 training item\n", + "Loading a list of files, stack them together and add a new dimension as first dimension.\n", + "\n", + "And use the meta data of the first image to represent the stacked result." ] }, { "cell_type": "code", "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "image data shape:(3, 64, 128, 96)\n", + "meta data:{'0008|0016': '1.2.840.10008.5.1.4.1.1.7.2', '0008|0018': '1.2.826.0.1.3680043.2.1125.1.94931769078714184976009575030415682', '0008|0020': '20200916', '0008|0030': '144617.169601 ', '0008|0050': '', '0008|0060': 'OT', '0008|0090': '', '0010|0010': '', '0010|0020': '', '0010|0030': '', '0010|0040': '', '0020|000d': '1.2.826.0.1.3680043.2.1125.1.56532469368370024412556481105546590', '0020|000e': '1.2.826.0.1.3680043.2.1125.1.73480589435790155521906873355216294', '0020|0010': '', '0020|0011': '', '0020|0013': '', '0020|0052': '1.2.826.0.1.3680043.2.1125.1.50900872491841559297805988659007312', '0028|0002': '1', '0028|0004': 'MONOCHROME2 ', '0028|0008': '64', '0028|0009': '(5200,9230)', '0028|0010': '128', '0028|0011': '96', '0028|0100': '8', '0028|0101': '8', '0028|0102': '7', '0028|0103': '0', '0028|1052': '0 ', '0028|1053': '1 ', '0028|1054': 'US', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n", + " [0., 1., 0.],\n", + " [0., 0., 1.]]), 'original_affine': array([[1., 0., 0., 0.],\n", + " [0., 1., 0., 0.],\n", + " [0., 0., 1., 0.],\n", + " [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n", + " [0., 1., 0., 0.],\n", + " [0., 0., 1., 0.],\n", + " [0., 0., 0., 1.]]), 'spatial_shape': [96, 128, 64], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.dcm'}\n" + ] + } + ], + "source": [ + "filenames = [\"test_image.dcm\", \"test_image2.dcm\", \"test_image3.dcm\"]\n", + "for i, name in enumerate(filenames):\n", + " filenames[i] = os.path.join(tempdir, name)\n", + " itk_np_view = itk.image_view_from_array(dcm_image)\n", + " itk.imwrite(itk_np_view, filenames[i])\n", + "data, meta = LoadImage()(filenames)\n", + "\n", + "print(f\"image data shape:{data.shape}\")\n", + "print(f\"meta data:{meta}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Load 2D image in PNG format" + ] + }, + { + "cell_type": "code", + "execution_count": 15, "metadata": { "tags": [] }, @@ -280,18 +324,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "image data shape:(256, 256)\n", + "image data shape:(128, 256)\n", "meta data:{'origin': array([0., 0.]), 'spacing': array([1., 1.]), 'direction': array([[1., 0.],\n", " [0., 1.]]), 'original_affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", " [0., 0., 1.]]), 'affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", - " [0., 0., 1.]]), 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmp8k6mrupv/test_image.png'}\n" + " [0., 0., 1.]]), 'spatial_shape': [256, 128], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.png'}\n" ] } ], "source": [ - "test_image = np.random.randint(0, 256, size=[256, 256])\n", + "test_image = np.random.randint(0, 256, size=[128, 256])\n", "filename = os.path.join(tempdir, \"test_image.png\")\n", "Image.fromarray(test_image.astype(\"uint8\")).save(filename)\n", "data, meta = LoadImage()(filename)\n", @@ -315,20 +359,20 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "image data shape:(256, 256)\n", + "image data shape:(128, 256)\n", "meta data:{'origin': array([0., 0.]), 'spacing': array([1., 1.]), 'direction': array([[1., 0.],\n", " [0., 1.]]), 'original_affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", " [0., 0., 1.]]), 'affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", - " [0., 0., 1.]]), 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmp8k6mrupv/test_image.png'}\n" + " [0., 0., 1.]]), 'spatial_shape': [256, 128], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.png'}\n" ] } ], @@ -353,15 +397,15 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "image data shape:(256, 256, 2)\n", - "meta data:{'format': None, 'mode': 'LA', 'width': 256, 'height': 256, 'info': {}, 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmp8k6mrupv/test_image.png'}\n" + "image data shape:(128, 256, 2)\n", + "meta data:{'format': None, 'mode': 'LA', 'width': 256, 'height': 128, 'info': {}, 'spatial_shape': [256, 128], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.png'}\n" ] } ], @@ -383,7 +427,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 18, "metadata": { "tags": [] }, @@ -398,7 +442,7 @@ " [0., 1., 0.],\n", " [0., 0., 1.]]), 'affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", - " [0., 0., 1.]]), 'spatial_shape': [256, 256], 'filename_or_obj': '/tmp/tmp8k6mrupv/test_image.png'}\n" + " [0., 0., 1.]]), 'spatial_shape': [256, 128], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.png'}\n" ] } ], @@ -426,7 +470,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 19, "metadata": {}, "outputs": [], "source": [ From 09651473194a31d3b6125b59136ebdf3ce9e80d7 Mon Sep 17 00:00:00 2001 From: Nic Ma Date: Thu, 17 Sep 2020 09:30:45 +0800 Subject: [PATCH 3/3] [DLMED] update ITK shape Signed-off-by: Nic Ma --- load_medical_images.ipynb | 59 +++++++++++---------------------------- 1 file changed, 17 insertions(+), 42 deletions(-) diff --git a/load_medical_images.ipynb b/load_medical_images.ipynb index 6df3781d92..f0af97b1b0 100644 --- a/load_medical_images.ipynb +++ b/load_medical_images.ipynb @@ -66,36 +66,11 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "MONAI version: 0.1.0+397.g9e476ca.dirty\n", - "Python version: 3.6.10 |Anaconda, Inc.| (default, May 8 2020, 02:54:21) [GCC 7.3.0]\n", - "Numpy version: 1.19.1\n", - "Pytorch version: 1.7.0a0+8deb4fe\n", - "\n", - "Optional dependencies:\n", - "Pytorch Ignite version: 0.3.0\n", - "Nibabel version: 3.1.1\n", - "scikit-image version: 0.15.0\n", - "Pillow version: 7.2.0\n", - "Tensorboard version: 1.15.0+nv\n", - "gdown version: 3.12.2\n", - "TorchVision version: 0.8.0a0\n", - "ITK version: 5.1.1\n", - "\n", - "For details about installing the optional dependencies, please visit:\n", - " https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies\n", - "\n" - ] - } - ], + "outputs": [], "source": [ "# Copyright 2020 MONAI Consortium\n", "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", @@ -161,7 +136,7 @@ " [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", " [0., 0., 1., 0.],\n", - " [0., 0., 0., 1.]]), 'spatial_shape': [96, 128, 64], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.nii.gz'}\n" + " [0., 0., 0., 1.]]), 'spatial_shape': [64, 128, 96], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.nii.gz'}\n" ] } ], @@ -206,7 +181,7 @@ " [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", " [0., 0., 1., 0.],\n", - " [0., 0., 0., 1.]]), 'spatial_shape': [96, 128, 64], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.nii.gz'}\n" + " [0., 0., 0., 1.]]), 'spatial_shape': [64, 128, 96], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.nii.gz'}\n" ] } ], @@ -239,7 +214,7 @@ "output_type": "stream", "text": [ "image data shape:(64, 128, 96)\n", - "meta data:{'0008|0016': '1.2.840.10008.5.1.4.1.1.7.2', '0008|0018': '1.2.826.0.1.3680043.2.1125.1.80122288036627477213837978683890917', '0008|0020': '20200916', '0008|0030': '144614.579926 ', '0008|0050': '', '0008|0060': 'OT', '0008|0090': '', '0010|0010': '', '0010|0020': '', '0010|0030': '', '0010|0040': '', '0020|000d': '1.2.826.0.1.3680043.2.1125.1.86756564143559857384328393610373279', '0020|000e': '1.2.826.0.1.3680043.2.1125.1.18075541727410629229137138485058946', '0020|0010': '', '0020|0011': '', '0020|0013': '', '0020|0052': '1.2.826.0.1.3680043.2.1125.1.35779275716963140613846603007887561', '0028|0002': '1', '0028|0004': 'MONOCHROME2 ', '0028|0008': '64', '0028|0009': '(5200,9230)', '0028|0010': '128', '0028|0011': '96', '0028|0100': '8', '0028|0101': '8', '0028|0102': '7', '0028|0103': '0', '0028|1052': '0 ', '0028|1053': '1 ', '0028|1054': 'US', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n", + "meta data:{'0008|0016': '1.2.840.10008.5.1.4.1.1.7.2', '0008|0018': '1.2.826.0.1.3680043.2.1125.1.64732409224407717340688313759116819', '0008|0020': '20200917', '0008|0030': '012951.461675 ', '0008|0050': '', '0008|0060': 'OT', '0008|0090': '', '0010|0010': '', '0010|0020': '', '0010|0030': '', '0010|0040': '', '0020|000d': '1.2.826.0.1.3680043.2.1125.1.22199909824554379284164927198357157', '0020|000e': '1.2.826.0.1.3680043.2.1125.1.64330868579047092348166172965815757', '0020|0010': '', '0020|0011': '', '0020|0013': '', '0020|0052': '1.2.826.0.1.3680043.2.1125.1.36887462277578362563842268362959985', '0028|0002': '1', '0028|0004': 'MONOCHROME2 ', '0028|0008': '64', '0028|0009': '(5200,9230)', '0028|0010': '128', '0028|0011': '96', '0028|0100': '8', '0028|0101': '8', '0028|0102': '7', '0028|0103': '0', '0028|1052': '0 ', '0028|1053': '1 ', '0028|1054': 'US', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", " [0., 0., 1.]]), 'original_affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", @@ -247,7 +222,7 @@ " [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", " [0., 0., 1., 0.],\n", - " [0., 0., 0., 1.]]), 'spatial_shape': [96, 128, 64], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.dcm'}\n" + " [0., 0., 0., 1.]]), 'spatial_shape': [64, 128, 96], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.dcm'}\n" ] } ], @@ -282,7 +257,7 @@ "output_type": "stream", "text": [ "image data shape:(3, 64, 128, 96)\n", - "meta data:{'0008|0016': '1.2.840.10008.5.1.4.1.1.7.2', '0008|0018': '1.2.826.0.1.3680043.2.1125.1.94931769078714184976009575030415682', '0008|0020': '20200916', '0008|0030': '144617.169601 ', '0008|0050': '', '0008|0060': 'OT', '0008|0090': '', '0010|0010': '', '0010|0020': '', '0010|0030': '', '0010|0040': '', '0020|000d': '1.2.826.0.1.3680043.2.1125.1.56532469368370024412556481105546590', '0020|000e': '1.2.826.0.1.3680043.2.1125.1.73480589435790155521906873355216294', '0020|0010': '', '0020|0011': '', '0020|0013': '', '0020|0052': '1.2.826.0.1.3680043.2.1125.1.50900872491841559297805988659007312', '0028|0002': '1', '0028|0004': 'MONOCHROME2 ', '0028|0008': '64', '0028|0009': '(5200,9230)', '0028|0010': '128', '0028|0011': '96', '0028|0100': '8', '0028|0101': '8', '0028|0102': '7', '0028|0103': '0', '0028|1052': '0 ', '0028|1053': '1 ', '0028|1054': 'US', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n", + "meta data:{'0008|0016': '1.2.840.10008.5.1.4.1.1.7.2', '0008|0018': '1.2.826.0.1.3680043.2.1125.1.55124182629917658983046437541692429', '0008|0020': '20200917', '0008|0030': '012953.377970 ', '0008|0050': '', '0008|0060': 'OT', '0008|0090': '', '0010|0010': '', '0010|0020': '', '0010|0030': '', '0010|0040': '', '0020|000d': '1.2.826.0.1.3680043.2.1125.1.54570928137383298712421968711546946', '0020|000e': '1.2.826.0.1.3680043.2.1125.1.79105954593572939973262028207196688', '0020|0010': '', '0020|0011': '', '0020|0013': '', '0020|0052': '1.2.826.0.1.3680043.2.1125.1.37569814707528773722727172695078933', '0028|0002': '1', '0028|0004': 'MONOCHROME2 ', '0028|0008': '64', '0028|0009': '(5200,9230)', '0028|0010': '128', '0028|0011': '96', '0028|0100': '8', '0028|0101': '8', '0028|0102': '7', '0028|0103': '0', '0028|1052': '0 ', '0028|1053': '1 ', '0028|1054': 'US', 'origin': array([0., 0., 0.]), 'spacing': array([1., 1., 1.]), 'direction': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", " [0., 0., 1.]]), 'original_affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", @@ -290,7 +265,7 @@ " [0., 0., 0., 1.]]), 'affine': array([[1., 0., 0., 0.],\n", " [0., 1., 0., 0.],\n", " [0., 0., 1., 0.],\n", - " [0., 0., 0., 1.]]), 'spatial_shape': [96, 128, 64], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.dcm'}\n" + " [0., 0., 0., 1.]]), 'spatial_shape': [64, 128, 96], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.dcm'}\n" ] } ], @@ -315,7 +290,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 6, "metadata": { "tags": [] }, @@ -330,7 +305,7 @@ " [0., 1., 0.],\n", " [0., 0., 1.]]), 'affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", - " [0., 0., 1.]]), 'spatial_shape': [256, 128], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.png'}\n" + " [0., 0., 1.]]), 'spatial_shape': [128, 256], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.png'}\n" ] } ], @@ -359,7 +334,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -372,7 +347,7 @@ " [0., 1., 0.],\n", " [0., 0., 1.]]), 'affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", - " [0., 0., 1.]]), 'spatial_shape': [256, 128], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.png'}\n" + " [0., 0., 1.]]), 'spatial_shape': [128, 256], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.png'}\n" ] } ], @@ -397,7 +372,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -405,7 +380,7 @@ "output_type": "stream", "text": [ "image data shape:(128, 256, 2)\n", - "meta data:{'format': None, 'mode': 'LA', 'width': 256, 'height': 128, 'info': {}, 'spatial_shape': [256, 128], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.png'}\n" + "meta data:{'format': None, 'mode': 'LA', 'width': 256, 'height': 128, 'info': {}, 'spatial_shape': [256, 128], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.png'}\n" ] } ], @@ -427,7 +402,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 9, "metadata": { "tags": [] }, @@ -442,7 +417,7 @@ " [0., 1., 0.],\n", " [0., 0., 1.]]), 'affine': array([[1., 0., 0.],\n", " [0., 1., 0.],\n", - " [0., 0., 1.]]), 'spatial_shape': [256, 128], 'filename_or_obj': '/tmp/tmpm35ixi48/test_image.png'}\n" + " [0., 0., 1.]]), 'spatial_shape': [128, 256], 'filename_or_obj': '/tmp/tmpq5gymhdr/test_image.png'}\n" ] } ], @@ -470,7 +445,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [