.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/physics/demo_mri_tour.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note New to DeepInverse? Get started with the basics with the :ref:`5 minute quickstart tutorial `. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_physics_demo_mri_tour.py: Tour of MRI functionality in DeepInverse ======================================== This example presents the various datasets, forward physics and models available in DeepInverse for Magnetic Resonance Imaging (MRI) problems: - Physics: :class:`deepinv.physics.MRI`, :class:`deepinv.physics.MultiCoilMRI`, :class:`deepinv.physics.DynamicMRI` - Datasets: raw kspace with the `FastMRI `__ dataset :class:`deepinv.datasets.FastMRISliceDataset` and an in-memory easy-to-use version :class:`deepinv.datasets.SimpleFastMRISliceDataset`, and raw dynamic k-t-space data with the `CMRxRecon `__ dataset. - Models: :class:`deepinv.models.VarNet` (VarNet :footcite:t:`hammernik2018learning`, E2E-VarNet :footcite:t:`sriram2020end`), :class:`deepinv.models.MoDL` (a simple MoDL :footcite:t:`aggarwal2018modl` unrolled model) Contents: 1. Get started with FastMRI (singlecoil + multicoil) 2. Train an accelerated MRI with neural networks 3. Load raw FastMRI data (singlecoil + multicoil) 4. Train using raw data 5. Explore 3D MRI 6. Explore dynamic MRI .. GENERATED FROM PYTHON SOURCE LINES 31-39 .. code-block:: Python import deepinv as dinv import torch, torchvision from torch.utils.data import DataLoader device = dinv.utils.get_freer_gpu() if torch.cuda.is_available() else "cpu" rng = torch.Generator(device=device).manual_seed(0) .. GENERATED FROM PYTHON SOURCE LINES 40-61 1. Get started with FastMRI ~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can get started with our simple `FastMRI `__ mini slice subsets which provide quick, easy-to-use, in-memory datasets which can be used for simulation experiments. .. important:: By using this dataset, you confirm that you have agreed to and signed the `FastMRI data use agreement `_. .. seealso:: Datasets :class:`deepinv.datasets.FastMRISliceDataset` :class:`deepinv.datasets.SimpleFastMRISliceDataset` We provide convenient datasets to easily load both raw and reconstructed FastMRI images. You can download more data on the `FastMRI site `_. Load mini demo knee and brain datasets (original data is 320x320 but we resize to 128 for speed): .. GENERATED FROM PYTHON SOURCE LINES 61-82 .. code-block:: Python transform = torchvision.transforms.Resize(128) knee_dataset = dinv.datasets.SimpleFastMRISliceDataset( dinv.utils.get_data_home(), anatomy="knee", transform=transform, train=True, download=True, ) brain_dataset = dinv.datasets.SimpleFastMRISliceDataset( dinv.utils.get_data_home(), anatomy="brain", transform=transform, train=True, download=True, ) img_size = knee_dataset[0].shape[-2:] # (128, 128) dinv.utils.plot({"knee": knee_dataset[0], "brain": brain_dataset[0]}) .. image-sg:: /auto_examples/physics/images/sphx_glr_demo_mri_tour_001.png :alt: knee, brain :srcset: /auto_examples/physics/images/sphx_glr_demo_mri_tour_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/820529 [00:00`_ dataset and simulate 3D single-coil or multi-coil Fourier measurements using :class:`deepinv.physics.MRI` or :class:`deepinv.physics.MultiCoilMRI`. .. GENERATED FROM PYTHON SOURCE LINES 415-436 .. code-block:: Python x = ( torch.from_numpy( dinv.utils.demo.load_np_url( "https://huggingface.co/datasets/deepinv/images/resolve/main/brainweb_t1_ICBM_1mm_subject_0.npy?download=true" ) ) .unsqueeze(0) .unsqueeze(0) .to(device) ) x = torch.cat([x, torch.zeros_like(x)], dim=1) # add imaginary dimension print(x.shape) # (B, C, D, H, W) where D is depth physics = dinv.physics.MultiCoilMRI(img_size=x.shape[1:], three_d=True, device=device) physics = dinv.physics.MRI(img_size=x.shape[1:], three_d=True, device=device) dinv.utils.plot_ortho3D([x, physics(x)], titles=["x", "y"]) .. image-sg:: /auto_examples/physics/images/sphx_glr_demo_mri_tour_008.png :alt: x, y :srcset: /auto_examples/physics/images/sphx_glr_demo_mri_tour_008.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none torch.Size([1, 2, 181, 217, 181]) /home/runner/work/deepinv/deepinv/deepinv/utils/plotting.py:1106: UserWarning: This figure was using a layout engine that is incompatible with subplots_adjust and/or tight_layout; not calling subplots_adjust. plt.subplots_adjust(hspace=0.05, wspace=0.05) .. GENERATED FROM PYTHON SOURCE LINES 437-448 6. Explore dynamic MRI ~~~~~~~~~~~~~~~~~~~~~~ Finally, we show how to use the dynamic MRI for image sequence data of shape ``(B, C, T, H, W)`` where ``T`` is the time dimension. Note that this is also compatible with 3D MRI. We use dynamic MRI data from the `CMRxRecon `_ challenge of cardiac cine sequences and load them using :class:`deepinv.datasets.CMRxReconSliceDataset` provided in deepinv. We download demo data from the first patient including ground truth images, undersampled kspace, and associated masks: .. GENERATED FROM PYTHON SOURCE LINES 448-469 .. code-block:: Python dinv.datasets.download_archive( dinv.utils.get_image_url("CMRxRecon.zip"), dinv.utils.get_data_home() / "CMRxRecon.zip", extract=True, ) dataset = dinv.datasets.CMRxReconSliceDataset( dinv.utils.get_data_home() / "CMRxRecon", ) x, y, params = next(iter(DataLoader(dataset))) print( f""" Ground truth: {x.shape} (B, C, T, H, W) Measurements: {y.shape} Acc. mask: {params["mask"].shape} """ ) .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/24695321 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_mri_tour.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_mri_tour.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_