def deconv_plotter(*, sources=None, recons, iter): """Function that plots the deconvolved images as the iterations go. Args: sources (ndarray): 3d array of sources recons (ndarray): 3d array of reconstructions iter (int): current iteration number """ num_sources = recons.shape[0] ssim1 = np.zeros(num_sources) if sources is not None: mse1 = np.mean((sources - recons)**2, axis=(1, 2)) psnr1 = 20 * np.log10(np.max(sources, axis=(1,2))/np.sqrt(mse1)) for i in range(num_sources): ssim1[i] = compare_ssim(sources[i], recons[i], data_range=np.max(recons[i])-np.min(recons[i])) title = 'Iteration: {}\n Recon. SSIM={}\n Recon. PSNR={}'.format(iter+1, ssim1, psnr1) else: title = 'Iteration: {}'.format(iter+1) plotter4d(recons, cmap='gist_heat', fignum=3, figsize=(5.6,8), title=title ) plt.pause(0.01)
from mas import sse_cost import numpy as np import matplotlib.pyplot as plt # %% psfs ----- ps = PhotonSieve() wavelengths = np.array([33.4, 33.5]) * 1e-9 sources = sources[np.array((0, 1))].reshape((2, 1, 160, 160)) psfs = PSFs( sieve=ps, source_wavelengths=wavelengths, measurement_wavelengths=wavelengths, num_copies=1 ) # %% contributions ----- contributions = get_contributions(sources=sources[:, 0, :, :], psfs=psfs, real=True) plt = plotter4d( contributions, title='measurement contributions', column_labels=wavelengths, sup_xlabel='source wavelengths', row_labels=wavelengths, sup_ylabel='measurement wavelengths', scale=True ) plt.savefig('contributions.png') plt.show()
# patch_based # TV bm3d_pnp # dncnn_pnp, model=model ), recon_init=recon, iternum=20, periter=5, nu=10**-1.5, lam=[10**-3.8]*num_sources, ) ssim_ = np.zeros(num_sources) mse_ = np.mean((sources_ref - recon)**2, axis=(1, 2)) psnr_ = 20 * np.log10(np.max(sources_ref, axis=(1,2))/np.sqrt(mse_)) for i in range(num_sources): ssim_[i] = ssim(sources_ref[i], recon[i], data_range=np.max(recon[i])-np.min(recon[i])) plotter4d(recon, cmap='gist_heat', figsize=(5.6,8), title='Recon. SSIM={}\n Recon. PSNR={}'.format(ssim_, psnr_) ) plotter4d(sources_ref, cmap='gist_heat', figsize=(5.6,8), title='Original' )
# csbs(psfs, sse_cost, 10, lam=1e-2, order=0) # fourier_slices(psfs) # %% ps ----- wavelengths = np.array([33.4e-9]) ps = PhotonSieve() psfs_ps = PSFs(ps, source_wavelengths=wavelengths, measurement_wavelengths=49, num_copies=5) plotter4d(np.fft.fftshift( np.abs(psfs.psf_dfts.reshape(7, 7, 301, 301)), axes=(2, 3), ), figsize=(5, 5), scale=True) plt.savefig('photonsieve_dfts.png', dpi=300) # %% sq ----- wavelengths = np.array([33.4e-9]) ps = PhotonSieve() psfs_sq = PSFs(ps, source_wavelengths=wavelengths, measurement_wavelengths=49, num_copies=5) def square(top, left, width, image_width=301, amplitude=1):
tikhonov_lam=1e-3) recon_csbs = tikhonov(psfs=psfs_csbs, measurements=meas_csbs, tikhonov_lam=1e-3) recon_focus -= np.mean(recon_focus, axis=(1, 2))[:, np.newaxis, np.newaxis] recon_csbs -= np.mean(recon_csbs, axis=(1, 2))[:, np.newaxis, np.newaxis] sources_comp = sources - np.mean(sources, axis=(1, 2))[:, np.newaxis, np.newaxis] result_focus = np.sum(compare_ssim(sources_comp, recon_focus)) result_csbs = np.sum(compare_ssim(sources_comp, recon_csbs)) print('focused SSIM sum:', result_focus) print('csbs SSIM sum:', result_csbs) plotter4d(recon_focus) plotter4d(recon_csbs) # plt.imsave('meas_focus0.png', meas_focus[0], cmap='gist_heat') # plt.imsave('meas_focus1.png', meas_focus[1], cmap='gist_heat') # plt.imsave('meas_csbs0.png', meas_csbs[0], cmap='gist_heat') # plt.imsave('meas_csbs1.png', meas_csbs[1], cmap='gist_heat') # plt.imsave('recon_focus0.png', recon_focus[0], cmap='gist_heat') # plt.imsave('recon_focus1.png', recon_focus[1], cmap='gist_heat') # plt.imsave('recon_csbs0.png', recon_csbs[0], cmap='gist_heat') # plt.imsave('recon_csbs1.png', recon_csbs[1], cmap='gist_heat')
noisy_nanoflare_list.append(noisy_nanoflare) noisy_nanoflares.append(noisy_nanoflare_list) # dimension: (num photon count, num measurements, X, Y) noisy_nanoflares = np.array(noisy_nanoflares) # %% plot ----- # single snapshot images plt = plotter4d( noisy_nanoflares[:, [0, -1]], title='Noisy, single-snapshot measurements at ', colorbar=False, row_labels=max_photon_count_list, column_labels=['t=0s', 't=19s'], sup_ylabel='Max photon count', figsize=(4.5, 10), cmap='gist_heat' ) plt.savefig('first_last_comparison.png', dpi=300) plt.figure() plt.imshow(measured_nanoflare_list[0], cmap='gist_heat') plt.axis('off') plt.title('Blurred, single-snapshot measurement') plt.savefig('single_measured.png', dpi=300) plt.figure() plt.imshow(nanoflare_list[0], cmap='gist_heat') plt.axis('off')
iterations=60, lam=10**-6, time_step=10**-2, liveplot=True) ###### COMPUTE PERFORMANCE METRICS ###### ssim_ = np.zeros(num_sources) mse_ = np.mean((sources_ref - recon)**2, axis=(1, 2)) psnr_ = 20 * np.log10(np.max(sources_ref, axis=(1, 2)) / np.sqrt(mse_)) for i in range(num_sources): ssim_[i] = ssim(sources_ref[i], recon[i], data_range=np.max(recon[i]) - np.min(recon[i])) plotter4d(recon, cmap='gist_heat', figsize=(5.6, 8), title='Recon. SSIM={}\n Recon. PSNR={}'.format(ssim_, psnr_)) print('ssim:{:.3f}, psnr:{:.2f}'.format(np.mean(ssim_, axis=0), np.mean(psnr_, axis=0))) plotter4d(measured_noisy, cmap='gist_heat', figsize=(5.6, 8), title='Noisy Meas') # plotter4d(sources, # title='Orig', # figsize=(5.6,8), # cmap='gist_heat' # )
def get_ssim(source, recon): return compare_ssim(recon, source) get_ssim = np.vectorize(get_ssim, signature='(a,b),(c,d)->()') csbs_modes = [ 'CSBS grid', 'CSBS focus', 'Naive grid', 'Naive focus', 'Original' ] recons[4] = sources plotter4d(recons[:, 0], cmap='gist_heat', scale=True, row_labels=csbs_modes, column_labels=['33.4nm', '33.5nm'], sup_xlabel='Source', sup_ylabel='CSBS mode', figsize=(5, 6)) plt.savefig('reconstructions.png', dpi=300) plt.close() # ssims = np.mean(get_ssim(sources, recons), axis=(1, 2)) ssims = np.mean(get_ssim(sources, recons), axis=(1, 2)) plt.plot(ssims, 'o') plt.grid(True) plt.xticks(np.arange(len(ssims)), csbs_modes) plt.savefig('ssim_comparison.png', dpi=300) plt.show()
tikhonov_order=order) psnr_focus.append(compare_psnr(sources, recon_focus)) psnr_csbs.append(compare_psnr(sources, recon_csbs)) ssim_focus.append(np.sum(compare_ssim(sources, recon_focus))) ssim_csbs.append(np.sum(compare_ssim(sources, recon_csbs))) print('psnr_focus:{} \npsnr_csbs:{} \nssim_focus:{} \nssim_csbs:{}'.format( np.array(psnr_focus).mean(), np.array(psnr_csbs).mean(), np.array(ssim_focus).mean(), np.array(ssim_csbs).mean())) plotter4d(recon_focus, title='recon_focus \n ssim:{} psnr:{}'.format( np.array(ssim_focus).mean(), np.array(psnr_focus).mean()), colorbar=True) plotter4d(recon_csbs, title='recon_csbs \n ssim:{} psnr:{}'.format( np.array(ssim_csbs).mean(), np.array(psnr_csbs).mean()), colorbar=True) plotter4d(sources, title='sources') # %% plotting z = np.zeros((160, 160, 3)) z[:, :, 0] = sources[0] z[:, :, 1] = sources[1] # plt.imsave('figures/sources.png', z)
sources = size_equalizer(sources, meas_size) # take multiple measurements with different noise measured_noisy_instances = np.zeros((num_instances, ) + measured.shape) for i in range(num_instances): for j in range(measured.shape[0]): measured_noisy_instances[i, j, 0] = np.fft.fftshift( add_noise(measured[j, 0], dbsnr=10, max_count=500, model='Poisson' # dbsnr=20, no_noise=no_noise, model='Gaussian' )) if len(measured_noisy_instances.shape) == 4: measured_noisy_instances = measured_noisy_instances[np.newaxis] plotter4d(sources, title='Orig', figsize=(5.6, 8), cmap='gist_heat') [k, num_sources, aa, bb] = psfs.selected_psfs.shape[:2] + sources.shape[2:] LAM = get_LAM(rows=aa, cols=bb, order=tikhonov_order) ssim = np.zeros((len(tikhonov_lam), num_instances, num_sources)) psnr = np.zeros((len(tikhonov_lam), num_instances, num_sources)) for i in range(len(tikhonov_lam)): SIG_inv = block_inv(psfs.selected_GAM + tikhonov_lam[i] * np.einsum('ij,kl', np.eye(num_sources), LAM)) for j in range(num_instances): # DFT of the kernel corresponding to (D^TD) recon = np.real( np.fft.ifft2( block_mul( SIG_inv, block_mul(psfs.selected_psf_dfts_h,
noise_level[1]).conj() # preview the correlation map computed by register_translation coarse_correlation = np.fft.fftshift(np.fft.ifft2(image_product)) # preview the correlation map computed by register_translation fine_correlation = _upsampled_dft(image_product, image_product.shape[0], 100, (offset * 100) + image_product.shape[0] // 2).conj() correlations.append([coarse_correlation, fine_correlation]) correlations = np.array(correlations) # %% plot ----- plt = plotter4d(noisy_list, title='Original and shifted images at various noise levels', colorbar=False, row_labels=max_photon_count_list, column_labels=['original', 'shifted'], sup_ylabel='max photon count', figsize=(4.5, 10), cmap='gist_heat') plt.savefig('images.png', dpi=300) plt = plotter4d(np.abs(correlations), title='Correlation stages', colorbar=False, row_labels=max_photon_count_list, column_labels=['coarse', 'fine'], sup_ylabel='max photon count', sup_xlabel='correlations', figsize=(4.5, 10), cmap='gist_heat') plt.savefig('correlations.png', dpi=300)