def draw_das(D, ax): idx_img = info['show_reconstruction'] sampler = D.sampler() S, _, _ = sampler.decode(D[idx_img]) sky_model = D.ground_truth[idx_img] A = phased_array.steering_operator(D.XYZ, D.R, D.wl) alpha = 1 / (2 * pylinalg.eighMax(A)) beta = 2 * D.lambda_[idx_img] * alpha * (1 - D.gamma) + 1 exec_time = time.time() das = spectral.DAS(D.XYZ, S, D.wl, D.R) * 2 * alpha / beta exec_time = time.time() - exec_time if info['interpolation_order'] is not None: N = info['interpolation_order'] approximate_kernel = True if (N > 15) else False interp = interpolate.Interpolator(N, approximate_kernel) N_s = N_px = D.R.shape[1] das = interp.__call__(weight=np.ones((N_s, )), support=D.R, f=das.reshape((1, N_px)), r=D.R) das = np.clip(das, a_min=0, a_max=None) das_plot = s2image.Image(data=das, grid=D.R) das_plot.draw(catalog=sky_model.xyz, projection=info['projection'], use_contours=False, catalog_kwargs=dict(edgecolor='g', ), ax=ax) ax.set_title(f'DAS, {exec_time:.02f} [s]')
def draw_apgd(D, ax): idx_img = info['show_reconstruction'] sampler = D.sampler() _, I_apgd, _ = sampler.decode(D[idx_img]) sky_model = D.ground_truth[idx_img] tts = D.tts[idx_img] N_iter = D.N_iter[idx_img] if info['interpolation_order'] is not None: N = info['interpolation_order'] approximate_kernel = True if (N > 15) else False interp = interpolate.Interpolator(N, approximate_kernel) N_s = N_px = D.R.shape[1] I_apgd = interp.__call__(weight=np.ones((N_s, )), support=D.R, f=I_apgd.reshape((1, N_px)), r=D.R) I_apgd = np.clip(I_apgd, a_min=0, a_max=None) apgd_plot = s2image.Image(data=I_apgd, grid=D.R) apgd_plot.draw(catalog=sky_model.xyz, projection=info['projection'], use_contours=False, catalog_kwargs=dict(edgecolor='g', ), ax=ax) ax.set_title(f'APGD {N_iter:02d} iter, {tts:.02f} [s]')
def draw_tau(D, P, ax): N_antenna, N_px, K = D.XYZ.shape[1], D.R.shape[1], int(P['K']) parameter = crnn.Parameter(N_antenna, N_px, K) p_apgd_vec, p_rnn_vec = P['p_opt'][[0, np.argmin(P['v_loss'])]] p_apgd = dict(zip(['mu', 'D', 'tau'], parameter.decode(p_apgd_vec))) p_rnn = dict(zip(['mu', 'D', 'tau'], parameter.decode(p_rnn_vec))) tau_diff = (p_apgd['tau'] - p_rnn['tau']) / linalg.norm(p_apgd['tau']) if info['interpolation_order'] is not None: N = info['interpolation_order'] approximate_kernel = True if (N > 15) else False interp = interpolate.Interpolator(N, approximate_kernel) N_s = N_px = D.R.shape[1] tau_diff = interp.__call__(weight=np.ones((N_s, )), support=D.R, f=tau_diff.reshape((1, N_px)), r=D.R) tau_plot = s2image.Image(data=tau_diff, grid=D.R) tau_plot.draw(projection=info['projection'], use_contours=False, catalog_kwargs=dict(edgecolor='g', ), ax=ax) ax.set_title( r'$\frac{\tau_{APGD} - \tau_{RNN}}{\left\| \tau_{APGD} \right\|_{2}}$' )
def draw_rnn_psf(D, P, ax): N_antenna, N_px, K = D.XYZ.shape[1], D.R.shape[1], int(P['K']) parameter = crnn.Parameter(N_antenna, N_px, K) R_focus = np.mean(D.R, axis=1) R_focus /= linalg.norm(R_focus) idx_focus = np.argmax(R_focus @ D.R) p_vec = P['p_opt'][np.argmin(P['v_loss'])] p = dict(zip(['mu', 'D', 'tau'], parameter.decode(p_vec))) Ln, _ = graph.laplacian_exp(D.R, normalized=True) fltr = graph.ConvolutionalFilter(Ln, K) filter = fltr.filter(p['mu'], e(idx_focus, N_px)) psf = np.abs(filter) psf[idx_focus] = 0 if info['interpolation_order'] is not None: N = info['interpolation_order'] approximate_kernel = True if (N > 15) else False interp = interpolate.Interpolator(N, approximate_kernel) N_s = N_px = D.R.shape[1] psf = interp.__call__(weight=np.ones((N_s, )), support=D.R, f=psf.reshape((1, N_px)), r=D.R) psf = np.clip(psf, a_min=0, a_max=None) psf_plot = s2image.Image(data=psf, grid=D.R) psf_plot.draw(projection=info['projection'], use_contours=False, catalog_kwargs=dict(edgecolor='g', ), ax=ax) ax.set_title(r'$\Psi_{RNN}(r, r_{0})$')
def draw_apgd_filter(D, ax): R_focus = np.mean(D.R, axis=1) R_focus /= linalg.norm(R_focus) idx_focus = np.argmax(R_focus @ D.R) A = phased_array.steering_operator(D.XYZ, D.R, D.wl) N_px = D.R.shape[1] alpha = 1 / (2 * pylinalg.eighMax(A)) beta = 2 * np.median(D.lambda_) * alpha * (1 - D.gamma) + 1 psf = pylinalg.psf_exp(D.XYZ, D.R, D.wl, center=R_focus) filter = (e(idx_focus, N_px) - 2 * alpha * psf) / beta if info['interpolation_order'] is not None: N = info['interpolation_order'] approximate_kernel = True if (N > 15) else False interp = interpolate.Interpolator(N, approximate_kernel) N_s = N_px = D.R.shape[1] filter = interp.__call__(weight=np.ones((N_s, )), support=D.R, f=filter.reshape((1, N_px)), r=D.R) filter = np.clip(filter, a_min=0, a_max=None) filter_plot = s2image.Image(data=filter, grid=D.R) filter_plot.draw(projection=info['projection'], use_contours=False, catalog_kwargs=dict(edgecolor='g', ), ax=ax) ax.set_title(r'$p_{\theta}^{APGD}\left(\widetilde{L}\right)$')
def draw_apgd_psf(D, ax): R_focus = np.mean(D.R, axis=1) R_focus /= linalg.norm(R_focus) A = phased_array.steering_operator(D.XYZ, D.R, D.wl) alpha = 1 / (2 * pylinalg.eighMax(A)) beta = 2 * np.median(D.lambda_) * alpha * (1 - D.gamma) + 1 psf = (pylinalg.psf_exp(D.XYZ, D.R, D.wl, center=R_focus) * (2 * alpha / beta)) if info['interpolation_order'] is not None: N = info['interpolation_order'] approximate_kernel = True if (N > 15) else False interp = interpolate.Interpolator(N, approximate_kernel) N_s = N_px = D.R.shape[1] psf = interp.__call__(weight=np.ones((N_s, )), support=D.R, f=psf.reshape((1, N_px)), r=D.R) psf = np.clip(psf, a_min=0, a_max=None) psf_plot = s2image.Image(data=psf, grid=D.R) psf_plot.draw(projection=info['projection'], use_contours=False, catalog_kwargs=dict(edgecolor='g', ), ax=ax) ax.set_title(r'$\Psi_{APGD}(r, r_{0})$')
def draw_rnn(D, P, ax): idx_img = info['show_reconstruction'] sampler = D.sampler() S, _, I_prev = sampler.decode(D[idx_img]) sky_model = D.ground_truth[idx_img] N_antenna, N_px, K, N_layer = D.XYZ.shape[1], D.R.shape[1], int( P['K']), int(P['N_layer']) parameter = crnn.Parameter(N_antenna, N_px, K) p_vec = P['p_opt'][np.argmin(P['v_loss'])] p = dict(zip(['mu', 'D', 'tau'], parameter.decode(p_vec))) Ln, _ = graph.laplacian_exp(D.R, normalized=True) rnn_eval = crnn.Evaluator( N_layer, parameter, p_vec, Ln, lambda _: func.retanh(P['tanh_lin_limit'], _)) exec_time = time.time() I_rnn = rnn_eval(S, I_prev) exec_time = time.time() - exec_time if info['interpolation_order'] is not None: N = info['interpolation_order'] approximate_kernel = True if (N > 15) else False interp = interpolate.Interpolator(N, approximate_kernel) N_s = N_px = D.R.shape[1] I_rnn = interp.__call__(weight=np.ones((N_s, )), support=D.R, f=I_rnn.reshape((1, N_px)), r=D.R) I_rnn = np.clip(I_rnn, a_min=0, a_max=None) rnn_plot = s2image.Image(data=I_rnn, grid=D.R) rnn_plot.draw(catalog=sky_model.xyz, projection=info['projection'], use_contours=False, catalog_kwargs=dict(edgecolor='g', ), ax=ax) ax.set_title(f'RNN {N_layer:02d} iter, {exec_time:.02f} [s]')
def draw_trunc(D, P, ax): idx_img = info['show_reconstruction'] sampler = D.sampler() S, _, I_prev = sampler.decode(D[idx_img]) sky_model = D.ground_truth[idx_img] N_layer = int(P['N_layer']) A = phased_array.steering_operator(D.XYZ, D.R, D.wl) lambda_ = D.lambda_[idx_img] I_trunc = apgd.solve(S, A, lambda_=lambda_, gamma=D.gamma, x0=I_prev.copy(), N_iter_max=N_layer) tts = I_trunc['time'] N_iter = I_trunc['niter'] I_trunc = I_trunc['sol'] if info['interpolation_order'] is not None: N = info['interpolation_order'] approximate_kernel = True if (N > 15) else False interp = interpolate.Interpolator(N, approximate_kernel) N_s = N_px = D.R.shape[1] I_trunc = interp.__call__(weight=np.ones((N_s, )), support=D.R, f=I_trunc.reshape((1, N_px)), r=D.R) I_trunc = np.clip(I_trunc, a_min=0, a_max=None) trunc_plot = s2image.Image(data=I_trunc, grid=D.R) trunc_plot.draw(catalog=sky_model.xyz, projection=info['projection'], use_contours=False, catalog_kwargs=dict(edgecolor='g', ), ax=ax) ax.set_title(f'APGD {N_iter:02d} iter, {tts:.02f} [s]')
def draw_learned_dirty_image(D, P, ax): idx_img = info['show_reconstruction'] N_antenna, N_px, K = D.XYZ.shape[1], D.R.shape[1], int(P['K']) parameter = crnn.Parameter(N_antenna, N_px, K) sampler = D.sampler() p_rnn_vec = P['p_opt'][np.argmin(P['v_loss'])] p_rnn = dict(zip(['mu', 'D', 'tau'], parameter.decode(p_rnn_vec))) S, _, _ = sampler.decode(D[idx_img]) sky_model = D.ground_truth[idx_img] Ds, Vs = linalg.eigh(S) idx = Ds > 0 # To avoid np.sqrt() issues. Ds, Vs = Ds[idx], Vs[:, idx] I_learned = linalg.norm(p_rnn['D'].conj().T @ (Vs * np.sqrt(Ds)), axis=1)**2 if info['interpolation_order'] is not None: N = info['interpolation_order'] approximate_kernel = True if (N > 15) else False interp = interpolate.Interpolator(N, approximate_kernel) N_s = N_px = D.R.shape[1] I_learned = interp.__call__(weight=np.ones((N_s, )), support=D.R, f=I_learned.reshape((1, N_px)), r=D.R) I_learned = np.clip(I_learned, a_min=0, a_max=None) learned_plot = s2image.Image(data=I_learned, grid=D.R) learned_plot.draw(catalog=sky_model.xyz, projection=info['projection'], use_contours=False, catalog_kwargs=dict(edgecolor='g', ), ax=ax) ax.set_title(r'diag$\left(D^{H} \hat{\Sigma} D\right)$')
def process(args): file = pathlib.Path(args.dataset).expanduser().absolute() if not (file.exists() and (file.suffix == '.npz')): raise ValueError('Dataset is non-conformant.') D = nn.DataSet.from_file(str(file)) N_sample = len(D) if not (0 <= args.img_idx < N_sample): raise ValueError('Parameter[img_idx] is out-of-bounds.') S, I_apgd, I_prev = D.sampler().decode(D[args.img_idx]) I_das = spectral.DAS(D.XYZ, S, D.wl, D.R) # Rescale DAS to lie on same range as APGD A = phased_array.steering_operator(D.XYZ, D.R, D.wl) alpha = 1 / (2 * pylinalg.eighMax(A)) beta = 2 * D.lambda_[args.img_idx] * alpha * (1 - D.gamma) + 1 I_das *= (2 * alpha) / beta if args.interpolation_order is not None: N = args.interpolation_order approximate_kernel = True if (N > 15) else False interp = interpolate.Interpolator(N, approximate_kernel) N_s = N_px = D.R.shape[1] I_prev = interp.__call__(weight=np.ones((N_s, )), support=D.R, f=I_prev.reshape((1, N_px)), r=D.R) I_prev = np.clip(I_prev, a_min=0, a_max=None) I_apgd = interp.__call__(weight=np.ones((N_s, )), support=D.R, f=I_apgd.reshape((1, N_px)), r=D.R) I_apgd = np.clip(I_apgd, a_min=0, a_max=None) I_das = interp.__call__(weight=np.ones((N_s, )), support=D.R, f=I_das.reshape((1, N_px)), r=D.R) I_das = np.clip(I_das, a_min=0, a_max=None) fig = plt.figure() ax_prev = fig.add_subplot(131) ax_apgd = fig.add_subplot(132) ax_das = fig.add_subplot(133) s2image.Image(I_prev, D.R).draw(catalog=D.ground_truth[args.img_idx].xyz, projection=args.projection, catalog_kwargs=dict(edgecolor='g', ), ax=ax_prev) ax_prev.set_title(r'$APGD_{init}$') s2image.Image(I_apgd, D.R).draw(catalog=D.ground_truth[args.img_idx].xyz, projection=args.projection, catalog_kwargs=dict(edgecolor='g', ), ax=ax_apgd) ax_apgd.set_title('APGD') s2image.Image(I_das, D.R).draw(catalog=D.ground_truth[args.img_idx].xyz, projection=args.projection, catalog_kwargs=dict(edgecolor='g', ), ax=ax_das) ax_das.set_title('DAS') fig.show()