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, use_cuda=False) # without cuda 2:12 mins, with cuda 6:55 #%% fig1, fig2 = plot_consistentcy_mat(corr_mat_log, corr_mat_lin, figdir=figdir, titstr="DCGAN") fig11, fig22 = plot_consistency_hist(corr_mat_log, corr_mat_lin,
label="all space") plt.ylabel("eigenvalue(log)") plt.xlabel("eig id") plt.legend() st = plt.suptitle( "Hessian Spectrum of StyleGAN2\n (error bar for [5,95] percentile among all samples)" ) plt.savefig(join(figdir, savename), bbox_extra_artists=[st]) # this is working. plt.show() return fig #%% plot_spectra( eigval_col, savename="Cat-f_spectra.jpg", ) #%% Go through all the models see how their thing is doing subpath = [f.path for f in os.scandir(SGdir) if f.is_dir()] subfdnm = [f.name for f in os.scandir(SGdir) if f.is_dir()] failnms = [] for fdnm in subfdnm: try: eigval_col, _, meta = scan_hess_npz(join(SGdir, fdnm)) eigval_col = eigval_col[::-1] plot_spectra(eigval_col, savename="%s-spectra.jpg" % fdnm) np.savez( join(figdir, "spectra_col_%s.npz" % fdnm), eigval_col=eigval_col, ) print(fdnm, "folder finished")
# 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, evc_ctrl, figdir=figdir, use_cuda=True, savelabel=modelnm) fig1, fig2 = plot_consistentcy_mat(corr_mat_log_ctrl, corr_mat_lin_ctrl, figdir=figdir, titstr="%s" % modelnm,
# Load the Hessian NPZ eva_ctrl, evc_ctrl, feat_ctrl, meta = scan_hess_npz(savedir, "Hess_BP_(\d*).npz", evakey='eva_BP', evckey='evc_BP', 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" % label), 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" % label, figdir=figdir) np.savez( join(figdir, "spectra_col_%s.npz" % label), 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=label) fig1, fig2 = plot_consistentcy_mat(corr_mat_log_ctrl, corr_mat_lin_ctrl, figdir=figdir, titstr="%s" % label,
preprocess=lambda img:img) #preprocess=lambda img: F.interpolate(img, (256, 256), mode='bilinear', align_corners=True)) print("%.2f sec" % (time() - T0)) # 109 sec np.savez(join(savedir, "Hess_BP_%03d.npz"%triali), eva_BP=eva_BP, evc_BP=evc_BP, H_BP=H_BP, 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)
plt.ylabel("eigenvalue") plt.xlabel("eig id") plt.legend() plt.sca(axs[1]) plt.plot(range(eigN), np.log10(eigmean), alpha=0.6) plt.fill_between(range(eigN), np.log10(eiglim[0, :]), np.log10(eiglim[1, :]), alpha=0.3, label=label) plt.ylabel("eigenvalue(log)") plt.xlabel("eig id") plt.legend() st = plt.suptitle("Hessian Spectrum of StyleGAN\n (error bar for [5,95] percentile among all samples)") plt.savefig(join(figdir, savename), bbox_extra_artists=[st]) # this is working. # fig.show() return fig fig = plot_spectra(data_BP["eva_BP"][np.newaxis, :], label="BP", savename="spectrum_onetrial.jpg") fig = plot_spectra(data_FI["H_col"][4, 0][np.newaxis, :], savename="spectrum_method_cmp.jpg", label="ForwardIter 1E-3", fig=fig) fig = plot_spectra(data_FI["H_col"][5, 0][np.newaxis, :], savename="spectrum_method_cmp.jpg", label="ForwardIter 3E-3", fig=fig) fig = plot_spectra(data_FI["H_col"][6, 0][np.newaxis, :], savename="spectrum_method_cmp.jpg", label="ForwardIter 1E-2", fig=fig) plt.show() #%% """ This is the smaller explicit version of StyleGAN. Very easy to work with """ #%% sys.path.append("E:\Github_Projects\style-based-gan-pytorch") sys.path.append("D:\Github\style-based-gan-pytorch") from model import StyledGenerator from generate import get_mean_style import math #%%
# feat=feat.cpu().detach().numpy()) #%% from hessian_analysis_tools import scan_hess_npz, average_H, compute_vector_hess_corr, plot_consistentcy_mat, \ plot_consistency_hist, plot_consistency_example, plot_spectra savedir = r"E:\OneDrive - Washington University in St. Louis\HessNetArchit\BigBiGAN\rand_Hessians" figdir = r"E:\OneDrive - Washington University in St. Louis\HessNetArchit\BigBiGAN" realfigdir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary\BigBiGAN" modelnm = "BigBiGAN_randinit" # 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) fig0 = plot_spectra(eigval_col=eva_ctrl, savename="%s_spectrum_med"%modelnm, figdir=figdir, median=True) 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 randinitd 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, nsamp=5, titstr="%s"%modelnm+"shift1E-7", savelabel=modelnm) #%%
Hessdir = join(rootdir, 'StyleGAN2') modelnm = "stylegan2-ffhq-config-f" modelsnm = "Face1024" # SGAN = loadStyleGAN2(modelnm+".pt", size=1024,) # SG = StyleGAN2_wrapper(SGAN, ) eigval_col, eigvec_col, feat_col, meta = scan_hess_npz(join(dataroot, modelnm), "Hess_BP_(\d*).npz", featkey="feat") feat_col = np.array(feat_col).squeeze() H_avg, eva_avg, evc_avg = average_H(eigval_col, eigvec_col) np.savez(join(Hessdir, "H_avg_%s.npz" % modelnm), H_avg=H_avg, eva_avg=eva_avg, evc_avg=evc_avg, feats=feat_col) fig0 = plot_spectra(eigval_col=eigval_col, savename="%s_spectrum" % modelnm) corr_mat_log, corr_mat_lin = compute_hess_corr( eigval_col, eigvec_col, figdir=figdir, use_cuda=False, savelabel=modelnm, ) fig1, fig2 = plot_consistentcy_mat(corr_mat_log, corr_mat_lin, figdir=figdir, titstr="StyleGAN2 %s" % modelnm, savelabel=modelnm) fig3 = plot_consistency_example(eigval_col, eigvec_col, figdir=figdir,
label = modelnm + "_fix_ctrl" eva_col_ctrl = np.load( join(saveroot, "summary", label, "spectra_col_%s.npz" % label))["eigval_col"] label = modelnm + "_W_fix" eva_col_W = np.load( join(saveroot, "summary", label, "spectra_col_%s.npz" % label))["eigval_col"] label = modelnm + "_W_fix_ctrl" eva_col_W_ctrl = np.load( join(saveroot, "summary", label, "spectra_col_%s.npz" % label))["eigval_col"] fig0 = plot_spectra(eva_col, label="Fix", titstr="StyleGAN2 %s" % modelsnm, savename="SG2_%s_shfl_spectrum_cmp" % modelsnm, figdir=summaryroot) fig0 = plot_spectra(eva_col_ctrl, label="Fix_Shuffled", titstr="StyleGAN2 %s" % modelsnm, fig=fig0, savename="SG2_%s_shfl_spectrum_cmp_Zspace" % modelsnm, figdir=summaryroot) fig0 = plot_spectra(eva_col_W, label="Fix Wspace", titstr="StyleGAN2 %s" % modelsnm, fig=fig0, savename="SG2_%s_shfl_spectrum_cmp" % modelsnm, figdir=summaryroot)
plt.plot(np.log10(eigval_arr).T, color="pink", lw=1.5, alpha=0.5) if control: plt.plot(range(len(eigmean)), np.log10(eigmean), alpha=0.7, color="green") # , eigval_arr.std(axis=0) plt.fill_between(range(len(eigmean)), np.log10(eiglim[0, :]), np.log10(eiglim[1, :]), alpha=0.5, color="purple", label="control") plt.ylabel("eigenvalue(log)") plt.xlabel("eig id") plt.xlim([-10, 130]) plt.legend() st = plt.suptitle("Hessian Spectrum of BigBiGAN in Different Spaces\n (error bar for [5,95] percentile)") plt.savefig(join(figdir, savename), bbox_extra_artists=[st]) # this is working. # plt.show() figdir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary\BigBiGAN" plot_spectra(control=False, savename="spectrum_stat_120_org.jpg", ) plot_spectra(indiv_trace=True, savename="spectrum_stat_120_traces.jpg", ) # plot_spectra(control=True, savename="spectrum_stat_cmp.jpg", ) #%% def corr_torch(V1, V2): C1 = (V1 - V1.mean()) C2 = (V2 - V2.mean()) return torch.dot(C1, C2) / C1.norm() / C2.norm() def corr_nan_torch(V1, V2): Msk = torch.isnan(V1) | torch.isnan(V2) return corr_torch(V1[~Msk], V2[~Msk]) #%% evc_col = [] eva_col = [] code_all = []