Skip to content

Commit 3741b49

Browse files
Nic-Mawyli
andauthored
Update minor bugs in all notebooks and examples for v0.3 release (Project-MONAI#40)
* [DLMED] fix bugs for v0.3 release Signed-off-by: Nic Ma <[email protected]> * [DLMED] fix examples Signed-off-by: Nic Ma <[email protected]> * Update unet_segmentation_3d_catalyst.ipynb revert to 6 epochs as this is a simple task Co-authored-by: Wenqi Li <[email protected]>
1 parent 23d9b00 commit 3741b49

10 files changed

+50
-45
lines changed

Diff for: 3d_segmentation/brats_segmentation_3d.ipynb

+5-5
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
},
5454
"outputs": [],
5555
"source": [
56-
"%pip install -qU \"monai[gdown, nibabel]\""
56+
"%pip install -qU \"monai[nibabel]\""
5757
]
5858
},
5959
{
@@ -373,20 +373,20 @@
373373
],
374374
"source": [
375375
"# pick one image from DecathlonDataset to visualize and check the 4 channels\n",
376-
"print(f\"image shape: {val_ds[9]['image'].shape}\")\n",
376+
"print(f\"image shape: {val_ds[2]['image'].shape}\")\n",
377377
"plt.figure(\"image\", (24, 6))\n",
378378
"for i in range(4):\n",
379379
" plt.subplot(1, 4, i + 1)\n",
380380
" plt.title(f\"image channel {i}\")\n",
381-
" plt.imshow(val_ds[9][\"image\"][i, :, :, 20].detach().cpu(), cmap=\"gray\")\n",
381+
" plt.imshow(val_ds[2][\"image\"][i, :, :, 20].detach().cpu(), cmap=\"gray\")\n",
382382
"plt.show()\n",
383383
"# also visualize the 3 channels label corresponding to this image\n",
384-
"print(f\"label shape: {val_ds[9]['label'].shape}\")\n",
384+
"print(f\"label shape: {val_ds[2]['label'].shape}\")\n",
385385
"plt.figure(\"label\", (18, 6))\n",
386386
"for i in range(3):\n",
387387
" plt.subplot(1, 3, i + 1)\n",
388388
" plt.title(f\"label channel {i}\")\n",
389-
" plt.imshow(val_ds[9][\"label\"][i, :, :, 20].detach().cpu())\n",
389+
" plt.imshow(val_ds[2][\"label\"][i, :, :, 20].detach().cpu())\n",
390390
"plt.show()"
391391
]
392392
},

Diff for: 3d_segmentation/spleen_segmentation_3d_lightning.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
}
6161
],
6262
"source": [
63-
"%pip install -q \"monai[gdown, nibabel]\""
63+
"%pip install -q \"monai[nibabel, tensorboard]\""
6464
]
6565
},
6666
{

Diff for: acceleration/distributed_training/brats_training_ddp.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ def main_worker(args):
236236

237237
# create UNet, DiceLoss and Adam optimizer
238238
device = torch.device(f"cuda:{args.local_rank}")
239+
torch.cuda.set_device(device)
239240
if args.network == "UNet":
240241
model = UNet(
241242
dimensions=3,
@@ -250,7 +251,7 @@ def main_worker(args):
250251
loss_function = DiceLoss(to_onehot_y=False, sigmoid=True, squared_pred=True)
251252
optimizer = torch.optim.Adam(model.parameters(), lr=args.lr, weight_decay=1e-5, amsgrad=True)
252253
# wrap the model with DistributedDataParallel module
253-
model = DistributedDataParallel(model, device_ids=[args.local_rank])
254+
model = DistributedDataParallel(model, device_ids=[device])
254255

255256
# start a typical PyTorch training
256257
total_epoch = args.epochs

Diff for: acceleration/fast_training_tutorial.ipynb

+5-8
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@
294294
"\n",
295295
" train_trans, val_trans = transformations()\n",
296296
" # set CacheDataset for MONAI training\n",
297-
" if monai_train:\n",
297+
" if fast:\n",
298298
" train_ds = CacheDataset(data=train_files, transform=train_trans, cache_rate=1.0, num_workers=8)\n",
299299
" val_ds = CacheDataset(data=val_files, transform=val_trans, cache_rate=1.0, num_workers=5)\n",
300300
" # don't need many workers because already cached the data\n",
@@ -313,7 +313,7 @@
313313
" loss_function = DiceLoss(to_onehot_y=True, softmax=True)\n",
314314
"\n",
315315
" # set Novograd optimizer for MONAI training\n",
316-
" if monai_train:\n",
316+
" if fast:\n",
317317
" # Novograd paper suggests to use a bigger LR than Adam,\n",
318318
" # because Adam does normalization by element-wise second moments\n",
319319
" optimizer = Novograd(model.parameters(), learning_rate * 10)\n",
@@ -341,7 +341,7 @@
341341
" inputs, labels = batch_data['image'].to(device), batch_data['label'].to(device)\n",
342342
" optimizer.zero_grad()\n",
343343
" # set AMP for MONAI training\n",
344-
" if monai_train:\n",
344+
" if fast:\n",
345345
" with torch.cuda.amp.autocast():\n",
346346
" outputs = model(inputs)\n",
347347
" loss = loss_function(outputs, labels)\n",
@@ -359,8 +359,6 @@
359359
" f\" step time: {(time.time() - step_start):.4f}\")\n",
360360
" epoch_loss /= step\n",
361361
" epoch_loss_values.append(epoch_loss)\n",
362-
" #if monai_train:\n",
363-
" # train_ds.update_cache()\n",
364362
" print(f\"epoch {epoch + 1} average loss: {epoch_loss:.4f}\")\n",
365363
"\n",
366364
" if (epoch + 1) % val_interval == 0:\n",
@@ -373,7 +371,7 @@
373371
" roi_size = (160, 160, 128)\n",
374372
" sw_batch_size = 4\n",
375373
" # set AMP for MONAI validation\n",
376-
" if monai_train:\n",
374+
" if fast:\n",
377375
" with torch.cuda.amp.autocast():\n",
378376
" val_outputs = sliding_window_inference(val_inputs, roi_size, sw_batch_size, model)\n",
379377
" else:\n",
@@ -396,8 +394,7 @@
396394
" f\" best mean dice: {best_metric:.4f} at epoch: {best_metric_epoch}\")\n",
397395
" print(f\"time consuming of epoch {epoch + 1} is: {(time.time() - epoch_start):.4f}\")\n",
398396
" epoch_times.append(time.time() - epoch_start)\n",
399-
" #if monai_train:\n",
400-
" # train_ds.shutdown()\n",
397+
"\n",
401398
" print(f\"train completed, best_metric: {best_metric:.4f} at epoch: {best_metric_epoch}\"\n",
402399
" f\" total time: {(time.time() - total_start):.4f}\")\n",
403400
" return epoch_num, epoch_loss_values, metric_values, epoch_times, best_metrics_epochs_and_time"

Diff for: acceleration/multi_gpu_test.ipynb

+33-23
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,23 @@
5353
"name": "stdout",
5454
"output_type": "stream",
5555
"text": [
56-
"MONAI version: 0.2.0\n",
57-
"Python version: 3.6.9 |Anaconda, Inc.| (default, Jul 30 2019, 19:07:31) [GCC 7.3.0]\n",
58-
"Numpy version: 1.18.1\n",
59-
"Pytorch version: 1.6.0\n",
56+
"MONAI version: 0.3.0rc4\n",
57+
"Python version: 3.6.10 |Anaconda, Inc.| (default, May 8 2020, 02:54:21) [GCC 7.3.0]\n",
58+
"OS version: Linux (4.4.0-131-generic)\n",
59+
"Numpy version: 1.19.1\n",
60+
"Pytorch version: 1.7.0a0+8deb4fe\n",
61+
"MONAI flags: HAS_EXT = False, USE_COMPILED = False\n",
6062
"\n",
6163
"Optional dependencies:\n",
62-
"Pytorch Ignite version: NOT INSTALLED or UNKNOWN VERSION.\n",
64+
"Pytorch Ignite version: 0.4.2\n",
6365
"Nibabel version: 3.1.1\n",
6466
"scikit-image version: 0.15.0\n",
65-
"Pillow version: 7.2.0\n",
66-
"Tensorboard version: 2.1.0\n",
67+
"Pillow version: 7.0.0\n",
68+
"Tensorboard version: 2.2.0\n",
69+
"gdown version: 3.12.2\n",
70+
"TorchVision version: 0.8.0a0\n",
71+
"ITK version: 5.1.0\n",
72+
"tqdm version: 4.50.0\n",
6773
"\n",
6874
"For details about installing the optional dependencies, please visit:\n",
6975
" https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies\n",
@@ -101,20 +107,20 @@
101107
},
102108
{
103109
"cell_type": "code",
104-
"execution_count": 3,
110+
"execution_count": 2,
105111
"metadata": {},
106112
"outputs": [],
107113
"source": [
108114
"lr = 1e-3\n",
109-
"\n",
115+
"device = torch.device(\"cuda:0\")\n",
110116
"net = UNet(\n",
111117
" dimensions=2,\n",
112118
" in_channels=1,\n",
113119
" out_channels=1,\n",
114120
" channels=(16, 32, 64, 128, 256),\n",
115121
" strides=(2, 2, 2, 2),\n",
116122
" num_res_units=2,\n",
117-
")\n",
123+
").to(device)\n",
118124
"\n",
119125
"\n",
120126
"def fake_loss(y_pred, y):\n",
@@ -135,7 +141,7 @@
135141
},
136142
{
137143
"cell_type": "code",
138-
"execution_count": 4,
144+
"execution_count": 3,
139145
"metadata": {
140146
"pycharm": {
141147
"name": "#%%\n"
@@ -150,22 +156,23 @@
150156
"\tepoch: 2\n",
151157
"\tepoch_length: 2\n",
152158
"\tmax_epochs: 2\n",
153-
"\toutput: 40707.8984375\n",
159+
"\toutput: 34253.46875\n",
154160
"\tbatch: <class 'tuple'>\n",
155161
"\tmetrics: <class 'dict'>\n",
156162
"\tdataloader: <class 'generator'>\n",
157-
"\tseed: 12"
163+
"\tseed: <class 'NoneType'>\n",
164+
"\ttimes: <class 'dict'>"
158165
]
159166
},
160-
"execution_count": 4,
167+
"execution_count": 3,
161168
"metadata": {},
162169
"output_type": "execute_result"
163170
}
164171
],
165172
"source": [
166173
"opt = torch.optim.Adam(net.parameters(), lr)\n",
167-
"trainer = create_multigpu_supervised_trainer(net, opt, fake_loss, [torch.device(\"cuda:0\")])\n",
168-
"trainer.run(fake_data_stream(), 2, 2)\n"
174+
"trainer = create_multigpu_supervised_trainer(net, opt, fake_loss, [device])\n",
175+
"trainer.run(fake_data_stream(), 2, 2)"
169176
]
170177
},
171178
{
@@ -177,7 +184,7 @@
177184
},
178185
{
179186
"cell_type": "code",
180-
"execution_count": 5,
187+
"execution_count": 4,
181188
"metadata": {
182189
"pycharm": {
183190
"name": "#%%\n"
@@ -193,22 +200,23 @@
193200
"\tepoch: 2\n",
194201
"\tepoch_length: 2\n",
195202
"\tmax_epochs: 2\n",
196-
"\toutput: 35669.37109375\n",
203+
"\toutput: 30694.720703125\n",
197204
"\tbatch: <class 'tuple'>\n",
198205
"\tmetrics: <class 'dict'>\n",
199206
"\tdataloader: <class 'generator'>\n",
200-
"\tseed: 12"
207+
"\tseed: <class 'NoneType'>\n",
208+
"\ttimes: <class 'dict'>"
201209
]
202210
},
203-
"execution_count": 5,
211+
"execution_count": 4,
204212
"metadata": {},
205213
"output_type": "execute_result"
206214
}
207215
],
208216
"source": [
209217
"opt = torch.optim.Adam(net.parameters(), lr)\n",
210218
"trainer = create_multigpu_supervised_trainer(net, opt, fake_loss, None)\n",
211-
"trainer.run(fake_data_stream(), 2, 2)\n"
219+
"trainer.run(fake_data_stream(), 2, 2)"
212220
]
213221
},
214222
{
@@ -235,11 +243,12 @@
235243
"\tepoch: 2\n",
236244
"\tepoch_length: 2\n",
237245
"\tmax_epochs: 2\n",
238-
"\toutput: 29662.359375\n",
246+
"\toutput: 26988.939453125\n",
239247
"\tbatch: <class 'tuple'>\n",
240248
"\tmetrics: <class 'dict'>\n",
241249
"\tdataloader: <class 'generator'>\n",
242-
"\tseed: 12"
250+
"\tseed: <class 'NoneType'>\n",
251+
"\ttimes: <class 'dict'>"
243252
]
244253
},
245254
"execution_count": 6,
@@ -248,6 +257,7 @@
248257
}
249258
],
250259
"source": [
260+
"net = net.to(torch.device(\"cpu:0\"))\n",
251261
"opt = torch.optim.Adam(net.parameters(), lr)\n",
252262
"trainer = create_multigpu_supervised_trainer(net, opt, fake_loss, [])\n",
253263
"trainer.run(fake_data_stream(), 2, 2)"

Diff for: modules/dynunet_tutorial.ipynb

+1
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,7 @@
454454
" out_channels=n_class,\n",
455455
" kernel_size=kernels,\n",
456456
" strides=strides,\n",
457+
" upsamle_kernel_size=strides[1:],\n",
457458
" norm_name=\"instance\",\n",
458459
" deep_supervision=True,\n",
459460
" deep_supr_num=2,\n",

Diff for: modules/engines/gan_evaluation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def main():
4343
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
4444

4545
# load generator
46-
network_filepath = glob("./model_out/*.pth")[0]
46+
network_filepath = glob("./model_out/*.pt")[0]
4747
data = torch.load(network_filepath)
4848
latent_size = 64
4949
gen_net = Generator(

Diff for: modules/mednist_GAN_tutorial.ipynb

+1-4
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@
115115
"import tarfile\n",
116116
"import urllib\n",
117117
"\n",
118-
"import IPython\n",
119118
"import matplotlib.pyplot as plt\n",
120119
"import torch\n",
121120
"\n",
@@ -394,9 +393,7 @@
394393
" step += 1\n",
395394
"\n",
396395
" epoch_loss /= step\n",
397-
" epoch_loss_values.append((step, epoch_loss))\n",
398-
"\n",
399-
" IPython.display.clear_output()"
396+
" epoch_loss_values.append((step, epoch_loss))"
400397
]
401398
},
402399
{

Diff for: modules/mednist_GAN_workflow_array.ipynb

-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@
145145
"import sys\n",
146146
"import shutil\n",
147147
"\n",
148-
"import IPython\n",
149148
"import matplotlib.pyplot as plt\n",
150149
"import torch\n",
151150
"\n",

Diff for: modules/public_datasets.ipynb

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
}
4242
],
4343
"source": [
44-
"%pip install -qU \"monai[gdown, nibabel, ignite]\""
44+
"%pip install -qU \"monai[nibabel, ignite]\""
4545
]
4646
},
4747
{

0 commit comments

Comments
 (0)