baddot_imgs_neg, nrow=5, padding=2, ) save_imgrid( baddot_imgs_neg, join(savedir, "GAN_baddots_neg.png"), nrow=5, padding=2, ) #%% modelsnm = "StyleGAN2-Face512_Z" savedir = join(saveroot, modelsnm) os.makedirs(savedir, exist_ok=True) SGAN = loadStyleGAN2("ffhq-512-avg-tpurun1.pt") G = StyleGAN2_wrapper(SGAN) H, eva, evc = load_Haverage("StyleGAN2-Face512_Z", descend=True) randvec = G.sample_vector(10, 'cpu') distnorm = randvec.norm(dim=1).mean().item() # distnorm = np.sqrt(randvec.shape[1]) #%% baddot_imgs_pos = G.visualize_batch_np(distnorm * evc[:, :20].T) show_imgrid( baddot_imgs_pos, nrow=5, padding=2, ) save_imgrid( baddot_imgs_pos, join(savedir, "GAN_space_baddots_pos.png"), nrow=5,
from numpy.linalg import norm import matplotlib.pylab as plt from time import time from os.path import join from imageio import imwrite, imsave from torchvision.transforms import ToPILImage from torchvision.utils import make_grid rootdir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary" figsumdir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary\Figure7" """Note the loading and visualization is mostly deterministic, reproducible.""" #%% StyleGAN2 Face 1024 Hessdir = join(rootdir, 'StyleGAN2') modelnm = "stylegan2-ffhq-config-f" modelsnm = "Face1024" SGAN2 = loadStyleGAN2(modelnm+".pt") SG2 = StyleGAN2_wrapper(SGAN2) with np.load(join(Hessdir, "H_avg_%s.npz"%modelnm)) as data: eva_avg, evc_avg = data["eva_avg"], data["evc_avg"]#, data["feat_col"] #%% refvec = 0 * torch.randn(1, 512).cuda() img = SG2.visualize(refvec) ToPILImage()(make_grid(img).cpu()).show() #%% vis_eigen_frame(evc_avg, eva_avg, SG2, ref_code=refvec.cpu(), figdir=figsumdir, namestr="StyleGAN2_%s"%modelsnm, transpose=False, eiglist=None, eig_rng=(0, 20), maxdist=0.0005, rown=5, sphere=False, ) #%% from geometry_utils import LExpMap, SExpMap from build_montages import build_montages
import torch import torch.optim as optim import torch.nn.functional as F from torchvision.transforms import ToPILImage, ToTensor from hessian_eigenthings.power_iter import Operator, deflated_power_iteration from hessian_eigenthings.lanczos import lanczos from lanczos_generalized import lanczos_generalized from GAN_hvp_operator import GANHVPOperator, GANForwardHVPOperator, compute_hessian_eigenthings, get_full_hessian import sys import numpy as np import matplotlib.pylab as plt from time import time from os.path import join from imageio import imwrite from build_montages import build_montages, color_framed_montages import torchvision.models as tv from GAN_utils import loadStyleGAN2, StyleGAN2_wrapper, ckpt_root g_ema = loadStyleGAN2("stylegan2-cat-config-f.pt") G = StyleGAN2_wrapper(g_ema) #%% paramnum = 0 for name, param in g_ema.state_dict().items(): paramnum += np.prod(param.shape) for name, param in g_ema.state_dict().items(): print(name, list(param.shape), "%.1f%%"%(100*np.prod(param.shape) / paramnum)) #%%
#%% SGAN = loadStyleGAN2('ffhq-512-avg-tpurun1.pt') #%% shuffled_SD = shuffle_state_dict(SGAN.state_dict()) #%% torch.save(shuffled_SD, join(datadir, "StyleGAN2_ffhq-512-avg-tpurun1_shuffle.pt")) # print(name, Weight.shape, Weight.mean().item(), Weight.std().item()) #%% SGAN_sf = loadStyleGAN2('ffhq-512-avg-tpurun1.pt') SGAN_sf.load_state_dict( torch.load(join(datadir, "StyleGAN2_ffhq-512-avg-tpurun1_shuffle.pt"))) G_sf = StyleGAN2_wrapper(SGAN_sf) #%% img = G_sf.visualize(torch.randn(1, 512).cuda()).cpu() ToPILImage()(img[0, :].cpu()).show() #%% def Hess_hook(module, fea_in, fea_out): print("hooker on %s" % module.__class__) ref_feat = fea_out.detach().clone() ref_feat.requires_grad_(False) L2dist = torch.pow(fea_out - ref_feat, 2).sum() L2dist_col.append(L2dist) return None
maxdist=0.3, rown=5, 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,