# feat=feat.cpu().detach().numpy()) #%% from hessian_analysis_tools import scan_hess_npz, average_H, plot_consistentcy_mat, plot_consistency_hist, plot_consistency_example savedir = r"E:\OneDrive - Washington University in St. Louis\HessNetArchit\StyleGAN\ctrl_Hessians" figdir = r"E:\OneDrive - Washington University in St. Louis\HessNetArchit\StyleGAN" realfigdir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary\StyleGAN" modelnm = "StyleGAN_shuffle" # Load the Hessian NPZ eva_ctrl, evc_ctrl, feat_ctrl, meta = scan_hess_npz(savedir, "eig_full_trial(\d*).npz", evakey='eva', evckey='evc', featkey="feat") # compute the Mean Hessian and save H_avg, eva_avg, evc_avg = average_H(eva_ctrl, evc_ctrl) np.savez(join(figdir, "H_avg_%s.npz" % modelnm), H_avg=H_avg, eva_avg=eva_avg, evc_avg=evc_avg, feats=feat_ctrl) # compute and plot spectra fig0 = plot_spectra(eigval_col=eva_ctrl, savename="%s_spectrum" % modelnm, figdir=figdir) np.savez( join(figdir, "spectra_col_%s.npz" % modelnm), eigval_col=eva_ctrl, ) # compute and plot the correlation between hessian at different points corr_mat_log_ctrl, corr_mat_lin_ctrl = compute_hess_corr(eva_ctrl,
eva_BP = data["eva_BP"] evc_BP = data["evc_BP"] eva_col.append(eva_BP) evc_col.append(evc_BP) eva_col = np.array(eva_col) #%% figdir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary\DCGAN" savedir = r"E:\Cluster_Backup\DCGAN" from hessian_analysis_tools import plot_spectra, compute_hess_corr, plot_consistency_example, plot_consistentcy_mat, \ plot_consistency_hist, average_H, scan_hess_npz eva_col, evc_col, feat_col, meta = scan_hess_npz(savedir, "Hessian_cmp_(\d*).npz", featkey="feat") feat_col = np.array(feat_col).squeeze() H_avg, eva_avg, evc_avg = average_H(eva_col, evc_col) np.savez(join(figdir, "H_avg_%s.npz" % "DCGAN"), H_avg=H_avg, eva_avg=eva_avg, evc_avg=evc_avg, feats=feat_col) #%% fig = plot_spectra( eva_col, figdir=figdir, titstr="DCGAN", ) #%% corr_mat_log, corr_mat_lin = compute_hess_corr(eva_col, evc_col, figdir=figdir,
#%% def average_H(eigval_col, eigvec_col): """Compute the average Hessian over a bunch of positions""" nH = len(eigvec_col) dimen = eigval_col.shape[1] H_avg = np.zeros((dimen, dimen)) for iH in range(nH): H = (eigvec_col[iH] * eigval_col[iH][np.newaxis, :]) @ eigvec_col[iH].T H_avg += H H_avg /= nH eva_avg, evc_avg = np.linalg.eigh(H_avg) return H_avg, eva_avg, evc_avg H_avg, eva_avg, evc_avg = average_H(eigval_col, eigvec_col) #%% maxang = 1.5 figdir = "E:\Cluster_Backup\StyleGAN2_axis\Face256" for eigid in list(range(20)) + list(range(20, 60, 2)) + list( range(60, 200, 4)): mtg = vis_eigen_action( eigvec=evc_avg[:, -eigid - 1], ref_codes=feat_arr[[0, 2, 4, 6, 8, 10, 12, 14, 16, 20, 40, 60], :], G=G, sphere=True, maxdist=1.5, rown=5, transpose=False, namestr="SG2_Face256_AVGeig%d_Sph%.1f" % (eigid, maxang), figdir=figdir)