Skip to content

update tutorials/modules for metatensor #771

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 18 commits into from
Jul 13, 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
2 changes: 1 addition & 1 deletion acceleration/fast_training_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@
" with torch.no_grad():\n",
" val_loader_iterator = iter(val_loader)\n",
"\n",
" for val_step in range(len(val_loader)):\n",
" for _ in range(len(val_loader)):\n",
" # profiling: val dataload\n",
" with nvtx.annotate(\"dataload\", color=\"red\") if profiling else no_profiling:\n",
" val_data = next(val_loader_iterator)\n",
Expand Down
2 changes: 1 addition & 1 deletion modules/2d_slices_from_3d_sampling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@
" device=torch.device(\"cpu\"),\n",
" padding_mode=\"replicate\",\n",
" )\n",
" val_output = slice_inferer(val_images, model)\n",
" val_output = slice_inferer(val_images, model).cpu()\n",
" dice_metric(y_pred=val_output > 0.5, y=val_data[\"seg\"])\n",
" print(\"Dice: \", dice_metric.get_buffer()[-1][0])\n",
" fig = plt.figure(figsize=(10, 4))\n",
Expand Down
4 changes: 2 additions & 2 deletions modules/2d_slices_from_3d_training.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@
" device=torch.device(\"cpu\"),\n",
" padding_mode=\"replicate\",\n",
" )\n",
" val_output = slice_inferer(val_images, model)\n",
" val_output = slice_inferer(val_images, model).to(\"cpu\")\n",
" dice_metric(y_pred=val_output > 0.5, y=val_data[\"seg\"])\n",
" print(\"Dice: \", dice_metric.get_buffer()[-1][0])\n",
" fig = plt.figure(figsize=(10, 4))\n",
Expand Down Expand Up @@ -549,7 +549,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
"version": "3.8.0"
}
},
"nbformat": 4,
Expand Down
155 changes: 86 additions & 69 deletions modules/3d_image_transforms.ipynb

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions modules/benchmark_global_mutual_information.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@
" a2: numpy array of shape HWD\n",
" \"\"\"\n",
" antspyx_result = ants.image_mutual_information(\n",
" ants.from_numpy(a1),\n",
" ants.from_numpy(a2)\n",
" ants.from_numpy(a1.detach().cpu().numpy()),\n",
" ants.from_numpy(a2.detach().cpu().numpy())\n",
" )\n",
" monai_result = GlobalMutualInformationLoss(\n",
" kernel_type=\"b-spline\",\n",
Expand Down Expand Up @@ -471,7 +471,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
"version": "3.8.0"
}
},
"nbformat": 4,
Expand Down
54 changes: 29 additions & 25 deletions modules/image_dataset.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {
"execution": {
"iopub.execute_input": "2021-01-14T12:34:04.909819Z",
Expand All @@ -59,26 +59,29 @@
"name": "stdout",
"output_type": "stream",
"text": [
"MONAI version: 0.6.0rc1+23.gc6793fd0\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: c6793fd0f316a448778d0047664aaf8c1895fe1c\n",
"MONAI version: 0.9.1rc1+6.gcbe16eb3\n",
"Numpy version: 1.23.0\n",
"Pytorch version: 1.12.0+cu102\n",
"MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False\n",
"MONAI rev id: cbe16eb326830ef31479fd34ae1e44d27fc2d64d\n",
"MONAI __file__: /home/wenqil/Documents/MONAI/monai/__init__.py\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",
"Pytorch Ignite version: 0.4.9\n",
"Nibabel version: 4.0.1\n",
"scikit-image version: 0.19.3\n",
"Pillow version: 9.2.0\n",
"Tensorboard version: 2.9.1\n",
"gdown version: 4.5.1\n",
"TorchVision version: 0.13.0+cu102\n",
"tqdm version: 4.64.0\n",
"lmdb version: 1.3.0\n",
"psutil version: 5.9.1\n",
"pandas version: 1.4.3\n",
"einops version: 0.4.1\n",
"transformers version: 4.20.1\n",
"mlflow version: 1.27.0\n",
"pynrrd version: 0.4.3\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",
Expand Down Expand Up @@ -124,7 +127,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -150,7 +153,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {
"execution": {
"iopub.execute_input": "2021-01-14T12:34:07.530773Z",
Expand All @@ -173,8 +176,9 @@
"source": [
"class TestCompose(Compose):\n",
" def __call__(self, data, meta):\n",
" data = self.transforms[0](data, meta) # ensure channel first\n",
" data, _, meta[\"affine\"] = self.transforms[1](data, meta[\"affine\"]) # spacing\n",
" data = self.transforms[0](data) # ensure channel first\n",
" data = self.transforms[1](data) # spacing\n",
" meta = data.meta\n",
" if len(self.transforms) == 3:\n",
" return self.transforms[2](data), meta # image contrast\n",
" return data, meta\n",
Expand Down Expand Up @@ -213,7 +217,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -227,7 +231,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.8.0"
}
},
"nbformat": 4,
Expand Down
5 changes: 3 additions & 2 deletions modules/integrate_3rd_party_transforms.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
" ScaleIntensityRanged(keys=[\"image\"], a_min=-57,\n",
" a_max=164, b_min=0.0, b_max=1.0, clip=True),\n",
" CropForegroundd(keys=[\"image\", \"label\"], source_key=\"image\"),\n",
" EnsureTyped(keys=[\"image\", \"label\"], data_type=\"numpy\"),\n",
"]"
]
},
Expand Down Expand Up @@ -472,7 +473,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -486,7 +487,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.10"
"version": "3.8.0"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions modules/interpretability/cats_and_dogs.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@
" axes = np.asarray(axes) if nims == 1 else axes\n",
" for d, ax in zip(data, axes.ravel()):\n",
" # channel last for matplotlib\n",
" im = np.moveaxis(d[\"image\"], 0, -1)\n",
" im = np.moveaxis(d[\"image\"].detach().cpu().numpy(), 0, -1)\n",
" ax.imshow(im, cmap='gray')\n",
" ax.set_title(Animals(d['label']).name, fontsize=25)\n",
" ax.axis(\"off\")\n",
Expand Down Expand Up @@ -608,7 +608,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
"version": "3.8.0"
}
},
"nbformat": 4,
Expand Down
1,117 changes: 61 additions & 1,056 deletions modules/inverse_transforms_and_test_time_augmentations.ipynb

Large diffs are not rendered by default.

71 changes: 38 additions & 33 deletions modules/nifti_read_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,29 @@
"name": "stdout",
"output_type": "stream",
"text": [
"MONAI version: 0.8.1\n",
"Numpy version: 1.21.5\n",
"Pytorch version: 1.10.2\n",
"MONAI flags: HAS_EXT = False, USE_COMPILED = False\n",
"MONAI rev id: 71ff399a3ea07aef667b23653620a290364095b1\n",
"MONAI version: 0.9.1rc2+2.gc0268266.dirty\n",
"Numpy version: 1.23.0\n",
"Pytorch version: 1.12.0+cu102\n",
"MONAI flags: HAS_EXT = False, USE_COMPILED = False, USE_META_DICT = False\n",
"MONAI rev id: c0268266f688c3bacd9c92ef7458f54b6afdf609\n",
"MONAI __file__: /home/wenqil/Documents/MONAI/monai/__init__.py\n",
"\n",
"Optional dependencies:\n",
"Pytorch Ignite version: 0.4.8\n",
"Nibabel version: 3.2.1\n",
"scikit-image version: 0.19.1\n",
"Pillow version: 9.0.0\n",
"Tensorboard version: 2.8.0\n",
"gdown version: 4.2.0\n",
"TorchVision version: 0.11.3\n",
"tqdm version: 4.62.3\n",
"Pytorch Ignite version: 0.4.9\n",
"Nibabel version: 4.0.1\n",
"scikit-image version: 0.19.3\n",
"Pillow version: 9.2.0\n",
"Tensorboard version: 2.9.1\n",
"gdown version: 4.5.1\n",
"TorchVision version: 0.13.0+cu102\n",
"tqdm version: 4.64.0\n",
"lmdb version: 1.3.0\n",
"psutil version: 5.9.0\n",
"pandas version: 1.1.5\n",
"einops version: 0.4.0\n",
"transformers version: 4.16.1\n",
"mlflow version: 1.23.1\n",
"psutil version: 5.9.1\n",
"pandas version: 1.4.3\n",
"einops version: 0.4.1\n",
"transformers version: 4.20.1\n",
"mlflow version: 1.27.0\n",
"pynrrd version: 0.4.3\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",
Expand Down Expand Up @@ -100,7 +102,6 @@
"from monai.data import (\n",
" ArrayDataset, GridPatchDataset, create_test_image_3d, PatchIter)\n",
"from monai.transforms import (\n",
" AddChannel,\n",
" Compose,\n",
" LoadImage,\n",
" RandSpatialCrop,\n",
Expand All @@ -125,11 +126,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {
"tags": []
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"/tmp/tmpkzde8m5b\n"
]
}
],
"source": [
"directory = os.environ.get(\"MONAI_DATA_DIRECTORY\")\n",
"root_dir = tempfile.mkdtemp() if directory is None else directory\n",
Expand Down Expand Up @@ -177,7 +186,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"torch.Size([5, 1, 64, 64, 64]) torch.Size([5, 1, 64, 64, 64])\n"
"(5, 1, 64, 64, 64) (5, 1, 64, 64, 64)\n"
]
}
],
Expand All @@ -187,20 +196,16 @@
"\n",
"imtrans = Compose(\n",
" [\n",
" LoadImage(image_only=True),\n",
" LoadImage(image_only=True, ensure_channel_first=True),\n",
" ScaleIntensity(),\n",
" AddChannel(),\n",
" RandSpatialCrop((64, 64, 64), random_size=False),\n",
" EnsureType(),\n",
" ]\n",
")\n",
"\n",
"segtrans = Compose(\n",
" [\n",
" LoadImage(image_only=True),\n",
" AddChannel(),\n",
" LoadImage(image_only=True, ensure_channel_first=True),\n",
" RandSpatialCrop((64, 64, 64), random_size=False),\n",
" EnsureType(),\n",
" ]\n",
")\n",
"\n",
Expand Down Expand Up @@ -236,10 +241,10 @@
}
],
"source": [
"imtrans = Compose([LoadImage(image_only=True),\n",
" ScaleIntensity(), AddChannel(), EnsureType()])\n",
"imtrans = Compose([LoadImage(image_only=True, ensure_channel_first=True),\n",
" ScaleIntensity(), EnsureType()])\n",
"\n",
"segtrans = Compose([LoadImage(image_only=True), AddChannel(), EnsureType()])\n",
"segtrans = Compose([LoadImage(image_only=True, ensure_channel_first=True), EnsureType()])\n",
"\n",
"ds = ArrayDataset(images, imtrans, segs, segtrans)\n",
"patch_iter = PatchIter(patch_size=(64, 64, 64), start_pos=(0, 0, 0))\n",
Expand Down Expand Up @@ -283,7 +288,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -297,7 +302,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.10"
"version": "3.8.0"
}
},
"nbformat": 4,
Expand Down
Loading