diff --git a/.github/workflows/test-modified.yml b/.github/workflows/test-modified.yml index 2365d17302..4e410ffe67 100644 --- a/.github/workflows/test-modified.yml +++ b/.github/workflows/test-modified.yml @@ -53,6 +53,7 @@ jobs: do if [[ $line == *.ipynb ]] then + date ./runner.sh -p " -and -wholename './${line}'" fi done diff --git a/2d_classification/mednist_tutorial.ipynb b/2d_classification/mednist_tutorial.ipynb index 43b854e085..fbd1729977 100644 --- a/2d_classification/mednist_tutorial.ipynb +++ b/2d_classification/mednist_tutorial.ipynb @@ -194,7 +194,7 @@ "compressed_file = os.path.join(root_dir, \"MedNIST.tar.gz\")\n", "data_dir = os.path.join(root_dir, \"MedNIST\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/2d_classification/monai_201.ipynb b/2d_classification/monai_201.ipynb index 832ba7d486..a199d9d713 100644 --- a/2d_classification/monai_201.ipynb +++ b/2d_classification/monai_201.ipynb @@ -195,12 +195,13 @@ "max_epochs = 5\n", "save_interval = 2\n", "out_dir = \"./eval\"\n", - "model = densenet121(spatial_dims=2, in_channels=1, out_channels=6).to(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", + "model = densenet121(spatial_dims=2, in_channels=1, out_channels=6).to(device)\n", "\n", "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n", "\n", "evaluator = SupervisedEvaluator(\n", - " device=torch.device(\"cuda:0\"),\n", + " device=device,\n", " val_data_loader=DataLoader(valdata, batch_size=512, shuffle=False, num_workers=4),\n", " network=model,\n", " inferer=SimpleInferer(),\n", @@ -209,7 +210,7 @@ ")\n", "\n", "trainer = SupervisedTrainer(\n", - " device=torch.device(\"cuda:0\"),\n", + " device=device,\n", " max_epochs=max_epochs,\n", " train_data_loader=DataLoader(dataset, batch_size=512, shuffle=True, num_workers=4),\n", " network=model,\n", @@ -313,7 +314,7 @@ ], "source": [ "evaluator = SupervisedEvaluator(\n", - " device=torch.device(\"cuda:0\"),\n", + " device=device,\n", " val_data_loader=DataLoader(testdata, batch_size=1, num_workers=0),\n", " network=model,\n", " inferer=SimpleInferer(),\n", diff --git a/2d_registration/registration_mednist.ipynb b/2d_registration/registration_mednist.ipynb index 18f3dedf93..6fa7adcc17 100644 --- a/2d_registration/registration_mednist.ipynb +++ b/2d_registration/registration_mednist.ipynb @@ -376,7 +376,7 @@ } ], "source": [ - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "model = GlobalNet(\n", " image_size=(64, 64), spatial_dims=2, in_channels=2, num_channel_initial=16, depth=3 # moving and fixed\n", ").to(device)\n", diff --git a/3d_classification/densenet_training_array.ipynb b/3d_classification/densenet_training_array.ipynb index 2863cd92a0..087e23e71e 100644 --- a/3d_classification/densenet_training_array.ipynb +++ b/3d_classification/densenet_training_array.ipynb @@ -206,7 +206,7 @@ " dataset_dir = os.path.join(root_dir, \"ixi\")\n", " tarfile_name = f\"{dataset_dir}.tar\"\n", "\n", - " download_and_extract(resource, tarfile_name, dataset_dir, md5)" + " download_and_extract(resource, tarfile_name, dataset_dir, md5, \"md5\")" ] }, { diff --git a/3d_registration/learn2reg_nlst_paired_lung_ct.ipynb b/3d_registration/learn2reg_nlst_paired_lung_ct.ipynb index 69d49a9894..4e311a8deb 100644 --- a/3d_registration/learn2reg_nlst_paired_lung_ct.ipynb +++ b/3d_registration/learn2reg_nlst_paired_lung_ct.ipynb @@ -598,7 +598,7 @@ "outputs": [], "source": [ "# device, optimizer, epoch and batch settings\n", - "device = \"cuda:0\"\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "batch_size = 4\n", "lr = 1e-4\n", "weight_decay = 1e-5\n", diff --git a/3d_regression/densenet_training_array.ipynb b/3d_regression/densenet_training_array.ipynb index b9bc311060..26194d9227 100644 --- a/3d_regression/densenet_training_array.ipynb +++ b/3d_regression/densenet_training_array.ipynb @@ -211,7 +211,7 @@ " dataset_dir = os.path.join(root_dir, \"ixi\")\n", " tarfile_name = f\"{dataset_dir}.tar\"\n", "\n", - " download_and_extract(resource, tarfile_name, dataset_dir, md5)" + " download_and_extract(resource, tarfile_name, dataset_dir, md5, \"md5\")" ] }, { diff --git a/3d_segmentation/brats_segmentation_3d.ipynb b/3d_segmentation/brats_segmentation_3d.ipynb index ad52c737e7..087b453b9e 100644 --- a/3d_segmentation/brats_segmentation_3d.ipynb +++ b/3d_segmentation/brats_segmentation_3d.ipynb @@ -442,7 +442,7 @@ "VAL_AMP = True\n", "\n", "# standard PyTorch program style: create SegResNet, DiceLoss and Adam optimizer\n", - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "model = SegResNet(\n", " blocks_down=[1, 2, 2, 4],\n", " blocks_up=[1, 1, 1],\n", diff --git a/3d_segmentation/spleen_segmentation_3d.ipynb b/3d_segmentation/spleen_segmentation_3d.ipynb index c931724682..efdedb68cb 100644 --- a/3d_segmentation/spleen_segmentation_3d.ipynb +++ b/3d_segmentation/spleen_segmentation_3d.ipynb @@ -210,7 +210,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { @@ -432,7 +432,7 @@ "outputs": [], "source": [ "# standard PyTorch program style: create UNet, DiceLoss and Adam optimizer\n", - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "model = UNet(\n", " spatial_dims=3,\n", " in_channels=1,\n", diff --git a/3d_segmentation/spleen_segmentation_3d_lightning.ipynb b/3d_segmentation/spleen_segmentation_3d_lightning.ipynb index d6459cfbbc..57738bec1a 100644 --- a/3d_segmentation/spleen_segmentation_3d_lightning.ipynb +++ b/3d_segmentation/spleen_segmentation_3d_lightning.ipynb @@ -206,7 +206,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { @@ -432,7 +432,7 @@ "\n", "# initialise Lightning's trainer.\n", "trainer = pytorch_lightning.Trainer(\n", - " devices=[0],\n", + " devices=1,\n", " max_epochs=600,\n", " logger=tb_logger,\n", " enable_checkpointing=True,\n", @@ -652,7 +652,7 @@ ], "source": [ "net.eval()\n", - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "net.to(device)\n", "with torch.no_grad():\n", " for i, val_data in enumerate(net.val_dataloader()):\n", diff --git a/3d_segmentation/spleen_segmentation_3d_visualization_basic.ipynb b/3d_segmentation/spleen_segmentation_3d_visualization_basic.ipynb index f7ec118cb3..9fb74c0616 100644 --- a/3d_segmentation/spleen_segmentation_3d_visualization_basic.ipynb +++ b/3d_segmentation/spleen_segmentation_3d_visualization_basic.ipynb @@ -230,7 +230,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/3d_segmentation/unet_segmentation_3d_ignite.ipynb b/3d_segmentation/unet_segmentation_3d_ignite.ipynb index dc870bf965..9940524b07 100644 --- a/3d_segmentation/unet_segmentation_3d_ignite.ipynb +++ b/3d_segmentation/unet_segmentation_3d_ignite.ipynb @@ -263,7 +263,7 @@ "outputs": [], "source": [ "# Create UNet, DiceLoss and Adam optimizer\n", - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "net = UNet(\n", " spatial_dims=3,\n", " in_channels=1,\n", diff --git a/3d_segmentation/unetr_btcv_segmentation_3d_lightning.ipynb b/3d_segmentation/unetr_btcv_segmentation_3d_lightning.ipynb index 1fae50750a..762aa8af0a 100644 --- a/3d_segmentation/unetr_btcv_segmentation_3d_lightning.ipynb +++ b/3d_segmentation/unetr_btcv_segmentation_3d_lightning.ipynb @@ -637,7 +637,7 @@ "\n", "# initialise Lightning's trainer.\n", "trainer = pytorch_lightning.Trainer(\n", - " devices=[0],\n", + " devices=1,\n", " max_epochs=net.max_epochs,\n", " check_val_every_n_epoch=net.check_val,\n", " callbacks=checkpoint_callback,\n", diff --git a/acceleration/TensorRT_inference_acceleration.ipynb b/acceleration/TensorRT_inference_acceleration.ipynb index 35439faafa..05d00dc795 100644 --- a/acceleration/TensorRT_inference_acceleration.ipynb +++ b/acceleration/TensorRT_inference_acceleration.ipynb @@ -169,7 +169,7 @@ "compressed_file = os.path.join(root_dir, \"endoscopic_tool_dataset.zip\")\n", "data_root = os.path.join(root_dir, \"endoscopic_tool_dataset\")\n", "if not os.path.exists(data_root):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/acceleration/automatic_mixed_precision.ipynb b/acceleration/automatic_mixed_precision.ipynb index dbeddcc7a8..cf9316631e 100644 --- a/acceleration/automatic_mixed_precision.ipynb +++ b/acceleration/automatic_mixed_precision.ipynb @@ -90,12 +90,17 @@ " ScaleIntensityRanged,\n", " Spacingd,\n", ")\n", - "from monai.utils import get_torch_version_tuple, set_determinism\n", + "from monai.utils import set_determinism\n", "\n", "print_config()\n", "\n", - "if get_torch_version_tuple() < (1, 6):\n", - " raise RuntimeError(\"AMP feature only exists in PyTorch version greater than v1.6.\")" + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", + "\n", + "if not torch.cuda.is_available() or torch.cuda.device_count() == 0:\n", + " print(\n", + " \"Warning: no CUDA device available, this notebook will still run but the AMP \"\n", + " \"feature will not provide any acceleration.\"\n", + " )" ] }, { @@ -145,7 +150,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_root = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_root):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { @@ -277,7 +282,6 @@ " num_workers=1,\n", " )\n", " val_loader = DataLoader(val_ds, batch_size=1, num_workers=1)\n", - " device = torch.device(\"cuda:0\")\n", " model = UNet(\n", " spatial_dims=3,\n", " in_channels=1,\n", @@ -431,7 +435,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -471,8 +475,11 @@ } ], "source": [ - "print(torch.cuda.get_device_name(0))\n", - "print(torch.cuda.memory_summary(0, abbreviated=True))" + "if device.type == \"cuda\":\n", + " print(torch.cuda.get_device_name(0))\n", + " print(torch.cuda.memory_summary(0, abbreviated=True))\n", + "else:\n", + " print(\"Not using a CUDA device!\")" ] }, { @@ -511,7 +518,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -551,8 +558,11 @@ } ], "source": [ - "print(torch.cuda.get_device_name(0))\n", - "print(torch.cuda.memory_summary(0, abbreviated=True))" + "if device.type == \"cuda\":\n", + " print(torch.cuda.get_device_name(0))\n", + " print(torch.cuda.memory_summary(0, abbreviated=True))\n", + "else:\n", + " print(\"Not using a CUDA device!\")" ] }, { @@ -787,7 +797,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "monai", "language": "python", "name": "python3" }, @@ -801,7 +811,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.13" + "version": "3.10.20" } }, "nbformat": 4, diff --git a/acceleration/dataset_type_performance.ipynb b/acceleration/dataset_type_performance.ipynb index f8c2911c59..894f601704 100644 --- a/acceleration/dataset_type_performance.ipynb +++ b/acceleration/dataset_type_performance.ipynb @@ -156,7 +156,7 @@ " num_workers=8,\n", " )\n", " val_loader = DataLoader(val_ds, batch_size=1, num_workers=4)\n", - " device = torch.device(\"cuda:0\")\n", + " device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", " model = UNet(\n", " spatial_dims=3,\n", " in_channels=1,\n", @@ -311,7 +311,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/acceleration/fast_training_tutorial.ipynb b/acceleration/fast_training_tutorial.ipynb index 85b197231b..4719ac61e0 100644 --- a/acceleration/fast_training_tutorial.ipynb +++ b/acceleration/fast_training_tutorial.ipynb @@ -252,7 +252,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_root = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_root):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/acceleration/threadbuffer_performance.ipynb b/acceleration/threadbuffer_performance.ipynb index 9e127bbd64..10d4251f74 100644 --- a/acceleration/threadbuffer_performance.ipynb +++ b/acceleration/threadbuffer_performance.ipynb @@ -120,7 +120,7 @@ "metadata": {}, "outputs": [], "source": [ - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "net = UNet(2, 1, 1, (8, 16, 32), (2, 2), num_res_units=2).to(device)\n", "loss_function = Dice(sigmoid=True)\n", "optimizer = torch.optim.Adam(net.parameters(), 1e-5)\n", diff --git a/acceleration/transform_speed.ipynb b/acceleration/transform_speed.ipynb index e4b61e29e4..dd974ad30f 100644 --- a/acceleration/transform_speed.ipynb +++ b/acceleration/transform_speed.ipynb @@ -84,7 +84,12 @@ ")\n", "from monai.utils import first\n", "\n", - "print_config()" + "print_config()\n", + "\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", + "\n", + "if not torch.cuda.is_available() or torch.cuda.device_count() == 0:\n", + " print(\"Warning: no CUDA device available, this notebook will run but no GPU acceleration will be present.\")" ] }, { @@ -308,7 +313,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": { "tags": [] }, @@ -332,7 +337,7 @@ " translate_range=(96, 96, 96),\n", " spatial_size=(64, 64, 64),\n", " mode=\"bilinear\",\n", - " device=torch.device(\"cuda:0\"),\n", + " device=device,\n", ")\n", "rand_affine_seg = RandAffine(\n", " prob=1.0,\n", @@ -340,7 +345,7 @@ " translate_range=(96, 96, 96),\n", " spatial_size=(64, 64, 64),\n", " mode=\"nearest\",\n", - " device=torch.device(\"cuda:0\"),\n", + " device=device,\n", ")\n", "\n", "imtrans = Compose([LoadImage(image_only=True), ScaleIntensity(), EnsureChannelFirst(), rand_affine_img])\n", @@ -377,7 +382,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": { "tags": [] }, @@ -415,8 +420,11 @@ } ], "source": [ - "print(torch.cuda.get_device_name(0))\n", - "print(torch.cuda.memory_summary(0, abbreviated=True))" + "if device.type == \"cuda\":\n", + " print(torch.cuda.get_device_name(0))\n", + " print(torch.cuda.memory_summary(0, abbreviated=True))\n", + "else:\n", + " print(\"Not using a CUDA device!\")" ] }, { @@ -458,7 +466,7 @@ " spatial_size=(64, 64, 64),\n", " mode=3,\n", " padding_mode=\"reflect\",\n", - " device=torch.device(\"cuda:0\"),\n", + " device=device,\n", ")\n", "rand_affine_seg = RandAffine(\n", " prob=1.0,\n", @@ -467,7 +475,7 @@ " spatial_size=(64, 64, 64),\n", " mode=0,\n", " padding_mode=\"reflect\",\n", - " device=torch.device(\"cuda:0\"),\n", + " device=device,\n", ")\n", "\n", "imtrans = Compose([LoadImage(image_only=True), ScaleIntensity(), EnsureChannelFirst(), rand_affine_img])\n", diff --git a/bundle/05_spleen_segmentation_lightning.ipynb b/bundle/05_spleen_segmentation_lightning.ipynb index 43cc966f82..2190348bb5 100644 --- a/bundle/05_spleen_segmentation_lightning.ipynb +++ b/bundle/05_spleen_segmentation_lightning.ipynb @@ -218,7 +218,7 @@ "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)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/bundle/further_features.md b/bundle/further_features.md index 65c3f0e431..95d21653f9 100644 --- a/bundle/further_features.md +++ b/bundle/further_features.md @@ -47,7 +47,7 @@ root_dir = tempfile.mkdtemp() if directory is None else directory compressed_file = os.path.join(root_dir, "Task09_Spleen.tar") data_dir = os.path.join(root_dir, "Task09_Spleen") if not os.path.exists(data_dir): - download_and_extract(resource, compressed_file, root_dir, md5) + download_and_extract(resource, compressed_file, root_dir, md5, "md5") ``` ## Define train config - Set imports and input / output environments diff --git a/bundle/pythonic_usage_guidance/pythonic_bundle_access.ipynb b/bundle/pythonic_usage_guidance/pythonic_bundle_access.ipynb index 84535100c7..8af48d50a2 100644 --- a/bundle/pythonic_usage_guidance/pythonic_bundle_access.ipynb +++ b/bundle/pythonic_usage_guidance/pythonic_bundle_access.ipynb @@ -134,7 +134,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/computer_assisted_intervention/endoscopic_inbody_classification.ipynb b/computer_assisted_intervention/endoscopic_inbody_classification.ipynb index fb97c7f474..58e8c1b204 100644 --- a/computer_assisted_intervention/endoscopic_inbody_classification.ipynb +++ b/computer_assisted_intervention/endoscopic_inbody_classification.ipynb @@ -36,6 +36,7 @@ "execution_count": 1, "id": "5f9e4066", "metadata": {}, + "outputs": [], "source": [ "!python -c \"import monai\" || pip install -q \"monai-weekly[pillow, ignite, tqdm]\"\n", "!python -c \"import matplotlib\" || pip install -q matplotlib\n", @@ -159,7 +160,7 @@ "set_url = r\"https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/inbody_outbody_samples.zip\"\n", "md5_hash = r\"cce8f3beb1fb2e8fc2429e073c927489\"\n", "compress_filename = r\"inbody_outbody_samples.zip\"\n", - "download_and_extract(set_url, compress_filename, endo_dir, md5_hash)" + "download_and_extract(set_url, compress_filename, endo_dir, md5_hash, \"md5\")" ] }, { diff --git a/computer_assisted_intervention/video_seg.ipynb b/computer_assisted_intervention/video_seg.ipynb index fb3ab667f6..346eac7251 100644 --- a/computer_assisted_intervention/video_seg.ipynb +++ b/computer_assisted_intervention/video_seg.ipynb @@ -131,7 +131,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -153,8 +153,8 @@ "vid_filepath = os.path.join(endo_dir, \"endo.mp4\")\n", "seg_filepath = os.path.join(endo_dir, \"endo_seg.mp4\")\n", "\n", - "download_url(vid_url, vid_filepath, vid_hash)\n", - "download_url(seg_url, seg_filepath, seg_hash)" + "download_url(vid_url, vid_filepath, vid_hash, \"md5\")\n", + "download_url(seg_url, seg_filepath, seg_hash, \"md5\")" ] }, { diff --git a/deep_atlas/deep_atlas_tutorial.ipynb b/deep_atlas/deep_atlas_tutorial.ipynb index 799a85fd35..e0adf31cc9 100644 --- a/deep_atlas/deep_atlas_tutorial.ipynb +++ b/deep_atlas/deep_atlas_tutorial.ipynb @@ -142,7 +142,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "576b9aa6", "metadata": {}, "outputs": [ @@ -211,10 +211,12 @@ "# Set deterministic training for reproducibility\n", "monai.utils.set_determinism(seed=2938649572)\n", "\n", - "# Turn off the TF32 precision. Otherwise, because of the rounding error, the ssim_loss will be a weird value on some Ampere GPUs, like L40, 3090Ti and 3080Ti.\n", - "# Track this issue (https://github.com/Project-MONAI/tutorials/issues/1336) for more detail.\n", - "torch.backends.cuda.matmul.allow_tf32 = False\n", - "torch.backends.cudnn.allow_tf32 = False\n", + "if torch.cuda.is_available():\n", + " # Turn off the TF32 precision. Otherwise, because of the rounding error, the ssim_loss will be a\n", + " # weird value on some Ampere GPUs, like L40, 3090Ti and 3080Ti.\n", + " # Track this issue (https://github.com/Project-MONAI/tutorials/issues/1336) for more detail.\n", + " torch.backends.cuda.matmul.allow_tf32 = False\n", + " torch.backends.cudnn.allow_tf32 = False\n", "\n", "monai.config.print_config()" ] @@ -288,7 +290,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "549f9bee", "metadata": {}, "outputs": [], @@ -301,7 +303,7 @@ "\n", " compressed_file = os.path.join(root_dir, \"oasis_cross-sectional_disc1.tar.gz\")\n", " if not os.path.exists(data_dir):\n", - " monai.apps.utils.download_and_extract(resource, compressed_file, data_dir, md5)" + " monai.apps.utils.download_and_extract(resource, compressed_file, data_dir, md5, \"md5\")" ] }, { @@ -429,7 +431,7 @@ "metadata": {}, "outputs": [], "source": [ - "device = torch.device(\"cuda:0\")" + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")" ] }, { @@ -1292,7 +1294,8 @@ "\n", "# Free up some memory\n", "del loss, predicted_segs, true_segs, imgs\n", - "torch.cuda.empty_cache()" + "if torch.cuda.is_available():\n", + " torch.cuda.empty_cache()" ] }, { @@ -1352,7 +1355,7 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": null, "id": "91d2f6ec", "metadata": {}, "outputs": [ @@ -1424,7 +1427,7 @@ "test_seg_true = data_item[\"seg\"]\n", "seg_net.eval()\n", "with torch.no_grad():\n", - " test_seg_predicted = seg_net(test_input.unsqueeze(0).cuda()).cpu()\n", + " test_seg_predicted = seg_net(test_input.unsqueeze(0).to(device)).cpu()\n", "\n", "print(\"Original image from validation set:\")\n", "preview_image(test_input[0])\n", @@ -1434,7 +1437,8 @@ "preview_image(torch.argmax(torch.softmax(test_seg_predicted, dim=1), dim=1, keepdim=True)[0, 0])\n", "\n", "del test_seg_predicted\n", - "torch.cuda.empty_cache()" + "if torch.cuda.is_available():\n", + " torch.cuda.empty_cache()" ] }, { @@ -1838,7 +1842,8 @@ "\n", " # Free up memory\n", " del loss, loss_sim, loss_reg, loss_ana\n", - " torch.cuda.empty_cache()\n", + " if torch.cuda.is_available():\n", + " torch.cuda.empty_cache()\n", "\n", " # ------------------------------------------------\n", " # seg_net training, with reg_net frozen\n", @@ -1942,7 +1947,8 @@ " seg1_predicted,\n", " seg2_predicted,\n", " )\n", - " torch.cuda.empty_cache()\n", + " if torch.cuda.is_available():\n", + " torch.cuda.empty_cache()\n", "\n", "print(f\"\\n\\nBest reg_net validation loss: {best_reg_validation_loss}\")\n", "print(f\"Best seg_net validation loss: {best_seg_validation_loss}\")" @@ -2059,7 +2065,7 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": null, "id": "8972921e", "metadata": {}, "outputs": [ @@ -2138,7 +2144,7 @@ "test_seg_true = data_item[\"seg\"]\n", "seg_net.eval()\n", "with torch.no_grad():\n", - " test_seg_predicted = seg_net(test_input.unsqueeze(0).cuda()).cpu()\n", + " test_seg_predicted = seg_net(test_input.unsqueeze(0).to(device)).cpu()\n", " loss = dice_loss(test_seg_predicted, test_seg_true.unsqueeze(0)).item()\n", "\n", "print(\"original image from validation set:\")\n", @@ -2150,7 +2156,8 @@ "print(f\"dice loss: {loss}\")\n", "\n", "del test_seg_predicted\n", - "torch.cuda.empty_cache()" + "if torch.cuda.is_available():\n", + " torch.cuda.empty_cache()" ] }, { @@ -2163,7 +2170,7 @@ }, { "cell_type": "code", - "execution_count": 60, + "execution_count": null, "id": "5801a48e", "metadata": {}, "outputs": [ @@ -2323,7 +2330,8 @@ "print(f\"number of folds: {(det <= 0).sum()}\")\n", "\n", "del reg_net_example_output, img12, example_warped_image\n", - "torch.cuda.empty_cache()" + "if torch.cuda.is_available():\n", + " torch.cuda.empty_cache()" ] }, { @@ -2359,7 +2367,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": null, "id": "ab53ebb0", "metadata": {}, "outputs": [ @@ -2401,7 +2409,8 @@ " negative_det_values += det[det <= 0].tolist()\n", "\n", "del img12, displacement_fields\n", - "torch.cuda.empty_cache()\n", + "if torch.cuda.is_available():\n", + " torch.cuda.empty_cache()\n", "\n", "plt.hist(fold_counts)\n", "plt.title(\"Number of folds\")\n", diff --git a/deployment/bentoml/mednist_classifier_bentoml.ipynb b/deployment/bentoml/mednist_classifier_bentoml.ipynb index 14f0b271a2..90b6401f36 100644 --- a/deployment/bentoml/mednist_classifier_bentoml.ipynb +++ b/deployment/bentoml/mednist_classifier_bentoml.ipynb @@ -186,7 +186,7 @@ "compressed_file = os.path.join(root_dir, \"MedNIST.tar.gz\")\n", "data_dir = os.path.join(root_dir, \"MedNIST\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { @@ -281,7 +281,7 @@ }, "outputs": [], "source": [ - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "net = DenseNet121(spatial_dims=2, in_channels=1, out_channels=len(class_names)).to(device)\n", "loss_function = torch.nn.CrossEntropyLoss()\n", "opt = torch.optim.Adam(net.parameters(), 1e-5)\n", diff --git a/experiment_management/bundle_integrate_mlflow.ipynb b/experiment_management/bundle_integrate_mlflow.ipynb index c3cc2d4b92..26da75823b 100644 --- a/experiment_management/bundle_integrate_mlflow.ipynb +++ b/experiment_management/bundle_integrate_mlflow.ipynb @@ -126,7 +126,7 @@ "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "print(data_dir)\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/experiment_management/spleen_segmentation_aim.ipynb b/experiment_management/spleen_segmentation_aim.ipynb index f5da6a9e93..8a79f98c55 100644 --- a/experiment_management/spleen_segmentation_aim.ipynb +++ b/experiment_management/spleen_segmentation_aim.ipynb @@ -146,7 +146,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource_link, compressed_file, root_dir, md5)" + " download_and_extract(resource_link, compressed_file, root_dir, md5, \"md5\")" ] }, { @@ -407,7 +407,7 @@ "outputs": [], "source": [ "# standard PyTorch program style: create UNet, DiceLoss and Adam optimizer\n", - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "model = UNet(**UNet_metadata).to(device)\n", "loss_function = DiceLoss(to_onehot_y=True, softmax=True)\n", "loss_type = \"DiceLoss\"\n", diff --git a/experiment_management/spleen_segmentation_mlflow.ipynb b/experiment_management/spleen_segmentation_mlflow.ipynb index 0f11b2a004..3201962ad8 100644 --- a/experiment_management/spleen_segmentation_mlflow.ipynb +++ b/experiment_management/spleen_segmentation_mlflow.ipynb @@ -146,7 +146,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { @@ -392,7 +392,7 @@ "outputs": [], "source": [ "# standard PyTorch program style: create UNet, DiceLoss and Adam optimizer\n", - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "\n", "# easily save all model arguments\n", "UNet_metadata = {\n", diff --git a/experiment_management/unet_segmentation_3d_ignite_clearml.ipynb b/experiment_management/unet_segmentation_3d_ignite_clearml.ipynb index a454086fdd..09c3cc1aee 100644 --- a/experiment_management/unet_segmentation_3d_ignite_clearml.ipynb +++ b/experiment_management/unet_segmentation_3d_ignite_clearml.ipynb @@ -274,7 +274,7 @@ "source": [ "# Create UNet, DiceLoss and Adam optimizer\n", "\n", - "device = None # torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "net = UNet(\n", " spatial_dims=3,\n", " in_channels=1,\n", diff --git a/full_gpu_inference_pipeline/client/non_ensemble/client.ipynb b/full_gpu_inference_pipeline/client/non_ensemble/client.ipynb index 65a93c6675..811e510fea 100644 --- a/full_gpu_inference_pipeline/client/non_ensemble/client.ipynb +++ b/full_gpu_inference_pipeline/client/non_ensemble/client.ipynb @@ -159,7 +159,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/generation/2d_super_resolution/2d_sd_super_resolution_lightning.ipynb b/generation/2d_super_resolution/2d_sd_super_resolution_lightning.ipynb index 817efe233c..edbe98887d 100644 --- a/generation/2d_super_resolution/2d_sd_super_resolution_lightning.ipynb +++ b/generation/2d_super_resolution/2d_sd_super_resolution_lightning.ipynb @@ -695,7 +695,7 @@ "source": [ "def get_scale_factor():\n", " ae_net.eval()\n", - " device = torch.device(\"cuda:0\")\n", + " device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", " ae_net.to(device)\n", "\n", " train_loader = ae_net.train_dataloader()\n", @@ -1201,7 +1201,7 @@ "\n", "def get_images_to_plot():\n", " d_net.eval()\n", - " device = torch.device(\"cuda:0\")\n", + " device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", " d_net.to(device)\n", "\n", " val_loader = d_net.val_dataloader()\n", diff --git a/generation/maisi/maisi_train_vae_tutorial.ipynb b/generation/maisi/maisi_train_vae_tutorial.ipynb index 58358d9f8e..7d9497d8d6 100644 --- a/generation/maisi/maisi_train_vae_tutorial.ipynb +++ b/generation/maisi/maisi_train_vae_tutorial.ipynb @@ -191,7 +191,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_path_1 = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_path_1):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)\n", + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")\n", "\n", "# Only include 20 of the images for quick demo purpose\n", "train_images_1 = sorted(glob.glob(os.path.join(data_path_1, \"imagesTr\", \"*.nii.gz\")))[:20]\n", @@ -206,7 +206,7 @@ "compressed_file = os.path.join(root_dir, \"Task01_BrainTumour.tar\")\n", "data_path_2 = os.path.join(root_dir, \"Task01_BrainTumour\")\n", "if not os.path.exists(data_path_2):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)\n", + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")\n", "\n", "# Only include 30 of the images for quick demo purpose\n", "train_images_2 = sorted(glob.glob(os.path.join(data_path_2, \"imagesTr\", \"*.nii.gz\")))[:30]\n", diff --git a/hugging_face/finetune_vista3d_for_hugging_face_pipeline.ipynb b/hugging_face/finetune_vista3d_for_hugging_face_pipeline.ipynb index 908d7985b8..83687e70c4 100644 --- a/hugging_face/finetune_vista3d_for_hugging_face_pipeline.ipynb +++ b/hugging_face/finetune_vista3d_for_hugging_face_pipeline.ipynb @@ -176,7 +176,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/hugging_face/hugging_face_pipeline_for_monai.ipynb b/hugging_face/hugging_face_pipeline_for_monai.ipynb index 36d09e8a99..f76e8fbc2e 100644 --- a/hugging_face/hugging_face_pipeline_for_monai.ipynb +++ b/hugging_face/hugging_face_pipeline_for_monai.ipynb @@ -186,7 +186,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { @@ -523,10 +523,11 @@ "download(\"spleen_ct_segmentation\", bundle_dir=bundle_dir, progress=False)\n", "weight_dir = os.path.join(bundle_dir, \"spleen_ct_segmentation/models/model.pt\")\n", "\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "config = MONAIUNetConfig()\n", "monai_unet = MONAIUNet(config)\n", "monai_unet.unet.load_state_dict(torch.load(weight_dir, weights_only=True))\n", - "pipeline = SpleenCTSegmentationPipeline(model=monai_unet, device=torch.device(\"cuda:0\"), save_output=False)\n", + "pipeline = SpleenCTSegmentationPipeline(model=monai_unet, device=device, save_output=False)\n", "\n", "numpy_transform = ToNumpy()\n", "loader = LoadImage()\n", diff --git a/microscopy/multichannel_microscopy_classification.ipynb b/microscopy/multichannel_microscopy_classification.ipynb index 527e37bdc9..2df8bdf66f 100644 --- a/microscopy/multichannel_microscopy_classification.ipynb +++ b/microscopy/multichannel_microscopy_classification.ipynb @@ -155,7 +155,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "9fe67439", "metadata": {}, "outputs": [ @@ -178,9 +178,8 @@ ], "source": [ "url = \"https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/rxrx1_subset_monai.zip\"\n", - "download_and_extract(\n", - " url, filepath=\"./rxrx1_subset_monai.zip\", output_dir=\".\", hash_val=\"5eea02f6b0a6d8cbce6ad66949257438\"\n", - ")" + "md5 = \"5eea02f6b0a6d8cbce6ad66949257438\"\n", + "download_and_extract(url, filepath=\"./rxrx1_subset_monai.zip\", output_dir=\".\", hash_val=md5, hash_type=\"md5\")" ] }, { @@ -791,7 +790,7 @@ "metadata": {}, "outputs": [], "source": [ - "device = \"cuda:0\"\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "model = DenseNet169(spatial_dims=2, in_channels=6, out_channels=num_classes, pretrained=True)\n", "# Note: In the DenseNet169 constructor, the parameter in_channels=6 already adds a\n", "# 6-channel input layer to the network and connects it to the following\n", diff --git a/modules/3d_image_transforms.ipynb b/modules/3d_image_transforms.ipynb index 9d55f995c1..36fa39b54b 100644 --- a/modules/3d_image_transforms.ipynb +++ b/modules/3d_image_transforms.ipynb @@ -130,7 +130,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/modules/autoencoder_mednist.ipynb b/modules/autoencoder_mednist.ipynb index 7b32779f60..94a02a3649 100644 --- a/modules/autoencoder_mednist.ipynb +++ b/modules/autoencoder_mednist.ipynb @@ -175,7 +175,7 @@ "compressed_file = os.path.join(root_dir, \"MedNIST.tar.gz\")\n", "data_dir = os.path.join(root_dir, \"MedNIST\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/modules/cross_validation_models_ensemble.ipynb b/modules/cross_validation_models_ensemble.ipynb index 057a612c9c..0ada70c7e9 100644 --- a/modules/cross_validation_models_ensemble.ipynb +++ b/modules/cross_validation_models_ensemble.ipynb @@ -160,7 +160,7 @@ "source": [ "set_determinism(seed=0)\n", "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n", - "device = torch.device(\"cuda:0\")" + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")" ] }, { diff --git a/modules/csv_datasets.ipynb b/modules/csv_datasets.ipynb index 3e4b46e5b5..6db00af2c4 100644 --- a/modules/csv_datasets.ipynb +++ b/modules/csv_datasets.ipynb @@ -140,7 +140,7 @@ "compressed_file = os.path.join(root_dir, \"MedNIST.tar.gz\")\n", "data_dir = os.path.join(root_dir, \"MedNIST\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/modules/decollate_batch.ipynb b/modules/decollate_batch.ipynb index c0e4c36401..7fc2df1536 100644 --- a/modules/decollate_batch.ipynb +++ b/modules/decollate_batch.ipynb @@ -164,7 +164,7 @@ "source": [ "set_determinism(seed=0)\n", "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n", - "device = torch.device(\"cuda:0\")" + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")" ] }, { diff --git a/modules/engines/gan_training.py b/modules/engines/gan_training.py index c0d941b90f..a8917c60bc 100644 --- a/modules/engines/gan_training.py +++ b/modules/engines/gan_training.py @@ -61,7 +61,7 @@ def main(): md5_value = "0bc7306e7427e00ad1c5526a6677552d" extract_dir = "data" tar_save_path = os.path.join(extract_dir, "MedNIST.tar.gz") - download_and_extract(mednist_url, tar_save_path, extract_dir, md5_value) + download_and_extract(mednist_url, tar_save_path, extract_dir, md5_value, "md5") hand_dir = os.path.join(extract_dir, "MedNIST", "Hand") real_data = [{"hand": os.path.join(hand_dir, filename)} for filename in os.listdir(hand_dir)] diff --git a/modules/integrate_3rd_party_transforms.ipynb b/modules/integrate_3rd_party_transforms.ipynb index 191efb283a..2b6b7d1a46 100644 --- a/modules/integrate_3rd_party_transforms.ipynb +++ b/modules/integrate_3rd_party_transforms.ipynb @@ -154,7 +154,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/modules/interpretability/cats_and_dogs.ipynb b/modules/interpretability/cats_and_dogs.ipynb index 53b625b9b8..af210540d2 100644 --- a/modules/interpretability/cats_and_dogs.ipynb +++ b/modules/interpretability/cats_and_dogs.ipynb @@ -147,7 +147,7 @@ " + \"3E1C3F21-ECDB-4869-8368-6DEBA77B919F/kagglecatsanddogs_5340.zip\"\n", " )\n", " md5 = \"e137a4507370d942469b6d267a24ea04\"\n", - " download_and_extract(url, output_dir=data_path, hash_val=md5)" + " download_and_extract(url, output_dir=data_path, hash_val=md5, hash_type=\"md5\")" ] }, { diff --git a/modules/interpretability/covid_classification.ipynb b/modules/interpretability/covid_classification.ipynb index 17586b8926..655c5e1256 100644 --- a/modules/interpretability/covid_classification.ipynb +++ b/modules/interpretability/covid_classification.ipynb @@ -139,7 +139,7 @@ "train_md5 = \"3e8d3e6ca43903ead0666eb6ec8849d8\"\n", "train_zip = os.path.join(root_dir, \"covid_train.zip\")\n", "train_dir = os.path.join(root_dir, \"covid\")\n", - "download_and_extract(train_url, train_zip, train_dir, train_md5)" + "download_and_extract(train_url, train_zip, train_dir, train_md5, \"md5\")" ] }, { diff --git a/modules/jupyter_utils.ipynb b/modules/jupyter_utils.ipynb index c677ec407f..2b0ef354f5 100644 --- a/modules/jupyter_utils.ipynb +++ b/modules/jupyter_utils.ipynb @@ -123,7 +123,7 @@ "outputs": [], "source": [ "# Create UNet, DiceLoss and Adam optimizer\n", - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "net = UNet(spatial_dims=2, in_channels=1, out_channels=1, channels=(8, 16, 32), strides=(2, 2)).to(device)\n", "\n", "lossfn = DiceLoss(sigmoid=True)\n", diff --git a/modules/lazy_resampling_benchmark.ipynb b/modules/lazy_resampling_benchmark.ipynb index 4c677945a0..2259e18489 100644 --- a/modules/lazy_resampling_benchmark.ipynb +++ b/modules/lazy_resampling_benchmark.ipynb @@ -151,7 +151,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/modules/mednist_GAN_tutorial.ipynb b/modules/mednist_GAN_tutorial.ipynb index 50a108b440..f0ba5e340d 100644 --- a/modules/mednist_GAN_tutorial.ipynb +++ b/modules/mednist_GAN_tutorial.ipynb @@ -199,7 +199,7 @@ "compressed_file = os.path.join(root_dir, \"MedNIST.tar.gz\")\n", "data_dir = os.path.join(root_dir, \"MedNIST\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)\n", + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")\n", "\n", "hands = [os.path.join(data_dir, \"Hand\", x) for x in os.listdir(os.path.join(data_dir, \"Hand\"))]" ] @@ -261,7 +261,7 @@ "metadata": {}, "outputs": [], "source": [ - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "disc_net = Discriminator(\n", " in_shape=(1, 64, 64),\n", " channels=(8, 16, 32, 64, 1),\n", diff --git a/modules/mednist_GAN_workflow_array.ipynb b/modules/mednist_GAN_workflow_array.ipynb index c3db4d8cea..770b080b57 100644 --- a/modules/mednist_GAN_workflow_array.ipynb +++ b/modules/mednist_GAN_workflow_array.ipynb @@ -174,7 +174,7 @@ "compressed_file = os.path.join(root_dir, \"MedNIST.tar.gz\")\n", "data_dir = os.path.join(root_dir, \"MedNIST\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)\n", + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")\n", "\n", "hands = [os.path.join(data_dir, \"Hand\", x) for x in os.listdir(os.path.join(data_dir, \"Hand\"))]" ] @@ -214,7 +214,7 @@ "source": [ "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n", "set_determinism(0)\n", - "device = torch.device(\"cuda:0\")" + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")" ] }, { diff --git a/modules/mednist_GAN_workflow_dict.ipynb b/modules/mednist_GAN_workflow_dict.ipynb index 1842617580..5e1a0b4c6e 100644 --- a/modules/mednist_GAN_workflow_dict.ipynb +++ b/modules/mednist_GAN_workflow_dict.ipynb @@ -173,7 +173,7 @@ "compressed_file = os.path.join(root_dir, \"MedNIST.tar.gz\")\n", "data_dir = os.path.join(root_dir, \"MedNIST\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { @@ -221,7 +221,7 @@ "source": [ "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n", "set_determinism(0)\n", - "device = torch.device(\"cuda:0\")" + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")" ] }, { diff --git a/modules/postprocessing_transforms.ipynb b/modules/postprocessing_transforms.ipynb index f3db627edc..3a2cf92c7e 100644 --- a/modules/postprocessing_transforms.ipynb +++ b/modules/postprocessing_transforms.ipynb @@ -201,7 +201,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { @@ -353,7 +353,7 @@ "outputs": [], "source": [ "# standard PyTorch program style: create UNet, DiceLoss and Adam optimizer\n", - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "model = UNet(\n", " spatial_dims=3,\n", " in_channels=1,\n", diff --git a/modules/public_datasets.ipynb b/modules/public_datasets.ipynb index 25ecf5f59d..ab5e5cd483 100644 --- a/modules/public_datasets.ipynb +++ b/modules/public_datasets.ipynb @@ -316,7 +316,7 @@ "metadata": {}, "outputs": [], "source": [ - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "net = DenseNet121(spatial_dims=2, in_channels=1, out_channels=6).to(device)\n", "loss = torch.nn.CrossEntropyLoss()\n", "opt = torch.optim.Adam(net.parameters(), 1e-5)" @@ -505,7 +505,7 @@ "metadata": {}, "outputs": [], "source": [ - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "net = UNet(\n", " spatial_dims=3,\n", " in_channels=1,\n", @@ -620,7 +620,7 @@ " dataset_dir = os.path.join(root_dir, \"ixi\")\n", " tarfile_name = f\"{dataset_dir}.tar\"\n", " if download:\n", - " download_and_extract(self.resource, tarfile_name, dataset_dir, self.md5)\n", + " download_and_extract(self.resource, tarfile_name, dataset_dir, self.md5, \"md5\")\n", " # as a quick demo, we just use 10 images to show\n", "\n", " self.datalist = [\n", diff --git a/modules/tcia_dataset.ipynb b/modules/tcia_dataset.ipynb index 09a8a51aed..a1c7d17a08 100644 --- a/modules/tcia_dataset.ipynb +++ b/modules/tcia_dataset.ipynb @@ -703,7 +703,7 @@ "outputs": [], "source": [ "# standard PyTorch program style: create UNet, DiceLoss and Adam optimizer\n", - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "model = UNet(\n", " spatial_dims=3,\n", " in_channels=1,\n", diff --git a/modules/transform_visualization.ipynb b/modules/transform_visualization.ipynb index 90e0632494..6f3ef2cfd3 100644 --- a/modules/transform_visualization.ipynb +++ b/modules/transform_visualization.ipynb @@ -150,7 +150,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/modules/transforms_demo_2d.ipynb b/modules/transforms_demo_2d.ipynb index d7a408d243..f9141ff77d 100644 --- a/modules/transforms_demo_2d.ipynb +++ b/modules/transforms_demo_2d.ipynb @@ -203,7 +203,7 @@ "compressed_file = os.path.join(root_dir, \"warwick_qu_dataset_released_2016_07_08.zip\")\n", "data_dir = os.path.join(root_dir, \"Warwick QU Dataset (Released 2016_07_08)\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/modules/varautoencoder_mednist.ipynb b/modules/varautoencoder_mednist.ipynb index e2454cea32..9b97a9a82e 100644 --- a/modules/varautoencoder_mednist.ipynb +++ b/modules/varautoencoder_mednist.ipynb @@ -224,7 +224,7 @@ "\n", " compressed_file = os.path.join(root_dir, \"MedNIST.tar.gz\")\n", " if not os.path.exists(mednist_folder):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { diff --git a/modules/workflow_profiling.ipynb b/modules/workflow_profiling.ipynb index 01c9140b7b..e490a36e3e 100644 --- a/modules/workflow_profiling.ipynb +++ b/modules/workflow_profiling.ipynb @@ -962,7 +962,7 @@ } ], "source": [ - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "net = UNet(\n", " spatial_dims=3,\n", " in_channels=1,\n", @@ -1310,7 +1310,7 @@ "from monai.engines import SupervisedTrainer # noqa\n", "from ignite.engine import Events # noqa\n", "\n", - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "net = UNet(\n", " spatial_dims=3,\n", " in_channels=1,\n", diff --git a/pathology/hovernet/hovernet_torch.ipynb b/pathology/hovernet/hovernet_torch.ipynb index 23f9172166..a8e7b49783 100644 --- a/pathology/hovernet/hovernet_torch.ipynb +++ b/pathology/hovernet/hovernet_torch.ipynb @@ -519,7 +519,7 @@ ], "source": [ "stage = 0\n", - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "\n", "\n", "def create_model(stage, device):\n", diff --git a/performance_profiling/radiology/train_base_nvtx.py b/performance_profiling/radiology/train_base_nvtx.py index ff54fbb7ff..b5e5b43eb0 100644 --- a/performance_profiling/radiology/train_base_nvtx.py +++ b/performance_profiling/radiology/train_base_nvtx.py @@ -59,7 +59,7 @@ compressed_file = os.path.join(root_dir, "Task09_Spleen.tar") data_root = os.path.join(root_dir, "Task09_Spleen") if not os.path.exists(data_root): - download_and_extract(resource, compressed_file, root_dir, md5) + download_and_extract(resource, compressed_file, root_dir, md5, "md5") out_dir = "./outputs_base" diff --git a/performance_profiling/radiology/train_fast_nvtx.py b/performance_profiling/radiology/train_fast_nvtx.py index 029e192c56..aad4aef225 100644 --- a/performance_profiling/radiology/train_fast_nvtx.py +++ b/performance_profiling/radiology/train_fast_nvtx.py @@ -63,7 +63,7 @@ compressed_file = os.path.join(root_dir, "Task09_Spleen.tar") data_root = os.path.join(root_dir, "Task09_Spleen") if not os.path.exists(data_root): - download_and_extract(resource, compressed_file, root_dir, md5) + download_and_extract(resource, compressed_file, root_dir, md5, "md5") out_dir = "./outputs_fast" diff --git a/runner.sh b/runner.sh index 5a24aa2a2c..36225ec59b 100755 --- a/runner.sh +++ b/runner.sh @@ -139,7 +139,6 @@ skip_run_papermill=("${skip_run_papermill[@]}" .*TCIA_PROSTATEx_Prostate_MRI_Ana skip_run_papermill=("${skip_run_papermill[@]}" .*maisi_inference_tutorial.ipynb*) skip_run_papermill=("${skip_run_papermill[@]}" .*image_restoration.ipynb*) # monai.networks.nets.restormer not yet in dev branch skip_run_papermill=("${skip_run_papermill[@]}" .*05_spleen_segmentation_lightning*) # requires GPU; hardcoded .to("cuda") with no CPU fallback -skip_run_papermill=("${skip_run_papermill[@]}" .*deep_atlas_tutorial*) # requires GPU; device hardcoded to "cuda:0" # output formatting separator="" diff --git a/self_supervised_pretraining/vit_unetr_ssl/ssl_train.ipynb b/self_supervised_pretraining/vit_unetr_ssl/ssl_train.ipynb index 92198ef9d9..32019b9842 100644 --- a/self_supervised_pretraining/vit_unetr_ssl/ssl_train.ipynb +++ b/self_supervised_pretraining/vit_unetr_ssl/ssl_train.ipynb @@ -248,7 +248,7 @@ "# Training Config\n", "\n", "# Define Network ViT backbone & Loss & Optimizer\n", - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "model = ViTAutoEnc(\n", " in_channels=1,\n", " img_size=(96, 96, 96),\n", diff --git a/vista_3d/vista3d_spleen_finetune.ipynb b/vista_3d/vista3d_spleen_finetune.ipynb index a8564ce16a..d2ebc08eb1 100644 --- a/vista_3d/vista3d_spleen_finetune.ipynb +++ b/vista_3d/vista3d_spleen_finetune.ipynb @@ -167,7 +167,7 @@ "compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n", "data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n", "if not os.path.exists(data_dir):\n", - " download_and_extract(resource, compressed_file, root_dir, md5)" + " download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")" ] }, { @@ -436,7 +436,7 @@ "outputs": [], "source": [ "# standard PyTorch program style: create UNet, DiceLoss and Adam optimizer\n", - "device = torch.device(\"cuda:0\")\n", + "device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n", "model = vista3d132(encoder_embed_dim=48, in_channels=1).to(device)\n", "model.load_state_dict(torch.load(os.path.join(root_dir, \"model.pt\"), weights_only=True))\n", "loss_function = DiceLoss(to_onehot_y=False, sigmoid=True)\n",