|
14 | 14 | "This tutorial shows how to apply the automatic mixed precision (AMP) feature of PyTorch to training and validation programs. \n",
|
15 | 15 | "It's modified from the Spleen 3D segmentation tutorial notebook, and compares the training speed and memory usage with/without AMP.\n",
|
16 | 16 | "\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 | + "[](https://colab.research.google.com/github/Project-MONAI/MONAI/blob/master/examples/notebooks/automatic_mixed_precision.ipynb)" |
18 | 20 | ]
|
19 | 21 | },
|
20 | 22 | {
|
|
62 | 64 | "cell_type": "markdown",
|
63 | 65 | "metadata": {},
|
64 | 66 | "source": [
|
65 |
| - "## Set MSD Spleen dataset path" |
| 67 | + "## Setup environment" |
66 | 68 | ]
|
67 | 69 | },
|
68 | 70 | {
|
69 | 71 | "cell_type": "code",
|
70 | 72 | "execution_count": 2,
|
71 | 73 | "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": {}, |
72 | 170 | "outputs": [],
|
73 | 171 | "source": [
|
74 |
| - "data_root = '/workspace/data/medical/Task09_Spleen'\n", |
75 | 172 | "train_images = sorted(glob.glob(os.path.join(data_root, 'imagesTr', '*.nii.gz')))\n",
|
76 | 173 | "train_labels = sorted(glob.glob(os.path.join(data_root, 'labelsTr', '*.nii.gz')))\n",
|
77 | 174 | "data_dicts = [{'image': image_name, 'label': label_name}\n",
|
|
88 | 185 | },
|
89 | 186 | {
|
90 | 187 | "cell_type": "code",
|
91 |
| - "execution_count": 3, |
| 188 | + "execution_count": 7, |
92 | 189 | "metadata": {},
|
93 | 190 | "outputs": [],
|
94 | 191 | "source": [
|
|
128 | 225 | },
|
129 | 226 | {
|
130 | 227 | "cell_type": "code",
|
131 |
| - "execution_count": 4, |
| 228 | + "execution_count": 8, |
132 | 229 | "metadata": {
|
133 | 230 | "scrolled": true
|
134 | 231 | },
|
|
247 | 344 | "cell_type": "markdown",
|
248 | 345 | "metadata": {},
|
249 | 346 | "source": [
|
250 |
| - "## Expected memory usage during training with AMP" |
| 347 | + "## Check the memory usage during training with AMP" |
251 | 348 | ]
|
252 | 349 | },
|
253 | 350 | {
|
254 |
| - "cell_type": "markdown", |
| 351 | + "cell_type": "code", |
| 352 | + "execution_count": 14, |
255 | 353 | "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 | + ], |
256 | 381 | "source": [
|
257 |
| - "" |
| 382 | + "! nvidia-smi" |
258 | 383 | ]
|
259 | 384 | },
|
260 | 385 | {
|
|
281 | 406 | "cell_type": "markdown",
|
282 | 407 | "metadata": {},
|
283 | 408 | "source": [
|
284 |
| - "## Expected memory usage during training without AMP" |
| 409 | + "## Check the memory usage during training without AMP" |
285 | 410 | ]
|
286 | 411 | },
|
287 | 412 | {
|
288 |
| - "cell_type": "markdown", |
| 413 | + "cell_type": "code", |
| 414 | + "execution_count": 16, |
289 | 415 | "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 | + ], |
290 | 443 | "source": [
|
291 |
| - "" |
| 444 | + "! nvidia-smi" |
292 | 445 | ]
|
293 | 446 | },
|
294 | 447 | {
|
|
0 commit comments