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 savedir = r"E:\OneDrive - Washington University in St. Louis\HessNetArchit\StyleGAN\ctrl_Hessians" os.makedirs(savedir, exist_ok=True) for triali in tqdm(range(38, 50)): feat = torch.randn(1, 512).cuda() eigvals, eigvects, H = hessian_compute( G_sf, feat, ImDist, hessian_method="BP", ) np.savez(join(savedir, "eig_full_trial%d.npz" % (triali)), H=H, eva=eigvals, evc=eigvects, feat=feat.cpu().detach().numpy()) # feat.requires_grad_(True) # for blocki in [0, 3, 5, 8, 10, 12]: # L2dist_col = [] # torch.cuda.empty_cache() # H1 = SGAN_sf.convs[blocki].register_forward_hook(Hess_hook) # img = SGAN_sf([feat], truncation=1) # H1.remove() # T0 = time()
DCGAN, ): self.DCGAN = DCGAN def visualize(self, code, scale=1.0): imgs = self.DCGAN(code, ) # Matlab version default to 0.7 return torch.clamp((imgs + 1.0) / 2.0, 0, 1) * scale G = DCGAN_wrapper(model.avgG) #%% noise, _ = model.buildNoiseData(1) feat = noise.detach().clone().cuda() T0 = time() eva_BI, evc_BI, H_BI = hessian_compute(G, feat, ImDist, hessian_method="BackwardIter") print("%.2f sec" % (time() - T0)) # 13.40 sec T0 = time() eva_FI, evc_FI, H_FI = hessian_compute(G, feat, ImDist, hessian_method="ForwardIter") print("%.2f sec" % (time() - T0)) # 6.89 sec T0 = time() eva_BP, evc_BP, H_BP = hessian_compute(G, feat, ImDist, hessian_method="BP") print("%.2f sec" % (time() - T0)) # 12.5 sec print("Correlation of Flattened Hessian matrix BP vs BackwardIter %.3f" % np.corrcoef(H_BP.flatten(), H_BI.flatten())[0, 1]) print("Correlation of Flattened Hessian matrix BP vs ForwardIter %.3f" % np.corrcoef(H_BP.flatten(), H_FI.flatten())[0, 1])
# np.savez(join("E:\OneDrive - Washington University in St. Louis\Hessian_summary\StyleGAN2", "Hess_cmp_%d.npz"%triali), eva_BI=eva_BI, evc_BI=evc_BI, H_BI=H_BI, # eva_FI=eva_FI, evc_FI=evc_FI, H_FI=H_FI, H_col=H_col, # eva_BP=eva_BP, evc_BP=evc_BP, H_BP=H_BP, feat=feat.detach().cpu().numpy()) # print("Save finished") #%% """Compute spectrum for different models through CMD interface. """ modelname = "ffhq-256-config-e-003810" # 109 sec SGAN = loadStyleGAN2(modelname+".pt", size=256, channel_multiplier=1) # 491 sec per BP G = StyleGAN2_wrapper(SGAN) savedir = join(rootdir, modelname) os.makedirs(savedir, exist_ok=True) for triali in range(150): feat = torch.randn(1, 512).detach().clone().cuda() T0 = time() eva_BP, evc_BP, H_BP = hessian_compute(G, feat, ImDist, hessian_method="BP", 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()) modelname = "stylegan2-cat-config-f" SGAN = loadStyleGAN2(modelname+".pt", size=256, channel_multiplier=2) G = StyleGAN2_wrapper(SGAN) savedir = join(rootdir, modelname) os.makedirs(savedir, exist_ok=True) for triali in range(150): feat = torch.randn(1, 512).detach().clone().cuda() T0 = time() eva_BP, evc_BP, H_BP = hessian_compute(G, feat, ImDist, hessian_method="BP", 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())
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 savedir = r"E:\OneDrive - Washington University in St. Louis\HessNetArchit\BigBiGAN\ctrl_Hessians" # join(datadir, "ctrl_Hessians") os.makedirs(savedir, exist_ok=True) for triali in tqdm(range(0, 200)): feat = torch.randn(1, 120).cuda() eigvals, eigvects, H = hessian_compute( G_sf, feat, ImDist, hessian_method="BP", ) np.savez(join(savedir, "eig_full_trial%d.npz" % (triali)), H=H, eva=eigvals, evc=eigvects, feat=feat.cpu().detach().numpy()) # feat.requires_grad_(True) # for blocki in [0, 3, 5, 8, 10, 12]: # L2dist_col = [] # torch.cuda.empty_cache() # H1 = SGAN_sf.convs[blocki].register_forward_hook(Hess_hook) # img = SGAN_sf([feat], truncation=1) # H1.remove() # T0 = time()
return cc_12, cclog_12, cc_21, cclog_21 #%% saveroot = r"E:\Cluster_Backup" #%% BGAN = loadBigGAN() G = BigGAN_wrapper(BGAN) savedir = join(saveroot, "ImDist_cmp\\BigGAN") os.makedirs(savedir, exist_ok=True) SSIM_stat_col = [] MSE_stat_col = [] for idx in range(100): refvec = G.sample_vector(1, device="cuda") # 0.1 * torch.randn(1, 256) eigvals_PS, eigvects_PS, H_PS = hessian_compute(G, refvec, ImDist, hessian_method="BP") eigvals_SSIM, eigvects_SSIM, H_SSIM = hessian_compute(G, refvec, D, hessian_method="BP") eigvals_MSE, eigvects_MSE, H_MSE = hessian_compute(G, refvec, MSE, hessian_method="BP") #% eigvals_L1, eigvects_L1, H_L1 = hessian_compute(G, refvec, L1, hessian_method="BP") print("SSIM - LPIPS comparison") cc_SSIM, logcc_SSIM, reg_coef_SSIM, logreg_coef_SSIM = spectra_cmp( -eigvals_SSIM[::-1], eigvals_PS, show=True) H_cc_SSIM, logH_cc_SSIM = Hessian_cmp(-eigvals_SSIM[::-1], eigvects_SSIM,
return torch.clamp((imgs + 1.0) / 2.0, 0, 1) * scale G = StyleGAN_wrapper(model.cuda()) #%% savedir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary\StyleGAN_hr" #%% data = np.load(join(savedir, "Hessian_EPS_BP.npz")) H_BP = data["H_BP"] feat = torch.tensor(data['feat']).detach().cuda() #%% # noise = torch.randn(1, 512) # feat = noise.detach().clone().cuda() # G.StyleGAN.cuda() H_col = [] for EPS in [1E-6, 1E-5, 1E-4, 3E-4, 1E-3, 3E-3, 1E-2, 3E-2, 1E-1, ]: T0 = time() eva_FI, evc_FI, H_FI = hessian_compute(G, feat, ImDist, hessian_method="ForwardIter", EPS=EPS, preprocess=lambda img: F.interpolate(img, (256, 256), mode='bilinear', align_corners=True)) print("%.2f sec" % (time() - T0)) # 260.5 sec print("EPS %.1e Correlation of Flattened Hessian matrix BP vs ForwardIter %.3f" % ( EPS, np.corrcoef(H_BP.flatten(), H_FI.flatten())[0, 1])) H_col.append((eva_FI, evc_FI, H_FI)) np.savez(join(savedir, "Hessian_EPS_accuracy.npz"), H_col=H_col, feat=feat.detach().cpu().numpy()) print("Save Completed. ") #%% np.savez(join(savedir, "Hessian_EPS_BP.npz"), eva_BP=eva_BP, evc_BP=evc_BP, H_BP=H_BP, feat=feat.detach().cpu().numpy()) #%% G.StyleGAN.to("cpu") feat.cpu() T0 = time() eva_BI, evc_BI, H_BI = hessian_compute(G, feat, ImDist, hessian_method="BackwardIter", preprocess=lambda img: F.interpolate(img, (256, 256), mode='bilinear', align_corners=True), device="cpu") print("%.2f sec" % (time() - T0)) # this will exceed gpu memory
csr_end = min(csr + B, imgn) imgs = self.visualize(codes[csr:csr_end, :], scale=scale, res=res).to(device) img_all = imgs if img_all is None else torch.cat( (img_all, imgs), 0) csr = csr_end return img_all Gw = PGGAN_wrapper2(G) #%% T0 = time() eigvals, eigvects, H = hessian_compute( Gw, refvec, ImDist, hessian_method="BackwardIter", cutoff=50, preprocess=lambda img: F.interpolate(img, size=(256, 256)), device="cuda") # EPS=1E-5, print("%.3f sec" % (time() - T0)) # 77sec #%% T0 = time() eigvals_BP, eigvects_BP, H_BP = hessian_compute( Gw, refvec, ImDist, hessian_method="BP", preprocess=lambda img: F.interpolate(img, size=(256, 256)), device="cuda") # EPS=1E-5, print("%.3f sec" % (time() - T0)) # 377sec #%%
"E:\OneDrive - Washington University in St. Louis\HessNetArchit\StyleGAN", "StyleGAN_shuffle.pt"))) savedir = join(saveroot, label) os.makedirs(savedir, exist_ok=True) print(savedir) for triali in range(0, 80): if args.wspace: feat_z = torch.randn(1, 512).cuda() feat = G.StyleGAN.style(feat_z) else: feat = torch.randn(1, 512).cuda() T0 = time() eva_BP, evc_BP, H_BP = hessian_compute(G, feat, ImDist, hessian_method="BP", 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_%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,
#%% SG.random = False vec = torch.randn(1, 512).cuda() img1 = SG.visualize(vec) img2 = SG.visualize(vec) L1max = (img1 - img2).abs().max() dsim = ImDist(img1, img2) print("Maximal difference between trial L1max %.3f Dsim %.3f" % (L1max, dsim)) # random Maximal difference between trial L1max 0.610 Dsim 0.069 # fixednoise Maximal difference between trial L1max 0.000 Dsim 0.000 #%% Effect of allowing noise in Z space spectra SG.use_wspace(False) SG.random = False feat = torch.randn(1, 512) eva_BP, evc_BP, H_BP = hessian_compute(SG, feat, ImDist, hessian_method="BP") SG.random = True eva_BP_rnd, evc_BP_rnd, H_BP_rnd = hessian_compute(SG, feat, ImDist, hessian_method="BP") #%% figdir = r"E:\OneDrive - Washington University in St. Louis\Hessian_summary\StyleGAN" plt.figure(figsize=[8, 4]) plt.subplot(121) plt.plot(eva_BP) plt.plot(eva_BP_rnd) plt.ylabel("eig") plt.subplot(122) plt.plot(np.log10(np.abs(eva_BP)), label="fixednoise") plt.plot(np.log10(np.abs(eva_BP_rnd)), label="random")