Skip to content

Commit d33f0f7

Browse files
committed
[DLMED] update transforms and AMP notebooks
1 parent 1b3183c commit d33f0f7

File tree

4 files changed

+320
-95
lines changed

4 files changed

+320
-95
lines changed

3d_image_transforms.ipynb

Lines changed: 156 additions & 84 deletions
Large diffs are not rendered by default.

automatic_mixed_precision.ipynb

Lines changed: 164 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"This tutorial shows how to apply the automatic mixed precision (AMP) feature of PyTorch to training and validation programs. \n",
1515
"It's modified from the Spleen 3D segmentation tutorial notebook, and compares the training speed and memory usage with/without AMP.\n",
1616
"\n",
17-
"The Spleen dataset can be downloaded from http://medicaldecathlon.com/."
17+
"The Spleen dataset can be downloaded from http://medicaldecathlon.com/.\n",
18+
"\n",
19+
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Project-MONAI/MONAI/blob/master/examples/notebooks/automatic_mixed_precision.ipynb)"
1820
]
1921
},
2022
{
@@ -62,16 +64,111 @@
6264
"cell_type": "markdown",
6365
"metadata": {},
6466
"source": [
65-
"## Set MSD Spleen dataset path"
67+
"## Setup environment"
6668
]
6769
},
6870
{
6971
"cell_type": "code",
7072
"execution_count": 2,
7173
"metadata": {},
74+
"outputs": [
75+
{
76+
"name": "stdout",
77+
"output_type": "stream",
78+
"text": [
79+
"Note: you may need to restart the kernel to use updated packages.\n"
80+
]
81+
}
82+
],
83+
"source": [
84+
"%pip install -qU \"monai[gdown, nibabel]\""
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": 3,
90+
"metadata": {},
91+
"outputs": [
92+
{
93+
"name": "stdout",
94+
"output_type": "stream",
95+
"text": [
96+
"Note: you may need to restart the kernel to use updated packages.\n"
97+
]
98+
}
99+
],
100+
"source": [
101+
"%pip install -qU matplotlib\n",
102+
"%matplotlib inline"
103+
]
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"metadata": {},
108+
"source": [
109+
"## Setup data directory\n",
110+
"\n",
111+
"You can specify a directory with the `MONAI_DATA_DIRECTORY` environment variable. \n",
112+
"This allows you to save results and reuse downloads. \n",
113+
"If not specified a temporary directory will be used."
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 4,
119+
"metadata": {},
120+
"outputs": [
121+
{
122+
"name": "stdout",
123+
"output_type": "stream",
124+
"text": [
125+
"root dir is: /workspace/data/medical\n"
126+
]
127+
}
128+
],
129+
"source": [
130+
"directory = os.environ.get(\"MONAI_DATA_DIRECTORY\")\n",
131+
"root_dir = tempfile.mkdtemp() if directory is None else directory\n",
132+
"print(f\"root dir is: {root_dir}\")"
133+
]
134+
},
135+
{
136+
"cell_type": "markdown",
137+
"metadata": {},
138+
"source": [
139+
"## Download dataset\n",
140+
"\n",
141+
"Downloads and extracts the Decathlon Spleen dataset."
142+
]
143+
},
144+
{
145+
"cell_type": "code",
146+
"execution_count": 5,
147+
"metadata": {},
148+
"outputs": [],
149+
"source": [
150+
"resource = \"https://drive.google.com/uc?id=1jzeNU1EKnK81PyTsrx0ujfNl-t0Jo8uE\"\n",
151+
"md5 = \"410d4a301da4e5b2f6f86ec3ddba524e\"\n",
152+
"\n",
153+
"compressed_file = os.path.join(root_dir, \"Task09_Spleen.tar\")\n",
154+
"data_root = os.path.join(root_dir, \"Task09_Spleen\")\n",
155+
"if not os.path.exists(data_root):\n",
156+
" download_and_extract(resource, compressed_file, root_dir, md5)"
157+
]
158+
},
159+
{
160+
"cell_type": "markdown",
161+
"metadata": {},
162+
"source": [
163+
"## Set MSD Spleen dataset path"
164+
]
165+
},
166+
{
167+
"cell_type": "code",
168+
"execution_count": 6,
169+
"metadata": {},
72170
"outputs": [],
73171
"source": [
74-
"data_root = '/workspace/data/medical/Task09_Spleen'\n",
75172
"train_images = sorted(glob.glob(os.path.join(data_root, 'imagesTr', '*.nii.gz')))\n",
76173
"train_labels = sorted(glob.glob(os.path.join(data_root, 'labelsTr', '*.nii.gz')))\n",
77174
"data_dicts = [{'image': image_name, 'label': label_name}\n",
@@ -88,7 +185,7 @@
88185
},
89186
{
90187
"cell_type": "code",
91-
"execution_count": 3,
188+
"execution_count": 7,
92189
"metadata": {},
93190
"outputs": [],
94191
"source": [
@@ -128,7 +225,7 @@
128225
},
129226
{
130227
"cell_type": "code",
131-
"execution_count": 4,
228+
"execution_count": 8,
132229
"metadata": {
133230
"scrolled": true
134231
},
@@ -247,14 +344,42 @@
247344
"cell_type": "markdown",
248345
"metadata": {},
249346
"source": [
250-
"## Expected memory usage during training with AMP"
347+
"## Check the memory usage during training with AMP"
251348
]
252349
},
253350
{
254-
"cell_type": "markdown",
351+
"cell_type": "code",
352+
"execution_count": 14,
255353
"metadata": {},
354+
"outputs": [
355+
{
356+
"name": "stdout",
357+
"output_type": "stream",
358+
"text": [
359+
"Tue Aug 11 05:17:40 2020 \r\n",
360+
"+-----------------------------------------------------------------------------+\r\n",
361+
"| NVIDIA-SMI 440.44 Driver Version: 440.44 CUDA Version: 10.2 |\r\n",
362+
"|-------------------------------+----------------------+----------------------+\r\n",
363+
"| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\r\n",
364+
"| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\r\n",
365+
"|===============================+======================+======================|\r\n",
366+
"| 0 Tesla V100-PCIE... Off | 00000000:02:00.0 Off | 0 |\r\n",
367+
"| N/A 38C P0 36W / 250W | 3329MiB / 32510MiB | 0% Default |\r\n",
368+
"+-------------------------------+----------------------+----------------------+\r\n",
369+
"| 1 Tesla V100-PCIE... Off | 00000000:03:00.0 Off | 0 |\r\n",
370+
"| N/A 40C P0 38W / 250W | 4722MiB / 32510MiB | 0% Default |\r\n",
371+
"+-------------------------------+----------------------+----------------------+\r\n",
372+
" \r\n",
373+
"+-----------------------------------------------------------------------------+\r\n",
374+
"| Processes: GPU Memory |\r\n",
375+
"| GPU PID Type Process name Usage |\r\n",
376+
"|=============================================================================|\r\n",
377+
"+-----------------------------------------------------------------------------+\r\n"
378+
]
379+
}
380+
],
256381
"source": [
257-
"![image](./images/mem_amp_on.png)"
382+
"! nvidia-smi"
258383
]
259384
},
260385
{
@@ -281,14 +406,42 @@
281406
"cell_type": "markdown",
282407
"metadata": {},
283408
"source": [
284-
"## Expected memory usage during training without AMP"
409+
"## Check the memory usage during training without AMP"
285410
]
286411
},
287412
{
288-
"cell_type": "markdown",
413+
"cell_type": "code",
414+
"execution_count": 16,
289415
"metadata": {},
416+
"outputs": [
417+
{
418+
"name": "stdout",
419+
"output_type": "stream",
420+
"text": [
421+
"Tue Aug 11 05:20:12 2020 \r\n",
422+
"+-----------------------------------------------------------------------------+\r\n",
423+
"| NVIDIA-SMI 440.44 Driver Version: 440.44 CUDA Version: 10.2 |\r\n",
424+
"|-------------------------------+----------------------+----------------------+\r\n",
425+
"| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |\r\n",
426+
"| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |\r\n",
427+
"|===============================+======================+======================|\r\n",
428+
"| 0 Tesla V100-PCIE... Off | 00000000:02:00.0 Off | 0 |\r\n",
429+
"| N/A 41C P0 37W / 250W | 4579MiB / 32510MiB | 0% Default |\r\n",
430+
"+-------------------------------+----------------------+----------------------+\r\n",
431+
"| 1 Tesla V100-PCIE... Off | 00000000:03:00.0 Off | 0 |\r\n",
432+
"| N/A 40C P0 38W / 250W | 4722MiB / 32510MiB | 0% Default |\r\n",
433+
"+-------------------------------+----------------------+----------------------+\r\n",
434+
" \r\n",
435+
"+-----------------------------------------------------------------------------+\r\n",
436+
"| Processes: GPU Memory |\r\n",
437+
"| GPU PID Type Process name Usage |\r\n",
438+
"|=============================================================================|\r\n",
439+
"+-----------------------------------------------------------------------------+\r\n"
440+
]
441+
}
442+
],
290443
"source": [
291-
"![image](./images/mem_amp_off.png)"
444+
"! nvidia-smi"
292445
]
293446
},
294447
{

images/mem_amp_off.png

-59.8 KB
Binary file not shown.

images/mem_amp_on.png

-59.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)