Skip to content

Commit 6863062

Browse files
yuchen-xuYuchen Xu
andauthored
Update acceleration tutorials for MetaTensor (#786)
* updated AMP tutorial for 0.9.1rc2 * updated for 0.9.1rc2 compatibility * modification done, pending final checks * tests run on 0.9.1rc3, ready for review * updated for 0.9.1rc3 Co-authored-by: Yuchen Xu <[email protected]>
1 parent 4270a2d commit 6863062

File tree

5 files changed

+67
-234
lines changed

5 files changed

+67
-234
lines changed

acceleration/automatic_mixed_precision.ipynb

Lines changed: 21 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,9 @@
2828
},
2929
{
3030
"cell_type": "code",
31-
"execution_count": 2,
31+
"execution_count": null,
3232
"metadata": {},
33-
"outputs": [
34-
{
35-
"name": "stdout",
36-
"output_type": "stream",
37-
"text": [
38-
"Note: you may need to restart the kernel to use updated packages.\n"
39-
]
40-
}
41-
],
33+
"outputs": [],
4234
"source": [
4335
"!python -c \"import monai\" || pip install -q \"monai-weekly[nibabel, tqdm]\"\n",
4436
"!python -c \"import matplotlib\" || pip install -q matplotlib\n",
@@ -54,40 +46,9 @@
5446
},
5547
{
5648
"cell_type": "code",
57-
"execution_count": 1,
49+
"execution_count": null,
5850
"metadata": {},
59-
"outputs": [
60-
{
61-
"name": "stdout",
62-
"output_type": "stream",
63-
"text": [
64-
"MONAI version: 0.6.0rc1+15.gf3d436a0\n",
65-
"Numpy version: 1.20.3\n",
66-
"Pytorch version: 1.9.0a0+c3d40fd\n",
67-
"MONAI flags: HAS_EXT = True, USE_COMPILED = False\n",
68-
"MONAI rev id: f3d436a09deefcf905ece2faeec37f55ab030003\n",
69-
"\n",
70-
"Optional dependencies:\n",
71-
"Pytorch Ignite version: 0.4.5\n",
72-
"Nibabel version: 3.2.1\n",
73-
"scikit-image version: 0.15.0\n",
74-
"Pillow version: 8.2.0\n",
75-
"Tensorboard version: 2.5.0\n",
76-
"gdown version: 3.13.0\n",
77-
"TorchVision version: 0.10.0a0\n",
78-
"ITK version: 5.1.2\n",
79-
"tqdm version: 4.53.0\n",
80-
"lmdb version: 1.2.1\n",
81-
"psutil version: 5.8.0\n",
82-
"pandas version: 1.1.4\n",
83-
"einops version: 0.3.0\n",
84-
"\n",
85-
"For details about installing the optional dependencies, please visit:\n",
86-
" https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies\n",
87-
"\n"
88-
]
89-
}
90-
],
51+
"outputs": [],
9152
"source": [
9253
"# Copyright 2020 MONAI Consortium\n",
9354
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
@@ -110,7 +71,7 @@
11071
"import torch\n",
11172
"from monai.apps import download_and_extract\n",
11273
"from monai.config import print_config\n",
113-
"from monai.data import CacheDataset, list_data_collate, decollate_batch\n",
74+
"from monai.data import CacheDataset, DataLoader, decollate_batch\n",
11475
"from monai.inferers import sliding_window_inference\n",
11576
"from monai.losses import DiceLoss\n",
11677
"from monai.metrics import DiceMetric\n",
@@ -127,11 +88,8 @@
12788
" RandCropByPosNegLabeld,\n",
12889
" ScaleIntensityRanged,\n",
12990
" Spacingd,\n",
130-
" EnsureTyped,\n",
131-
" EnsureType,\n",
13291
")\n",
13392
"from monai.utils import get_torch_version_tuple, set_determinism\n",
134-
"from torch.utils.data import DataLoader\n",
13593
"\n",
13694
"print_config()\n",
13795
"\n",
@@ -154,17 +112,11 @@
154112
},
155113
{
156114
"cell_type": "code",
157-
"execution_count": 2,
158-
"metadata": {},
159-
"outputs": [
160-
{
161-
"name": "stdout",
162-
"output_type": "stream",
163-
"text": [
164-
"root dir is: /workspace/data/medical\n"
165-
]
166-
}
167-
],
115+
"execution_count": null,
116+
"metadata": {
117+
"scrolled": true
118+
},
119+
"outputs": [],
168120
"source": [
169121
"directory = os.environ.get(\"MONAI_DATA_DIRECTORY\")\n",
170122
"root_dir = tempfile.mkdtemp() if directory is None else directory\n",
@@ -182,7 +134,7 @@
182134
},
183135
{
184136
"cell_type": "code",
185-
"execution_count": 3,
137+
"execution_count": null,
186138
"metadata": {},
187139
"outputs": [],
188140
"source": [
@@ -204,7 +156,7 @@
204156
},
205157
{
206158
"cell_type": "code",
207-
"execution_count": 4,
159+
"execution_count": null,
208160
"metadata": {},
209161
"outputs": [],
210162
"source": [
@@ -230,7 +182,7 @@
230182
},
231183
{
232184
"cell_type": "code",
233-
"execution_count": 5,
185+
"execution_count": null,
234186
"metadata": {},
235187
"outputs": [],
236188
"source": [
@@ -275,7 +227,6 @@
275227
" fg_indices_key=\"label_fg\",\n",
276228
" bg_indices_key=\"label_bg\",\n",
277229
" ),\n",
278-
" EnsureTyped(keys=[\"image\", \"label\"]),\n",
279230
" ]\n",
280231
" )\n",
281232
" val_transforms = Compose(\n",
@@ -297,7 +248,6 @@
297248
" clip=True,\n",
298249
" ),\n",
299250
" CropForegroundd(keys=[\"image\", \"label\"], source_key=\"image\"),\n",
300-
" EnsureTyped(keys=[\"image\", \"label\"]),\n",
301251
" ]\n",
302252
" )\n",
303253
" return train_transforms, val_transforms"
@@ -313,7 +263,7 @@
313263
},
314264
{
315265
"cell_type": "code",
316-
"execution_count": 6,
266+
"execution_count": null,
317267
"metadata": {
318268
"scrolled": true
319269
},
@@ -335,7 +285,6 @@
335285
" batch_size=2,\n",
336286
" shuffle=True,\n",
337287
" num_workers=1,\n",
338-
" collate_fn=list_data_collate,\n",
339288
" )\n",
340289
" val_loader = DataLoader(val_ds, batch_size=1, num_workers=1)\n",
341290
" device = torch.device(\"cuda:0\")\n",
@@ -352,8 +301,8 @@
352301
" optimizer = torch.optim.Adam(model.parameters(), 1e-4)\n",
353302
" scaler = torch.cuda.amp.GradScaler() if amp else None\n",
354303
"\n",
355-
" post_pred = Compose([EnsureType(), AsDiscrete(argmax=True, to_onehot=2)])\n",
356-
" post_label = Compose([EnsureType(), AsDiscrete(to_onehot=2)])\n",
304+
" post_pred = Compose([AsDiscrete(argmax=True, to_onehot=2)])\n",
305+
" post_label = Compose([AsDiscrete(to_onehot=2)])\n",
357306
"\n",
358307
" dice_metric = DiceMetric(include_background=False, reduction=\"mean\", get_not_nans=False)\n",
359308
"\n",
@@ -474,7 +423,9 @@
474423
{
475424
"cell_type": "code",
476425
"execution_count": null,
477-
"metadata": {},
426+
"metadata": {
427+
"scrolled": true
428+
},
478429
"outputs": [],
479430
"source": [
480431
"set_determinism(seed=0)\n",
@@ -856,7 +807,7 @@
856807
],
857808
"metadata": {
858809
"kernelspec": {
859-
"display_name": "Python 3",
810+
"display_name": "Python 3 (ipykernel)",
860811
"language": "python",
861812
"name": "python3"
862813
},
@@ -870,7 +821,7 @@
870821
"name": "python",
871822
"nbconvert_exporter": "python",
872823
"pygments_lexer": "ipython3",
873-
"version": "3.7.10"
824+
"version": "3.8.13"
874825
}
875826
},
876827
"nbformat": 4,

acceleration/dataset_type_performance.ipynb

Lines changed: 14 additions & 59 deletions
Large diffs are not rendered by default.

acceleration/fast_training_tutorial.ipynb

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
},
4646
{
4747
"cell_type": "code",
48-
"execution_count": 1,
48+
"execution_count": null,
4949
"metadata": {
5050
"vscode": {
5151
"languageId": "python"
@@ -126,8 +126,6 @@
126126
" ScaleIntensityRanged,\n",
127127
" Spacingd,\n",
128128
" ToDeviced,\n",
129-
" EnsureTyped,\n",
130-
" EnsureType,\n",
131129
")\n",
132130
"from monai.utils import set_determinism\n",
133131
"\n",
@@ -180,7 +178,7 @@
180178
},
181179
{
182180
"cell_type": "code",
183-
"execution_count": 4,
181+
"execution_count": null,
184182
"metadata": {
185183
"vscode": {
186184
"languageId": "python"
@@ -234,7 +232,7 @@
234232
},
235233
{
236234
"cell_type": "code",
237-
"execution_count": 5,
235+
"execution_count": null,
238236
"metadata": {
239237
"vscode": {
240238
"languageId": "python"
@@ -298,7 +296,7 @@
298296
},
299297
{
300298
"cell_type": "code",
301-
"execution_count": 19,
299+
"execution_count": null,
302300
"metadata": {
303301
"vscode": {
304302
"languageId": "python"
@@ -328,7 +326,7 @@
328326
},
329327
{
330328
"cell_type": "code",
331-
"execution_count": 8,
329+
"execution_count": null,
332330
"metadata": {
333331
"vscode": {
334332
"languageId": "python"
@@ -364,8 +362,6 @@
364362
" bg_postfix=\"_bg\",\n",
365363
" image_key=\"image\",\n",
366364
" )),\n",
367-
" # change to execute transforms with Tensor data\n",
368-
" range_func(\"EnsureType\", EnsureTyped(keys=[\"image\", \"label\"])),\n",
369365
" ]\n",
370366
"\n",
371367
" if fast:\n",
@@ -407,7 +403,6 @@
407403
" clip=True,\n",
408404
" ),\n",
409405
" CropForegroundd(keys=[\"image\", \"label\"], source_key=\"image\"),\n",
410-
" EnsureTyped(keys=[\"image\", \"label\"]),\n",
411406
" ]\n",
412407
" if fast:\n",
413408
" # move the data to GPU and cache to avoid CPU -> GPU sync in every epoch\n",
@@ -438,7 +433,7 @@
438433
},
439434
{
440435
"cell_type": "code",
441-
"execution_count": 16,
436+
"execution_count": null,
442437
"metadata": {
443438
"vscode": {
444439
"languageId": "python"
@@ -518,8 +513,8 @@
518513
" norm=Norm.BATCH,\n",
519514
" ).to(device)\n",
520515
"\n",
521-
" post_pred = Compose([EnsureType(), AsDiscrete(argmax=True, to_onehot=2)])\n",
522-
" post_label = Compose([EnsureType(), AsDiscrete(to_onehot=2)])\n",
516+
" post_pred = Compose([AsDiscrete(argmax=True, to_onehot=2)])\n",
517+
" post_label = Compose([AsDiscrete(to_onehot=2)])\n",
523518
"\n",
524519
" dice_metric = DiceMetric(include_background=False, reduction=\"mean\", get_not_nans=False)\n",
525520
"\n",
@@ -1020,7 +1015,7 @@
10201015
},
10211016
{
10221017
"cell_type": "code",
1023-
"execution_count": 15,
1018+
"execution_count": null,
10241019
"metadata": {
10251020
"vscode": {
10261021
"languageId": "python"

0 commit comments

Comments
 (0)