From 005278ce2d10fcecc0679b66052f831eb6e9ab4f Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Thu, 18 Aug 2022 11:16:17 +0100 Subject: [PATCH 1/2] add cupy demo Signed-off-by: Wenqi Li --- acceleration/transform_speed.ipynb | 185 ++++++++++++++++++++++++----- 1 file changed, 155 insertions(+), 30 deletions(-) diff --git a/acceleration/transform_speed.ipynb b/acceleration/transform_speed.ipynb index 7eb39c85d8..56f199da20 100644 --- a/acceleration/transform_speed.ipynb +++ b/acceleration/transform_speed.ipynb @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": { "tags": [] }, @@ -38,11 +38,45 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": { "tags": [] }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "MONAI version: 0.9.1+61.g1befe23a\n", + "Numpy version: 1.19.2\n", + "Pytorch version: 1.8.0a0+52ea372\n", + "MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False\n", + "MONAI rev id: 1befe23ae5443e64fbe627e9e5275c9a8160be73\n", + "MONAI __file__: /opt/conda/lib/python3.8/site-packages/monai/__init__.py\n", + "\n", + "Optional dependencies:\n", + "Pytorch Ignite version: 0.4.9\n", + "Nibabel version: 4.0.1\n", + "scikit-image version: 0.19.3\n", + "Pillow version: 9.2.0\n", + "Tensorboard version: 1.15.0+nv\n", + "gdown version: 4.5.1\n", + "TorchVision version: 0.9.0a0\n", + "tqdm version: 4.53.0\n", + "lmdb version: 1.1.1\n", + "psutil version: 5.8.0\n", + "pandas version: 1.1.4\n", + "einops version: 0.4.1\n", + "transformers version: 4.21.1\n", + "mlflow version: 1.28.0\n", + "pynrrd version: 0.4.3\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" + ] + } + ], "source": [ "# Copyright 2020 MONAI Consortium\n", "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", @@ -99,11 +133,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": { "tags": [] }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "/tmp/tmpvaqesd_z\n" + ] + } + ], "source": [ "directory = os.environ.get(\"MONAI_DATA_DIRECTORY\")\n", "if directory:\n", @@ -124,7 +166,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -140,7 +182,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -167,7 +209,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "torch.Size([3, 1, 256, 256, 256]) torch.Size([3, 1, 256, 256, 256])\n" + "(3, 1, 256, 256, 256) (3, 1, 256, 256, 256)\n" ] } ], @@ -194,8 +236,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 23.3 ms, sys: 133 ms, total: 156 ms\n", - "Wall time: 8.6 s\n" + "CPU times: user 26.1 ms, sys: 172 ms, total: 198 ms\n", + "Wall time: 11 s\n" ] } ], @@ -225,7 +267,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "torch.Size([3, 1, 64, 64, 64]) torch.Size([3, 1, 64, 64, 64])\n" + "(3, 1, 64, 64, 64) (3, 1, 64, 64, 64)\n" ] } ], @@ -272,8 +314,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 20.4 ms, sys: 1.07 s, total: 1.09 s\n", - "Wall time: 22.6 s\n" + "CPU times: user 37.6 ms, sys: 498 ms, total: 536 ms\n", + "Wall time: 24.6 s\n" ] } ], @@ -310,7 +352,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "torch.Size([3, 1, 64, 64, 64]) torch.Size([3, 1, 64, 64, 64])\n" + "(3, 1, 64, 64, 64) (3, 1, 64, 64, 64)\n" ] } ], @@ -325,7 +367,6 @@ " translate_range=(96, 96, 96),\n", " spatial_size=(64, 64, 64),\n", " mode=\"bilinear\",\n", - " as_tensor_output=True,\n", " device=torch.device(\"cuda:0\"),\n", ")\n", "rand_affine_seg = RandAffine(\n", @@ -334,7 +375,6 @@ " translate_range=(96, 96, 96),\n", " spatial_size=(64, 64, 64),\n", " mode=\"nearest\",\n", - " as_tensor_output=True,\n", " device=torch.device(\"cuda:0\"),\n", ")\n", "\n", @@ -365,8 +405,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 3.25 s, sys: 1.06 s, total: 4.31 s\n", - "Wall time: 4.31 s\n" + "CPU times: user 19.4 s, sys: 2.67 s, total: 22 s\n", + "Wall time: 4.83 s\n" ] } ], @@ -385,7 +425,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Tesla V100-SXM3-32GB\n", + "Tesla V100-SXM2-16GB-N\n", "|===========================================================================|\n", "| PyTorch CUDA memory summary, device ID 0 |\n", "|---------------------------------------------------------------------------|\n", @@ -393,21 +433,21 @@ "|===========================================================================|\n", "| Metric | Cur Usage | Peak Usage | Tot Alloc | Tot Freed |\n", "|---------------------------------------------------------------------------|\n", - "| Allocated memory | 12288 KB | 88064 KB | 1188 MB | 1176 MB |\n", + "| Allocated memory | 16387 KB | 24580 KB | 118883 KB | 102496 KB |\n", "|---------------------------------------------------------------------------|\n", - "| Active memory | 12288 KB | 88064 KB | 1188 MB | 1176 MB |\n", + "| Active memory | 16387 KB | 24580 KB | 118883 KB | 102496 KB |\n", "|---------------------------------------------------------------------------|\n", - "| GPU reserved memory | 159744 KB | 159744 KB | 159744 KB | 0 B |\n", + "| GPU reserved memory | 43008 KB | 43008 KB | 43008 KB | 0 B |\n", "|---------------------------------------------------------------------------|\n", - "| Non-releasable memory | 8192 KB | 77823 KB | 833 MB | 825 MB |\n", + "| Non-releasable memory | 6141 KB | 22527 KB | 274525 KB | 268384 KB |\n", "|---------------------------------------------------------------------------|\n", - "| Allocations | 4 | 12 | 208 | 204 |\n", + "| Allocations | 8 | 15 | 226 | 218 |\n", "|---------------------------------------------------------------------------|\n", - "| Active allocs | 4 | 12 | 208 | 204 |\n", + "| Active allocs | 8 | 15 | 226 | 218 |\n", "|---------------------------------------------------------------------------|\n", - "| GPU reserved segments | 7 | 7 | 7 | 0 |\n", + "| GPU reserved segments | 3 | 3 | 3 | 0 |\n", "|---------------------------------------------------------------------------|\n", - "| Non-releasable allocs | 1 | 4 | 133 | 132 |\n", + "| Non-releasable allocs | 5 | 7 | 165 | 160 |\n", "|===========================================================================|\n", "\n" ] @@ -418,6 +458,91 @@ "print(torch.cuda.memory_summary(0, abbreviated=True))" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 4. Test image-patch loading with preprocessing on GPU using the Cupy backend:\n", + "\n", + "In the cupy package is installed correctly along with MONAI, \n", + "setting the `mode` to an integer in `[0-5]` and `device` to a cuda device will enable the cupy backend resampling.\n", + "\n", + "- random rotate (256, 256, 256)-voxel in the plane axes=(1, 2)\n", + "- extract random (64, 64, 64) patches\n", + "- implemented in MONAI using the cupy backend for high-order spline interpolation" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(3, 1, 64, 64, 64) (3, 1, 64, 64, 64)\n" + ] + } + ], + "source": [ + "images = sorted(glob.glob(os.path.join(root_dir, \"im*.nii.gz\")))\n", + "segs = sorted(glob.glob(os.path.join(root_dir, \"seg*.nii.gz\")))\n", + "\n", + "# same parameter with different interpolation mode for image and segmentation\n", + "rand_affine_img = RandAffine(\n", + " prob=1.0,\n", + " rotate_range=np.pi / 4,\n", + " translate_range=(96, 96, 96),\n", + " spatial_size=(64, 64, 64),\n", + " mode=3,\n", + " padding_mode=\"reflect\",\n", + " device=torch.device(\"cuda:0\"),\n", + ")\n", + "rand_affine_seg = RandAffine(\n", + " prob=1.0,\n", + " rotate_range=np.pi / 4,\n", + " translate_range=(96, 96, 96),\n", + " spatial_size=(64, 64, 64),\n", + " mode=0,\n", + " padding_mode=\"reflect\",\n", + " device=torch.device(\"cuda:0\"),\n", + ")\n", + "\n", + "imtrans = Compose(\n", + " [LoadImage(image_only=True), ScaleIntensity(),\n", + " EnsureChannelFirst(), rand_affine_img]\n", + ")\n", + "\n", + "segtrans = Compose([LoadImage(image_only=True),\n", + " EnsureChannelFirst(), rand_affine_seg])\n", + "\n", + "ds = ArrayDataset(images, imtrans, segs, segtrans)\n", + "loader = torch.utils.data.DataLoader(ds, batch_size=3, num_workers=0)\n", + "\n", + "im, seg = first(loader)\n", + "\n", + "print(im.shape, seg.shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CPU times: user 15.4 s, sys: 3.15 s, total: 18.5 s\n", + "Wall time: 7.7 s\n" + ] + } + ], + "source": [ + "%time data = next(iter(loader))" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -429,7 +554,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -440,7 +565,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -454,7 +579,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.8.5" } }, "nbformat": 4, From bc072d58bc295c18eabbf311b8f1aa527aa12fce Mon Sep 17 00:00:00 2001 From: Wenqi Li Date: Thu, 18 Aug 2022 17:45:35 +0100 Subject: [PATCH 2/2] remove print config Signed-off-by: Wenqi Li --- acceleration/transform_speed.ipynb | 42 +++--------------------------- 1 file changed, 4 insertions(+), 38 deletions(-) diff --git a/acceleration/transform_speed.ipynb b/acceleration/transform_speed.ipynb index 56f199da20..45aa99dc7e 100644 --- a/acceleration/transform_speed.ipynb +++ b/acceleration/transform_speed.ipynb @@ -38,45 +38,11 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": { "tags": [] }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "MONAI version: 0.9.1+61.g1befe23a\n", - "Numpy version: 1.19.2\n", - "Pytorch version: 1.8.0a0+52ea372\n", - "MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False\n", - "MONAI rev id: 1befe23ae5443e64fbe627e9e5275c9a8160be73\n", - "MONAI __file__: /opt/conda/lib/python3.8/site-packages/monai/__init__.py\n", - "\n", - "Optional dependencies:\n", - "Pytorch Ignite version: 0.4.9\n", - "Nibabel version: 4.0.1\n", - "scikit-image version: 0.19.3\n", - "Pillow version: 9.2.0\n", - "Tensorboard version: 1.15.0+nv\n", - "gdown version: 4.5.1\n", - "TorchVision version: 0.9.0a0\n", - "tqdm version: 4.53.0\n", - "lmdb version: 1.1.1\n", - "psutil version: 5.8.0\n", - "pandas version: 1.1.4\n", - "einops version: 0.4.1\n", - "transformers version: 4.21.1\n", - "mlflow version: 1.28.0\n", - "pynrrd version: 0.4.3\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", @@ -565,7 +531,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -579,7 +545,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.5" + "version": "3.8.12" } }, "nbformat": 4,