Repository navigation
Expand file tree
/
Copy pathphotocurrent_mapping.py
More file actions
459 lines (384 loc) · 14.5 KB
/
Copy pathphotocurrent_mapping.py
File metadata and controls
459 lines (384 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# =============================================================================
# This file is part of LION library
# License : GPL-3
#
# Author : Thanh Trung (Troy) Vu
# =============================================================================
# %% [markdown]
# # Plug-and-play ADMM for Photocurrent Mapping image reconstruction
#
# This notebook demonstrates Plug-and-Play (PnP) ADMM image reconstruction for
# Photocurrent Mapping (PCM) data.
#
# Starting from a high-resolution current map of a CIGS solar cell, subsampled
# PCM measurements are simulated using the `PhotocurrentMapOp` operator. Several
# reconstruction methods are then compared:
#
# - Zero-filled pseudo-inverse reconstruction.
# - Two compressed sensing baselines with a wavelet sparsity prior:
# FISTA with an $\ell_1$ penalty and SPGL1.
# - PnP-ADMM with a pre-trained DRUNet denoiser as prior.
#
# The goal is not to optimise performance, but to illustrate how
# classical sparse reconstruction methods and PnP can be combined with the LION
# operators for PCM.
# %% [markdown]
# ## Setup
# %% [markdown]
# ### Device configuration
#
# Set the default device to a GPU if available. If multiple GPUs are present,
# the desired GPU index can be specified here.
# %%
import torch
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
torch.set_default_device(device)
# %% [markdown]
# ### Imports
#
# Import the required libraries, including LION operators and reconstruction algorithms for PCM.
# %%
from datetime import datetime
from pathlib import Path
from typing import Callable
import deepinv
import matplotlib.pyplot as plt
import numpy as np
from jaxtyping import Float
from torchmetrics import PeakSignalNoiseRatio, StructuralSimilarityIndexMeasure
from LION.classical_algorithms import fista_l1
from LION.classical_algorithms.spgl1_torch import spgl1_torch
from LION.operators import CompositeOp, Wavelet2D
from LION.operators.DebiasOp import debias_ls
# LION imports
from LION.operators.pcm.PhotocurrentMapOp import PhotocurrentMapOp, Subsampler
from LION.reconstructors.PnP import PnP
GrayscaleImage2D = Float[torch.Tensor, "height width"]
Measurement1D = Float[torch.Tensor, "num_measurements"]
# %% [markdown]
# ### Define the data file paths
#
# The example uses a single $256 \times 256$ current map of a CIGS solar cell
# stored as a NumPy array. This image will serve as the ground truth in the
# experiments.
# %%
data_dir = Path("/home/t/Documents/GIT/LION/data/photocurrent_data")
# data_dir = Path("your/path/to/photocurrent_data")
assert data_dir.exists(), f"Data directory {data_dir} does not exist."
cigs_filename = "example_CIGS_256x256.npy"
assert (data_dir / cigs_filename).exists(), f"Data file not found in {data_dir}."
# %% [markdown]
# ### Set a directory to save logs and results
#
# Each run is stored in a separate subdirectory named with the current date and
# time, which makes it easier to keep track of different experiments.
# %%
current_datetime_str = datetime.now().strftime("%Y%m%d_%H%M%S")
log_dir = Path("pcm_demo_output") / current_datetime_str
log_dir.mkdir(parents=True, exist_ok=True)
# %% [markdown]
# ## Experiment
# %% [markdown]
# Define a general function to run the photocurrent mapping reconstruction using a reconstruction method.
#
# The helper function `run_pcm_demo`:
#
# - Builds the PCM operator and simulates subsampled measurements.
# - Computes the zero-filled pseudo-inverse reconstruction.
# - Runs a chosen reconstruction method given by `recon_fn`.
# - Reports PSNR and SSIM for both reconstructions, displays and saves the images.
# %% mystnb={"code_prompt_show": "Show plotting details"} tags=["hide-cell"]
def show_images(
images: list[torch.Tensor],
fig_filepath: Path,
titles: list[str] | None = None,
suptitle: str | None = None,
) -> None:
"""Plot images."""
n_images = len(images)
_, axes = plt.subplots(1, n_images, squeeze=False, figsize=(n_images * 4, 4))
for i in range(n_images):
axes[0][i].imshow(images[i].detach().cpu().squeeze(), cmap="gray")
axes[0][i].axis("off")
if titles:
axes[0][i].set_title(titles[i])
if suptitle:
plt.suptitle(suptitle, x=0.4, y=-0.05, fontsize=16)
plt.savefig(fig_filepath, dpi=150)
# %%
def run_pcm_demo(
recon_method_name: str,
recon_fn: Callable[[PhotocurrentMapOp, Measurement1D], GrayscaleImage2D],
ground_truth_image: GrayscaleImage2D,
image_name: str,
J: int, # image size will be 2^J x 2^J
subtract_from_J: int = 1,
delta_divided_by: int = 4,
log_dir: Path | str = ".",
device: torch.device | str = "cuda:0",
):
N = 1 << J
im_tensor = torch.tensor(ground_truth_image).unsqueeze(0).unsqueeze(0) # (1,1,H,W)
coarseJ = J - subtract_from_J
delta = 1.0 / delta_divided_by
sampling_percentage = delta * 100
in_order_measurements_percentage = 1 / (1 << (subtract_from_J * 2)) * 100
print(f"Sampling rate: {sampling_percentage:.2f}%")
print(f"In-order measurements: {in_order_measurements_percentage:.2f}%")
subsampler = Subsampler(n=N * N, coarseJ=coarseJ, delta=delta)
pcm_op = PhotocurrentMapOp(J=J, subsampler=subsampler, device=device)
y_subsampled_tensor = pcm_op(im_tensor)
zero_filled_recon_tensor = (
pcm_op.pseudo_inv(y_subsampled_tensor).unsqueeze(0).unsqueeze(0)
)
recon_tensor = (
recon_fn(
pcm_op=pcm_op,
pcm_measurement=y_subsampled_tensor,
)
.unsqueeze(0)
.unsqueeze(0)
)
data_range = (im_tensor.max() - im_tensor.min()).item()
psnr = PeakSignalNoiseRatio(data_range=data_range).to(device)
ssim = StructuralSimilarityIndexMeasure(data_range=data_range).to(device)
psnr_zero_filled = psnr(zero_filled_recon_tensor, im_tensor)
psnr_recon = psnr(recon_tensor, im_tensor)
ssim_zero_filled = ssim(zero_filled_recon_tensor, im_tensor)
ssim_recon = ssim(recon_tensor, im_tensor)
show_images(
[im_tensor, zero_filled_recon_tensor, recon_tensor],
fig_filepath=Path(log_dir)
/ f"{image_name}_{recon_method_name}_sampling_percentage={sampling_percentage:.2f}_in_order_measurements={in_order_measurements_percentage:.2f}.png",
titles=[
"Original Image",
f"Zero-filled Reconstruction\nPSNR: {psnr_zero_filled:.2f} dB, SSIM: {ssim_zero_filled:.4f}",
f"{recon_method_name} Reconstruction\nPSNR: {psnr_recon:.2f} dB, SSIM: {ssim_recon:.4f}",
],
suptitle=(
"PCM Reconstructions Comparison\n"
+ f"sampling rate: {sampling_percentage:.2f}%, in-order measurements: {in_order_measurements_percentage:.2f}%"
),
)
# %% [markdown]
# Load the data.
#
# The array `cigs_raw_data` is a single 2D image representing the current map
# of the CIGS device. This will be used as the ground truth image for all
# reconstruction methods.
# %%
cigs_raw_data: GrayscaleImage2D = np.load(data_dir / cigs_filename)
print(f"CIGS data shape: {cigs_raw_data.shape}")
# %% [markdown]
# In the examples below:
#
# - `J = 8` so the image size is $2^J \times 2^J = 256 \times 256$.
# - `delta_divided_by = 4` corresponds to a sampling rate
# $\delta = 1 / 4 = 0.25$, that is, 25% of the total measurements.
# - `subtract_from_J = 2` keeps a central $2^{J-2} \times 2^{J-2} = 64 \times 64$
# block of in-order measurements, corresponding to 6.25% of the total;
# the remaining samples are taken in a compressed sensing fashion.
# %%
J = 8 # image size is 2^J x 2^J = 256x256
delta_divided_by = 4 # 25% sampling
subtract_from_J = (
2 # keep 2^{J-2} x 2^{J-2} = 64x64 in-order measurements, or 6.25% of the total
)
# %% [markdown]
# ## First experiment: PnP-ADMM on CIGS data
#
# In this section the PCM PnP-ADMM algorithm is tested on the CIGS data.
# %% [markdown]
# Define the prior function using a pre-trained DRUNet denoiser and the
# corresponding Plug-and-Play ADMM solver.
#
# DRUNet is a deep convolutional denoiser proposed by:
#
# > Kai Zhang, Yawei Li, Wangmeng Zuo, Lei Zhang, Luc Van Gool, and
# > Radu Timofte, "Plug-and-Play Image Restoration with Deep Denoiser Prior,"
# > IEEE Transactions on Pattern Analysis and Machine Intelligence, 44(10),
# > 6360–6376, 2022.
#
# In the PnP-ADMM framework, the proximal step of a regulariser is replaced by
# an off-the-shelf denoiser. Here DRUNet acts as a powerful learned prior for
# the PCM inverse problem, while the data fidelity term is handled by ADMM.
# %%
denoiser_DRUNet = deepinv.models.DRUNet(
pretrained="download", in_channels=1, out_channels=1, device=device
)
def denoiser_fn(x: GrayscaleImage2D) -> GrayscaleImage2D:
with torch.no_grad():
return (
denoiser_DRUNet(x.unsqueeze(0).unsqueeze(0), sigma=0.05)
.squeeze(0)
.squeeze(0)
)
def run_pnp_admm(
pcm_op: PhotocurrentMapOp, pcm_measurement: Measurement1D
) -> GrayscaleImage2D:
parameters = PnP.default_parameters("ADMM")
parameters.max_iter = 100
parameters.eta = 1e5 # ADMM step size
parameters.cg_max_iter = 100
parameters.cg_tol = 1e-7
print(
f"Running PnP-ADMM reconstruction: {parameters.max_iter} iterations, "
f"cg_max_iter={parameters.cg_max_iter}..."
)
pnp = PnP(pcm_op, denoiser_fn, parameters)
return pnp.reconstruct(pcm_measurement)
# %%
run_pcm_demo(
recon_method_name="PnP-ADMM",
recon_fn=run_pnp_admm,
ground_truth_image=cigs_raw_data,
image_name="cigs",
J=8, # image size is 2^J x 2^J = 256x256
delta_divided_by=4, # 25% sampling
subtract_from_J=2, # keep 2^{J-2} x 2^{J-2} = 64x64 in-order measurements, or 6.25% of the total
log_dir=log_dir,
device=device,
)
# %% [markdown]
# The code above runs the PnP-ADMM reconstruction and compares it to the
# zero-filled pseudo-inverse.
#
# Although PnP-ADMM substantially improves PSNR and SSIM, it can smooth out
# fine-scale structures. In the context of defect detection, these small
# features can be crucial, so high PSNR and SSIM alone are not sufficient to
# guarantee that the reconstruction is fit for purpose.
#
# In the next sections, two compressed sensing baselines with a wavelet
# sparsity prior are explored and compared to the PnP-ADMM result.
# %% [markdown]
# ## Compressed sensing baseline: FISTA with wavelet sparsity
#
# This section applies FISTA with an $\ell_1$-penalty on wavelet coefficients
# as a classical compressed sensing baseline.
#
# Let $\Phi$ denote the PCM forward operator and $\Psi$ a 2D wavelet
# transform with inverse $\Psi^{-1}$. The composite operator
# $A = \Phi \Psi^{-1}$ acts on wavelet coefficients $w$.
# FISTA approximately solves the standard $\ell_1$-regularised problem
#
# $$
# \min_w \frac{1}{2} \lVert A w - y \rVert_2^2
# + \lambda \lVert w \rVert_1,
# $$
#
# and the final current map is obtained as $x = \Psi^{-1} w$.
#
# An optional debiasing step is included at the end to reduce the bias induced
# by the $\ell_1$ penalty on the active support.
# %%
def run_fista_l1(
pcm_op: PhotocurrentMapOp, pcm_measurement: Measurement1D
) -> GrayscaleImage2D:
lam = 10 # Good for Daubechies 4 wavelet transform
max_iter = 1000
tol = 1e-5
debias_max_iter = 10 # TODO: Debiasing seems to not make much difference here
debias_support_tol = 1e-5
debias_tol = 1e-7
height, width = pcm_op.domain_shape
# Wavelet transform Psi
wavelet = Wavelet2D((height, width), wavelet_name="db4", device=device)
# Composite operator A = Phi Psi^{-1}
A_op = CompositeOp(wavelet, pcm_op, device=device)
print("Running FISTA reconstruction: " f"{max_iter} iterations, lambda={lam}...")
w_hat = fista_l1(
op=A_op,
y=pcm_measurement,
lam=lam,
max_iter=max_iter,
tol=tol,
L=None,
verbose=True,
)
# Optional debiasing
print(f"Running debiasing: {debias_max_iter} iterations...")
w_debias = debias_ls(
op=A_op,
y=pcm_measurement,
w=w_hat,
support_tol=debias_support_tol,
max_iter=debias_max_iter,
tol=debias_tol,
verbose=True,
)
# Current map reconstruction
cs_result_tensor = wavelet.inverse(w_debias)
return cs_result_tensor
# %%
run_pcm_demo(
recon_method_name="FISTA-L1",
recon_fn=run_fista_l1,
ground_truth_image=cigs_raw_data,
image_name="cigs",
J=8, # image size is 2^J x 2^J = 256x256
delta_divided_by=4, # 25% sampling
subtract_from_J=2, # keep 2^{J-2} x 2^{J-2} = 64x64 in-order measurements, or 6.25% of the total
log_dir=log_dir,
device=device,
)
# %% [markdown]
# ## Compressed sensing baseline: SPGL1 with wavelet sparsity
#
# This section applies the SPGL1 algorithm as a second compressed sensing
# baseline, again using a wavelet sparsity prior in the same setting
# $A = \Phi \Psi^{-1}$.
#
# SPGL1 is a spectral projected gradient method that efficiently solves
# large-scale $\ell_1$-regularised problems and basis pursuit denoising
# formulations. In this example it is run with default parameters suitable
# for the PCM problem size, followed by the same optional debiasing step
# used for FISTA.
# %%
def run_spgl1(
pcm_op: PhotocurrentMapOp, pcm_measurement: Measurement1D
) -> GrayscaleImage2D:
lam: float = 1e-3
max_iter = 1000
tol: float = 1e-4
debias_max_iter = 10
debias_support_tol = 1e-5
debias_tol = 1e-7
height, width = pcm_op.domain_shape
# Wavelet transform Psi
wavelet = Wavelet2D((height, width), wavelet_name="db4", device=device)
# Composite operator A = Phi Psi^{-1}
A_op = CompositeOp(wavelet, pcm_op, device=device)
print("Running SPGL1 reconstruction: " f"{max_iter} iterations, lambda={lam}...")
w_hat = spgl1_torch(
op=A_op,
y=pcm_measurement,
iter_lim=max_iter,
verbosity=0,
opt_tol=tol,
)
# Optional debiasing
print(f"Running debiasing: {debias_max_iter} iterations...")
w_debias = debias_ls(
op=A_op,
y=pcm_measurement,
w=w_hat,
support_tol=debias_support_tol,
max_iter=debias_max_iter,
tol=debias_tol,
verbose=True,
)
# Current map reconstruction
cs_result_tensor = wavelet.inverse(w_debias)
return cs_result_tensor
# %%
run_pcm_demo(
recon_method_name="SPGL1",
recon_fn=run_spgl1,
ground_truth_image=cigs_raw_data,
image_name="cigs",
J=8, # image size is 2^J x 2^J = 256x256
delta_divided_by=4, # 25% sampling
subtract_from_J=2, # keep 2^{J-2} x 2^{J-2} = 64x64 in-order measurements, or 6.25% of the total
log_dir=log_dir,
device=device,
)