Skip to content

Commit b6a83da

Browse files
committed
fix #1649
Signed-off-by: YunLiu <[email protected]>
1 parent 1783005 commit b6a83da

File tree

2 files changed

+102
-72
lines changed

2 files changed

+102
-72
lines changed

2d_classification/mednist_tutorial.ipynb

Lines changed: 53 additions & 45 deletions
Large diffs are not rendered by default.

2d_classification/monai_101.ipynb

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
},
4141
{
4242
"cell_type": "code",
43-
"execution_count": null,
43+
"execution_count": 1,
4444
"metadata": {},
4545
"outputs": [],
4646
"source": [
@@ -57,45 +57,47 @@
5757
},
5858
{
5959
"cell_type": "code",
60-
"execution_count": null,
60+
"execution_count": 1,
6161
"metadata": {},
6262
"outputs": [
6363
{
6464
"name": "stderr",
6565
"output_type": "stream",
6666
"text": [
67-
"/usr/local/lib/python3.8/dist-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
67+
"/usr/local/lib/python3.10/dist-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
6868
" from .autonotebook import tqdm as notebook_tqdm\n"
6969
]
7070
},
7171
{
7272
"name": "stdout",
7373
"output_type": "stream",
7474
"text": [
75-
"MONAI version: 1.2.0rc4+21.g7f067564\n",
75+
"MONAI version: 1.3.0\n",
7676
"Numpy version: 1.22.2\n",
77-
"Pytorch version: 2.0.0a0+1767026\n",
78-
"MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False\n",
79-
"MONAI rev id: 7f06756472fd5514c3c2f2a6710e3fa4d1748e90\n",
80-
"MONAI __file__: /workspace/monai/monai-in-dev/monai/__init__.py\n",
77+
"Pytorch version: 2.1.0a0+29c30b1\n",
78+
"MONAI flags: HAS_EXT = True, USE_COMPILED = False, USE_META_DICT = False\n",
79+
"MONAI rev id: 865972f7a791bf7b42efbcd87c8402bd865b329e\n",
80+
"MONAI __file__: /opt/monai/monai/__init__.py\n",
8181
"\n",
8282
"Optional dependencies:\n",
8383
"Pytorch Ignite version: 0.4.11\n",
8484
"ITK version: 5.3.0\n",
8585
"Nibabel version: 5.1.0\n",
86-
"scikit-image version: 0.20.0\n",
86+
"scikit-image version: 0.22.0\n",
87+
"scipy version: 1.11.1\n",
8788
"Pillow version: 9.2.0\n",
8889
"Tensorboard version: 2.9.0\n",
8990
"gdown version: 4.7.1\n",
90-
"TorchVision version: 0.15.0a0\n",
91+
"TorchVision version: 0.16.0a0\n",
9192
"tqdm version: 4.65.0\n",
9293
"lmdb version: 1.4.1\n",
9394
"psutil version: 5.9.4\n",
9495
"pandas version: 1.5.2\n",
9596
"einops version: 0.6.1\n",
9697
"transformers version: 4.21.3\n",
97-
"mlflow version: 2.3.0\n",
98+
"mlflow version: 2.7.1\n",
9899
"pynrrd version: 1.0.0\n",
100+
"clearml version: 1.13.1\n",
99101
"\n",
100102
"For details about installing the optional dependencies, please visit:\n",
101103
" https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies\n",
@@ -111,12 +113,14 @@
111113
"import sys\n",
112114
"import tempfile\n",
113115
"import torch\n",
116+
"import ignite\n",
114117
"\n",
115118
"from monai.apps import MedNISTDataset\n",
116119
"from monai.config import print_config\n",
117120
"from monai.data import DataLoader\n",
118-
"from monai.engines import SupervisedTrainer\n",
119-
"from monai.handlers import StatsHandler\n",
121+
"from monai.engines import SupervisedTrainer, SupervisedEvaluator\n",
122+
"from monai.handlers import StatsHandler, TensorBoardStatsHandler, ValidationHandler\n",
123+
"from monai.handlers.utils import from_engine\n",
120124
"from monai.inferers import SimpleInferer\n",
121125
"from monai.networks import eval_mode\n",
122126
"from monai.networks.nets import densenet121\n",
@@ -139,14 +143,14 @@
139143
},
140144
{
141145
"cell_type": "code",
142-
"execution_count": null,
146+
"execution_count": 2,
143147
"metadata": {},
144148
"outputs": [
145149
{
146150
"name": "stdout",
147151
"output_type": "stream",
148152
"text": [
149-
"/workspace/data\n"
153+
"/workspace/Data\n"
150154
]
151155
}
152156
],
@@ -171,7 +175,7 @@
171175
},
172176
{
173177
"cell_type": "code",
174-
"execution_count": null,
178+
"execution_count": 3,
175179
"metadata": {},
176180
"outputs": [],
177181
"source": [
@@ -205,28 +209,30 @@
205209
},
206210
{
207211
"cell_type": "code",
208-
"execution_count": null,
212+
"execution_count": 4,
209213
"metadata": {},
210214
"outputs": [
211215
{
212216
"name": "stdout",
213217
"output_type": "stream",
214218
"text": [
215-
"2023-04-21 15:37:46,567 - INFO - Verified 'MedNIST.tar.gz', md5: 0bc7306e7427e00ad1c5526a6677552d.\n",
216-
"2023-04-21 15:37:46,567 - INFO - File exists: /workspace/data/MedNIST.tar.gz, skipped downloading.\n",
217-
"2023-04-21 15:37:46,568 - INFO - Non-empty folder exists in /workspace/data/MedNIST, skipped extracting.\n"
219+
"2024-02-22 05:34:20,240 - INFO - Verified 'MedNIST.tar.gz', md5: 0bc7306e7427e00ad1c5526a6677552d.\n",
220+
"2024-02-22 05:34:20,241 - INFO - File exists: /workspace/Data/MedNIST.tar.gz, skipped downloading.\n",
221+
"2024-02-22 05:34:20,241 - INFO - Non-empty folder exists in /workspace/Data/MedNIST, skipped extracting.\n"
218222
]
219223
},
220224
{
221225
"name": "stderr",
222226
"output_type": "stream",
223227
"text": [
224-
"Loading dataset: 100%|██████████| 47164/47164 [00:18<00:00, 2525.58it/s]\n"
228+
"Loading dataset: 100%|██████████| 47164/47164 [00:19<00:00, 2430.78it/s]\n",
229+
"Loading dataset: 100%|██████████| 5895/5895 [00:02<00:00, 2508.13it/s]\n"
225230
]
226231
}
227232
],
228233
"source": [
229-
"dataset = MedNISTDataset(root_dir=root_dir, transform=transform, section=\"training\", download=True)"
234+
"dataset = MedNISTDataset(root_dir=root_dir, transform=transform, section=\"training\", download=True)\n",
235+
"valdata = MedNISTDataset(root_dir=root_dir, transform=transform, section=\"validation\", download=False)"
230236
]
231237
},
232238
{
@@ -238,19 +244,31 @@
238244
"\n",
239245
"To train a model that can perform the classification task, we will use the DenseNet-121 which is known for its performance on the ImageNet dataset.\n",
240246
"\n",
241-
"For a typical supervised training workflow, MONAI provides `SupervisedTrainer` to define the hyper-parameters."
247+
"For a typical supervised training workflow, MONAI provides `SupervisedTrainer` to define the hyper-parameters and `SupervisedEvaluator` to track model progress with handlers to save metrics. "
242248
]
243249
},
244250
{
245251
"cell_type": "code",
246-
"execution_count": null,
252+
"execution_count": 5,
247253
"metadata": {},
248254
"outputs": [],
249255
"source": [
250256
"max_epochs = 5\n",
251257
"model = densenet121(spatial_dims=2, in_channels=1, out_channels=6).to(\"cuda:0\")\n",
252258
"\n",
253259
"logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n",
260+
"\n",
261+
"evaluator = SupervisedEvaluator(\n",
262+
" device=torch.device(\"cuda:0\"),\n",
263+
" val_data_loader=DataLoader(valdata, batch_size=512, shuffle=False, num_workers=4),\n",
264+
" network=model,\n",
265+
" inferer=SimpleInferer(),\n",
266+
" key_val_metric={\"val_acc\": ignite.metrics.Accuracy(from_engine([\"pred\", \"label\"]))},\n",
267+
" val_handlers=[\n",
268+
" StatsHandler(iteration_log=False),\n",
269+
" TensorBoardStatsHandler(iteration_log=False)\n",
270+
" ],\n",
271+
")\n",
254272
"trainer = SupervisedTrainer(\n",
255273
" device=torch.device(\"cuda:0\"),\n",
256274
" max_epochs=max_epochs,\n",
@@ -259,7 +277,11 @@
259277
" optimizer=torch.optim.Adam(model.parameters(), lr=1e-5),\n",
260278
" loss_function=torch.nn.CrossEntropyLoss(),\n",
261279
" inferer=SimpleInferer(),\n",
262-
" train_handlers=StatsHandler(),\n",
280+
" train_handlers=[\n",
281+
" ValidationHandler(validator=evaluator, epoch_level=True, interval=1),\n",
282+
" StatsHandler(),\n",
283+
" TensorBoardStatsHandler(tag_name=\"train_loss\", output_transform=from_engine([\"loss\"], first=True))\n",
284+
" ],\n",
263285
")"
264286
]
265287
},
@@ -290,7 +312,7 @@
290312
},
291313
{
292314
"cell_type": "code",
293-
"execution_count": null,
315+
"execution_count": 7,
294316
"metadata": {},
295317
"outputs": [
296318
{
@@ -344,7 +366,7 @@
344366
"name": "python",
345367
"nbconvert_exporter": "python",
346368
"pygments_lexer": "ipython3",
347-
"version": "3.8.10"
369+
"version": "3.10.12"
348370
}
349371
},
350372
"nbformat": 4,

0 commit comments

Comments
 (0)