diff --git a/acceleration/transform_speed.ipynb b/acceleration/transform_speed.ipynb index 7eb39c85d8..45aa99dc7e 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": [] }, @@ -99,11 +99,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 +132,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -140,7 +148,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -167,7 +175,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 +202,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 +233,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 +280,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 +318,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 +333,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 +341,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 +371,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 +391,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 +399,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 +424,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 +520,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -454,7 +545,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.8.12" } }, "nbformat": 4,