diff --git a/bundle/05_spleen_segmentation_lightning.ipynb b/bundle/05_spleen_segmentation_lightning.ipynb index 77ac86184c..6b4b0c6c6c 100644 --- a/bundle/05_spleen_segmentation_lightning.ipynb +++ b/bundle/05_spleen_segmentation_lightning.ipynb @@ -103,7 +103,11 @@ } ], "source": [ + "from monai.apps import download_and_extract\n", "from monai.config import print_config\n", + "import os\n", + "import shutil\n", + "import tempfile\n", "\n", "print_config()" ] @@ -173,6 +177,85 @@ "which tree && tree SpleenSegLightning || true" ] }, + { + "cell_type": "markdown", + "id": "6228ccdd-d2d5-4344-b838-aa58e63f04f9", + "metadata": {}, + "source": [ + "## Download dataset and put into a directory" + ] + }, + { + "cell_type": "markdown", + "id": "fac6ab59-ca77-4d0e-ab0d-e5df91ead522", + "metadata": {}, + "source": [ + "First, we set up a temporary data directory, download the data, and move to a temporary directory `data_dir`." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "672f0168-950a-440e-b77c-f785a92c0f74", + "metadata": { + "ExecuteTime": { + "end_time": "2023-10-16T14:44:10.513242253Z", + "start_time": "2023-10-16T14:44:02.546210817Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "None\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Task09_Spleen.tar: 1.50GB [06:23, 4.19MB/s] " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2023-10-20 02:06:52,471 - INFO - Downloaded: /tmp/tmphygfp0cy/Task09_Spleen.tar\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2023-10-20 02:06:54,888 - INFO - Verified 'Task09_Spleen.tar', md5: 410d4a301da4e5b2f6f86ec3ddba524e.\n", + "2023-10-20 02:06:54,889 - INFO - Writing into directory: /tmp/tmphygfp0cy.\n" + ] + } + ], + "source": [ + "resource = \"https://msd-for-monai.s3-us-west-2.amazonaws.com/Task09_Spleen.tar\"\n", + "md5 = \"410d4a301da4e5b2f6f86ec3ddba524e\"\n", + "\n", + "directory = os.environ.get(\"DATA_DIR\")\n", + "print(directory)\n", + "root_dir = tempfile.mkdtemp() if directory is None else directory\n", + "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", + "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", + "os.environ[\"DATA_DIR\"] = data_dir\n", + "\n", + "if not os.path.exists(data_dir):\n", + " download_and_extract(resource, compressed_file, root_dir, md5)" + ] + }, { "cell_type": "markdown", "id": "5888c9bd-5022-40b5-9dec-84d9f737f868", @@ -185,7 +268,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "id": "b29f053b-cf16-4ffc-bbe7-d9433fdfa872", "metadata": { "ExecuteTime": { @@ -265,7 +348,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "id": "d11681af-3210-4b2b-b7bd-8ad8dedfe230", "metadata": { "ExecuteTime": { @@ -330,7 +413,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "id": "2c15149785c2192", "metadata": { "ExecuteTime": { @@ -464,7 +547,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "id": "d49daec7d4ce0b75", "metadata": { "ExecuteTime": { @@ -487,6 +570,7 @@ ], "source": [ "%%writefile SpleenSegLightning/scripts/main.py\n", + "\n", "from scripts.model import MySegNet\n", "import pytorch_lightning\n", "\n", @@ -522,7 +606,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "id": "4dfd052e-abe7-473a-bbf4-25674a3b20ea", "metadata": { "ExecuteTime": { @@ -557,7 +641,7 @@ "\n", "# define hyperparameters for the lightning trainer\n", "max_epochs: 50\n", - "default_root_dir: $@bundle_dir+\"/lightning_logs\"\n", + "default_root_dir: $@bundle_dir+\"/logs\"\n", "check_val_every_n_epoch: 1\n", "\n", "lightninig_param: '${\n", @@ -676,7 +760,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, "id": "1d8ac6fd81493874", "metadata": { "ExecuteTime": { @@ -702,17 +786,17 @@ "final_id None\n", "tracking None\n", "bundle_dir ./SpleenSegLightning\n", - "data_dir ./Task09_Spleen\n", + "data_dir /tmp/tmphygfp0cy/Task09_Spleen\n", "max_epochs 1\n", - "2023-10-18 11:36:59,810 - INFO - --- input summary of monai.bundle.scripts.run ---\n", - "2023-10-18 11:36:59,810 - INFO - > config_file: ['./SpleenSegLightning/configs/common.yaml',\n", + "2023-10-20 02:07:03,286 - INFO - --- input summary of monai.bundle.scripts.run ---\n", + "2023-10-20 02:07:03,286 - INFO - > config_file: ['./SpleenSegLightning/configs/common.yaml',\n", " './SpleenSegLightning/configs/train.yaml']\n", - "2023-10-18 11:36:59,810 - INFO - > meta_file: './SpleenSegLightning/configs/metadata.json'\n", - "2023-10-18 11:36:59,811 - INFO - > run_id: 'train'\n", - "2023-10-18 11:36:59,811 - INFO - > bundle_dir: './SpleenSegLightning'\n", - "2023-10-18 11:36:59,811 - INFO - > data_dir: './Task09_Spleen'\n", - "2023-10-18 11:36:59,811 - INFO - > max_epochs: 1\n", - "2023-10-18 11:36:59,811 - INFO - ---\n", + "2023-10-20 02:07:03,286 - INFO - > meta_file: './SpleenSegLightning/configs/metadata.json'\n", + "2023-10-20 02:07:03,286 - INFO - > run_id: 'train'\n", + "2023-10-20 02:07:03,286 - INFO - > bundle_dir: './SpleenSegLightning'\n", + "2023-10-20 02:07:03,286 - INFO - > data_dir: '/tmp/tmphygfp0cy/Task09_Spleen'\n", + "2023-10-20 02:07:03,286 - INFO - > max_epochs: 1\n", + "2023-10-20 02:07:03,286 - INFO - ---\n", "\n", "\n" ] @@ -723,13 +807,13 @@ "text": [ "monai.bundle.workflows ConfigWorkflow.__init__:workflow_type: Current default value of argument `workflow_type=None` has been deprecated since version 1.2. It will be changed to `workflow_type=train` in version 1.4.\n", "Default logging file in SpleenSegLightning/configs/logging.conf does not exist, skipping logging.\n", - "Loading dataset: 100%|██████████| 32/32 [00:49<00:00, 1.53s/it]\n", - "Loading dataset: 100%|██████████| 9/9 [00:10<00:00, 1.17s/it]\n", + "Loading dataset: 100%|██████████| 32/32 [00:38<00:00, 1.22s/it]\n", + "Loading dataset: 100%|██████████| 9/9 [00:08<00:00, 1.03it/s]\n", "GPU available: True (cuda), used: True\n", "TPU available: False, using: 0 TPU cores\n", "IPU available: False, using: 0 IPUs\n", "HPU available: False, using: 0 HPUs\n", - "Missing logger folder: SpleenSegLightning/lightning_logs/lightning_logs\n", + "Missing logger folder: SpleenSegLightning/logs/lightning_logs\n", "LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0,1]\n", "\n", " | Name | Type | Params\n", @@ -748,20 +832,20 @@ "output_type": "stream", "text": [ "configure_optimizers 0.0001\n", - "Epoch 0: 100%|██████████| 32/32 [00:09<00:00, 3.24it/s, v_num=0]\n", + "Epoch 0: 100%|██████████| 32/32 [00:04<00:00, 6.54it/s, v_num=0]\n", "Validation: 0it [00:00, ?it/s]\u001b[A\n", "Validation: 0%| | 0/9 [00:00 config_file: ['./SpleenSegLightning/configs/common.yaml',\n", + "data_dir /tmp/tmphygfp0cy/Task09_Spleen\n", + "ckpt_file ./SpleenSegLightning/logs/lightning_logs/version_0/checkpoints/epoch=0-step=32.ckpt\n", + "2023-10-20 02:08:21,419 - INFO - --- input summary of monai.bundle.scripts.run ---\n", + "2023-10-20 02:08:21,419 - INFO - > config_file: ['./SpleenSegLightning/configs/common.yaml',\n", " './SpleenSegLightning/configs/evaluate.yaml']\n", - "2023-10-18 11:38:38,049 - INFO - > meta_file: './SpleenSegLightning/configs/metadata.json'\n", - "2023-10-18 11:38:38,049 - INFO - > run_id: 'evaluate'\n", - "2023-10-18 11:38:38,049 - INFO - > bundle_dir: './SpleenSegLightning'\n", - "2023-10-18 11:38:38,049 - INFO - > data_dir: './Task09_Spleen'\n", - "2023-10-18 11:38:38,049 - INFO - > ckpt_file: './epoch=599-step=9600.ckpt'\n", - "2023-10-18 11:38:38,049 - INFO - ---\n", + "2023-10-20 02:08:21,419 - INFO - > meta_file: './SpleenSegLightning/configs/metadata.json'\n", + "2023-10-20 02:08:21,419 - INFO - > run_id: 'evaluate'\n", + "2023-10-20 02:08:21,420 - INFO - > bundle_dir: './SpleenSegLightning'\n", + "2023-10-20 02:08:21,420 - INFO - > data_dir: '/tmp/tmphygfp0cy/Task09_Spleen'\n", + "2023-10-20 02:08:21,420 - INFO - > ckpt_file: './SpleenSegLightning/logs/lightning_logs/version_0/checkpoints/epoch=0-step=32.ckpt'\n", + "2023-10-20 02:08:21,420 - INFO - ---\n", "\n", "\n" ] @@ -1014,22 +1097,31 @@ "text": [ "monai.bundle.workflows ConfigWorkflow.__init__:workflow_type: Current default value of argument `workflow_type=None` has been deprecated since version 1.2. It will be changed to `workflow_type=train` in version 1.4.\n", "Default logging file in SpleenSegLightning/configs/logging.conf does not exist, skipping logging.\n", - "Loading dataset: 100%|██████████| 9/9 [00:10<00:00, 1.18s/it]\n", + "Loading dataset: 100%|██████████| 9/9 [00:08<00:00, 1.01it/s]\n", "GPU available: True (cuda), used: True\n", "TPU available: False, using: 0 TPU cores\n", "IPU available: False, using: 0 IPUs\n", "HPU available: False, using: 0 HPUs\n", - "Restoring states from the checkpoint path at ./epoch=599-step=9600.ckpt\n", + "Restoring states from the checkpoint path at ./SpleenSegLightning/logs/lightning_logs/version_0/checkpoints/epoch=0-step=32.ckpt\n", "LOCAL_RANK: 0 - CUDA_VISIBLE_DEVICES: [0,1]\n", - "Loaded model weights from the checkpoint at ./epoch=599-step=9600.ckpt\n" + "Loaded model weights from the checkpoint at ./SpleenSegLightning/logs/lightning_logs/version_0/checkpoints/epoch=0-step=32.ckpt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Validation DataLoader 0: 100%|██████████| 9/9 [00:14<00:00, 1.66s/it]current epoch: 0 current mean dice: 0.0184\n", + "best mean dice: 0.0184 at epoch: 0\n", + "Validation DataLoader 0: 100%|██████████| 9/9 [00:14<00:00, 1.66s/it]\n" ] } ], "source": [ "%%bash\n", "\n", - "DATA_DIR=\"./Task09_Spleen\"\n", "BUNDLE=\"./SpleenSegLightning\"\n", + "CKPT_FILE=$(find \"$BUNDLE\" -type f -name \"*.ckpt\" | head -n 1)\n", "export PYTHONPATH=\"$BUNDLE\"\n", "\n", "python -m monai.bundle run evaluate \\\n", @@ -1037,7 +1129,33 @@ " --data_dir \"$DATA_DIR\" \\\n", " --meta_file \"$BUNDLE/configs/metadata.json\" \\\n", " --config_file \"['$BUNDLE/configs/common.yaml','$BUNDLE/configs/evaluate.yaml']\" \\\n", - " --ckpt_file \"./epoch=599-step=9600.ckpt\"" + " --ckpt_file \"$CKPT_FILE\"" + ] + }, + { + "cell_type": "markdown", + "id": "e930919d-61fb-4e1b-bb00-8f1bbb69acf1", + "metadata": {}, + "source": [ + "## Cleanup data directory" + ] + }, + { + "cell_type": "markdown", + "id": "9e98026d-998d-4645-83ec-afe3a448b3e5", + "metadata": {}, + "source": [ + "Remove directory if a temporary was used." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "b273214f-7653-468c-b311-fa8d097b5bee", + "metadata": {}, + "outputs": [], + "source": [ + "shutil.rmtree(root_dir)" ] }, {