Skip to content

Update acceleration tutorials for MetaTensor #786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 21 additions & 70 deletions acceleration/automatic_mixed_precision.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,9 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"outputs": [],
"source": [
"!python -c \"import monai\" || pip install -q \"monai-weekly[nibabel, tqdm]\"\n",
"!python -c \"import matplotlib\" || pip install -q matplotlib\n",
Expand All @@ -54,40 +46,9 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MONAI version: 0.6.0rc1+15.gf3d436a0\n",
"Numpy version: 1.20.3\n",
"Pytorch version: 1.9.0a0+c3d40fd\n",
"MONAI flags: HAS_EXT = True, USE_COMPILED = False\n",
"MONAI rev id: f3d436a09deefcf905ece2faeec37f55ab030003\n",
"\n",
"Optional dependencies:\n",
"Pytorch Ignite version: 0.4.5\n",
"Nibabel version: 3.2.1\n",
"scikit-image version: 0.15.0\n",
"Pillow version: 8.2.0\n",
"Tensorboard version: 2.5.0\n",
"gdown version: 3.13.0\n",
"TorchVision version: 0.10.0a0\n",
"ITK version: 5.1.2\n",
"tqdm version: 4.53.0\n",
"lmdb version: 1.2.1\n",
"psutil version: 5.8.0\n",
"pandas version: 1.1.4\n",
"einops version: 0.3.0\n",
"\n",
"For details about installing the optional dependencies, please visit:\n",
" https://docs.monai.io/en/latest/installation.html#installing-the-recommended-dependencies\n",
"\n"
]
}
],
"outputs": [],
"source": [
"# Copyright 2020 MONAI Consortium\n",
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
Expand All @@ -110,7 +71,7 @@
"import torch\n",
"from monai.apps import download_and_extract\n",
"from monai.config import print_config\n",
"from monai.data import CacheDataset, list_data_collate, decollate_batch\n",
"from monai.data import CacheDataset, DataLoader, decollate_batch\n",
"from monai.inferers import sliding_window_inference\n",
"from monai.losses import DiceLoss\n",
"from monai.metrics import DiceMetric\n",
Expand All @@ -127,11 +88,8 @@
" RandCropByPosNegLabeld,\n",
" ScaleIntensityRanged,\n",
" Spacingd,\n",
" EnsureTyped,\n",
" EnsureType,\n",
")\n",
"from monai.utils import get_torch_version_tuple, set_determinism\n",
"from torch.utils.data import DataLoader\n",
"\n",
"print_config()\n",
"\n",
Expand All @@ -154,17 +112,11 @@
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"root dir is: /workspace/data/medical\n"
]
}
],
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"directory = os.environ.get(\"MONAI_DATA_DIRECTORY\")\n",
"root_dir = tempfile.mkdtemp() if directory is None else directory\n",
Expand All @@ -182,7 +134,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -204,7 +156,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -230,7 +182,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -275,7 +227,6 @@
" fg_indices_key=\"label_fg\",\n",
" bg_indices_key=\"label_bg\",\n",
" ),\n",
" EnsureTyped(keys=[\"image\", \"label\"]),\n",
" ]\n",
" )\n",
" val_transforms = Compose(\n",
Expand All @@ -297,7 +248,6 @@
" clip=True,\n",
" ),\n",
" CropForegroundd(keys=[\"image\", \"label\"], source_key=\"image\"),\n",
" EnsureTyped(keys=[\"image\", \"label\"]),\n",
" ]\n",
" )\n",
" return train_transforms, val_transforms"
Expand All @@ -313,7 +263,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {
"scrolled": true
},
Expand All @@ -335,7 +285,6 @@
" batch_size=2,\n",
" shuffle=True,\n",
" num_workers=1,\n",
" collate_fn=list_data_collate,\n",
" )\n",
" val_loader = DataLoader(val_ds, batch_size=1, num_workers=1)\n",
" device = torch.device(\"cuda:0\")\n",
Expand All @@ -352,8 +301,8 @@
" optimizer = torch.optim.Adam(model.parameters(), 1e-4)\n",
" scaler = torch.cuda.amp.GradScaler() if amp else None\n",
"\n",
" post_pred = Compose([EnsureType(), AsDiscrete(argmax=True, to_onehot=2)])\n",
" post_label = Compose([EnsureType(), AsDiscrete(to_onehot=2)])\n",
" post_pred = Compose([AsDiscrete(argmax=True, to_onehot=2)])\n",
" post_label = Compose([AsDiscrete(to_onehot=2)])\n",
"\n",
" dice_metric = DiceMetric(include_background=False, reduction=\"mean\", get_not_nans=False)\n",
"\n",
Expand Down Expand Up @@ -474,7 +423,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"set_determinism(seed=0)\n",
Expand Down Expand Up @@ -856,7 +807,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -870,7 +821,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.10"
"version": "3.8.13"
}
},
"nbformat": 4,
Expand Down
73 changes: 14 additions & 59 deletions acceleration/dataset_type_performance.ipynb

Large diffs are not rendered by default.

23 changes: 9 additions & 14 deletions acceleration/fast_training_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "python"
Expand Down Expand Up @@ -126,8 +126,6 @@
" ScaleIntensityRanged,\n",
" Spacingd,\n",
" ToDeviced,\n",
" EnsureTyped,\n",
" EnsureType,\n",
")\n",
"from monai.utils import set_determinism\n",
"\n",
Expand Down Expand Up @@ -180,7 +178,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "python"
Expand Down Expand Up @@ -234,7 +232,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "python"
Expand Down Expand Up @@ -298,7 +296,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "python"
Expand Down Expand Up @@ -328,7 +326,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "python"
Expand Down Expand Up @@ -364,8 +362,6 @@
" bg_postfix=\"_bg\",\n",
" image_key=\"image\",\n",
" )),\n",
" # change to execute transforms with Tensor data\n",
" range_func(\"EnsureType\", EnsureTyped(keys=[\"image\", \"label\"])),\n",
" ]\n",
"\n",
" if fast:\n",
Expand Down Expand Up @@ -407,7 +403,6 @@
" clip=True,\n",
" ),\n",
" CropForegroundd(keys=[\"image\", \"label\"], source_key=\"image\"),\n",
" EnsureTyped(keys=[\"image\", \"label\"]),\n",
" ]\n",
" if fast:\n",
" # move the data to GPU and cache to avoid CPU -> GPU sync in every epoch\n",
Expand Down Expand Up @@ -438,7 +433,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "python"
Expand Down Expand Up @@ -518,8 +513,8 @@
" norm=Norm.BATCH,\n",
" ).to(device)\n",
"\n",
" post_pred = Compose([EnsureType(), AsDiscrete(argmax=True, to_onehot=2)])\n",
" post_label = Compose([EnsureType(), AsDiscrete(to_onehot=2)])\n",
" post_pred = Compose([AsDiscrete(argmax=True, to_onehot=2)])\n",
" post_label = Compose([AsDiscrete(to_onehot=2)])\n",
"\n",
" dice_metric = DiceMetric(include_background=False, reduction=\"mean\", get_not_nans=False)\n",
"\n",
Expand Down Expand Up @@ -1020,7 +1015,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "python"
Expand Down
Loading