Self-supervised learning with Equivariant Imaging for MRI.

This example shows you how to train a reconstruction network for an MRI inverse problem on a fully self-supervised way, i.e., using measurement data only.

The equivariant imaging loss is presented in “Equivariant Imaging: Learning Beyond the Range Space”.

import deepinv as dinv
from torch.utils.data import DataLoader
import torch
from pathlib import Path
from torchvision import transforms
from deepinv.optim.prior import PnP
from deepinv.utils.demo import load_dataset, load_degradation, demo_mri_model
from deepinv.models.utils import get_weights_url

Setup paths for data loading and results.

BASE_DIR = Path(".")
ORIGINAL_DATA_DIR = BASE_DIR / "datasets"
DATA_DIR = BASE_DIR / "measurements"
CKPT_DIR = BASE_DIR / "ckpts"

# Set the global random seed from pytorch to ensure reproducibility of the example.
torch.manual_seed(0)

device = dinv.utils.get_freer_gpu() if torch.cuda.is_available() else "cpu"

Load base image datasets and degradation operators.

In this example, we use a subset of the single-coil FastMRI dataset as the base image dataset. It consists of 973 knee images of size 320x320.

Note

We reduce to the size to 128x128 for faster training in the demo.

operation = "MRI"
train_dataset_name = "fastmri_knee_singlecoil"
img_size = 128

transform = transforms.Compose([transforms.Resize(img_size)])

train_dataset = load_dataset(
    train_dataset_name, ORIGINAL_DATA_DIR, transform, train=True
)
test_dataset = load_dataset(
    train_dataset_name, ORIGINAL_DATA_DIR, transform, train=False
)
Downloading datasets/fastmri_knee_singlecoil.pt

  0%|          | 0.00/399M [00:00<?, ?iB/s]
  0%|          | 1.17M/399M [00:00<00:37, 10.7MiB/s]
  1%|          | 2.24M/399M [00:00<00:37, 10.6MiB/s]
  1%|          | 3.31M/399M [00:00<00:37, 10.6MiB/s]
  1%|          | 4.37M/399M [00:00<00:37, 10.5MiB/s]
  1%|▏         | 5.45M/399M [00:00<00:36, 10.6MiB/s]
  2%|▏         | 6.51M/399M [00:00<00:36, 10.6MiB/s]
  2%|▏         | 7.60M/399M [00:00<00:36, 10.7MiB/s]
  2%|▏         | 8.67M/399M [00:00<00:36, 10.6MiB/s]
  2%|▏         | 9.73M/399M [00:00<00:36, 10.6MiB/s]
  3%|▎         | 10.8M/399M [00:01<00:36, 10.6MiB/s]
  3%|▎         | 11.9M/399M [00:01<00:36, 10.5MiB/s]
  3%|▎         | 12.9M/399M [00:01<00:36, 10.5MiB/s]
  4%|▎         | 14.0M/399M [00:01<00:36, 10.5MiB/s]
  4%|▍         | 15.0M/399M [00:01<00:36, 10.4MiB/s]
  4%|▍         | 16.1M/399M [00:01<00:36, 10.5MiB/s]
  4%|▍         | 17.1M/399M [00:01<00:36, 10.4MiB/s]
  5%|▍         | 18.2M/399M [00:01<00:36, 10.4MiB/s]
  5%|▍         | 19.2M/399M [00:01<00:36, 10.4MiB/s]
  5%|▌         | 20.2M/399M [00:01<00:36, 10.4MiB/s]
  5%|▌         | 21.3M/399M [00:02<00:36, 10.3MiB/s]
  6%|▌         | 22.3M/399M [00:02<00:36, 10.3MiB/s]
  6%|▌         | 23.4M/399M [00:02<00:36, 10.3MiB/s]
  6%|▌         | 24.4M/399M [00:02<00:35, 10.4MiB/s]
  6%|▋         | 25.5M/399M [00:02<00:35, 10.4MiB/s]
  7%|▋         | 26.5M/399M [00:02<00:35, 10.4MiB/s]
  7%|▋         | 27.6M/399M [00:02<00:35, 10.4MiB/s]
  7%|▋         | 28.6M/399M [00:02<00:35, 10.5MiB/s]
  7%|▋         | 29.7M/399M [00:02<00:35, 10.5MiB/s]
  8%|▊         | 30.7M/399M [00:02<00:35, 10.5MiB/s]
  8%|▊         | 31.8M/399M [00:03<00:34, 10.6MiB/s]
  8%|▊         | 32.9M/399M [00:03<00:36, 9.97MiB/s]
  9%|▊         | 33.9M/399M [00:03<00:36, 10.1MiB/s]
  9%|▉         | 35.0M/399M [00:03<00:35, 10.2MiB/s]
  9%|▉         | 36.0M/399M [00:03<00:35, 10.2MiB/s]
  9%|▉         | 37.1M/399M [00:03<00:35, 10.3MiB/s]
 10%|▉         | 38.1M/399M [00:03<00:35, 10.3MiB/s]
 10%|▉         | 39.2M/399M [00:03<00:34, 10.3MiB/s]
 10%|█         | 40.2M/399M [00:03<00:34, 10.3MiB/s]
 10%|█         | 41.3M/399M [00:03<00:34, 10.3MiB/s]
 11%|█         | 42.3M/399M [00:04<00:34, 10.3MiB/s]
 11%|█         | 43.4M/399M [00:04<00:34, 10.4MiB/s]
 11%|█         | 44.5M/399M [00:04<00:33, 10.5MiB/s]
 11%|█▏        | 45.5M/399M [00:04<00:33, 10.5MiB/s]
 12%|█▏        | 46.6M/399M [00:04<00:33, 10.6MiB/s]
 12%|█▏        | 47.7M/399M [00:04<00:33, 10.5MiB/s]
 12%|█▏        | 48.7M/399M [00:04<00:33, 10.5MiB/s]
 12%|█▏        | 49.8M/399M [00:04<00:33, 10.4MiB/s]
 13%|█▎        | 50.9M/399M [00:04<00:32, 10.6MiB/s]
 13%|█▎        | 51.9M/399M [00:04<00:32, 10.6MiB/s]
 13%|█▎        | 53.0M/399M [00:05<00:32, 10.6MiB/s]
 14%|█▎        | 54.1M/399M [00:05<00:32, 10.5MiB/s]
 14%|█▍        | 55.1M/399M [00:05<00:32, 10.5MiB/s]
 14%|█▍        | 56.2M/399M [00:05<00:32, 10.5MiB/s]
 14%|█▍        | 57.2M/399M [00:05<00:32, 10.5MiB/s]
 15%|█▍        | 58.3M/399M [00:05<00:32, 10.4MiB/s]
 15%|█▍        | 59.3M/399M [00:05<00:32, 10.3MiB/s]
 15%|█▌        | 60.3M/399M [00:05<00:32, 10.3MiB/s]
 15%|█▌        | 61.4M/399M [00:05<00:32, 10.3MiB/s]
 16%|█▌        | 62.4M/399M [00:05<00:32, 10.4MiB/s]
 16%|█▌        | 63.5M/399M [00:06<00:32, 10.4MiB/s]
 16%|█▌        | 64.6M/399M [00:06<00:31, 10.5MiB/s]
 16%|█▋        | 65.6M/399M [00:06<00:31, 10.5MiB/s]
 17%|█▋        | 66.7M/399M [00:06<00:31, 10.5MiB/s]
 17%|█▋        | 67.7M/399M [00:06<00:31, 10.5MiB/s]
 17%|█▋        | 68.8M/399M [00:06<00:31, 10.5MiB/s]
 18%|█▊        | 69.9M/399M [00:06<00:31, 10.5MiB/s]
 18%|█▊        | 70.9M/399M [00:06<00:33, 9.91MiB/s]
 18%|█▊        | 72.0M/399M [00:06<00:32, 10.0MiB/s]
 18%|█▊        | 73.0M/399M [00:07<00:32, 10.1MiB/s]
 19%|█▊        | 74.0M/399M [00:07<00:31, 10.2MiB/s]
 19%|█▉        | 75.1M/399M [00:07<00:31, 10.3MiB/s]
 19%|█▉        | 76.1M/399M [00:07<00:31, 10.3MiB/s]
 19%|█▉        | 77.2M/399M [00:07<00:31, 10.4MiB/s]
 20%|█▉        | 78.2M/399M [00:07<00:30, 10.3MiB/s]
 20%|█▉        | 79.3M/399M [00:07<00:30, 10.3MiB/s]
 20%|██        | 80.3M/399M [00:07<00:30, 10.4MiB/s]
 20%|██        | 81.4M/399M [00:07<00:30, 10.4MiB/s]
 21%|██        | 82.4M/399M [00:07<00:30, 10.4MiB/s]
 21%|██        | 83.5M/399M [00:08<00:30, 10.4MiB/s]
 21%|██        | 84.5M/399M [00:08<00:30, 10.4MiB/s]
 21%|██▏       | 85.6M/399M [00:08<00:30, 10.4MiB/s]
 22%|██▏       | 86.6M/399M [00:08<00:30, 10.4MiB/s]
 22%|██▏       | 87.7M/399M [00:08<00:29, 10.4MiB/s]
 22%|██▏       | 88.7M/399M [00:08<00:29, 10.4MiB/s]
 23%|██▎       | 89.8M/399M [00:08<00:29, 10.5MiB/s]
 23%|██▎       | 90.9M/399M [00:08<00:29, 10.5MiB/s]
 23%|██▎       | 91.9M/399M [00:08<00:29, 10.5MiB/s]
 23%|██▎       | 93.0M/399M [00:08<00:29, 10.5MiB/s]
 24%|██▎       | 94.1M/399M [00:09<00:28, 10.6MiB/s]
 24%|██▍       | 95.1M/399M [00:09<00:28, 10.6MiB/s]
 24%|██▍       | 96.2M/399M [00:09<00:28, 10.6MiB/s]
 24%|██▍       | 97.3M/399M [00:09<00:28, 10.6MiB/s]
 25%|██▍       | 98.3M/399M [00:09<00:28, 10.6MiB/s]
 25%|██▍       | 99.4M/399M [00:09<00:28, 10.5MiB/s]
 25%|██▌       | 100M/399M [00:09<00:28, 10.5MiB/s]
 25%|██▌       | 102M/399M [00:09<00:29, 10.1MiB/s]
 26%|██▌       | 103M/399M [00:09<00:28, 10.2MiB/s]
 26%|██▌       | 104M/399M [00:09<00:28, 10.3MiB/s]
 26%|██▋       | 105M/399M [00:10<00:28, 10.3MiB/s]
 27%|██▋       | 106M/399M [00:10<00:28, 10.3MiB/s]
 27%|██▋       | 107M/399M [00:10<00:28, 10.4MiB/s]
 27%|██▋       | 108M/399M [00:10<00:28, 10.3MiB/s]
 27%|██▋       | 109M/399M [00:10<00:27, 10.5MiB/s]
 28%|██▊       | 110M/399M [00:10<00:27, 10.5MiB/s]
 28%|██▊       | 111M/399M [00:10<00:27, 10.5MiB/s]
 28%|██▊       | 112M/399M [00:10<00:27, 10.4MiB/s]
 28%|██▊       | 113M/399M [00:10<00:27, 10.4MiB/s]
 29%|██▊       | 114M/399M [00:10<00:26, 10.7MiB/s]
 29%|██▉       | 115M/399M [00:11<00:26, 10.6MiB/s]
 29%|██▉       | 116M/399M [00:11<00:26, 10.6MiB/s]
 29%|██▉       | 117M/399M [00:11<00:26, 10.6MiB/s]
 30%|██▉       | 119M/399M [00:11<00:26, 10.6MiB/s]
 30%|███       | 120M/399M [00:11<00:26, 10.5MiB/s]
 30%|███       | 121M/399M [00:11<00:26, 10.5MiB/s]
 31%|███       | 122M/399M [00:11<00:25, 10.7MiB/s]
 31%|███       | 123M/399M [00:11<00:27, 10.0MiB/s]
 31%|███       | 124M/399M [00:11<00:27, 10.1MiB/s]
 31%|███▏      | 125M/399M [00:11<00:26, 10.2MiB/s]
 32%|███▏      | 126M/399M [00:12<00:26, 10.2MiB/s]
 32%|███▏      | 127M/399M [00:12<00:26, 10.3MiB/s]
 32%|███▏      | 128M/399M [00:12<00:26, 10.3MiB/s]
 32%|███▏      | 129M/399M [00:12<00:26, 10.3MiB/s]
 33%|███▎      | 130M/399M [00:12<00:25, 10.4MiB/s]
 33%|███▎      | 131M/399M [00:12<00:25, 10.5MiB/s]
 33%|███▎      | 132M/399M [00:12<00:25, 10.4MiB/s]
 33%|███▎      | 133M/399M [00:12<00:25, 10.3MiB/s]
 34%|███▎      | 134M/399M [00:12<00:25, 10.3MiB/s]
 34%|███▍      | 135M/399M [00:12<00:25, 10.3MiB/s]
 34%|███▍      | 136M/399M [00:13<00:25, 10.3MiB/s]
 34%|███▍      | 137M/399M [00:13<00:25, 10.3MiB/s]
 35%|███▍      | 139M/399M [00:13<00:25, 10.3MiB/s]
 35%|███▌      | 140M/399M [00:13<00:24, 10.4MiB/s]
 35%|███▌      | 141M/399M [00:13<00:24, 10.4MiB/s]
 36%|███▌      | 142M/399M [00:13<00:24, 10.4MiB/s]
 36%|███▌      | 143M/399M [00:13<00:24, 10.4MiB/s]
 36%|███▌      | 144M/399M [00:13<00:24, 10.4MiB/s]
 36%|███▋      | 145M/399M [00:13<00:24, 10.5MiB/s]
 37%|███▋      | 146M/399M [00:14<00:24, 10.4MiB/s]
 37%|███▋      | 147M/399M [00:14<00:24, 10.4MiB/s]
 37%|███▋      | 148M/399M [00:14<00:24, 10.4MiB/s]
 37%|███▋      | 149M/399M [00:14<00:24, 10.4MiB/s]
 38%|███▊      | 150M/399M [00:14<00:24, 10.3MiB/s]
 38%|███▊      | 151M/399M [00:14<00:23, 10.4MiB/s]
 38%|███▊      | 152M/399M [00:14<00:23, 10.4MiB/s]
 38%|███▊      | 153M/399M [00:14<00:23, 10.4MiB/s]
 39%|███▊      | 154M/399M [00:14<00:24, 9.83MiB/s]
 39%|███▉      | 155M/399M [00:14<00:24, 10.0MiB/s]
 39%|███▉      | 156M/399M [00:15<00:23, 10.1MiB/s]
 39%|███▉      | 157M/399M [00:15<00:23, 10.2MiB/s]
 40%|███▉      | 158M/399M [00:15<00:23, 10.3MiB/s]
 40%|████      | 160M/399M [00:15<00:22, 10.4MiB/s]
 40%|████      | 161M/399M [00:15<00:22, 10.4MiB/s]
 41%|████      | 162M/399M [00:15<00:22, 10.4MiB/s]
 41%|████      | 163M/399M [00:15<00:22, 10.4MiB/s]
 41%|████      | 164M/399M [00:15<00:22, 10.4MiB/s]
 41%|████▏     | 165M/399M [00:15<00:22, 10.4MiB/s]
 42%|████▏     | 166M/399M [00:15<00:22, 10.4MiB/s]
 42%|████▏     | 167M/399M [00:16<00:22, 10.5MiB/s]
 42%|████▏     | 168M/399M [00:16<00:22, 10.5MiB/s]
 42%|████▏     | 169M/399M [00:16<00:21, 10.5MiB/s]
 43%|████▎     | 170M/399M [00:16<00:21, 10.5MiB/s]
 43%|████▎     | 171M/399M [00:16<00:21, 10.6MiB/s]
 43%|████▎     | 172M/399M [00:16<00:21, 10.6MiB/s]
 43%|████▎     | 173M/399M [00:16<00:21, 10.5MiB/s]
 44%|████▎     | 174M/399M [00:16<00:21, 10.5MiB/s]
 44%|████▍     | 175M/399M [00:16<00:21, 10.5MiB/s]
 44%|████▍     | 176M/399M [00:16<00:21, 10.4MiB/s]
 45%|████▍     | 178M/399M [00:17<00:20, 10.6MiB/s]
 45%|████▍     | 179M/399M [00:17<00:20, 10.6MiB/s]
 45%|████▌     | 180M/399M [00:17<00:20, 10.6MiB/s]
 45%|████▌     | 181M/399M [00:17<00:20, 10.7MiB/s]
 46%|████▌     | 182M/399M [00:17<00:21, 10.1MiB/s]
 46%|████▌     | 183M/399M [00:17<00:21, 10.1MiB/s]
 46%|████▌     | 184M/399M [00:17<00:21, 10.2MiB/s]
 46%|████▋     | 185M/399M [00:17<00:20, 10.2MiB/s]
 47%|████▋     | 186M/399M [00:17<00:20, 10.3MiB/s]
 47%|████▋     | 187M/399M [00:17<00:20, 10.3MiB/s]
 47%|████▋     | 188M/399M [00:18<00:20, 10.3MiB/s]
 47%|████▋     | 189M/399M [00:18<00:20, 10.3MiB/s]
 48%|████▊     | 190M/399M [00:18<00:20, 10.3MiB/s]
 48%|████▊     | 191M/399M [00:18<00:20, 10.3MiB/s]
 48%|████▊     | 192M/399M [00:18<00:19, 10.4MiB/s]
 49%|████▊     | 193M/399M [00:18<00:19, 10.4MiB/s]
 49%|████▉     | 194M/399M [00:18<00:19, 10.4MiB/s]
 49%|████▉     | 195M/399M [00:18<00:19, 10.4MiB/s]
 49%|████▉     | 196M/399M [00:18<00:19, 10.4MiB/s]
 50%|████▉     | 198M/399M [00:18<00:19, 10.5MiB/s]
 50%|████▉     | 199M/399M [00:19<00:18, 10.5MiB/s]
 50%|█████     | 200M/399M [00:19<00:19, 10.5MiB/s]
 50%|█████     | 201M/399M [00:19<00:18, 10.4MiB/s]
 51%|█████     | 202M/399M [00:19<00:18, 10.4MiB/s]
 51%|█████     | 203M/399M [00:19<00:19, 10.3MiB/s]
 51%|█████     | 204M/399M [00:19<00:19, 10.2MiB/s]
 51%|█████▏    | 205M/399M [00:19<00:18, 10.2MiB/s]
 52%|█████▏    | 206M/399M [00:19<00:18, 10.3MiB/s]
 52%|█████▏    | 207M/399M [00:19<00:18, 10.3MiB/s]
 52%|█████▏    | 208M/399M [00:19<00:18, 10.3MiB/s]
 52%|█████▏    | 209M/399M [00:20<00:18, 10.3MiB/s]
 53%|█████▎    | 210M/399M [00:20<00:18, 10.4MiB/s]
 53%|█████▎    | 211M/399M [00:20<00:18, 10.4MiB/s]
 53%|█████▎    | 212M/399M [00:20<00:17, 10.4MiB/s]
 54%|█████▎    | 213M/399M [00:20<00:17, 10.5MiB/s]
 54%|█████▍    | 214M/399M [00:20<00:17, 10.4MiB/s]
 54%|█████▍    | 215M/399M [00:20<00:17, 10.4MiB/s]
 54%|█████▍    | 216M/399M [00:20<00:17, 10.4MiB/s]
 55%|█████▍    | 217M/399M [00:20<00:17, 10.4MiB/s]
 55%|█████▍    | 219M/399M [00:21<00:17, 10.4MiB/s]
 55%|█████▌    | 220M/399M [00:21<00:17, 10.4MiB/s]
 55%|█████▌    | 221M/399M [00:21<00:17, 10.3MiB/s]
 56%|█████▌    | 222M/399M [00:21<00:17, 10.3MiB/s]
 56%|█████▌    | 223M/399M [00:21<00:16, 10.4MiB/s]
 56%|█████▌    | 224M/399M [00:21<00:16, 10.4MiB/s]
 56%|█████▋    | 225M/399M [00:21<00:16, 10.4MiB/s]
 57%|█████▋    | 226M/399M [00:21<00:16, 10.5MiB/s]
 57%|█████▋    | 227M/399M [00:21<00:16, 10.5MiB/s]
 57%|█████▋    | 228M/399M [00:21<00:16, 10.4MiB/s]
 57%|█████▋    | 229M/399M [00:22<00:16, 10.4MiB/s]
 58%|█████▊    | 230M/399M [00:22<00:16, 10.4MiB/s]
 58%|█████▊    | 231M/399M [00:22<00:16, 10.4MiB/s]
 58%|█████▊    | 232M/399M [00:22<00:16, 10.4MiB/s]
 59%|█████▊    | 233M/399M [00:22<00:15, 10.4MiB/s]
 59%|█████▉    | 234M/399M [00:22<00:15, 10.4MiB/s]
 59%|█████▉    | 235M/399M [00:22<00:15, 10.4MiB/s]
 59%|█████▉    | 236M/399M [00:22<00:15, 10.5MiB/s]
 60%|█████▉    | 237M/399M [00:22<00:15, 10.5MiB/s]
 60%|█████▉    | 238M/399M [00:22<00:15, 10.4MiB/s]
 60%|██████    | 239M/399M [00:23<00:15, 10.4MiB/s]
 60%|██████    | 241M/399M [00:23<00:15, 10.4MiB/s]
 61%|██████    | 242M/399M [00:23<00:15, 10.4MiB/s]
 61%|██████    | 243M/399M [00:23<00:14, 10.5MiB/s]
 61%|██████    | 244M/399M [00:23<00:14, 10.4MiB/s]
 61%|██████▏   | 245M/399M [00:23<00:14, 10.4MiB/s]
 62%|██████▏   | 246M/399M [00:23<00:14, 10.4MiB/s]
 62%|██████▏   | 247M/399M [00:23<00:14, 10.3MiB/s]
 62%|██████▏   | 248M/399M [00:23<00:14, 10.3MiB/s]
 62%|██████▏   | 249M/399M [00:23<00:14, 10.4MiB/s]
 63%|██████▎   | 250M/399M [00:24<00:14, 10.1MiB/s]
 63%|██████▎   | 251M/399M [00:24<00:18, 7.94MiB/s]
 63%|██████▎   | 252M/399M [00:24<00:15, 9.19MiB/s]
 64%|██████▎   | 253M/399M [00:24<00:15, 9.51MiB/s]
 64%|██████▍   | 254M/399M [00:24<00:14, 9.78MiB/s]
 64%|██████▍   | 255M/399M [00:24<00:14, 10.1MiB/s]
 64%|██████▍   | 257M/399M [00:24<00:14, 10.1MiB/s]
 65%|██████▍   | 258M/399M [00:24<00:13, 10.2MiB/s]
 65%|██████▍   | 259M/399M [00:24<00:13, 10.2MiB/s]
 65%|██████▌   | 260M/399M [00:25<00:13, 10.2MiB/s]
 65%|██████▌   | 261M/399M [00:25<00:13, 10.3MiB/s]
 66%|██████▌   | 262M/399M [00:25<00:13, 10.3MiB/s]
 66%|██████▌   | 263M/399M [00:25<00:13, 10.4MiB/s]
 66%|██████▌   | 264M/399M [00:25<00:12, 10.4MiB/s]
 66%|██████▋   | 265M/399M [00:25<00:12, 10.4MiB/s]
 67%|██████▋   | 266M/399M [00:25<00:12, 10.4MiB/s]
 67%|██████▋   | 267M/399M [00:25<00:12, 10.4MiB/s]
 67%|██████▋   | 268M/399M [00:25<00:12, 10.4MiB/s]
 68%|██████▊   | 269M/399M [00:25<00:12, 10.4MiB/s]
 68%|██████▊   | 270M/399M [00:26<00:12, 10.4MiB/s]
 68%|██████▊   | 271M/399M [00:26<00:12, 10.3MiB/s]
 68%|██████▊   | 272M/399M [00:26<00:12, 10.4MiB/s]
 69%|██████▊   | 273M/399M [00:26<00:12, 10.4MiB/s]
 69%|██████▉   | 274M/399M [00:26<00:11, 10.4MiB/s]
 69%|██████▉   | 275M/399M [00:26<00:12, 10.2MiB/s]
 69%|██████▉   | 276M/399M [00:26<00:11, 10.3MiB/s]
 70%|██████▉   | 277M/399M [00:26<00:11, 10.3MiB/s]
 70%|██████▉   | 279M/399M [00:26<00:11, 10.3MiB/s]
 70%|███████   | 280M/399M [00:26<00:11, 10.5MiB/s]
 70%|███████   | 281M/399M [00:27<00:11, 10.5MiB/s]
 71%|███████   | 282M/399M [00:27<00:11, 10.4MiB/s]
 71%|███████   | 283M/399M [00:27<00:11, 10.4MiB/s]
 71%|███████   | 284M/399M [00:27<00:11, 10.4MiB/s]
 71%|███████▏  | 285M/399M [00:27<00:11, 10.3MiB/s]
 72%|███████▏  | 286M/399M [00:27<00:10, 10.3MiB/s]
 72%|███████▏  | 287M/399M [00:27<00:10, 10.3MiB/s]
 72%|███████▏  | 288M/399M [00:27<00:10, 10.2MiB/s]
 73%|███████▎  | 289M/399M [00:27<00:10, 10.3MiB/s]
 73%|███████▎  | 290M/399M [00:27<00:10, 10.4MiB/s]
 73%|███████▎  | 291M/399M [00:28<00:10, 10.5MiB/s]
 73%|███████▎  | 292M/399M [00:28<00:10, 10.5MiB/s]
 74%|███████▎  | 293M/399M [00:28<00:10, 10.4MiB/s]
 74%|███████▍  | 294M/399M [00:28<00:10, 10.4MiB/s]
 74%|███████▍  | 295M/399M [00:28<00:09, 10.4MiB/s]
 74%|███████▍  | 296M/399M [00:28<00:09, 10.3MiB/s]
 75%|███████▍  | 297M/399M [00:28<00:09, 10.2MiB/s]
 75%|███████▍  | 298M/399M [00:28<00:09, 10.2MiB/s]
 75%|███████▌  | 299M/399M [00:28<00:09, 10.3MiB/s]
 75%|███████▌  | 301M/399M [00:28<00:09, 10.3MiB/s]
 76%|███████▌  | 302M/399M [00:29<00:09, 10.3MiB/s]
 76%|███████▌  | 303M/399M [00:29<00:09, 10.3MiB/s]
 76%|███████▌  | 304M/399M [00:29<00:09, 10.4MiB/s]
 76%|███████▋  | 305M/399M [00:29<00:09, 10.4MiB/s]
 77%|███████▋  | 306M/399M [00:29<00:08, 10.4MiB/s]
 77%|███████▋  | 307M/399M [00:29<00:08, 10.4MiB/s]
 77%|███████▋  | 308M/399M [00:29<00:08, 10.4MiB/s]
 78%|███████▊  | 309M/399M [00:29<00:08, 10.3MiB/s]
 78%|███████▊  | 310M/399M [00:29<00:08, 10.3MiB/s]
 78%|███████▊  | 311M/399M [00:30<00:08, 10.3MiB/s]
 78%|███████▊  | 312M/399M [00:30<00:08, 10.3MiB/s]
 79%|███████▊  | 313M/399M [00:30<00:08, 10.3MiB/s]
 79%|███████▉  | 314M/399M [00:30<00:08, 10.4MiB/s]
 79%|███████▉  | 315M/399M [00:30<00:07, 10.4MiB/s]
 79%|███████▉  | 316M/399M [00:30<00:07, 10.5MiB/s]
 80%|███████▉  | 317M/399M [00:30<00:07, 10.5MiB/s]
 80%|███████▉  | 318M/399M [00:30<00:07, 10.5MiB/s]
 80%|████████  | 319M/399M [00:30<00:07, 10.4MiB/s]
 80%|████████  | 321M/399M [00:30<00:07, 10.5MiB/s]
 81%|████████  | 322M/399M [00:31<00:07, 10.5MiB/s]
 81%|████████  | 323M/399M [00:31<00:07, 10.4MiB/s]
 81%|████████  | 324M/399M [00:31<00:07, 10.4MiB/s]
 81%|████████▏ | 325M/399M [00:31<00:07, 10.4MiB/s]
 82%|████████▏ | 326M/399M [00:31<00:07, 10.4MiB/s]
 82%|████████▏ | 327M/399M [00:31<00:06, 10.3MiB/s]
 82%|████████▏ | 328M/399M [00:31<00:06, 10.3MiB/s]
 83%|████████▎ | 329M/399M [00:31<00:06, 10.3MiB/s]
 83%|████████▎ | 330M/399M [00:31<00:06, 10.3MiB/s]
 83%|████████▎ | 331M/399M [00:31<00:06, 10.4MiB/s]
 83%|████████▎ | 332M/399M [00:32<00:06, 10.4MiB/s]
 84%|████████▎ | 333M/399M [00:32<00:06, 10.4MiB/s]
 84%|████████▍ | 334M/399M [00:32<00:06, 10.4MiB/s]
 84%|████████▍ | 335M/399M [00:32<00:06, 10.3MiB/s]
 84%|████████▍ | 336M/399M [00:32<00:06, 10.4MiB/s]
 85%|████████▍ | 337M/399M [00:32<00:05, 10.4MiB/s]
 85%|████████▍ | 338M/399M [00:32<00:05, 10.4MiB/s]
 85%|████████▌ | 339M/399M [00:32<00:05, 10.4MiB/s]
 85%|████████▌ | 340M/399M [00:32<00:05, 10.4MiB/s]
 86%|████████▌ | 341M/399M [00:32<00:05, 10.4MiB/s]
 86%|████████▌ | 342M/399M [00:33<00:05, 10.4MiB/s]
 86%|████████▌ | 344M/399M [00:33<00:05, 10.4MiB/s]
 86%|████████▋ | 345M/399M [00:33<00:05, 10.5MiB/s]
 87%|████████▋ | 346M/399M [00:33<00:05, 10.5MiB/s]
 87%|████████▋ | 347M/399M [00:33<00:04, 10.4MiB/s]
 87%|████████▋ | 348M/399M [00:33<00:04, 10.4MiB/s]
 88%|████████▊ | 349M/399M [00:33<00:04, 10.4MiB/s]
 88%|████████▊ | 350M/399M [00:33<00:04, 10.4MiB/s]
 88%|████████▊ | 351M/399M [00:33<00:04, 10.4MiB/s]
 88%|████████▊ | 352M/399M [00:33<00:04, 10.5MiB/s]
 89%|████████▊ | 353M/399M [00:34<00:04, 10.4MiB/s]
 89%|████████▉ | 354M/399M [00:34<00:04, 10.4MiB/s]
 89%|████████▉ | 355M/399M [00:34<00:04, 10.4MiB/s]
 89%|████████▉ | 356M/399M [00:34<00:04, 10.4MiB/s]
 90%|████████▉ | 357M/399M [00:34<00:03, 10.4MiB/s]
 90%|████████▉ | 358M/399M [00:34<00:03, 10.4MiB/s]
 90%|█████████ | 359M/399M [00:34<00:03, 10.4MiB/s]
 90%|█████████ | 360M/399M [00:34<00:03, 10.4MiB/s]
 91%|█████████ | 361M/399M [00:34<00:03, 10.4MiB/s]
 91%|█████████ | 362M/399M [00:34<00:03, 10.4MiB/s]
 91%|█████████ | 363M/399M [00:35<00:03, 10.4MiB/s]
 91%|█████████▏| 365M/399M [00:35<00:03, 10.4MiB/s]
 92%|█████████▏| 366M/399M [00:35<00:03, 10.4MiB/s]
 92%|█████████▏| 367M/399M [00:35<00:03, 10.5MiB/s]
 92%|█████████▏| 368M/399M [00:35<00:02, 10.4MiB/s]
 93%|█████████▎| 369M/399M [00:35<00:02, 10.4MiB/s]
 93%|█████████▎| 370M/399M [00:35<00:02, 10.4MiB/s]
 93%|█████████▎| 371M/399M [00:35<00:02, 10.4MiB/s]
 93%|█████████▎| 372M/399M [00:35<00:02, 10.4MiB/s]
 94%|█████████▎| 373M/399M [00:35<00:02, 10.4MiB/s]
 94%|█████████▍| 374M/399M [00:36<00:02, 10.4MiB/s]
 94%|█████████▍| 375M/399M [00:36<00:02, 10.6MiB/s]
 94%|█████████▍| 376M/399M [00:36<00:02, 10.5MiB/s]
 95%|█████████▍| 377M/399M [00:36<00:02, 10.5MiB/s]
 95%|█████████▍| 378M/399M [00:36<00:01, 10.5MiB/s]
 95%|█████████▌| 379M/399M [00:36<00:01, 10.5MiB/s]
 95%|█████████▌| 380M/399M [00:36<00:01, 10.5MiB/s]
 96%|█████████▌| 381M/399M [00:36<00:01, 10.4MiB/s]
 96%|█████████▌| 382M/399M [00:36<00:01, 10.4MiB/s]
 96%|█████████▌| 383M/399M [00:36<00:01, 10.4MiB/s]
 96%|█████████▋| 384M/399M [00:37<00:01, 10.4MiB/s]
 97%|█████████▋| 385M/399M [00:37<00:01, 10.3MiB/s]
 97%|█████████▋| 387M/399M [00:37<00:01, 10.4MiB/s]
 97%|█████████▋| 388M/399M [00:37<00:01, 10.3MiB/s]
 98%|█████████▊| 389M/399M [00:37<00:00, 10.3MiB/s]
 98%|█████████▊| 390M/399M [00:37<00:00, 10.4MiB/s]
 98%|█████████▊| 391M/399M [00:37<00:00, 10.4MiB/s]
 98%|█████████▊| 392M/399M [00:37<00:00, 10.5MiB/s]
 99%|█████████▊| 393M/399M [00:37<00:00, 10.5MiB/s]
 99%|█████████▉| 394M/399M [00:37<00:00, 10.4MiB/s]
 99%|█████████▉| 395M/399M [00:38<00:00, 10.4MiB/s]
 99%|█████████▉| 396M/399M [00:38<00:00, 10.5MiB/s]
100%|█████████▉| 397M/399M [00:38<00:00, 10.5MiB/s]
100%|█████████▉| 398M/399M [00:38<00:00, 10.4MiB/s]
100%|██████████| 399M/399M [00:38<00:00, 10.4MiB/s]
/home/runner/work/deepinv/deepinv/deepinv/utils/demo.py:21: FutureWarning: You are using `torch.load` with `weights_only=False` (the current default value), which uses the default pickle module implicitly. It is possible to construct malicious pickle data which will execute arbitrary code during unpickling (See https://github.com/pytorch/pytorch/blob/main/SECURITY.md#untrusted-models for more details). In a future release, the default value for `weights_only` will be flipped to `True`. This limits the functions that could be executed during unpickling. Arbitrary objects will no longer be allowed to be loaded via this mode unless they are explicitly allowlisted by the user via `torch.serialization.add_safe_globals`. We recommend you start setting `weights_only=True` for any use case where you don't have full control of the loaded file. Please open an issue on GitHub for any issues related to this experimental feature.
  x = torch.load(str(root_dir) + ".pt")

Generate a dataset of knee images and load it.

mask = load_degradation("mri_mask_128x128.npy", ORIGINAL_DATA_DIR)

# defined physics
physics = dinv.physics.MRI(mask=mask, device=device)

# Use parallel dataloader if using a GPU to fasten training,
# otherwise, as all computes are on CPU, use synchronous data loading.
num_workers = 4 if torch.cuda.is_available() else 0
n_images_max = (
    900 if torch.cuda.is_available() else 5
)  # number of images used for training
# (the dataset has up to 973 images, however here we use only 900)

my_dataset_name = "demo_equivariant_imaging"
measurement_dir = DATA_DIR / train_dataset_name / operation
deepinv_datasets_path = dinv.datasets.generate_dataset(
    train_dataset=train_dataset,
    test_dataset=test_dataset,
    physics=physics,
    device=device,
    save_dir=measurement_dir,
    train_datapoints=n_images_max,
    num_workers=num_workers,
    dataset_filename=str(my_dataset_name),
)

train_dataset = dinv.datasets.HDF5Dataset(path=deepinv_datasets_path, train=True)
test_dataset = dinv.datasets.HDF5Dataset(path=deepinv_datasets_path, train=False)
mri_mask_128x128.npy degradation downloaded in datasets
Dataset has been saved in measurements/fastmri_knee_singlecoil/MRI

Set up the reconstruction network

As a reconstruction network, we use an unrolled network (half-quadratic splitting) with a trainable denoising prior based on the DnCNN architecture as an example of a model-based deep learning architecture from MoDL. See deepinv.utils.demo.demo_mri_model() for details.

model = demo_mri_model(device=device)

Set up the training parameters

We choose a self-supervised training scheme with two losses: the measurement consistency loss (MC) and the equivariant imaging loss (EI). The EI loss requires a group of transformations to be defined. The forward model should not be equivariant to these transformations. Here we use the group of 4 rotations of 90 degrees, as the accelerated MRI acquisition is not equivariant to rotations (while it is equivariant to translations).

See docs for full list of available transforms.

Note

We use a pretrained model to reduce training time. You can get the same results by training from scratch for 150 epochs.

epochs = 1  # choose training epochs
learning_rate = 5e-4
batch_size = 16 if torch.cuda.is_available() else 1

# choose self-supervised training losses
# generates 4 random rotations per image in the batch
losses = [dinv.loss.MCLoss(), dinv.loss.EILoss(dinv.transform.Rotate(n_trans=4))]

# choose optimizer and scheduler
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate, weight_decay=1e-8)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=int(epochs * 0.8) + 1)

# start with a pretrained model to reduce training time
file_name = "new_demo_ei_ckp_150_v3.pth"
url = get_weights_url(model_name="demo", file_name=file_name)
ckpt = torch.hub.load_state_dict_from_url(
    url,
    map_location=lambda storage, loc: storage,
    file_name=file_name,
)
# load a checkpoint to reduce training time
model.load_state_dict(ckpt["state_dict"])
optimizer.load_state_dict(ckpt["optimizer"])
Downloading: "https://huggingface.co/deepinv/demo/resolve/main/new_demo_ei_ckp_150_v3.pth?download=true" to /home/runner/.cache/torch/hub/checkpoints/new_demo_ei_ckp_150_v3.pth

  0%|          | 0.00/2.17M [00:00<?, ?B/s]
 52%|█████▏    | 1.12M/2.17M [00:00<00:00, 10.9MB/s]
100%|██████████| 2.17M/2.17M [00:00<00:00, 11.0MB/s]

Train the network

verbose = True  # print training information
wandb_vis = False  # plot curves and images in Weight&Bias

train_dataloader = DataLoader(
    train_dataset, batch_size=batch_size, num_workers=num_workers, shuffle=True
)
test_dataloader = DataLoader(
    test_dataset, batch_size=batch_size, num_workers=num_workers, shuffle=False
)

# Initialize the trainer
trainer = dinv.Trainer(
    model,
    physics=physics,
    epochs=epochs,
    scheduler=scheduler,
    losses=losses,
    optimizer=optimizer,
    train_dataloader=train_dataloader,
    plot_images=True,
    device=device,
    save_path=str(CKPT_DIR / operation),
    verbose=verbose,
    wandb_vis=wandb_vis,
    show_progress_bar=False,  # disable progress bar for better vis in sphinx gallery.
    ckp_interval=10,
)

model = trainer.train()
Ground truth, Measurement, Reconstruction
The model has 187019 trainable parameters
Train epoch 0: MCLoss=0.0, EILoss=0.0, TotalLoss=0.0, PSNR=37.645

Test the network

trainer.test(test_dataloader)
Ground truth, Measurement, No learning, Reconstruction
Eval epoch 0: PSNR=38.288, PSNR no learning=29.389
Test results:
PSNR no learning: 29.389 +- 3.411
PSNR: 38.288 +- 2.265

{'PSNR no learning': 29.388799536718082, 'PSNR no learning_std': 3.4114185158882959, 'PSNR': 38.287860243287803, 'PSNR_std': 2.2653385780895827}

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

Gallery generated by Sphinx-Gallery