Beispiel #1
0
evc_col = []
for triali in tqdm(range(300)):
    data = np.load(join(savedir, "Hessian_cmp_%d.npz" % triali))
    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",
)
#%%
Beispiel #2
0
                      "eig_genBlock%02d_trial%d.npz" % (blocki, triali)),
                 H=H00,
                 eva=eva00,
                 evc=evc00,
                 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\StyleGAN2\ctrl_Hessians"
figdir = r"E:\OneDrive - Washington University in St. Louis\HessNetArchit\StyleGAN2"

modelnm = "StyleGAN2_Face512_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),
Beispiel #3
0
    #     H00 = get_full_hessian(L2dist_col[0], feat)
    #     eva00, evc00 = np.linalg.eigh(H00)
    #     print("Spent %.2f sec computing" % (time() - T0))
    #     np.savez(join(savedir, "eig_genBlock%02d_trial%d.npz"%(blocki, triali)), H=H00, eva=eva00, evc=evc00,
    #              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),
Beispiel #4
0
#%% StyleGAN2
SGdir = r"E:\Cluster_Backup\StyleGAN2"
SGfigdir = "E:\OneDrive - Washington University in St. Louis\Hessian_summary\StyleGAN2"
#%
npzpaths = glob(join(Hdir, "*.npz"))
npzfns = [path.split("\\")[-1] for path in npzpaths]
eigval_col = []
for fn, path in zip(npzfns, npzpaths):
    data = np.load(path)
    evas = data["eigvals"]
    eigval_col.append(evas)
eigval_col = np.array(eigval_col)
#%% StyleGAN2
Hdir = r"E:\Cluster_Backup\StyleGAN2\stylegan2-cat-config-f"
eigval_col, _, meta = scan_hess_npz(Hdir, npzpat="Hess_BP_(\d*).npz", evakey='eva_BP', evckey=None, )
np.savez(join(summarydir, "StyleGAN2", "spectra_col_stylegan2-cat-config-f_BP.npz"), eigval_col=eigval_col, )

Hdir = r"E:\Cluster_Backup\StyleGAN2\ffhq-256-config-e-003810"
eigval_col, _, meta = scan_hess_npz(Hdir, npzpat="Hess_BP_(\d*).npz", evakey='eva_BP', evckey=None, )
np.savez(join(summarydir, "StyleGAN2", "spectra_col_ffhq-256-config-e-003810_BP.npz"), eigval_col=eigval_col, )
#%% StyleGAN
Hdir = r"E:\Cluster_Backup\StyleGAN"
eigval_col, _, meta = scan_hess_npz(Hdir, npzpat="Hessian_rand_(\d*).npz", evakey='eva_BP', evckey=None, )
np.savez(join(summarydir, "StyleGAN", "spectra_col_face256_BP.npz"), eigval_col=eigval_col, )
#%% PGGAN
Hdir = r"E:\Cluster_Backup\PGGAN"
eigval_col, _, meta = scan_hess_npz(Hdir, npzpat='Hessian_cmp_(\d*).npz', evakey='eva_BP', evckey=None, )
np.savez(join(summarydir, "PGGAN", "spectra_col_BP.npz"), eigval_col=eigval_col, )
#%% DCGAN
Hdir = r"E:\Cluster_Backup\DCGAN"
Beispiel #5
0
    plt.ylabel("log10(eig/eigmax)" if ylog else "eig/eigmax")
    plt.xlabel("rank normalized to latent dim" if xnorm else "ranks")
    plt.xlim(xlim)
    if ylim is not None: plt.ylim(ylim)
    plt.title("Spectra Compared Across GANs")
    plt.legend(loc="best")
    plt.savefig(join(rootdir, fn+".png"))
    plt.savefig(join(rootdir, fn+".pdf"))
    plt.show()
    return fig

#%%
FC6figdir = r"E:\OneDrive - Washington University in St. " \
            r"Louis\Hessian_summary\fc6GAN" #r"E:\Cluster_Backup\FC6GAN\summary"
FC6dir = r"E:\Cluster_Backup\FC6GAN"
eva_col, _, feat_arr, meta = scan_hess_npz(FC6dir, "evol_(\d*)_bpfull.npz", featkey='code', evakey='eigvals',
                                                     evckey=None)#'eigvects')

#%%

eigvals_col = np.array(eigvals_col)[:, ::-1]
code_all = np.array(code_all)
np.savez(join(FC6figdir, "spectra_col_evol.npz"), eigval_col=eigvals_col, )
#%%
"""Visualize the spectra of different GANs all in one place"""
rootdir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary"
spaceD = [4096, 120, 256, 120, 512, 512, 512, 512, 512]
GANlist = ["FC6", "DCGAN-fashion", "BigGAN", "BigBiGAN", "PGGAN-face", "StyleGAN-face", "StyleGAN2-face512",
           "StyleGAN2-face256", "StyleGAN2-cat", ]
           # "StyleGAN-face-Forw", "StyleGAN-cat-Forw"]
fnlist = ["FC6GAN\\spectra_col_evol.npz",
          "DCGAN\\spectra_col_BP.npz",
Beispiel #6
0
    np.savez(join(datadir, "Hessian_rand_%d.npz" % triali), eva_BP=eva_BP, evc_BP=evc_BP, H_BP=H_BP,
        feat=feat.detach().cpu().numpy())
#%%
# eva_col = []
# evc_col = []
# for triali in tqdm(range(300)):
#     data = np.load(join(datadir, "Hessian_rand_%d.npz" % triali))
#     eva_col.append(data["eva_BP"])
#     evc_col.append(data["evc_BP"])
# #%%
# eva_col = np.array(eva_col)
datadir = r"E:\Cluster_Backup\StyleGAN"
figdir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary\StyleGAN"

os.makedirs(figdir, exist_ok=True)
eva_col, evc_col, feat_col, meta = scan_hess_npz(datadir, "Hessian_rand_(\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"%"StyleGAN"), H_avg=H_avg, eva_avg=eva_avg, evc_avg=evc_avg, feats=feat_col)
#%%
fig = plot_spectra(eva_col, figdir=figdir, titstr="StyleGAN", )
#%%
corr_mat_log, corr_mat_lin = compute_hess_corr(eva_col, evc_col, figdir=figdir, use_cuda=True)
# without cuda 12:11 mins, with cuda 8:21
# corr_mat_log, corr_mat_lin = compute_hess_corr(eva_col, evc_col, figdir=figdir, use_cuda=False)
#%
fig1, fig2 = plot_consistentcy_mat(corr_mat_log, corr_mat_lin, posN=300, figdir=figdir, titstr="StyleGAN")
#%
fig3 = plot_consistency_example(eva_col, evc_col, figdir=figdir, nsamp=5, titstr="StyleGAN",)
fig3.show()
#%%
Beispiel #7
0
        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_%d.npz" % triali),
             eva_BP=eva_BP,
             evc_BP=evc_BP,
             H_BP=H_BP,
             feat=feat.detach().cpu().numpy())

figdir = join(saveroot, "summary", label)  # saveroot
os.makedirs(figdir, exist_ok=True)
# modelnm = "StyleGAN_wspace_shuffle"
# 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),
Beispiel #8
0
from hessian_axis_visualize import vis_eigen_explore, vis_eigen_action, vis_eigen_action_row, vis_eigen_explore_row
from GAN_utils import loadStyleGAN2, StyleGAN2_wrapper, loadBigGAN, BigGAN_wrapper
import matplotlib.pylab as plt
import matplotlib
summarydir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary"
#%%
dataroot = "E:\\Cluster_Backup\\StyleGAN2"
modelnm = "stylegan2-cat-config-f"
modelsnm = "Cat256"
SGAN = loadStyleGAN2(
    modelnm + ".pt",
    size=256,
)
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)
#%%
vis_eigen_action_row(
    eigvec_col[0],
    feat_col[0, :],
    maxdist=120,
    rown=7,
)
#%%
veci = 76
figdir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary\Figure3"
vis_eigen_explore_row(
Beispiel #9
0
    corr_mat_log_nodiag)]) # 0.292
print("Correlation between code correlation  and  Hessian similarity (non-diagonal) ", cc_lin)
print("Correlation between code correlation  and  log Hessian similarity (non-diagonal) ", cc_log)
# Correlation between code correlation  and  Hessian similarity (non-diagonal)  0.020121576393265176
# Correlation between code correlation  and  log Hessian similarity (non-diagonal)  0.022462684881444483

#%% Newer version API
from hessian_analysis_tools import scan_hess_npz, average_H, compute_hess_corr, compute_vector_hess_corr, \
    plot_consistentcy_mat, plot_consistency_hist, plot_consistency_example, plot_spectra
savedir = r"E:\Cluster_Backup\fc6_shfl_fixGAN"
figdir = r"E:\OneDrive - Washington University in St. Louis\HessNetArchit\FC6GAN"
realfigdir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary\FC6GAN"

modelnm = "FC6GAN_shuffle_evol"
# Load the Hessian NPZ
eva_ctrl, evc_ctrl, feat_ctrl, meta = scan_hess_npz(savedir, "evol_(\d*)_bpfull.npz", evakey='eigvals', evckey='eigvects',
                                                    featkey='code')
# 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)
corr_mat_vec_ctrl = compute_vector_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, nsamp=5, titstr="%s"%modelnm, savelabel=modelnm)
#%%
with np.load(join(figdir, "spectra_col_%s.npz"%modelnm)) as data:
Beispiel #10
0
                     transpose=False,
                     namestr="eig_clas%d" % eigi)

    #%% BigBiGAN on ImageNet
    from GAN_utils import BigBiGAN_wrapper, loadBigBiGAN
    from torchvision.transforms import ToPILImage
    BBGAN = loadBigBiGAN().cuda()
    BBG = BigBiGAN_wrapper(BBGAN)
    # EmbedMat = BG.BigGAN.embeddings.weight.cpu().numpy()
    #%%
    from GAN_hessian_compute import hessian_compute, get_full_hessian
    from hessian_analysis_tools import scan_hess_npz, compute_hess_corr, plot_spectra
    npzdir = r"E:\OneDrive - Washington University in St. Louis\HessGANCmp\BigBiGAN"
    eigval_col, eigvec_col, feat_col, meta = scan_hess_npz(
        npzdir,
        npzpat="Hess_norm9_(\d*).npz",
        evakey='eigvals',
        evckey='eigvects',
        featkey="vect")
    feat_arr = np.array(feat_col).squeeze()
    #%%
    eigid = 20
    figdir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary\BigBiGAN"
    mtg = vis_eigen_action(eigvec=eigvec_col[12][:, -eigid - 1],
                           ref_codes=feat_arr[[
                               12,
                               0,
                               2,
                               4,
                               6,
                               8,
                               10,
Beispiel #11
0
    transpose=True)

#%% StyleGAN2
"""StyleGAN2 model"""
from hessian_analysis_tools import scan_hess_npz, average_H, compute_hess_corr, plot_consistentcy_mat, plot_consistency_example
figdir = join(rootdir, 'StyleGAN2')
Hessdir = join(rootdir, 'StyleGAN2')
dataroot = r"E:\Cluster_Backup\StyleGAN2"

#%% Face 512 model
modelnm = "ffhq-512-avg-tpurun1"
modelsnm = "Face512"
SGAN = loadStyleGAN2(modelnm + ".pt")
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)
#%%
mtg, codes_all = vis_eigen_frame(
    evc_avg,
    eva_avg,
    SG,
    ref_code=feat_col[5, :],
    figdir=figdir,
Beispiel #12
0
        ax = fig.add_subplot(spec[axi, axj])
        if axi == axj:
            ax.hist(np.log10(eigval_j), 20)
        else:
            ax.scatter(np.log10(eigval_j), np.log10(vHv_ij), s=15, alpha=0.6)
            ax.set_aspect(1, adjustable='datalim')
        if axi == 4:
            ax.set_xlabel("eigvals %d" % eigj)
        if axj == 0:
            ax.set_ylabel("vHv eigvects %d" % eigi)
ST = plt.suptitle("Consistency of Hessian Across Vectors\n"
                  "Cross scatter of EigenValues and vHv values for Hessian at 5 Random Vectors",
                  fontsize=18)
plt.savefig(join(figdir, "Hess_consistency_example_rnd%03d.jpg"%np.random.randint(1000)), bbox_extra_artists=[ST]) #
# this is working.
# plt.show()
#%%
datadir = r"E:\OneDrive - Washington University in St. Louis\HessGANCmp\BigBiGAN"
figdir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary\BigBiGAN"
from hessian_analysis_tools import plot_spectra, compute_hess_corr, plot_consistency_example, plot_consistentcy_mat, average_H, scan_hess_npz
eva_col, evc_col, feat_col, meta = scan_hess_npz(datadir, "Hess_norm(\d.*)_(\d\d\d)", featkey='vect',
                                                 evakey="eigvals", evckey="eigvects")
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"%"BigBiGAN"), H_avg=H_avg, eva_avg=eva_avg, evc_avg=evc_avg, feats=feat_col)
#%%
fig = plot_spectra(eva_col, figdir=figdir, titstr="BigBiGAN", )
#%
corr_mat_log, corr_mat_lin = compute_hess_corr(eva_col, evc_col, figdir=figdir, use_cuda=False)
fig1, fig2 = plot_consistentcy_mat(corr_mat_log, corr_mat_lin, figdir=figdir, titstr="BigBiGAN")
fig3 = plot_consistency_example(eva_col, evc_col, figdir=figdir, nsamp=5, titstr="BigBiGAN",)