Add Explicit hash_type Arguments and Device Fallback (#2077) · Project-MONAI/tutorials@b4b61f8 · GitHub
Skip to content

Commit b4b61f8

Browse files
Add Explicit hash_type Arguments and Device Fallback (#2077)
Fixes #2076. ### Description MONAI 1.6.1 hardened the use of downloading and extraction functions to use sha256 by default. This can be made compatible with old usage of these functions by explicitly adding `"md5"` as the `hash_type` to use this algorithm instead. In the future these should be changed to use sha256, this is lower priority since all the usage here relates to data downloading. ### Checks <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [ ] Avoid including large-size files in the PR. - [ ] Clean up long text outputs from code cells in the notebook. - [ ] For security purposes, please check the contents and remove any sensitive info such as user names and private key. - [ ] Ensure (1) hyperlinks and markdown anchors are working (2) use relative paths for tutorial repo files (3) put figure and graphs in the `./figure` folder - [ ] Notebook runs automatically `./runner.sh -t <path to .ipynb file>` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Dataset downloads across tutorials and workflows now explicitly use MD5 checksum verification. - Training, inference, and preprocessing examples now automatically use available CUDA hardware or fall back to CPU when no GPU is detected. - CUDA-specific operations now avoid running when CUDA is unavailable. - **Documentation** - Updated notebook download steps and execution states for improved reproducibility. - The Deep Atlas tutorial is now included in the standard notebook execution workflow. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 2ce5139 commit b4b61f8

63 files changed

Lines changed: 158 additions & 129 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/test-modified.yml‎

Lines changed: 1 addition & 0 deletions

‎2d_classification/mednist_tutorial.ipynb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@
194194
"compressed_file = os.path.join(root_dir, \"MedNIST.tar.gz\")\n",
195195
"data_dir = os.path.join(root_dir, \"MedNIST\")\n",
196196
"if not os.path.exists(data_dir):\n",
197-
" download_and_extract(resource, compressed_file, root_dir, md5)"
197+
" download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")"
198198
]
199199
},
200200
{

‎2d_classification/monai_201.ipynb‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,13 @@
195195
"max_epochs = 5\n",
196196
"save_interval = 2\n",
197197
"out_dir = \"./eval\"\n",
198-
"model = densenet121(spatial_dims=2, in_channels=1, out_channels=6).to(\"cuda:0\")\n",
198+
"device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n",
199+
"model = densenet121(spatial_dims=2, in_channels=1, out_channels=6).to(device)\n",
199200
"\n",
200201
"logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
201202
"\n",
202203
"evaluator = SupervisedEvaluator(\n",
203-
" device=torch.device(\"cuda:0\"),\n",
204+
" device=device,\n",
204205
" val_data_loader=DataLoader(valdata, batch_size=512, shuffle=False, num_workers=4),\n",
205206
" network=model,\n",
206207
" inferer=SimpleInferer(),\n",
@@ -209,7 +210,7 @@
209210
")\n",
210211
"\n",
211212
"trainer = SupervisedTrainer(\n",
212-
" device=torch.device(\"cuda:0\"),\n",
213+
" device=device,\n",
213214
" max_epochs=max_epochs,\n",
214215
" train_data_loader=DataLoader(dataset, batch_size=512, shuffle=True, num_workers=4),\n",
215216
" network=model,\n",
@@ -313,7 +314,7 @@
313314
],
314315
"source": [
315316
"evaluator = SupervisedEvaluator(\n",
316-
" device=torch.device(\"cuda:0\"),\n",
317+
" device=device,\n",
317318
" val_data_loader=DataLoader(testdata, batch_size=1, num_workers=0),\n",
318319
" network=model,\n",
319320
" inferer=SimpleInferer(),\n",

‎2d_registration/registration_mednist.ipynb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@
376376
}
377377
],
378378
"source": [
379-
"device = torch.device(\"cuda:0\")\n",
379+
"device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n",
380380
"model = GlobalNet(\n",
381381
" image_size=(64, 64), spatial_dims=2, in_channels=2, num_channel_initial=16, depth=3 # moving and fixed\n",
382382
").to(device)\n",

‎3d_classification/densenet_training_array.ipynb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@
206206
" dataset_dir = os.path.join(root_dir, \"ixi\")\n",
207207
" tarfile_name = f\"{dataset_dir}.tar\"\n",
208208
"\n",
209-
" download_and_extract(resource, tarfile_name, dataset_dir, md5)"
209+
" download_and_extract(resource, tarfile_name, dataset_dir, md5, \"md5\")"
210210
]
211211
},
212212
{

‎3d_registration/learn2reg_nlst_paired_lung_ct.ipynb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@
598598
"outputs": [],
599599
"source": [
600600
"# device, optimizer, epoch and batch settings\n",
601-
"device = \"cuda:0\"\n",
601+
"device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n",
602602
"batch_size = 4\n",
603603
"lr = 1e-4\n",
604604
"weight_decay = 1e-5\n",

‎3d_regression/densenet_training_array.ipynb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@
211211
" dataset_dir = os.path.join(root_dir, \"ixi\")\n",
212212
" tarfile_name = f\"{dataset_dir}.tar\"\n",
213213
"\n",
214-
" download_and_extract(resource, tarfile_name, dataset_dir, md5)"
214+
" download_and_extract(resource, tarfile_name, dataset_dir, md5, \"md5\")"
215215
]
216216
},
217217
{

‎3d_segmentation/brats_segmentation_3d.ipynb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@
442442
"VAL_AMP = True\n",
443443
"\n",
444444
"# standard PyTorch program style: create SegResNet, DiceLoss and Adam optimizer\n",
445-
"device = torch.device(\"cuda:0\")\n",
445+
"device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n",
446446
"model = SegResNet(\n",
447447
" blocks_down=[1, 2, 2, 4],\n",
448448
" blocks_up=[1, 1, 1],\n",

‎3d_segmentation/spleen_segmentation_3d.ipynb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@
210210
"compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n",
211211
"data_dir = os.path.join(root_dir, \"Task09_Spleen\")\n",
212212
"if not os.path.exists(data_dir):\n",
213-
" download_and_extract(resource, compressed_file, root_dir, md5)"
213+
" download_and_extract(resource, compressed_file, root_dir, md5, \"md5\")"
214214
]
215215
},
216216
{
@@ -432,7 +432,7 @@
432432
"outputs": [],
433433
"source": [
434434
"# standard PyTorch program style: create UNet, DiceLoss and Adam optimizer\n",
435-
"device = torch.device(\"cuda:0\")\n",
435+
"device = torch.device(\"cuda:0\" if torch.cuda.device_count() > 0 else \"cpu\")\n",
436436
"model = UNet(\n",
437437
" spatial_dims=3,\n",
438438
" in_channels=1,\n",

‎3d_segmentation/spleen_segmentation_3d_lightning.ipynb‎

Lines changed: 3 additions & 3 deletions

0 commit comments

Comments
 (0)