Gradient-step denoiser with PnP-ADMM
Browse files- 20260122_182641_example_CIGS_256x256_uniform_1_trials/photocurrent_mapping.py +965 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/masks/sample_20.0_percent_coarse_J=6_uniform_random.npy +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/masks/sample_50.0_percent_coarse_J=6_uniform_random.npy +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/masks/sample_80.0_percent_coarse_J=6_uniform_random.npy +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05/images/example_CIGS_256x256_pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05_sample_20.0_percent_coarse_J=6_uniform_random.png +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05/images/example_CIGS_256x256_pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05_sample_50.0_percent_coarse_J=6_uniform_random.png +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05/images/example_CIGS_256x256_pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05_sample_80.0_percent_coarse_J=6_uniform_random.png +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05/metrics.csv +4 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05/recons/example_CIGS_256x256_pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05_sample_20.0_percent_coarse_J=6_uniform_random.npy +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05/recons/example_CIGS_256x256_pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05_sample_50.0_percent_coarse_J=6_uniform_random.npy +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05/recons/example_CIGS_256x256_pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05_sample_80.0_percent_coarse_J=6_uniform_random.npy +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/spgl1_factor=1/images/example_CIGS_256x256_spgl1_factor=1_sample_20.0_percent_coarse_J=6_uniform_random.png +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/spgl1_factor=1/images/example_CIGS_256x256_spgl1_factor=1_sample_50.0_percent_coarse_J=6_uniform_random.png +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/spgl1_factor=1/images/example_CIGS_256x256_spgl1_factor=1_sample_80.0_percent_coarse_J=6_uniform_random.png +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/spgl1_factor=1/metrics.csv +4 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/spgl1_factor=1/recons/example_CIGS_256x256_spgl1_factor=1_sample_20.0_percent_coarse_J=6_uniform_random.npy +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/spgl1_factor=1/recons/example_CIGS_256x256_spgl1_factor=1_sample_50.0_percent_coarse_J=6_uniform_random.npy +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/spgl1_factor=1/recons/example_CIGS_256x256_spgl1_factor=1_sample_80.0_percent_coarse_J=6_uniform_random.npy +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/zero_filled/example_CIGS_256x256_sample_20.0_percent_coarse_J=6_uniform_random.npy +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/zero_filled/example_CIGS_256x256_sample_50.0_percent_coarse_J=6_uniform_random.npy +3 -0
- 20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/zero_filled/example_CIGS_256x256_sample_80.0_percent_coarse_J=6_uniform_random.npy +3 -0
20260122_182641_example_CIGS_256x256_uniform_1_trials/photocurrent_mapping.py
ADDED
|
@@ -0,0 +1,965 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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MM for Photocurrent Mapping image reconstruction
|
| 3 |
+
#
|
| 4 |
+
# This notebook demonstrates Plug-and-Play (PnP) ADMM image reconstruction for
|
| 5 |
+
# Photocurrent Mapping (PCM) data.
|
| 6 |
+
#
|
| 7 |
+
# Starting from a high-resolution current map of a CIGS solar cell, subsampled
|
| 8 |
+
# PCM measurements are simulated using the `PhotocurrentMapOp` operator. Several
|
| 9 |
+
# reconstruction methods are then compared:
|
| 10 |
+
#
|
| 11 |
+
# - Zero-filled pseudo-inverse reconstruction.
|
| 12 |
+
# - Two compressed sensing baselines with a wavelet sparsity prior:
|
| 13 |
+
# FISTA with an $\ell_1$ penalty and SPGL1.
|
| 14 |
+
# - PnP-ADMM with a pre-trained DRUNet denoiser as prior.
|
| 15 |
+
#
|
| 16 |
+
# The goal is not to optimise performance, but to illustrate how
|
| 17 |
+
# classical sparse reconstruction methods and PnP can be combined with the LION
|
| 18 |
+
# operators for PCM.
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# %% [markdown]
|
| 22 |
+
# ## Setup
|
| 23 |
+
|
| 24 |
+
# %% [markdown]
|
| 25 |
+
# ### Device configuration
|
| 26 |
+
#
|
| 27 |
+
# Set the default device to a GPU if available. If multiple GPUs are present,
|
| 28 |
+
# the desired GPU index can be specified here.
|
| 29 |
+
|
| 30 |
+
# %%
|
| 31 |
+
from __future__ import annotations
|
| 32 |
+
|
| 33 |
+
import torch
|
| 34 |
+
|
| 35 |
+
device = torch.device(
|
| 36 |
+
"mps"
|
| 37 |
+
if torch.backends.mps.is_available()
|
| 38 |
+
else "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 39 |
+
)
|
| 40 |
+
torch.set_default_device(device)
|
| 41 |
+
|
| 42 |
+
# %% [markdown]
|
| 43 |
+
# ### Imports
|
| 44 |
+
#
|
| 45 |
+
# Import the required libraries, including LION operators and reconstruction algorithms for PCM.
|
| 46 |
+
|
| 47 |
+
# %%
|
| 48 |
+
from datetime import datetime
|
| 49 |
+
from functools import partial
|
| 50 |
+
from pathlib import Path
|
| 51 |
+
from typing import Callable
|
| 52 |
+
|
| 53 |
+
import deepinv
|
| 54 |
+
import matplotlib
|
| 55 |
+
import matplotlib.pyplot as plt
|
| 56 |
+
import numpy as np
|
| 57 |
+
from jaxtyping import Float
|
| 58 |
+
from matplotlib.colors import ListedColormap
|
| 59 |
+
|
| 60 |
+
# from LION.utils.scale import choose_measurement_scale_factor
|
| 61 |
+
from plot_helper import PlotHelper
|
| 62 |
+
from torchmetrics.image import PeakSignalNoiseRatio, StructuralSimilarityIndexMeasure
|
| 63 |
+
from tqdm import tqdm as std_tqdm
|
| 64 |
+
|
| 65 |
+
from LION.classical_algorithms.fista import fista_l1
|
| 66 |
+
from LION.classical_algorithms.spgl1_torch import spgl1_torch
|
| 67 |
+
from LION.operators.CompositeOp import CompositeOp
|
| 68 |
+
from LION.operators.DebiasOp import debias_ls
|
| 69 |
+
from LION.operators.multilevel_sample import multilevel_sample
|
| 70 |
+
|
| 71 |
+
# LION imports
|
| 72 |
+
from LION.operators.PhotocurrentMapOp import PhotocurrentMapOp
|
| 73 |
+
from LION.operators.uniform_random_sample import uniform_random_sample
|
| 74 |
+
from LION.operators.Wavelet2D import Wavelet2D
|
| 75 |
+
from LION.reconstructors.PnP import PnP
|
| 76 |
+
|
| 77 |
+
# Use tqdm with dynamic column width that adapts to the terminal width
|
| 78 |
+
tqdm = partial(std_tqdm, dynamic_ncols=True)
|
| 79 |
+
tqdm_no_leave = partial(std_tqdm, dynamic_ncols=True, leave=False)
|
| 80 |
+
|
| 81 |
+
GrayscaleImage2D = Float[torch.Tensor, "height width"]
|
| 82 |
+
Measurement1D = Float[torch.Tensor, "num_measurements"]
|
| 83 |
+
|
| 84 |
+
# %% [markdown]
|
| 85 |
+
# ### Define the data file paths
|
| 86 |
+
#
|
| 87 |
+
# The example uses a single $256 \times 256$ current map of a CIGS solar cell
|
| 88 |
+
# stored as a NumPy array. This image will serve as the ground truth in the
|
| 89 |
+
# experiments.
|
| 90 |
+
|
| 91 |
+
# %%
|
| 92 |
+
data_dir = Path("data/photocurrent_data")
|
| 93 |
+
# data_dir = Path("your/path/to/photocurrent_data")
|
| 94 |
+
|
| 95 |
+
assert data_dir.exists(), f"Data directory {data_dir} does not exist."
|
| 96 |
+
|
| 97 |
+
root_output_dir = Path("../pdo")
|
| 98 |
+
root_output_dir.mkdir(parents=True, exist_ok=True)
|
| 99 |
+
|
| 100 |
+
# These images are provided with pixels in range [0, 1]
|
| 101 |
+
data_name, zoom, loc, loc1, loc2, roi = (
|
| 102 |
+
"CIGS_256x256",
|
| 103 |
+
2.5,
|
| 104 |
+
"center left",
|
| 105 |
+
3,
|
| 106 |
+
4,
|
| 107 |
+
(110, 210, 40, 40),
|
| 108 |
+
) # (x, y, w, h) with y increasing downwards
|
| 109 |
+
# data_name, zoom, loc, loc1, loc2, roi = "silicon_256x256", 2.5, "lower right", 2, 1, (194, 1, 60, 60) # (x, y, w, h) with y increasing downwards
|
| 110 |
+
# data_name, zoom, loc, loc1, loc2, roi = (
|
| 111 |
+
# "silicon_512x512",
|
| 112 |
+
# 3,
|
| 113 |
+
# "lower right",
|
| 114 |
+
# 2,
|
| 115 |
+
# 1,
|
| 116 |
+
# (400, 5, 100, 100),
|
| 117 |
+
# ) # (x, y, w, h) with y increasing downwards
|
| 118 |
+
# data_name, zoom, loc, loc1, loc2, roi = "organic_256x256", 2.5, "lower left", 2, 1, (70, 5, 50, 50) # (x, y, w, h) with y increasing downwards
|
| 119 |
+
# data_name, zoom, loc, loc1, loc2, roi = "perovskite_256x256", 2.5, "upper left", 3, 4, (90, 190, 50, 50) # (x, y, w, h) with y increasing downwards
|
| 120 |
+
data_name = "example_" + data_name # prefix with "example_"
|
| 121 |
+
is_out_of_distribution = False
|
| 122 |
+
clim = (0.0, 1.0)
|
| 123 |
+
inverses_sign = False
|
| 124 |
+
# R_high, R_low = 1.0, 0.0 # default for normalized images
|
| 125 |
+
is_out_of_distribution = False
|
| 126 |
+
factor = 1 # no scaling for normalized images
|
| 127 |
+
|
| 128 |
+
# # This sample was provided in image form at 512x512 resolution but the pixels are real measured current values
|
| 129 |
+
# data_name, zoom, loc, loc1, loc2, roi = "Si_256_512x512", 2.5, "lower left", 2, 1, (160, 60, 120, 120)
|
| 130 |
+
# clim = (0.0, 3e-5)
|
| 131 |
+
# inverses_sign = True
|
| 132 |
+
# R_high = 1e-4
|
| 133 |
+
# R_low = -5e-6
|
| 134 |
+
# factor = 1e5 # to scale up the photocurrent values for better numerical stability in SPGL1
|
| 135 |
+
|
| 136 |
+
# # This sample was provided in image form at 512x512 resolution but the pixels are real measured current values
|
| 137 |
+
# data_name, zoom, loc, loc1, loc2, roi = "Si_2_256_512x512", 2.5, "lower right", 2, 1, (322, 85, 100, 100)
|
| 138 |
+
# clim = (0.0, 1.5e-5)
|
| 139 |
+
# inverses_sign = True
|
| 140 |
+
# R_high = 2e-5
|
| 141 |
+
# R_low = -2e-6
|
| 142 |
+
# factor = 1e5 # to scale up the photocurrent values for better numerical stability in SPGL1
|
| 143 |
+
|
| 144 |
+
if "256x256" in data_name:
|
| 145 |
+
J_order = 8 # J=8 => 2^8=256
|
| 146 |
+
elif "512x512" in data_name:
|
| 147 |
+
J_order = 9 # J=9 => 2^9=512
|
| 148 |
+
else:
|
| 149 |
+
raise ValueError(f"Unknown data_name {data_name}, cannot determine order_size.")
|
| 150 |
+
|
| 151 |
+
# # # This is the same data as Si_256_512x512 but provided as measurement data and only up to 256x256 resolution
|
| 152 |
+
# data_name, zoom, loc, loc1, loc2, roi = "Si_256_measurement_data", 2, "lower left", 2, 1, (80, 30, 60, 60)
|
| 153 |
+
# # data_name, zoom, loc, loc1, loc2, roi = "Si_256_hadamard_measurement_vector", 2, "lower left", 2, 1, (80, 30, 60, 60)
|
| 154 |
+
# # data_name, zoom, loc, loc1, loc2, roi = "Si_256_reconstructed_image", 2, "lower left", 2, 1, (80, 30, 60, 60)
|
| 155 |
+
# clim = (0.0, 1e-6)
|
| 156 |
+
# R_high = 1e-6
|
| 157 |
+
# R_low = -1e-6
|
| 158 |
+
# factor = 1e7 # to scale up the photocurrent values for better numerical stability in SPGL1
|
| 159 |
+
# J_order = 8 # 2^8 x 2^8 = 256 x 256
|
| 160 |
+
|
| 161 |
+
# # # This is the same data as Si_2_256_512x512 but provided as measurement data and only up to 256x256 resolution
|
| 162 |
+
# data_name, zoom, loc, loc1, loc2, roi = "Si_2_256_measurement_data", 2, "lower left", 2, 1, (32, 42, 50, 50)
|
| 163 |
+
# # data_name, zoom, loc, loc1, loc2, roi = "Si_2_256_hadamard_measurement_vector", 2, "lower left", 2, 1, (32, 42, 50, 50)
|
| 164 |
+
# # data_name, zoom, loc, loc1, loc2, roi = "Si_2_256_reconstructed_image", 2, "lower left", 2, 1, (32, 42, 50, 50)
|
| 165 |
+
# clim = (0.0, 4e-7)
|
| 166 |
+
# R_high = 1e-6
|
| 167 |
+
# R_low = -1e-6
|
| 168 |
+
# factor = 1e7 # to scale up the photocurrent values for better numerical stability in SPGL1
|
| 169 |
+
# J_order = 8 # 2^8 x 2^8 = 256 x 256
|
| 170 |
+
|
| 171 |
+
# scale_eps = 1e-12
|
| 172 |
+
# is_out_of_distribution = True
|
| 173 |
+
# inverses_sign = True
|
| 174 |
+
|
| 175 |
+
tests_scale_ground_truth = False
|
| 176 |
+
|
| 177 |
+
data_filename = f"{data_name}.npy"
|
| 178 |
+
print("Loading data file:", data_filename)
|
| 179 |
+
assert (
|
| 180 |
+
data_dir / data_filename
|
| 181 |
+
).exists(), f"Data {data_filename} not found in {data_dir}."
|
| 182 |
+
|
| 183 |
+
# data_type = "original_measurement_data"
|
| 184 |
+
# data_type = "hadamard_measurement_vector"
|
| 185 |
+
data_type = "image"
|
| 186 |
+
print(f"The type of raw data is: {data_type}")
|
| 187 |
+
|
| 188 |
+
noise_seed = 42
|
| 189 |
+
noise_std = 0 # No noise
|
| 190 |
+
# noise_std = 0.05 # standard deviation of additive homoscedastic Gaussian white noise added to measurements
|
| 191 |
+
|
| 192 |
+
num_trials = 1
|
| 193 |
+
num_trials_skip = 0
|
| 194 |
+
|
| 195 |
+
runs_pnp_admm = True
|
| 196 |
+
# denoiser_name = "drunet"
|
| 197 |
+
denoiser_name = "gs_drunet"
|
| 198 |
+
# pnp_admm_iters = 1
|
| 199 |
+
# pnp_admm_iters = 20
|
| 200 |
+
pnp_admm_iters = 50
|
| 201 |
+
# pnp_admm_iters = 100
|
| 202 |
+
# pnp_admm_iters = 150
|
| 203 |
+
# pnp_admm_eta = 0.00001 # Undersampling artifacts may remain if eta is too small
|
| 204 |
+
# pnp_admm_eta = 0.00005 # Could still work
|
| 205 |
+
# pnp_admm_eta = 0.0001 # Could still work
|
| 206 |
+
# pnp_admm_eta = 0.001
|
| 207 |
+
# pnp_admm_eta = 0.005
|
| 208 |
+
pnp_admm_eta = 0.01 # Generally good
|
| 209 |
+
# pnp_admm_eta = 0.02
|
| 210 |
+
# pnp_admm_eta = 0.03
|
| 211 |
+
# pnp_admm_eta = 0.04
|
| 212 |
+
# pnp_admm_eta = 0.05
|
| 213 |
+
# pnp_admm_eta = 0.1
|
| 214 |
+
# pnp_admm_eta = 1
|
| 215 |
+
# pnp_admm_eta = 10
|
| 216 |
+
# pnp_admm_eta = 20
|
| 217 |
+
# pnp_admm_eta = 50
|
| 218 |
+
# pnp_admm_eta = 100 # Got nan for 100% sampling?
|
| 219 |
+
cg_iters = 20
|
| 220 |
+
# cg_iters = 50
|
| 221 |
+
cg_eps = 1e-20 # No real change compared to default, CG usually terminates quickly especially when measurements are small
|
| 222 |
+
# drunet_sigma = 0.01 # noise level for DRUNet denoiser
|
| 223 |
+
# drunet_sigma = 0.02 # noise level for DRUNet denoiser
|
| 224 |
+
drunet_sigma = 0.05 # noise level for DRUNet denoiser
|
| 225 |
+
# drunet_sigma = 0.1 # noise level for DRUNet denoiser
|
| 226 |
+
|
| 227 |
+
runs_fista_l1 = False
|
| 228 |
+
|
| 229 |
+
runs_spgl1 = True
|
| 230 |
+
|
| 231 |
+
# randomizing_scheme = "multilevel"
|
| 232 |
+
randomizing_scheme = "uniform"
|
| 233 |
+
|
| 234 |
+
cmap_max = 0.8 # take only the lower 0-80% of afmhot, reduce brightness
|
| 235 |
+
# cmap_max = 0.9 # take only the lower 0-90% of afmhot to avoid the white top
|
| 236 |
+
# cmap_max = 1.0 # take all of afmhot
|
| 237 |
+
adds_insets = True
|
| 238 |
+
# adds_insets = False
|
| 239 |
+
plot_helper = PlotHelper(
|
| 240 |
+
roi=roi,
|
| 241 |
+
zoom=zoom,
|
| 242 |
+
loc=loc,
|
| 243 |
+
show_rect=True,
|
| 244 |
+
cmap=ListedColormap(
|
| 245 |
+
matplotlib.colormaps["afmhot"](np.linspace(0.0, cmap_max, 256))
|
| 246 |
+
),
|
| 247 |
+
clim=clim,
|
| 248 |
+
loc1=loc1,
|
| 249 |
+
loc2=loc2,
|
| 250 |
+
)
|
| 251 |
+
|
| 252 |
+
# %% [markdown]
|
| 253 |
+
# Define a general function to run the photocurrent mapping reconstruction using a reconstruction method.
|
| 254 |
+
#
|
| 255 |
+
# The helper function `run_pcm_demo`:
|
| 256 |
+
#
|
| 257 |
+
# - Builds the PCM operator and simulates subsampled measurements.
|
| 258 |
+
# - Computes the zero-filled pseudo-inverse reconstruction.
|
| 259 |
+
# - Runs a chosen reconstruction method given by `recon_fn`.
|
| 260 |
+
# - Reports PSNR and SSIM for both reconstructions, displays and saves the images.
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
# %% mystnb={"code_prompt_show": "Show utility details"} tags=["hide-cell"]
|
| 264 |
+
def show_images_with_inset(
|
| 265 |
+
images: list[torch.Tensor],
|
| 266 |
+
fig_filepath: Path,
|
| 267 |
+
plot_helper: PlotHelper,
|
| 268 |
+
titles: list[str] | None = None,
|
| 269 |
+
suptitle: str | None = None,
|
| 270 |
+
adds_insets: bool = True,
|
| 271 |
+
) -> None:
|
| 272 |
+
"""Plot images."""
|
| 273 |
+
n_images = len(images)
|
| 274 |
+
fig, axes = plt.subplots(1, n_images, squeeze=False, figsize=(n_images * 4, 4))
|
| 275 |
+
|
| 276 |
+
for i in range(n_images):
|
| 277 |
+
img_np = images[i].squeeze().cpu().numpy()
|
| 278 |
+
ax: plt.Axes = axes[0][i]
|
| 279 |
+
if adds_insets:
|
| 280 |
+
plot_helper.add_zoom_inset(ax, img_np)
|
| 281 |
+
else:
|
| 282 |
+
ax.imshow(img_np, cmap=plot_helper.cmap, clim=plot_helper.clim)
|
| 283 |
+
ax.axis("off")
|
| 284 |
+
if titles:
|
| 285 |
+
ax.set_title(titles[i], fontsize=10)
|
| 286 |
+
if suptitle:
|
| 287 |
+
fig.subplots_adjust(bottom=0.18)
|
| 288 |
+
fig.text(0.5, 0.02, suptitle, ha="center", va="bottom", fontsize=16)
|
| 289 |
+
fig.savefig(fig_filepath, dpi=150)
|
| 290 |
+
plt.close(fig)
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
def make_csv(method_name: str, log_dir: Path | str) -> None:
|
| 294 |
+
log_dir = Path(log_dir) / method_name
|
| 295 |
+
log_dir.mkdir(parents=True, exist_ok=True)
|
| 296 |
+
csv_path = log_dir / "metrics.csv"
|
| 297 |
+
with csv_path.open("w") as f:
|
| 298 |
+
f.write(
|
| 299 |
+
"sampling_percentage, coarse_J, "
|
| 300 |
+
"mse_zero_filled, psnr_zero_filled, ssim_zero_filled, pearson_corr_zero_filled, "
|
| 301 |
+
"mse_recon, psnr_recon, ssim_recon, pearson_corr_recon\n"
|
| 302 |
+
)
|
| 303 |
+
|
| 304 |
+
|
| 305 |
+
# %%
|
| 306 |
+
def run_pcm_demo(
|
| 307 |
+
recon_description: str,
|
| 308 |
+
recon_fn: Callable[
|
| 309 |
+
[PhotocurrentMapOp, Measurement1D, GrayscaleImage2D], GrayscaleImage2D
|
| 310 |
+
],
|
| 311 |
+
ground_truth_image: GrayscaleImage2D,
|
| 312 |
+
method_name: str,
|
| 313 |
+
image_name: str,
|
| 314 |
+
J: int, # image size will be 2^J x 2^J
|
| 315 |
+
sampling_ratio: float,
|
| 316 |
+
coarse_J: int,
|
| 317 |
+
measurement_vector: Measurement1D | None = None,
|
| 318 |
+
log_dir: Path | str = ".",
|
| 319 |
+
device: torch.device | str = "cuda:0",
|
| 320 |
+
seed: int = 0,
|
| 321 |
+
):
|
| 322 |
+
zero_filled_dir = Path(log_dir) / "zero_filled"
|
| 323 |
+
zero_filled_dir.mkdir(parents=True, exist_ok=True)
|
| 324 |
+
masks_dir = Path(log_dir) / "masks"
|
| 325 |
+
masks_dir.mkdir(parents=True, exist_ok=True)
|
| 326 |
+
|
| 327 |
+
log_dir = Path(log_dir) / method_name
|
| 328 |
+
log_dir.mkdir(parents=True, exist_ok=True)
|
| 329 |
+
N = 1 << J
|
| 330 |
+
im_tensor = ground_truth_image.unsqueeze(0).unsqueeze(0) # (1,1,H,W)
|
| 331 |
+
|
| 332 |
+
sampling_percentage = sampling_ratio * 100
|
| 333 |
+
in_order_measurements_percentage = (1 << (2 * coarse_J)) / (N * N) * 100
|
| 334 |
+
print()
|
| 335 |
+
print(f"Sampling rate: {sampling_percentage}%")
|
| 336 |
+
print(f"Coarse levels to keep: {coarse_J} ({in_order_measurements_percentage}%)")
|
| 337 |
+
|
| 338 |
+
rng = np.random.default_rng(seed)
|
| 339 |
+
num_samples = int(sampling_ratio * N * N)
|
| 340 |
+
if randomizing_scheme == "multilevel":
|
| 341 |
+
sampled_indices = multilevel_sample(
|
| 342 |
+
J=J, num_samples=num_samples, coarse_J=coarse_J, alpha=1.0, rng=rng
|
| 343 |
+
)
|
| 344 |
+
elif randomizing_scheme == "uniform":
|
| 345 |
+
sampled_indices = uniform_random_sample(
|
| 346 |
+
J=J, num_samples=num_samples, coarse_J=coarse_J, rng=rng
|
| 347 |
+
)
|
| 348 |
+
else:
|
| 349 |
+
raise ValueError(f"Unknown sampling_scheme {randomizing_scheme}.")
|
| 350 |
+
pcm_op = PhotocurrentMapOp(J=J, sampled_indices=sampled_indices, device=device)
|
| 351 |
+
|
| 352 |
+
if measurement_vector is not None:
|
| 353 |
+
print(
|
| 354 |
+
f"Using provided measurement vector with shape {measurement_vector.shape}."
|
| 355 |
+
)
|
| 356 |
+
y_subsampled_tensor_noiseless = measurement_vector[sampled_indices]
|
| 357 |
+
else:
|
| 358 |
+
y_subsampled_tensor_noiseless = pcm_op(im_tensor)
|
| 359 |
+
|
| 360 |
+
y_subsampled_tensor = y_subsampled_tensor_noiseless # No noise
|
| 361 |
+
|
| 362 |
+
# noise_rng = torch.Generator(device=device)
|
| 363 |
+
# noise_rng.manual_seed(noise_seed)
|
| 364 |
+
# homoscedastic_noise = y_subsampled_tensor_noiseless.normal_(
|
| 365 |
+
# mean=0.0, std=noise_std, generator=noise_rng
|
| 366 |
+
# )
|
| 367 |
+
# noise = homoscedastic_noise
|
| 368 |
+
# noise = torch.zeros_like(y_subsampled_tensor_noiseless)
|
| 369 |
+
# assert torch.equal(noise, torch.zeros_like(noise)), "Noise is not zero!"
|
| 370 |
+
|
| 371 |
+
# y_subsampled_tensor = y_subsampled_tensor_noiseless + noise
|
| 372 |
+
|
| 373 |
+
zero_filled_recon_tensor = (
|
| 374 |
+
pcm_op.pseudo_inv(y_subsampled_tensor).unsqueeze(0).unsqueeze(0)
|
| 375 |
+
)
|
| 376 |
+
|
| 377 |
+
recon_tensor = (
|
| 378 |
+
recon_fn(
|
| 379 |
+
pcm_op=pcm_op,
|
| 380 |
+
pcm_measurement=y_subsampled_tensor,
|
| 381 |
+
initial_image=zero_filled_recon_tensor.squeeze(),
|
| 382 |
+
)
|
| 383 |
+
.unsqueeze(0)
|
| 384 |
+
.unsqueeze(0)
|
| 385 |
+
)
|
| 386 |
+
|
| 387 |
+
data_range = (im_tensor.max() - im_tensor.min()).item()
|
| 388 |
+
psnr = PeakSignalNoiseRatio(data_range=data_range).to(device)
|
| 389 |
+
ssim = StructuralSimilarityIndexMeasure(data_range=data_range).to(device)
|
| 390 |
+
|
| 391 |
+
psnr_zero_filled = psnr(zero_filled_recon_tensor, im_tensor)
|
| 392 |
+
psnr_recon = psnr(recon_tensor, im_tensor)
|
| 393 |
+
|
| 394 |
+
ssim_zero_filled = ssim(zero_filled_recon_tensor, im_tensor)
|
| 395 |
+
ssim_recon = ssim(recon_tensor, im_tensor)
|
| 396 |
+
|
| 397 |
+
mse_zero_filled = torch.mean((zero_filled_recon_tensor - im_tensor) ** 2).item()
|
| 398 |
+
mse_recon = torch.mean((recon_tensor - im_tensor) ** 2).item()
|
| 399 |
+
|
| 400 |
+
pearson_corr_zero_filled = torch.corrcoef(
|
| 401 |
+
torch.stack([zero_filled_recon_tensor.flatten(), im_tensor.flatten()])
|
| 402 |
+
)[0, 1].item()
|
| 403 |
+
pearson_corr_recon = torch.corrcoef(
|
| 404 |
+
torch.stack([recon_tensor.flatten(), im_tensor.flatten()])
|
| 405 |
+
)[0, 1].item()
|
| 406 |
+
|
| 407 |
+
csv_path = log_dir / "metrics.csv"
|
| 408 |
+
with csv_path.open("a") as f:
|
| 409 |
+
f.write(
|
| 410 |
+
f"{sampling_percentage}, {coarse_J}, "
|
| 411 |
+
f"{mse_zero_filled}, {psnr_zero_filled}, {ssim_zero_filled}, {pearson_corr_zero_filled}, "
|
| 412 |
+
f"{mse_recon}, {psnr_recon}, {ssim_recon}, {pearson_corr_recon}\n"
|
| 413 |
+
)
|
| 414 |
+
|
| 415 |
+
filename = f"{image_name}_{method_name}_sample_{sampling_percentage}_percent_coarse_J={coarse_J}_{randomizing_scheme}_random"
|
| 416 |
+
zero_filled_filename = f"{image_name}_sample_{sampling_percentage}_percent_coarse_J={coarse_J}_{randomizing_scheme}_random"
|
| 417 |
+
recons_dir = log_dir / "recons"
|
| 418 |
+
recons_dir.mkdir(parents=True, exist_ok=True)
|
| 419 |
+
np.save(
|
| 420 |
+
zero_filled_dir / f"{zero_filled_filename}.npy",
|
| 421 |
+
zero_filled_recon_tensor.squeeze().cpu().numpy(),
|
| 422 |
+
)
|
| 423 |
+
np.save(recons_dir / f"{filename}.npy", recon_tensor.squeeze().cpu().numpy())
|
| 424 |
+
|
| 425 |
+
mask_of_masks_np = np.zeros(N * N, dtype=bool)
|
| 426 |
+
mask_of_masks_np[sampled_indices] = True
|
| 427 |
+
np.save(
|
| 428 |
+
masks_dir
|
| 429 |
+
/ f"sample_{sampling_percentage}_percent_coarse_J={coarse_J}_{randomizing_scheme}_random.npy",
|
| 430 |
+
mask_of_masks_np.reshape(N, N),
|
| 431 |
+
)
|
| 432 |
+
|
| 433 |
+
images_dir = log_dir / "images"
|
| 434 |
+
images_dir.mkdir(parents=True, exist_ok=True)
|
| 435 |
+
|
| 436 |
+
show_images_with_inset(
|
| 437 |
+
[im_tensor, zero_filled_recon_tensor, recon_tensor],
|
| 438 |
+
fig_filepath=images_dir / f"{filename}.png",
|
| 439 |
+
plot_helper=plot_helper,
|
| 440 |
+
titles=[
|
| 441 |
+
"Original Image",
|
| 442 |
+
f"Inverse WHT (Zero-filled)\nPSNR: {psnr_zero_filled:.2f} dB, SSIM: {ssim_zero_filled:.4f}\nMSE: {mse_zero_filled:.3e}, Pearson Corr.: {pearson_corr_zero_filled:.4f}",
|
| 443 |
+
f"{recon_description}\nPSNR: {psnr_recon:.2f} dB, SSIM: {ssim_recon:.4f}\nMSE: {mse_recon:.3e}, Pearson Corr.: {pearson_corr_recon:.4f}",
|
| 444 |
+
],
|
| 445 |
+
suptitle=(
|
| 446 |
+
f"PCM Reconstructions, J={J} ({N}x{N} image)\n"
|
| 447 |
+
+ f"Sample {sampling_percentage:.2f}%, keep {coarse_J} coarse levels ({in_order_measurements_percentage}% here), the rest: {randomizing_scheme} random"
|
| 448 |
+
),
|
| 449 |
+
adds_insets=adds_insets,
|
| 450 |
+
)
|
| 451 |
+
|
| 452 |
+
|
| 453 |
+
# %%
|
| 454 |
+
if denoiser_name == "drunet":
|
| 455 |
+
denoiser = deepinv.models.DRUNet(
|
| 456 |
+
pretrained="download", in_channels=1, out_channels=1, device=device
|
| 457 |
+
)
|
| 458 |
+
elif denoiser_name == "gs_drunet":
|
| 459 |
+
denoiser = deepinv.models.GSDRUNet(
|
| 460 |
+
pretrained="download", in_channels=1, out_channels=1, device=device
|
| 461 |
+
)
|
| 462 |
+
else:
|
| 463 |
+
raise ValueError(f"Unknown denoiser_name {denoiser_name}.")
|
| 464 |
+
|
| 465 |
+
|
| 466 |
+
def run_pnp_admm(
|
| 467 |
+
pcm_op: PhotocurrentMapOp,
|
| 468 |
+
pcm_measurement: Measurement1D,
|
| 469 |
+
initial_image: GrayscaleImage2D,
|
| 470 |
+
) -> GrayscaleImage2D:
|
| 471 |
+
admm_iterations = pnp_admm_iters
|
| 472 |
+
admm_eta = pnp_admm_eta
|
| 473 |
+
cg_max_iter = cg_iters
|
| 474 |
+
_cg_eps = cg_eps
|
| 475 |
+
cg_rel_tol = 0.0
|
| 476 |
+
|
| 477 |
+
# print(
|
| 478 |
+
# f"Running PnP-ADMM reconstruction: {admm_iterations} iterations, cg_max_iter={cg_max_iter}..."
|
| 479 |
+
# )
|
| 480 |
+
|
| 481 |
+
if is_out_of_distribution:
|
| 482 |
+
a = max(R_high - R_low, scale_eps)
|
| 483 |
+
|
| 484 |
+
# pcm_measurement = (pcm_measurement - R_low) / a if is_out_of_distribution else pcm_measurement
|
| 485 |
+
|
| 486 |
+
def denoiser_fn(x: GrayscaleImage2D) -> GrayscaleImage2D:
|
| 487 |
+
with torch.no_grad():
|
| 488 |
+
model_input = (x - R_low) / a if is_out_of_distribution else x
|
| 489 |
+
model_output = (
|
| 490 |
+
denoiser(model_input.unsqueeze(0).unsqueeze(0), sigma=drunet_sigma)
|
| 491 |
+
.squeeze(0)
|
| 492 |
+
.squeeze(0)
|
| 493 |
+
)
|
| 494 |
+
model_output = (
|
| 495 |
+
a * model_output + R_low if is_out_of_distribution else model_output
|
| 496 |
+
)
|
| 497 |
+
return model_output
|
| 498 |
+
|
| 499 |
+
pnp = PnP(physics=pcm_op, prior_fn=denoiser_fn, default_algorithm="ADMM")
|
| 500 |
+
recon = pnp.admm_algorithm(
|
| 501 |
+
measurement=pcm_measurement,
|
| 502 |
+
eta=admm_eta,
|
| 503 |
+
max_iter=admm_iterations,
|
| 504 |
+
cg_max_iter=cg_max_iter,
|
| 505 |
+
cg_eps=_cg_eps,
|
| 506 |
+
cg_rel_tol=cg_rel_tol,
|
| 507 |
+
prog_bar=tqdm,
|
| 508 |
+
# cg_prog_bar=tqdm,
|
| 509 |
+
)
|
| 510 |
+
# recon = pnp.forward_backward_splitting(
|
| 511 |
+
# sino=pcm_measurement,
|
| 512 |
+
# )
|
| 513 |
+
|
| 514 |
+
return recon
|
| 515 |
+
# return a * recon + R_low if is_out_of_distribution else recon
|
| 516 |
+
|
| 517 |
+
|
| 518 |
+
# %%
|
| 519 |
+
def run_fista_l1(
|
| 520 |
+
pcm_op: PhotocurrentMapOp,
|
| 521 |
+
pcm_measurement: Measurement1D,
|
| 522 |
+
initial_image: GrayscaleImage2D,
|
| 523 |
+
) -> GrayscaleImage2D:
|
| 524 |
+
|
| 525 |
+
lam = 10 # Good for Daubechies 4 wavelet transform
|
| 526 |
+
# max_iter = 300
|
| 527 |
+
max_iter = 100
|
| 528 |
+
tol = 1e-5
|
| 529 |
+
|
| 530 |
+
debias_max_iter = 10
|
| 531 |
+
debias_support_tol = 1e-5
|
| 532 |
+
debias_tol = 1e-7
|
| 533 |
+
|
| 534 |
+
height, width = pcm_op.domain_shape
|
| 535 |
+
# Wavelet transform Psi
|
| 536 |
+
wavelet = Wavelet2D((height, width), wavelet_name="db4", device=device)
|
| 537 |
+
# Composite operator A = Phi Psi^{-1}
|
| 538 |
+
A_op = CompositeOp(wavelet, pcm_op, device=device)
|
| 539 |
+
|
| 540 |
+
# print("Running FISTA reconstruction: " f"{max_iter} iterations, lambda={lam}...")
|
| 541 |
+
w_hat = fista_l1(
|
| 542 |
+
op=A_op,
|
| 543 |
+
y=pcm_measurement,
|
| 544 |
+
lam=lam,
|
| 545 |
+
max_iter=max_iter,
|
| 546 |
+
tol=tol,
|
| 547 |
+
L=None,
|
| 548 |
+
verbose=False,
|
| 549 |
+
prog_bar=tqdm,
|
| 550 |
+
)
|
| 551 |
+
|
| 552 |
+
# Optional debiasing
|
| 553 |
+
# print(f"Running debiasing: {debias_max_iter} iterations...")
|
| 554 |
+
w_debias = debias_ls(
|
| 555 |
+
op=A_op,
|
| 556 |
+
y=pcm_measurement,
|
| 557 |
+
w=w_hat,
|
| 558 |
+
support_tol=debias_support_tol,
|
| 559 |
+
max_iter=debias_max_iter,
|
| 560 |
+
tol=debias_tol,
|
| 561 |
+
prog_bar=tqdm,
|
| 562 |
+
)
|
| 563 |
+
|
| 564 |
+
# Current map reconstruction
|
| 565 |
+
cs_result_tensor = wavelet.inverse(w_debias)
|
| 566 |
+
|
| 567 |
+
return cs_result_tensor
|
| 568 |
+
|
| 569 |
+
|
| 570 |
+
# %%
|
| 571 |
+
def run_spgl1(
|
| 572 |
+
pcm_op: PhotocurrentMapOp,
|
| 573 |
+
pcm_measurement: Measurement1D,
|
| 574 |
+
initial_image: GrayscaleImage2D,
|
| 575 |
+
) -> GrayscaleImage2D:
|
| 576 |
+
|
| 577 |
+
# max_iter = 1000
|
| 578 |
+
# max_iter = 200
|
| 579 |
+
max_iter = 100
|
| 580 |
+
# opt_tol = 1e-4
|
| 581 |
+
# bp_tol = 1e-6
|
| 582 |
+
# opt_tol = 1e-5
|
| 583 |
+
# bp_tol = 1e-7
|
| 584 |
+
|
| 585 |
+
debias_max_iter = 10
|
| 586 |
+
# debias_max_iter = 100
|
| 587 |
+
debias_support_tol = 1e-5
|
| 588 |
+
# debias_support_tol = 1e-6
|
| 589 |
+
# debias_support_tol = 1e-7
|
| 590 |
+
debias_tol = 1e-7
|
| 591 |
+
|
| 592 |
+
height, width = pcm_op.domain_shape
|
| 593 |
+
# Wavelet transform Psi
|
| 594 |
+
wavelet = Wavelet2D((height, width), wavelet_name="db4", device=device)
|
| 595 |
+
# Composite operator A = Phi Psi^{-1}
|
| 596 |
+
A_op = CompositeOp(wavelet, pcm_op, device=device)
|
| 597 |
+
|
| 598 |
+
# rhs_l2_norm = torch.linalg.norm(pcm_measurement).item()
|
| 599 |
+
# # relative_feasibility_tolerance = 1e-6
|
| 600 |
+
# relative_feasibility_tolerance = 1e-9
|
| 601 |
+
# absolute_feasibility_tolerance = relative_feasibility_tolerance * rhs_l2_norm
|
| 602 |
+
|
| 603 |
+
pcm_measurement = pcm_measurement * factor # scale up
|
| 604 |
+
|
| 605 |
+
# print("Running SPGL1 reconstruction: " f"{max_iter} iterations ...")
|
| 606 |
+
w_hat, _ = spgl1_torch(
|
| 607 |
+
op=A_op,
|
| 608 |
+
y=pcm_measurement,
|
| 609 |
+
iter_lim=max_iter,
|
| 610 |
+
# opt_tol=absolute_feasibility_tolerance,
|
| 611 |
+
# bp_tol=relative_feasibility_tolerance,
|
| 612 |
+
verbosity=0,
|
| 613 |
+
# opt_tol=opt_tol,
|
| 614 |
+
# bp_tol=bp_tol,
|
| 615 |
+
)
|
| 616 |
+
# Optional debiasing
|
| 617 |
+
# print(f"Running debiasing: {debias_max_iter} iterations...")
|
| 618 |
+
w_debias = debias_ls(
|
| 619 |
+
op=A_op,
|
| 620 |
+
y=pcm_measurement,
|
| 621 |
+
w=w_hat,
|
| 622 |
+
support_tol=debias_support_tol,
|
| 623 |
+
max_iter=debias_max_iter,
|
| 624 |
+
tol=debias_tol,
|
| 625 |
+
prog_bar=tqdm,
|
| 626 |
+
)
|
| 627 |
+
|
| 628 |
+
# Current map reconstruction
|
| 629 |
+
cs_result_tensor = wavelet.inverse(w_debias)
|
| 630 |
+
|
| 631 |
+
cs_result_tensor = cs_result_tensor / factor # scale back down
|
| 632 |
+
|
| 633 |
+
return cs_result_tensor
|
| 634 |
+
|
| 635 |
+
|
| 636 |
+
def make_test_cases() -> list[tuple[float, int]]:
|
| 637 |
+
min_coarse_J = 0
|
| 638 |
+
# min_coarse_J = 5
|
| 639 |
+
# min_coarse_J = J_order - 3 # keep 1.5625% of in-order measurements at least
|
| 640 |
+
# (sampling_ratio, coarse_J)
|
| 641 |
+
test_cases = []
|
| 642 |
+
num_pixels = 1 << (2 * J_order) # N*N
|
| 643 |
+
# for sampling_ in range(0, 9, 1):
|
| 644 |
+
# sampling_ratio = (sampling_ + 1) / 100.0 # from 0.01 to 0.09
|
| 645 |
+
# num_samples = int(sampling_ratio * num_pixels)
|
| 646 |
+
# for coarse_J in range(min_coarse_J, J_order): # from 0 to J_order-1 (not including J_order because that is 100% sampling)
|
| 647 |
+
# if coarse_J > 0:
|
| 648 |
+
# prev_num_coarse_samples = 1 << (2 * (coarse_J - 1))
|
| 649 |
+
# if prev_num_coarse_samples >= num_samples:
|
| 650 |
+
# continue
|
| 651 |
+
# test_cases.append((sampling_ratio, coarse_J))
|
| 652 |
+
for sampling_ratio in [0.2, 0.5, 0.8]:
|
| 653 |
+
# for sampling_ in range(1, 10, 1):
|
| 654 |
+
# sampling_ratio = sampling_ / 10.0 # from 0.1 to 0.9
|
| 655 |
+
test_cases.append(
|
| 656 |
+
(sampling_ratio, J_order - 2)
|
| 657 |
+
) # keep the first J-2 coarse levels, i.e. 6.25% in-order measurements
|
| 658 |
+
num_samples = int(sampling_ratio * num_pixels)
|
| 659 |
+
# for coarse_J in range(min_coarse_J, J_order): # from 0 to J_order-1 (not including J_order because that is 100% sampling)
|
| 660 |
+
# if coarse_J > 0:
|
| 661 |
+
# prev_num_coarse_samples = 1 << (2 * (coarse_J - 1))
|
| 662 |
+
# if prev_num_coarse_samples >= num_samples:
|
| 663 |
+
# continue
|
| 664 |
+
# test_cases.append((sampling_ratio, coarse_J))
|
| 665 |
+
# test_cases.append((1.0, J_order)) # 100% sampling
|
| 666 |
+
|
| 667 |
+
# # sampling_ratios = [0.1]
|
| 668 |
+
# sampling_ratios = [0.2]
|
| 669 |
+
# # # sampling_ratios = [0.25]
|
| 670 |
+
# # sampling_ratios = [0.5]
|
| 671 |
+
# # # sampling_ratios = [0.7]
|
| 672 |
+
# # # coarse_Js = [5] # keep 2^{coarse_J} x 2^{coarse_J} in-order measurements
|
| 673 |
+
# # # coarse_Js = [7] # keep 2^{coarse_J} x 2^{coarse_J} in-order measurements
|
| 674 |
+
# coarse_Js = list(range(0, J_order))
|
| 675 |
+
# test_cases = []
|
| 676 |
+
# test_cases += [
|
| 677 |
+
# (sampling_ratio, coarse_J)
|
| 678 |
+
# for sampling_ratio in sampling_ratios
|
| 679 |
+
# for coarse_J in coarse_Js
|
| 680 |
+
# ]
|
| 681 |
+
# test_cases = [
|
| 682 |
+
# # (0.3, 3),
|
| 683 |
+
# # (0.2, 2),
|
| 684 |
+
# (0.2, 6),
|
| 685 |
+
# # (0.2, 8),
|
| 686 |
+
# # (0.2, 7),
|
| 687 |
+
# # (0.5, 6),
|
| 688 |
+
# # (0.8, 6),
|
| 689 |
+
# # (1.0, 6),
|
| 690 |
+
# # (1.0, 7),
|
| 691 |
+
# # (0.1, 8),
|
| 692 |
+
# ]
|
| 693 |
+
|
| 694 |
+
# test_cases = test_cases[80:]
|
| 695 |
+
|
| 696 |
+
test_cases.reverse()
|
| 697 |
+
|
| 698 |
+
return test_cases
|
| 699 |
+
|
| 700 |
+
|
| 701 |
+
def run_experiments():
|
| 702 |
+
raw_data: GrayscaleImage2D | Measurement1D = np.load(data_dir / data_filename)
|
| 703 |
+
print(f"Raw data shape: {raw_data.shape}")
|
| 704 |
+
print(f"J_order: {J_order}")
|
| 705 |
+
|
| 706 |
+
if data_type == "image":
|
| 707 |
+
ground_truth_image: GrayscaleImage2D = torch.tensor(
|
| 708 |
+
raw_data, dtype=torch.float32, device=device
|
| 709 |
+
)
|
| 710 |
+
if inverses_sign:
|
| 711 |
+
ground_truth_image = -ground_truth_image
|
| 712 |
+
J_data = int(np.log2(ground_truth_image.shape[0]))
|
| 713 |
+
assert (
|
| 714 |
+
J_data == J_order
|
| 715 |
+
), f"Data J ({J_data}) does not match expected J_order ({J_order})."
|
| 716 |
+
print(f"Ground truth image shape: {ground_truth_image.shape}")
|
| 717 |
+
measurement_vector = None
|
| 718 |
+
elif data_type == "hadamard_measurement_vector":
|
| 719 |
+
# Reconstruct the image from the Hadamard measurement vector
|
| 720 |
+
J_data = int(np.log2(raw_data.shape[0]) / 2)
|
| 721 |
+
assert (
|
| 722 |
+
J_data == J_order
|
| 723 |
+
), f"Data J ({J_data}) does not match expected J_order ({J_order})."
|
| 724 |
+
measurement_vector = torch.tensor(raw_data, dtype=torch.float32, device=device)
|
| 725 |
+
if inverses_sign:
|
| 726 |
+
measurement_vector = -measurement_vector
|
| 727 |
+
|
| 728 |
+
index_of_max = torch.argmax(measurement_vector).item()
|
| 729 |
+
index_of_min = torch.argmin(measurement_vector).item()
|
| 730 |
+
print(
|
| 731 |
+
f"Max value in measurement vector: {measurement_vector[index_of_max].item()} at index {index_of_max}"
|
| 732 |
+
)
|
| 733 |
+
print(
|
| 734 |
+
f"Min value in measurement vector: {measurement_vector[index_of_min].item()} at index {index_of_min}"
|
| 735 |
+
)
|
| 736 |
+
exit()
|
| 737 |
+
|
| 738 |
+
pcm_op_full = PhotocurrentMapOp(J=J_order, device=device)
|
| 739 |
+
with torch.no_grad():
|
| 740 |
+
ground_truth_image = pcm_op_full.pseudo_inv(measurement_vector)
|
| 741 |
+
print(
|
| 742 |
+
f"Reconstructed ground truth image shape from Hadamard measurement vector: {ground_truth_image.shape}"
|
| 743 |
+
)
|
| 744 |
+
|
| 745 |
+
elif data_type == "original_measurement_data":
|
| 746 |
+
# Make a 1D array with num_lines//2 elements,
|
| 747 |
+
# where each element is the sum of the measured current multiplied by the pattern index sign.
|
| 748 |
+
num_measurements = raw_data.shape[0]
|
| 749 |
+
assert num_measurements % 2 == 0, "Number of measurements should be even."
|
| 750 |
+
|
| 751 |
+
if inverses_sign:
|
| 752 |
+
raw_data[:, 1] = -raw_data[:, 1]
|
| 753 |
+
|
| 754 |
+
index_of_max_raw = np.argmax(raw_data[:, 1])
|
| 755 |
+
index_of_min_raw = np.argmin(raw_data[:, 1])
|
| 756 |
+
min_raw_value = raw_data[index_of_min_raw, 1]
|
| 757 |
+
max_raw_value = raw_data[index_of_max_raw, 1]
|
| 758 |
+
print(
|
| 759 |
+
f"Max value in original measurement data: {max_raw_value} at index {index_of_max_raw}"
|
| 760 |
+
)
|
| 761 |
+
print(
|
| 762 |
+
f"Min value in original measurement data: {min_raw_value} at index {index_of_min_raw}"
|
| 763 |
+
)
|
| 764 |
+
# exit()
|
| 765 |
+
|
| 766 |
+
sign_vector = np.round(np.sign(raw_data[:, 0]))
|
| 767 |
+
sign_vector[:2] = [1.0, -1.0] # Ensure the first two patterns are +0 and -0
|
| 768 |
+
|
| 769 |
+
measurement_vector = torch.tensor(
|
| 770 |
+
(raw_data[:, 1] * sign_vector).reshape(-1, 2).sum(axis=1),
|
| 771 |
+
dtype=torch.float32,
|
| 772 |
+
device=device,
|
| 773 |
+
)
|
| 774 |
+
|
| 775 |
+
# index_of_max = torch.argmax(measurement_vector).item()
|
| 776 |
+
# index_of_min = torch.argmin(measurement_vector).item()
|
| 777 |
+
# min_value = measurement_vector[index_of_min].item()
|
| 778 |
+
# max_value = measurement_vector[index_of_max].item()
|
| 779 |
+
# print(f"Max value in measurement vector: {max_value} at index {index_of_max}")
|
| 780 |
+
# print(f"Min value in measurement vector: {min_value} at index {index_of_min}")
|
| 781 |
+
# exit()
|
| 782 |
+
|
| 783 |
+
pcm_op_full = PhotocurrentMapOp(J=J_order, device=device)
|
| 784 |
+
print(
|
| 785 |
+
f"pcm_op_full domain shape: {pcm_op_full.domain_shape}, range shape: {pcm_op_full.range_shape}"
|
| 786 |
+
)
|
| 787 |
+
with torch.no_grad():
|
| 788 |
+
ground_truth_image = pcm_op_full.pseudo_inv(measurement_vector)
|
| 789 |
+
print(
|
| 790 |
+
f"Reconstructed ground truth image shape from original measurement data: {ground_truth_image.shape}"
|
| 791 |
+
)
|
| 792 |
+
|
| 793 |
+
if tests_scale_ground_truth:
|
| 794 |
+
gt_min, gt_max = (
|
| 795 |
+
ground_truth_image.min().item(),
|
| 796 |
+
ground_truth_image.max().item(),
|
| 797 |
+
)
|
| 798 |
+
ground_truth_image = (ground_truth_image - gt_min) / (gt_max - gt_min)
|
| 799 |
+
print(
|
| 800 |
+
f"Normalized ground truth image to [0, 1]. Min: {ground_truth_image.min().item()}, Max: {ground_truth_image.max().item()}"
|
| 801 |
+
)
|
| 802 |
+
measurement_vector = None
|
| 803 |
+
|
| 804 |
+
test_cases = make_test_cases()
|
| 805 |
+
# print(f"Total number of test cases: {len(test_cases)}")
|
| 806 |
+
# print(test_cases)
|
| 807 |
+
# for sampling_ratio, coarse_J in test_cases:
|
| 808 |
+
# sampling_percentage = sampling_ratio * 100
|
| 809 |
+
# coarse_percentage = (1<<(2*coarse_J))/(1<<(2*J_order))*100
|
| 810 |
+
# print(f"Sampling: {sampling_percentage}%, coarse_J: {coarse_J} ({coarse_percentage}%)")
|
| 811 |
+
# assert sampling_percentage >= coarse_percentage, (
|
| 812 |
+
# "Sampling percentage must be larger than or equal to coarse percentage. "
|
| 813 |
+
# f"Got sampling {sampling_percentage}% and coarse {coarse_percentage}%."
|
| 814 |
+
# )
|
| 815 |
+
# return
|
| 816 |
+
|
| 817 |
+
# ### Set a directory to save logs and results
|
| 818 |
+
#
|
| 819 |
+
# Each run is stored in a separate subdirectory named with the current date and
|
| 820 |
+
# time, which makes it easier to keep track of different experiments.
|
| 821 |
+
|
| 822 |
+
current_datetime_str = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 823 |
+
experiment_log_dir = (
|
| 824 |
+
root_output_dir
|
| 825 |
+
/ f"{current_datetime_str}_{data_name}_{randomizing_scheme}_{num_trials}_trials"
|
| 826 |
+
)
|
| 827 |
+
experiment_log_dir.mkdir(parents=True, exist_ok=True)
|
| 828 |
+
|
| 829 |
+
for i_seed in tqdm(range(num_trials_skip, num_trials), desc="Running trials"):
|
| 830 |
+
print(f"\n=== Trial {i_seed} ===")
|
| 831 |
+
log_dir = experiment_log_dir / f"trial_{i_seed}"
|
| 832 |
+
log_dir.mkdir(parents=True, exist_ok=True)
|
| 833 |
+
|
| 834 |
+
# %% [markdown]
|
| 835 |
+
# ## First experiment: PnP-ADMM
|
| 836 |
+
#
|
| 837 |
+
# In this section the PCM PnP-ADMM algorithm is tested on the CIGS data.
|
| 838 |
+
|
| 839 |
+
# %% [markdown]
|
| 840 |
+
# Define the prior function using a pre-trained DRUNet denoiser and the
|
| 841 |
+
# corresponding Plug-and-Play ADMM solver.
|
| 842 |
+
#
|
| 843 |
+
# DRUNet is a deep convolutional denoiser proposed by:
|
| 844 |
+
#
|
| 845 |
+
# > Kai Zhang, Yawei Li, Wangmeng Zuo, Lei Zhang, Luc Van Gool, and
|
| 846 |
+
# > Radu Timofte, "Plug-and-Play Image Restoration with Deep Denoiser Prior,"
|
| 847 |
+
# > IEEE Transactions on Pattern Analysis and Machine Intelligence, 44(10),
|
| 848 |
+
# > 6360–6376, 2022.
|
| 849 |
+
#
|
| 850 |
+
# In the PnP-ADMM framework, the proximal step of a regulariser is replaced by
|
| 851 |
+
# an off-the-shelf denoiser. Here DRUNet acts as a powerful learned prior for
|
| 852 |
+
# the PCM inverse problem, while the data fidelity term is handled by ADMM.
|
| 853 |
+
|
| 854 |
+
# %%
|
| 855 |
+
if runs_pnp_admm:
|
| 856 |
+
method_name = f"pnp_admm_{denoiser_name}_iters={pnp_admm_iters}_eta={pnp_admm_eta}_cg_iters={cg_iters}_drunet_sigma={drunet_sigma}"
|
| 857 |
+
make_csv(method_name=method_name, log_dir=log_dir)
|
| 858 |
+
# for delta_divided_by, subtract_from_J in tqdm(test_cases, desc="Running PnP-ADMM experiments"):
|
| 859 |
+
for sampling_ratio, coarse_J in tqdm(
|
| 860 |
+
test_cases, desc="Running PnP-ADMM experiments"
|
| 861 |
+
):
|
| 862 |
+
run_pcm_demo(
|
| 863 |
+
recon_description=f"PnP-ADMM ({pnp_admm_iters} iters, η={pnp_admm_eta}, cg_iters={cg_iters}, σ={drunet_sigma})",
|
| 864 |
+
recon_fn=run_pnp_admm,
|
| 865 |
+
ground_truth_image=ground_truth_image,
|
| 866 |
+
method_name=method_name,
|
| 867 |
+
image_name=data_name,
|
| 868 |
+
J=J_order, # image size is 2^J x 2^J
|
| 869 |
+
sampling_ratio=sampling_ratio,
|
| 870 |
+
coarse_J=coarse_J,
|
| 871 |
+
log_dir=log_dir,
|
| 872 |
+
device=device,
|
| 873 |
+
seed=i_seed,
|
| 874 |
+
)
|
| 875 |
+
|
| 876 |
+
# %% [markdown]
|
| 877 |
+
# The code above runs the PnP-ADMM reconstruction and compares it to the
|
| 878 |
+
# zero-filled pseudo-inverse.
|
| 879 |
+
#
|
| 880 |
+
# Although PnP-ADMM substantially improves PSNR and SSIM, it can smooth out
|
| 881 |
+
# fine-scale structures. In the context of defect detection, these small
|
| 882 |
+
# features can be crucial, so high PSNR and SSIM alone are not sufficient to
|
| 883 |
+
# guarantee that the reconstruction is fit for purpose.
|
| 884 |
+
#
|
| 885 |
+
# In the next sections, two compressed sensing baselines with a wavelet
|
| 886 |
+
# sparsity prior are explored and compared to the PnP-ADMM result.
|
| 887 |
+
|
| 888 |
+
# %% [markdown]
|
| 889 |
+
# ## Compressed sensing baseline: FISTA with wavelet sparsity
|
| 890 |
+
#
|
| 891 |
+
# This section applies FISTA with an $\ell_1$-penalty on wavelet coefficients
|
| 892 |
+
# as a classical compressed sensing baseline.
|
| 893 |
+
#
|
| 894 |
+
# Let $\Phi$ denote the PCM forward operator and $\Psi$ a 2D wavelet
|
| 895 |
+
# transform with inverse $\Psi^{-1}$. The composite operator
|
| 896 |
+
# $A = \Phi \Psi^{-1}$ acts on wavelet coefficients $w$.
|
| 897 |
+
# FISTA approximately solves the standard $\ell_1$-regularised problem
|
| 898 |
+
#
|
| 899 |
+
# $$
|
| 900 |
+
# \min_w \frac{1}{2} \lVert A w - y \rVert_2^2
|
| 901 |
+
# + \lambda \lVert w \rVert_1,
|
| 902 |
+
# $$
|
| 903 |
+
#
|
| 904 |
+
# and the final current map is obtained as $x = \Psi^{-1} w$.
|
| 905 |
+
#
|
| 906 |
+
# An optional debiasing step is included at the end to reduce the bias induced
|
| 907 |
+
# by the $\ell_1$ penalty on the active support.
|
| 908 |
+
|
| 909 |
+
# %%
|
| 910 |
+
if runs_fista_l1:
|
| 911 |
+
make_csv(method_name="fista_l1", log_dir=log_dir)
|
| 912 |
+
for sampling_ratio, coarse_J in tqdm(
|
| 913 |
+
test_cases, desc="Running FISTA-L1 experiments"
|
| 914 |
+
):
|
| 915 |
+
run_pcm_demo(
|
| 916 |
+
recon_description="FISTA-L1",
|
| 917 |
+
recon_fn=run_fista_l1,
|
| 918 |
+
ground_truth_image=ground_truth_image,
|
| 919 |
+
method_name="fista_l1",
|
| 920 |
+
image_name=data_name,
|
| 921 |
+
J=J_order, # image size is 2^J x 2^J
|
| 922 |
+
sampling_ratio=sampling_ratio,
|
| 923 |
+
coarse_J=coarse_J,
|
| 924 |
+
log_dir=log_dir,
|
| 925 |
+
device=device,
|
| 926 |
+
seed=i_seed,
|
| 927 |
+
)
|
| 928 |
+
|
| 929 |
+
# %% [markdown]
|
| 930 |
+
# ## Compressed sensing baseline: SPGL1 with wavelet sparsity
|
| 931 |
+
#
|
| 932 |
+
# This section applies the SPGL1 algorithm as a second compressed sensing
|
| 933 |
+
# baseline, again using a wavelet sparsity prior in the same setting
|
| 934 |
+
# $A = \Phi \Psi^{-1}$.
|
| 935 |
+
#
|
| 936 |
+
# SPGL1 is a spectral projected gradient method that efficiently solves
|
| 937 |
+
# large-scale $\ell_1$-regularised problems and basis pursuit denoising
|
| 938 |
+
# formulations. In this example it is run with default parameters suitable
|
| 939 |
+
# for the PCM problem size, followed by the same optional debiasing step
|
| 940 |
+
# used for FISTA.
|
| 941 |
+
|
| 942 |
+
# %%
|
| 943 |
+
if runs_spgl1:
|
| 944 |
+
method_name = f"spgl1_factor={factor}"
|
| 945 |
+
make_csv(method_name=method_name, log_dir=log_dir)
|
| 946 |
+
for sampling_ratio, coarse_J in tqdm(
|
| 947 |
+
test_cases, desc="Running SPGL1 experiments"
|
| 948 |
+
):
|
| 949 |
+
run_pcm_demo(
|
| 950 |
+
recon_description="SPGL1",
|
| 951 |
+
recon_fn=run_spgl1,
|
| 952 |
+
ground_truth_image=ground_truth_image,
|
| 953 |
+
method_name=method_name,
|
| 954 |
+
image_name=data_name,
|
| 955 |
+
J=J_order, # image size is 2^J x 2^J
|
| 956 |
+
sampling_ratio=sampling_ratio,
|
| 957 |
+
coarse_J=coarse_J,
|
| 958 |
+
log_dir=log_dir,
|
| 959 |
+
device=device,
|
| 960 |
+
seed=i_seed,
|
| 961 |
+
)
|
| 962 |
+
|
| 963 |
+
|
| 964 |
+
if __name__ == "__main__":
|
| 965 |
+
run_experiments()
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/masks/sample_20.0_percent_coarse_J=6_uniform_random.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7ab8ee173bc5748f1dc0c59f29a478db2ec4097f327db561d9c65ce7b9d4aefe
|
| 3 |
+
size 65664
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/masks/sample_50.0_percent_coarse_J=6_uniform_random.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:92c2958280b1ace0c7e2ea2e76d7510767589a13ff9eb7c73ac2703d14502c21
|
| 3 |
+
size 65664
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/masks/sample_80.0_percent_coarse_J=6_uniform_random.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1010daf022b0047d5d0c0a719c78df72a8239d1ca59b02f10248a53a71e6d579
|
| 3 |
+
size 65664
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05/images/example_CIGS_256x256_pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05_sample_20.0_percent_coarse_J=6_uniform_random.png
ADDED
|
Git LFS Details
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05/images/example_CIGS_256x256_pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05_sample_50.0_percent_coarse_J=6_uniform_random.png
ADDED
|
Git LFS Details
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05/images/example_CIGS_256x256_pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05_sample_80.0_percent_coarse_J=6_uniform_random.png
ADDED
|
Git LFS Details
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05/metrics.csv
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
sampling_percentage, coarse_J, mse_zero_filled, psnr_zero_filled, ssim_zero_filled, pearson_corr_zero_filled, mse_recon, psnr_recon, ssim_recon, pearson_corr_recon
|
| 2 |
+
80.0, 6, 0.0012414116645231843, 28.51029396057129, 0.5397184491157532, 0.9965503811836243, 5.654777851304971e-06, 51.925296783447266, 0.9943547248840332, 0.9999841451644897
|
| 3 |
+
50.0, 6, 0.0027226863894611597, 25.09947967529297, 0.44537514448165894, 0.9924188852310181, 7.955688488436863e-06, 50.44267272949219, 0.9946114420890808, 0.9999784231185913
|
| 4 |
+
20.0, 6, 0.004477473441511393, 22.939123153686523, 0.5211963057518005, 0.9875015616416931, 7.346551865339279e-05, 40.78861618041992, 0.9826374053955078, 0.9997962117195129
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05/recons/example_CIGS_256x256_pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05_sample_20.0_percent_coarse_J=6_uniform_random.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0d6be326b5049d2876d93d47b4d8770518f70bdfab2cea7885b0ff4d533005d9
|
| 3 |
+
size 262272
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05/recons/example_CIGS_256x256_pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05_sample_50.0_percent_coarse_J=6_uniform_random.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:11f33e55851638c2e1622ec8e5f4d4cf38b4cff1db980f068b25309f5b125b33
|
| 3 |
+
size 262272
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05/recons/example_CIGS_256x256_pnp_admm_gs_drunet_iters=50_eta=0.01_cg_iters=20_drunet_sigma=0.05_sample_80.0_percent_coarse_J=6_uniform_random.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:58a4fc24d51c2aaba7e24099d959bc18fcaf24586d50881dbbb85b298e9788c8
|
| 3 |
+
size 262272
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/spgl1_factor=1/images/example_CIGS_256x256_spgl1_factor=1_sample_20.0_percent_coarse_J=6_uniform_random.png
ADDED
|
Git LFS Details
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/spgl1_factor=1/images/example_CIGS_256x256_spgl1_factor=1_sample_50.0_percent_coarse_J=6_uniform_random.png
ADDED
|
Git LFS Details
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/spgl1_factor=1/images/example_CIGS_256x256_spgl1_factor=1_sample_80.0_percent_coarse_J=6_uniform_random.png
ADDED
|
Git LFS Details
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/spgl1_factor=1/metrics.csv
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
sampling_percentage, coarse_J, mse_zero_filled, psnr_zero_filled, ssim_zero_filled, pearson_corr_zero_filled, mse_recon, psnr_recon, ssim_recon, pearson_corr_recon
|
| 2 |
+
80.0, 6, 0.0012414116645231843, 28.51029396057129, 0.5397184491157532, 0.9965503811836243, 2.099596258631209e-06, 56.228092193603516, 0.998619794845581, 0.9999946355819702
|
| 3 |
+
50.0, 6, 0.0027226863894611597, 25.09947967529297, 0.44537514448165894, 0.9924188852310181, 0.00013173255138099194, 38.25252151489258, 0.9336307048797607, 0.9996347427368164
|
| 4 |
+
20.0, 6, 0.004477473441511393, 22.939123153686523, 0.5211963057518005, 0.9875015616416931, 0.002738557755947113, 25.074235916137695, 0.6920914649963379, 0.9923802018165588
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/spgl1_factor=1/recons/example_CIGS_256x256_spgl1_factor=1_sample_20.0_percent_coarse_J=6_uniform_random.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:340d7d5459e4128d7f2e2078f2b65425b63e564d459574181f7f4d45105a02c3
|
| 3 |
+
size 262272
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/spgl1_factor=1/recons/example_CIGS_256x256_spgl1_factor=1_sample_50.0_percent_coarse_J=6_uniform_random.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8d11eac038ee767df1fae7fa9613a8c5261f0aee1656c2ae8a9a33b977ccea91
|
| 3 |
+
size 262272
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/spgl1_factor=1/recons/example_CIGS_256x256_spgl1_factor=1_sample_80.0_percent_coarse_J=6_uniform_random.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3554aae6eb3cd8a8588f9178e2cf4c37fd174521d786a10feca272952d21f840
|
| 3 |
+
size 262272
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/zero_filled/example_CIGS_256x256_sample_20.0_percent_coarse_J=6_uniform_random.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c5701cbfb90b62ea8b3f85addb5f8544b4dacd66b07bc1319bf44c5396f62e8d
|
| 3 |
+
size 262272
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/zero_filled/example_CIGS_256x256_sample_50.0_percent_coarse_J=6_uniform_random.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:76e2138daacb408acbca2dd7cd86499a82858460502ef9445f76bae1144a0e0f
|
| 3 |
+
size 262272
|
20260122_182641_example_CIGS_256x256_uniform_1_trials/trial_0/zero_filled/example_CIGS_256x256_sample_80.0_percent_coarse_J=6_uniform_random.npy
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c7244e4b780bf2b5feb9ce2633290f5cd4a548a3899f815afbce171893c92782
|
| 3 |
+
size 262272
|