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, figdir=figdir, titstr="DCGAN") #%% fig3 = plot_consistency_example( eva_col, evc_col,
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, corr_mat_lin = compute_hess_corr(eva_ctrl, evc_ctrl, 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_ctrl, evc_ctrl, figdir=figdir,
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, 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,
return fig 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,
plt.savefig(join(figdir, "Hess_%s_corrmat_lin.jpg" % Hlabel)) plt.savefig(join(figdir, "Hess_%s_corrmat_lin.pdf" % Hlabel)) plt.show() return fig1, fig2 #%% 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()] failnms2 = [] for fdnm in subfdnm: #["FFHQ512"]: try: eigval_col, eigvec_col, meta = scan_hess_npz(join(SGdir, fdnm)) posN = len(eigval_col) corr_mat_log, corr_mat_lin = compute_hess_corr(eigval_col, eigvec_col, fdnm=fdnm) plot_consistentcy_mat(corr_mat_log, corr_mat_lin, Hlabel=fdnm, posN=posN) print(fdnm, "folder finished") except: print(fdnm, "folder failed, please check") failnms2.append(fdnm) #%% from hessian_analysis_tools import plot_spectra, compute_hess_corr, plot_consistentcy_mat, plot_consistency_example def scan_hess_npz( Hdir,
plt.ylabel("density") plt.title( "Histogram of Non-Diag Correlation\n %s on %s scale\n mean %.3f median %.3f" % (GAN, "log" if log else "lin", corr_mean, corr_medi)) plt.show() return fig #%% GANsnm = "StyleGAN2-Face512" modeldir = join(summarydir, "StyleGAN2") eigval_col, eigvec_col, feat_col, meta = scan_hess_npz( r"E:\Cluster_Backup\StyleGAN2\ffhq-512-avg-tpurun1", featkey="feat") corr_mat_log, corr_mat_lin = compute_hess_corr( eigval_col, eigvec_col, savelabel="ffhq-512-avg-tpurun1", figdir=modeldir, ) plot_consistency_example(eigval_col, eigvec_col, nsamp=5, titstr=GANsnm, figdir=modeldir, savelabel="ffhq-512-avg-tpurun1") #%% corrmat_npz_dict = { "fc6GAN": "fc6GAN\\evol_hess_corr_mat.npz", "DCGAN": "DCGAN\\Hess__corr_mat.npz", "BigGAN": "BigGAN\\Hess_all_consistency_corr_mat.npz", "BigBiGAN": "BigBiGAN\\evol_hess_corr_mat.npz",
H_i = (evc_i * eva_i[np.newaxis, :]) @ evc_i.T for eigj in range(posN): eva_j, evc_j = eigval_col[eigj], eigvec_col[eigj] H_j = (evc_j * eva_j[np.newaxis, :]) @ evc_j.T # corr_mat_log[eigi, eigj] = \ # np.corrcoef(ma.masked_invalid(np.log10(vHv_ij)), ma.masked_invalid(np.log10(eva_j)))[0, 1] corr_mat_lin[eigi, eigj] = np.corrcoef(H_i.flatten(), H_j.flatten())[0, 1] print("%.1f sec" % (time() - T0)) # return corr_mat_lin corr_mat_vec = compute_vector_hess_corr(eva_col, evc_col, use_cuda=True) # 576 sec without cuda 92.7 sec with cuda #%% Older version hess corr corr_mat_log, corr_mat_lin = compute_hess_corr(eva_col, evc_col, use_cuda=True) #%% def plot_layer_consistency_mat(corr_mat_log, corr_mat_lin, corr_mat_vec, savelabel="", figdir=archdir, titstr="GAN", layernames=None): posN = corr_mat_log.shape[0] corr_mat_log_nodiag = corr_mat_log.copy() corr_mat_lin_nodiag = corr_mat_lin.copy() corr_mat_vec_nodiag = corr_mat_vec.copy() np.fill_diagonal(corr_mat_log_nodiag, np.nan)
# 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, nsamp=5, titstr="StyleGAN2 %s" % modelnm, savelabel=modelnm) #%% "ImageNet512"