example script Sparse2Inverse#169
Open
Alvaro-Exposito-MTZ wants to merge 7 commits intoCambridgeCIA:mainfrom
Open
example script Sparse2Inverse#169Alvaro-Exposito-MTZ wants to merge 7 commits intoCambridgeCIA:mainfrom
Alvaro-Exposito-MTZ wants to merge 7 commits intoCambridgeCIA:mainfrom
Conversation
AnderBiguri
reviewed
Jan 15, 2026
Comment on lines
82
to
86
| savefolder = pathlib.Path("/home/ea692/LION/LION/trained_models/Sparse2Inverse/Test/SparseAngleLowDoseCTRecon/SparseVSNoise/30sin2000ep/64Angles_Haarpsi_and_SSIM") | ||
| savefolder.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| #Load the trained model of Sparse2Inverse | ||
| model_Sparse, _, _ = UNet().load("/store/LION/ea692/LION/LION/trained_models/Sparse2Inverse/Train/SparseAngleLowDoseCTRecon/S2I.json") |
Member
There was a problem hiding this comment.
Lets make the example to be the training + testing example, so don't load a saved model
AnderBiguri
reviewed
Jan 15, 2026
Comment on lines
106
to
174
| def normalize_01(x,y): | ||
| x = (x - y.min())/ (y.max() - y.min()) | ||
| x[x>1]=1 | ||
| x[x<0]=0 | ||
| return x | ||
|
|
||
| #HAARPsi metric | ||
| haarpsi = HAARPsi(C=5.0, a=4.9) | ||
| haarpsi.eval() | ||
|
|
||
| haarpsi_values_sparse = [] | ||
|
|
||
| #SSIM metric | ||
| def my_ssim(x, y): | ||
| x = x.cpu().numpy().squeeze() | ||
| y = y.cpu().numpy().squeeze() | ||
| return ssim(x, y, data_range=x.max() - x.min()) | ||
|
|
||
| ssim_values_sparse = [] | ||
|
|
||
| # Fixed visualization window for all images to ensure fair visual comparison. | ||
| vmin, vmax = 0, 5 | ||
|
|
||
| for idx, (sino, target) in enumerate(dataloader): | ||
| sino = sino.to(device) | ||
| with torch.no_grad(): | ||
| model_reco_sparse = solver_sparse.reconstruct(sino).detach().cpu() | ||
| target_cpu = target.cpu() | ||
|
|
||
| target_n = normalize_01(target_cpu,target_cpu) | ||
| sparse_n = normalize_01(model_reco_sparse,target_cpu) | ||
|
|
||
| haarspi_sparse,_,_ = haarpsi(target_n, sparse_n) | ||
| ssim_sparse=my_ssim(target_n,sparse_n) | ||
|
|
||
| ssim_values_sparse.append(ssim_sparse) | ||
|
|
||
| haarpsi_values_sparse.append(haarspi_sparse.item()) | ||
|
|
||
| #Figure the comparison between target and reconstruction. | ||
| #Raw reconstructions are shown without normalization. | ||
| if idx == 0: | ||
| plt.figure(figsize=(12,4)) | ||
|
|
||
| plt.subplot(1,2,1) | ||
| plt.title("Target (clean)") | ||
| im0 = plt.imshow(target[0,0].cpu(), cmap="gray") | ||
| plt.axis("off") | ||
| im0.set_clim(vmin, vmax) | ||
|
|
||
| plt.subplot(1,2,2) | ||
| plt.title(f"Model raw reconstruction Sparse\nhaarpsi={haarspi_sparse.item():.3f}\nssim={ssim_sparse:.3f}") | ||
| im2 = plt.imshow(model_reco_sparse[0,0], cmap="gray") | ||
| plt.axis("off") | ||
| im2.set_clim(vmin, vmax) | ||
|
|
||
| plt.tight_layout() | ||
| plt.savefig(savefolder / "Reconstruction_Sparse2Inverse_SparseAngleLowDoseCTRecon_Haarspi_SSIM.png", dpi=150) | ||
| plt.close() | ||
|
|
||
| haarpsi_mean_sparse = np.mean(haarpsi_values_sparse) | ||
| haarpsi_std_sparse = np.std(haarpsi_values_sparse) | ||
|
|
||
| ssim_mean_sparse = np.mean(ssim_values_sparse) | ||
| ssim_std_sparse = np.std(ssim_values_sparse) | ||
|
|
||
| #Plot the metrics obtained throw all the sinograms tested | ||
| print(f"haarpsi mean Sparse SparseAngleLowDoseCTRecon 30sin2000ep 64 angles: {haarpsi_mean_sparse:.4f}, haarpsi std Sparse: {haarpsi_std_sparse:.4f}") | ||
| print(f"ssim mean Sparse SparseAngleLowDoseCTRecon 30sin2000ep 64 angles: {ssim_mean_sparse:.4f}, ssim std Sparse: {ssim_std_sparse:.4f}") |
Member
There was a problem hiding this comment.
For the example code, lets just add a testing with SSIM or PSNR and no plotting.
You can use
solver.set_testing(dataloader, my_ssim)
solver.test()
I think. check
I've changed my_ssim function and put solver.set_testing()
AnderBiguri
reviewed
Jan 26, 2026
Comment on lines
+104
to
+118
| def normalize_01(x,y): | ||
| x = (x - y.min())/ (y.max() - y.min()) | ||
| x[x>1]=1 | ||
| x[x<0]=0 | ||
| return x | ||
|
|
||
| #SSIM metric | ||
| def my_ssim(x, y): | ||
| x = x.detach().squeeze().cpu() | ||
| y = y.detach().squeeze().cpu() | ||
|
|
||
| target_n = normalize_01(y,y) | ||
| sparse_n = normalize_01(x,y) | ||
| return ssim(target_n, sparse_n, data_range=1) | ||
|
|
Member
There was a problem hiding this comment.
you don't need normalize for SSIM, yo can give the data range (of the target) to ssim. It would be also best if we can use LIONs SSIM in the example if thats ok?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.