<!-- DO NOT EDIT. -->
<!-- THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. -->
<!-- TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: -->
<!-- "auto_examples/models/demo_mri_pretrained.py" -->
<!-- LINE NUMBERS ARE GIVEN BELOW. -->

#### NOTE
New to DeepInverse? Get started with the basics with the
[5 minute quickstart tutorial](https://deepinv.org/auto_examples/basics/demo_quickstart.html.md#sphx-glr-auto-examples-basics-demo-quickstart-py)..

<a id="sphx-glr-auto-examples-models-demo-mri-pretrained-py"></a>

# Reconstruct undersampled k-space for cardiac and brain MRI

This demo reconstructs undersampled k-space data for 2D cardiac and brain MRI on:

* single-coil cardiac cine MRI from [CMRxRecon](https://cmrxrecon.github.io) train set ([`deepinv.datasets.CMRxReconSliceDataset`](https://deepinv.org/api/stubs/deepinv.datasets.CMRxReconSliceDataset.html.md#deepinv.datasets.CMRxReconSliceDataset)) <sup>[1](#footcite-wang2024cmrxrecon)</sup>;
* 12-coil brain k-space from [Calgary-Campinas](https://sites.google.com/view/calgary-campinas-dataset/) test set ([`deepinv.datasets.CalgarySliceDataset`](https://deepinv.org/api/stubs/deepinv.datasets.CalgarySliceDataset.html.md#deepinv.datasets.CalgarySliceDataset)) <sup>[2](#footcite-souza2018open)</sup>;
* 16-coil brain k-space from [FastMRI](https://fastmri.med.nyu.edu) test set ([`deepinv.datasets.FastMRISliceDataset`](https://deepinv.org/api/stubs/deepinv.datasets.FastMRISliceDataset.html.md#deepinv.datasets.FastMRISliceDataset)) <sup>[3](#footcite-zbontar2018fastmri)</sup>.

We demonstrate pretrained models:

* Joint-ICNet <sup>[4](#footcite-jun2021joint)</sup>, pretrained on Calgary data, from DIRECT;
* vSHARP <sup>[5](#footcite-yiasemis2024vsharp)</sup><sup>[6](#footcite-yiasemis2025uniform)</sup>, pretrained on fastMRI brain, knee, prostate, and CMRxRecon cardiac data as the [UNIFORM](https://openreview.net/forum?id=I13Y1nU6gs) model, from [DIRECT](https://huggingface.co/NKI-AI/direct-uniform);
* [`RAM`](https://deepinv.org/api/stubs/deepinv.models.RAM.html.md#deepinv.models.RAM) <sup>[7](#footcite-terris2025reconstruct)</sup>, pretrained on natural images, abdominal CT and knee MRI.

#### NOTE
This example requires [DIRECT](https://docs.aiforoncology.nl/direct/) (Netherlands Cancer Institute) and Python >=3.12. Install with `pip install deepinv[direct]`.

<!-- GENERATED FROM PYTHON SOURCE LINES 23-33 -->
```Python
import matplotlib.pyplot as plt
import torch
import deepinv as dinv
from torch.utils.data import DataLoader

device = dinv.utils.get_device()
metric = dinv.metric.SharpnessIndex()
```

```none
Selected GPU 0 with 28660.0 MiB free memory
```

<!-- GENERATED FROM PYTHON SOURCE LINES 35-44 -->

## Cardiac MRI reconstruction

Use a sample cardiac cine volume from [`deepinv.datasets.CMRxReconSliceDataset`](https://deepinv.org/api/stubs/deepinv.datasets.CMRxReconSliceDataset.html.md#deepinv.datasets.CMRxReconSliceDataset), which loads ground-truth fully-sampled recon, undersampled y, and mask.
Take the middle time-frame (the 5th time-frame out of 10) and a single slice for the demo, and construct a 2D [`deepinv.physics.MRI`](https://deepinv.org/api/stubs/deepinv.physics.MRI.html.md#deepinv.physics.MRI) physics.

#### NOTE
For dynamic MRI reconstruction, use [`deepinv.physics.DynamicMRI`](https://deepinv.org/api/stubs/deepinv.physics.DynamicMRI.html.md#deepinv.physics.DynamicMRI) along with a model that can reconstruct temporal data.

<!-- GENERATED FROM PYTHON SOURCE LINES 44-72 -->
```Python
dinv.datasets.download_archive(
    dinv.utils.get_image_url("CMRxRecon.zip"),
    dinv.utils.get_cache_home() / "CMRxRecon.zip",
    extract=True,
)

dataset = dinv.datasets.CMRxReconSliceDataset(
    dinv.utils.get_cache_home() / "CMRxRecon", use_dict_output=True
)
batch = next(iter(DataLoader(dataset)))
x, y, params = batch["x"], batch["y"].to(device), batch["params"]

# Remove time dim
x, y = x[:, :, x.shape[2] // 2], y[:, :, y.shape[2] // 2]
mask = params["mask"].squeeze(2).to(device)

dinv.utils.plot(
    {
        f"y of shape {tuple(y.shape)}": y,
        f"mask of acc {1 / mask.mean().item():.2f}": mask,
    },
    figsize=(6, 6),
    suptitle="CMRxRecon data",
)

physics = dinv.physics.MRI(img_size=mask.shape[-2:], mask=mask, device=device)
```

```none
File already downloaded: /local/jtachell/.cache/deepinv/CMRxRecon.zip. Skipping...

  0%|          | 0/1 [00:00<?, ?it/s]
100%|██████████| 1/1 [00:00<00:00,  2.90it/s]
100%|██████████| 1/1 [00:00<00:00,  2.89it/s]
/local/jtachell/deepinv/deepinv/deepinv/utils/plotting.py:519: UserWarning: This figure was using a layout engine that is incompatible with subplots_adjust and/or tight_layout; not calling subplots_adjust.
  fig.subplots_adjust(top=0.75)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 73-76 -->

Perform reconstruction with pretrained models.
We use the vSHARP 2D model from DIRECT, and RAM from Terris *et al.*<sup>[7](#footcite-terris2025reconstruct)</sup>.
We compare to the zero-filled reconstruction using the sharpness metric.

<!-- GENERATED FROM PYTHON SOURCE LINES 76-100 -->
```Python
vsharp = dinv.models.DIRECTModel(
    model_name="vsharp_cardiac", pretrained=True, device=device
)
ram = dinv.models.RAM(device=device, pretrained=True)

with torch.no_grad():
    x_zf = physics.A_adjoint(y).cpu()
    x_vsharp = vsharp(y, physics).cpu()

    # Although RAM is scale-equivariant, we bring y into friendlier scale (currently it's very small)
    x_ram = ram(y / x_zf.max(), physics).cpu() * x_zf.max()

dinv.utils.plot(
    {"Fully-sampled": x, "Zero-filled": x_zf, "vSHARP": x_vsharp, "RAM": x_ram},
    subtitles=[
        f"Sharpness: {metric(x).item():.1f}",
        f"{metric(x_zf).item():.1f}",
        f"{metric(x_vsharp).item():.1f}",
        f"{metric(x_ram).item():.1f}",
    ],
    figsize=(12, 4),
)
```

```none
Dropping unknown constructor arguments for UnetModel2d: backward_operator, forward_operator. If these were intentional config keys, the target class may be outdated or the keys may be misspelled.
```

<!-- GENERATED FROM PYTHON SOURCE LINES 101-107 -->

## Multicoil brain MRI reconstruction

We use a 5x accelerated multicoil Calgary-Campinas brain test volume with no ground truth,
with [`deepinv.datasets.CalgarySliceDataset`](https://deepinv.org/api/stubs/deepinv.datasets.CalgarySliceDataset.html.md#deepinv.datasets.CalgarySliceDataset), which loads undersampled y, mask (Poisson-disk), and estimated coil maps using ESPIRiT <sup>[8](#footcite-uecker2013espirit)</sup>.
Take a single slice for the demo, and construct a 2D [`deepinv.physics.MultiCoilMRI`](https://deepinv.org/api/stubs/deepinv.physics.MultiCoilMRI.html.md#deepinv.physics.MultiCoilMRI) physics.

<!-- GENERATED FROM PYTHON SOURCE LINES 107-139 -->
```Python
dinv.datasets.download_archive(
    dinv.utils.get_image_url("demo_calgary_test_e13991s3_P01536.7.h5"),
    dinv.utils.get_cache_home() / "calgary_test_5" / "e13991s3_P01536.7.h5",
)

dataset = dinv.datasets.CalgarySliceDataset(
    dinv.utils.get_cache_home() / "calgary_test_5",
    slice_index="middle",
    transform=dinv.datasets.CalgarySliceTransform(
        estimate_coil_maps=True, acs=24, espirit_crop=0.85
    ),
    use_dict_output=True,
)

batch = next(iter(DataLoader(dataset)))
y = batch["y"].to(device)

physics = dinv.physics.MultiCoilMRI(
    img_size=y.shape[-2:], **batch["params"], device=device
)

dinv.utils.plot(
    {
        f"Mask\nof shape {tuple(physics.mask.shape)}": physics.mask,
        f"0th coil map\nof shape {tuple(physics.coil_maps[:, [0]].shape)}": physics.coil_maps[
            :, [0]
        ],
    },
    figsize=(6, 6),
)
```

```none
File already downloaded: /local/jtachell/.cache/deepinv/calgary_test_5/e13991s3_P01536.7.h5. Skipping...

  0%|          | 0/1 [00:00<?, ?it/s]
100%|██████████| 1/1 [00:00<00:00, 19.13it/s]
/local/jtachell/deepinv/deepinv/.pixi/envs/docs/lib/python3.12/site-packages/sigpy/config.py:27: UserWarning: Importing cupy.cuda.cudnn failed. For more details, see the error stack below:
No module named 'cupyx.cudnn'
  warnings.warn(
```

<!-- GENERATED FROM PYTHON SOURCE LINES 140-144 -->

Perform reconstruction with pretrained models.
Note that vSHARP and Joint-ICNet <sup>[5](#footcite-yiasemis2024vsharp)</sup><sup>[4](#footcite-jun2021joint)</sup> estimate coil maps internally, whereas RAM uses the ESPIRiT maps.

Again, the baselines are zero-filled reconstruction, as well as the least-squares conjugate-gradient SENSE <sup>[9](#footcite-pruessmann1999sense)</sup>.

<!-- GENERATED FROM PYTHON SOURCE LINES 144-162 -->
```Python
vsharp = dinv.models.DIRECTModel(
    model_name="vsharp_brain", pretrained=True, device=device
)
jointicnet = dinv.models.DIRECTModel(
    model_name="jointicnet_5x", pretrained=True, device=device
)
ram = dinv.models.RAM(device=device, pretrained=True)

with torch.no_grad():
    x_zf = physics.A_adjoint(y).cpu()
    x_sense = physics.A_dagger(y).cpu()
    x_vsharp = vsharp(y, physics).cpu()
    x_jointicnet = jointicnet(y, physics).cpu()

    # y is very small, under the min sigma, breaking scale equivariance. Bring it into friendlier range:
    x_ram = ram(y / x_zf.max(), physics).cpu() * x_zf.max()
```

```none
Dropping unknown constructor arguments for UnetModel2d: backward_operator, forward_operator. If these were intentional config keys, the target class may be outdated or the keys may be misspelled.
```

<!-- GENERATED FROM PYTHON SOURCE LINES 163-166 -->

ESPIRiT estimates coil maps with arbitrary phase per pixel, because the phases are unconstrained, leading to low spatial correlation.
Even though RAM is not trained on multicoil MRI, it performs better when the phase maps are also smooth. We use
`physics.phase_correct_maps` to constrain the phases to a smooth map, improving performance.

<!-- GENERATED FROM PYTHON SOURCE LINES 166-204 -->
```Python
with torch.no_grad():
    coil_maps = physics.phase_correct_maps(x_zf)
    physics.update(coil_maps=coil_maps)

    x_ram_corrected = ram(y / x_zf.max(), physics).cpu() * x_zf.max()

fig, axs = plt.subplots(2, 3, figsize=(12, 8), layout="compressed")
dinv.utils.plot(
    {
        "Zero-filled": x_zf,
        "SENSE": x_sense,
        "vSHARP": x_vsharp,
    },
    subtitles=[
        f"Sharpness: {metric(x_zf).item():.1f}",
        f"{metric(x_sense).item():.1f}",
        f"{metric(x_vsharp).item():.1f}",
    ],
    fig=fig,
    axs=axs[:1],
    show=False,
)
dinv.utils.plot(
    {
        "Joint-ICNet": x_jointicnet,
        "RAM": x_ram,
        "RAM w/ corrected maps": x_ram_corrected,
    },
    subtitles=[
        f"{metric(x_jointicnet).item():.1f}",
        f"{metric(x_ram).item():.1f}",
        f"{metric(x_ram_corrected).item():.1f}",
    ],
    fig=fig,
    axs=axs[1:],
)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 205-207 -->

## FastMRI brain test set

<!-- GENERATED FROM PYTHON SOURCE LINES 207-254 -->
```Python
# We use a volume from the :class:`deepinv.datasets.FastMRISliceDataset` test set where GT was provided by the organisers for computing metrics.

dinv.datasets.download_archive(
    dinv.utils.get_image_url(
        "demo_fastmri_brain_multicoil_test_file_brain_AXT2_200_2000341.h5"
    ),
    dinv.utils.get_cache_home()
    / "fastmri_brain_multicoil_test"
    / "file_brain_AXT2_200_2000341.h5",
)
dinv.datasets.download_archive(
    dinv.utils.get_image_url(
        "demo_fastmri_brain_multicoil_test_full_file_brain_AXT2_200_2000341.h5"
    ),
    dinv.utils.get_cache_home()
    / "fastmri_brain_multicoil_test_full"
    / "file_brain_AXT2_200_2000341.h5",
)

dataset = dinv.datasets.FastMRISliceDataset(
    dinv.utils.get_cache_home() / "fastmri_brain_multicoil_test",
    target_root=dinv.utils.get_cache_home() / "fastmri_brain_multicoil_test_full",
    slice_index="middle",
    transform=dinv.datasets.MRISliceTransform(
        estimate_coil_maps=True, espirit_crop=0.85
    ),
    use_dict_output=True,
)

batch = next(iter(DataLoader(dataset)))
x, y = batch["x"], batch["y"].to(device)

physics = dinv.physics.MultiCoilMRI(
    img_size=y.shape[-2:], **batch["params"], device=device
)

dinv.utils.plot(
    {
        f"Mask, shape {tuple(physics.mask.shape)}": physics.mask,
        f"0th coil map, shape {tuple(physics.coil_maps[:, [0]].shape)}": physics.coil_maps[
            :, [0]
        ],
    },
    figsize=(6, 6),
)
```

```none
File already downloaded: /local/jtachell/.cache/deepinv/fastmri_brain_multicoil_test/file_brain_AXT2_200_2000341.h5. Skipping...
File already downloaded: /local/jtachell/.cache/deepinv/fastmri_brain_multicoil_test_full/file_brain_AXT2_200_2000341.h5. Skipping...

  0%|          | 0/1 [00:00<?, ?it/s]
100%|██████████| 1/1 [00:00<00:00, 11.63it/s]
```

<!-- GENERATED FROM PYTHON SOURCE LINES 255-257 -->

Perform reconstruction with pretrained models.
Note that vSHARP estimates coil maps internally, whereas RAM uses the ESPIRiT maps.

<!-- GENERATED FROM PYTHON SOURCE LINES 257-313 -->
```Python
vsharp = dinv.models.DIRECTModel(
    model_name="vsharp_brain", pretrained=True, device=device
)
ram = dinv.models.RAM(device=device, pretrained=True)

with torch.no_grad():
    x_zf = physics.A_adjoint(y).cpu()
    x_sense = physics.A_dagger(y).cpu()
    x_vsharp = vsharp(y, physics).cpu()

    # y is very small, under the min sigma, breaking scale equivariance. Bring it into friendlier range:
    x_ram = ram(y / x_zf.max(), physics).cpu() * x_zf.max()

    # As before, fix the phase of the coil maps
    physics.phase_correct_maps(x_zf)
    x_ram_corrected = ram(y / x_zf.max(), physics).cpu() * x_zf.max()

# Crop to FastMRI FOV
x_zf = physics.crop(x_zf, shape=x.shape[-2:])
x_sense = physics.crop(x_sense, shape=x.shape[-2:])
x_vsharp = physics.crop(x_vsharp, shape=x.shape[-2:])
x_ram = physics.crop(x_ram, shape=x.shape[-2:])
x_ram_corrected = physics.crop(x_ram_corrected, shape=x.shape[-2:])

fig, axs = plt.subplots(2, 3, figsize=(12, 8), layout="compressed")
dinv.utils.plot(
    {
        "Fully-sampled": x,
        "Zero-filled": x_zf,
        "SENSE": x_sense,
    },
    subtitles=[
        f"Sharpness: {metric(x).item():.1f}",
        f"{metric(x_zf).item():.1f}",
        f"{metric(x_sense).item():.1f}",
    ],
    fig=fig,
    axs=axs[:1],
    show=False,
)
dinv.utils.plot(
    {
        "vSHARP": x_vsharp,
        "RAM": x_ram,
        "RAM w/ corrected maps": x_ram_corrected,
    },
    subtitles=[
        f"{metric(x_vsharp).item():.1f}",
        f"{metric(x_ram).item():.1f}",
        f"{metric(x_ram_corrected).item():.1f}",
    ],
    fig=fig,
    axs=axs[1:],
)
```

```none
Dropping unknown constructor arguments for UnetModel2d: backward_operator, forward_operator. If these were intentional config keys, the target class may be outdated or the keys may be misspelled.
```

<!-- GENERATED FROM PYTHON SOURCE LINES 314-317 -->
* **References:**

* <a id='footcite-wang2024cmrxrecon'>**[1]**</a> Chengyan Wang, Jun Lyu, Shuo Wang, Chen Qin, Kunyuan Guo, Xinyu Zhang, Xiaotong Yu, Yan Li, Fanwen Wang, Jianhua Jin, and others. Cmrxrecon: a publicly available k-space dataset and benchmark to advance deep learning for cardiac mri. *Scientific Data*, 11(1):687, 2024.
* <a id='footcite-souza2018open'>**[2]**</a> Roberto Souza, Oeslle Lucena, Julia Garrafa, David Gobbi, Marina Saluzzi, Simone Appenzeller, Letícia Rittner, Richard Frayne, and Roberto Lotufo. An open, multi-vendor, multi-field-strength brain mr dataset and analysis of publicly available skull stripping methods agreement. *NeuroImage*, 170:482–494, 2018.
* <a id='footcite-zbontar2018fastmri'>**[3]**</a> Jure Zbontar, Florian Knoll, Anuroop Sriram, Tullie Murrell, Zhengnan Huang, Matthew J Muckley, Aaron Defazio, Ruben Stern, Patricia Johnson, Mary Bruno, and others. Fastmri: an open dataset and benchmarks for accelerated mri. *arXiv preprint arXiv:1811.08839*, 2018.
* <a id='footcite-jun2021joint'>**[4]**</a> Yohan Jun, Hyungseob Shin, Taejoon Eo, and Dosik Hwang. Joint deep model-based mr image and coil sensitivity reconstruction network (joint-icnet) for fast mri. In *Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition*, 5270–5279. 2021.
* <a id='footcite-yiasemis2024vsharp'>**[5]**</a> George Yiasemis, Nikita Moriakov, Jan-Jakob Sonke, and Jonas Teuwen. Vsharp: variable splitting half-quadratic admm algorithm for reconstruction of inverse-problems. *Magnetic Resonance Imaging*, 2024.
* <a id='footcite-yiasemis2025uniform'>**[6]**</a> George Yiasemis, Jonatan Ferm, Nikita Moriakov, Ritse Mann, Jan-Jakob Sonke, and Jonas Teuwen. Uniform: a unified deep learning framework for multi-organ and multi-contrast mri reconstruction. In *The 8th International Conference on Medical Imaging with Deep Learning*. 2025.
* <a id='footcite-terris2025reconstruct'>**[7]**</a> Matthieu Terris, Samuel Hurault, Maxime Song, and Julián Tachella. Reconstruct anything model: a lightweight foundation model for computational imaging. *arXiv preprint arXiv:2503.08915*, 2025.
* <a id='footcite-uecker2013espirit'>**[8]**</a> Martin Uecker, Peng Lai, Mark Murphy, Patrick Virtue, Michael Elad, John Pauly, Shreyas Vasanawala, and Michael Lustig. Espirit — an eigenvalue approach to autocalibrating parallel mri: where sense meets grappa. *Magnetic Resonance in Medicine*, May 2013. [doi:10.1002/mrm.24751](https://doi.org/10.1002/mrm.24751).
* <a id='footcite-pruessmann1999sense'>**[9]**</a> Klaas Pruessmann, Markus Weiger, Markus Scheidegger, and Peter Boesiger. Sense: sensitivity encoding for fast mri. *Magnetic Resonance in Medicine*, 1999. [doi:10.1002/(SICI)1522-2594(199911)42:5%3C952::AID-MRM16%3E3.0.CO;2-S](https://doi.org/10.1002/(SICI)1522-2594(199911)42:5%3C952::AID-MRM16%3E3.0.CO;2-S).

**Total running time of the script:** (0 minutes 37.327 seconds)

🏷 Tags: MRI

<a id="sphx-glr-download-auto-examples-models-demo-mri-pretrained-py"></a>

[`Download Jupyter notebook: demo_mri_pretrained.ipynb`](https://deepinv.org/auto_examples/models/_downloads/2ae504d3ee48a722f9ad13dd8aad6e34/demo_mri_pretrained.ipynb)

[`Download Python source code: demo_mri_pretrained.py`](https://deepinv.org/auto_examples/models/_downloads/7b60af24a9f1ef1bae80ecb1c6aefc89/demo_mri_pretrained.py)

[`Download zipped: demo_mri_pretrained.zip`](https://deepinv.org/auto_examples/models/_downloads/d594cfde3bccb3334542ff9dceb45745/demo_mri_pretrained.zip)

[Gallery generated by Sphinx-Gallery](https://sphinx-gallery.github.io)
