Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: L1 loss and PSNR with Self-Supervised Loss for benchmark_algos command #288

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions aydin/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
peak_signal_noise_ratio,
structural_similarity,
)
from skimage.util.dtype import dtype_range
from torch.utils.data import DataLoader

from aydin.analysis.resolution_estimate import resolution_estimate
Expand Down Expand Up @@ -401,20 +402,27 @@ def benchmark_algos(files, **kwargs):
0
] # Get a list of available denoisers

loss_function = mean_squared_error # Define the loss function
# Define the loss function
def loss_function(u, v):
return numpy.mean(numpy.abs(u - v))

self_supervised_loss_results = {}
ssl_psnr_results = {}
estimated_snr_results = {}
estimated_res_results = {}

# Iterate over the input images
for filename, image_array, metadata in zip(filenames, image_arrays, metadatas):
self_supervised_loss_results[filename] = {}
ssl_psnr_results[filename] = {}
estimated_snr_results[filename] = {}
estimated_res_results[filename] = {}

# Create a Dataset object to get random masks
array = normalise(image_array[numpy.newaxis, numpy.newaxis, :, :])
dataset = RandomMaskedDataset(array, patch_size=min(image_array.shape))
dataset = RandomMaskedDataset(
array, patch_size=min(image_array.shape), pixel_masking_probability=0.1
)
print(f"dataset length: {len(dataset)}, patch_size:{dataset.patch_size}")
data_loader = DataLoader(dataset, batch_size=16, num_workers=0, shuffle=False)
_, input_image, mask = next(iter(data_loader))
Expand All @@ -425,7 +433,7 @@ def benchmark_algos(files, **kwargs):

# Iterate over the available denoisers
for denoiser_name in denoiser_names:
ss_losses, snrs, res_estimates = [], [], []
ss_losses, ssl_psnrs, snrs, res_estimates = [], [], [], []
for _ in range(kwargs["nbruns"]):
# Get the specific restoration instance with given denoiser variant
denoiser_instance = get_denoiser_class_instance(variant=denoiser_name)
Expand All @@ -447,6 +455,14 @@ def benchmark_algos(files, **kwargs):
# Self-supervised loss
ss_losses.append(loss_function(denoised * mask, image_array * mask))

# PSNR with Self-supervised loss
dmax = dtype_range[denoised.dtype.type][1]

# We take square root of the MAX_I^2 term to help dimension units to cancel out with l1 loss
ssl_psnrs.append(
10 * numpy.log10(dmax) - 10 * numpy.log10(ss_losses[-1])
)

# SNR estimate
snrs.append(snr_estimate(denoised))

Expand All @@ -456,13 +472,15 @@ def benchmark_algos(files, **kwargs):
self_supervised_loss_results[filename] |= {
denoiser_name: numpy.average(ss_losses)
}
ssl_psnr_results[filename] |= {denoiser_name: numpy.average(ssl_psnrs)}
estimated_snr_results[filename] |= {denoiser_name: numpy.average(snrs)}
estimated_res_results[filename] |= {
denoiser_name: numpy.average(res_estimates)
}

result_pairs = [
("self_supervised_loss.csv", self_supervised_loss_results),
("ssl_psnr.csv", ssl_psnr_results),
("estimated_snr.csv", estimated_snr_results),
("res_estimate.csv", estimated_res_results),
]
Expand Down