np.savez( join(figdir, "spectra_col_%s.npz" % modelnm), eigval_col=eva_ctrl, ) # compute and plot the correlation between hessian at different points """Note the spectra of shuffled BigBiGAN doesn't make sense we use randomly initialized one instead""" #%% corr_mat_log_ctrl, corr_mat_lin_ctrl = compute_hess_corr(eva_ctrl + 1E-7, evc_ctrl, figdir=figdir, use_cuda=True, savelabel=modelnm + "shift1E-7") corr_mat_vec_ctrl = compute_vector_hess_corr(eva_ctrl + 1E-7, evc_ctrl, figdir=figdir, use_cuda=True, savelabel=modelnm + "shift1E-7") fig1, fig2 = plot_consistentcy_mat(corr_mat_log_ctrl, corr_mat_lin_ctrl, figdir=figdir, titstr="%s" % modelnm + "shift1E-7", savelabel=modelnm) fig11, fig22 = plot_consistency_hist(corr_mat_log_ctrl, corr_mat_lin_ctrl, figdir=figdir, titstr="%s" % modelnm + "shift1E-7", savelabel=modelnm) fig3 = plot_consistency_example(eva_ctrl + 1E-7, evc_ctrl, figdir=figdir,
feat=feat.detach().cpu().numpy(), feat_z=feat_z.detach().cpu().numpy()) #%% #% figdir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary\StyleGAN2_wspace" os.makedirs(figdir, exist_ok=True) # Load the Hessian NPZ eva_col, evc_col, feat_col, meta = scan_hess_npz(savedir, "Hess_BP_(\d*).npz", featkey="feat") # compute the Mean Hessian and save H_avg, eva_avg, evc_avg = average_H(eva_col, evc_col, ) np.savez(join(figdir, "H_avg_%s.npz"%modelnm), H_avg=H_avg, eva_avg=eva_avg, evc_avg=evc_avg, feats=feat_col) # compute and plot spectra fig0 = plot_spectra(eigval_col=eva_col, savename="%s_spectrum"%modelnm, figdir=figdir) np.savez(join(figdir, "spectra_col_%s.npz"%modelnm), eigval_col=eva_col, ) # compute and plot the correlation between hessian at different points corr_mat_log, corr_mat_lin = compute_hess_corr(eva_col, evc_col, figdir=figdir, use_cuda=False, savelabel=modelnm) corr_mat_vec = compute_vector_hess_corr(eva_col, evc_col, figdir=figdir, use_cuda=False, savelabel=modelnm) fig1, fig2 = plot_consistentcy_mat(corr_mat_log, corr_mat_lin, figdir=figdir, titstr="%s"%modelnm, savelabel=modelnm) fig11, fig22 = plot_consistency_hist(corr_mat_log, corr_mat_lin, figdir=figdir, titstr="%s"%modelnm, savelabel=modelnm) fig3 = plot_consistency_example(eva_col, evc_col, figdir=figdir, nsamp=5, titstr="%s"%modelnm, savelabel=modelnm) fig3 = plot_consistency_example(eva_col, evc_col, figdir=figdir, nsamp=3, titstr="%s"%modelnm, savelabel=modelnm) #%% "stylegan2-cat-config-f" modelname = "stylegan2-cat-config-f" modelnm = "stylegan2-cat-config-f"+"_wspace" modelsnm = "Cat256" SGAN = loadStyleGAN2(modelname+".pt", size=256, channel_multiplier=2) G = StyleGAN2_wrapper(SGAN) G.use_wspace(True) savedir = join(rootdir, modelname+"_wspace") os.makedirs(savedir, exist_ok=True)
from hessian_analysis_tools import plot_consistentcy_mat, compute_hess_corr, compute_vector_hess_corr, plot_layer_consistency_mat datadir = r"E:\OneDrive - Washington University in St. Louis\HessNetArchit\StyleGAN2" layernames = [("StyleBlock%02d" % blocki) for blocki in range(12)] # if blocki!=8 else "SelfAttention" eva_col, evc_col = [], [] for blocki in range(12): data = np.load(join(datadir, "eig_genBlock%02d_latent.npz"%blocki)) # data = np.load(join(datadir, "eig_ConvBlock%02d.npz"%blocki)) eva_col.append(data["eva"]) evc_col.append(data["evc"]) fig0 = plot_layer_spectra(eva_col, layernames=layernames, figdir=datadir, titstr="StyleGAN2", namestr="wspace_all_block") fig0 = plot_layer_spectra(eva_col, layernames=layernames, figdir=datadir, titstr="StyleGAN2", normalize=True, namestr="wspace_all_block_norm") corr_mat_log, corr_mat_lin = compute_hess_corr(eva_col, evc_col, savelabel="StyleGAN2_wspace", figdir=datadir) corr_mat_vec = compute_vector_hess_corr(eva_col, evc_col, savelabel="StyleGAN2_wspace", figdir=datadir) fig1, fig2, fig3 = plot_layer_consistency_mat(corr_mat_log, corr_mat_lin, corr_mat_vec, savelabel="StyleGAN2_wspace", figdir=datadir, titstr="StyleGAN2", layernames=layernames) #%% datadir = r"E:\OneDrive - Washington University in St. Louis\HessNetArchit\StyleGAN2" layernames = [("StyleBlock%02d" % blocki) for blocki in range(12)] # if blocki!=8 else "SelfAttention" eva_col, evc_col = [], [] for blocki in range(12): # data = np.load(join(datadir, "eig_genBlock%02d_latent.npz"%blocki)) data = np.load(join(datadir, "eig_genBlock%02d.npz"%blocki)) eva_col.append(data["eva"]) evc_col.append(data["evc"]) fig0 = plot_layer_spectra(eva_col, layernames=layernames, figdir=datadir, titstr="StyleGAN2", namestr="zspace_all_block") fig0 = plot_layer_spectra(eva_col, layernames=layernames, figdir=datadir, titstr="StyleGAN2", normalize=True, namestr="zspace_all_block_norm")
# 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, evc_ctrl, figdir=figdir, use_cuda=True, savelabel=modelnm) corr_mat_vec_ctrl = compute_vector_hess_corr(eva_ctrl, evc_ctrl, figdir=figdir, savelabel=modelnm) fig1, fig2 = plot_consistentcy_mat(corr_mat_log_ctrl, corr_mat_lin_ctrl, figdir=figdir, titstr="%s" % modelnm, savelabel=modelnm) fig11, fig22 = plot_consistency_hist(corr_mat_log_ctrl, corr_mat_lin_ctrl, figdir=figdir, titstr="%s" % modelnm, savelabel=modelnm) fig3 = plot_consistency_example(eva_ctrl, evc_ctrl, figdir=figdir, nsamp=5,