def plot_boxes(image, targets): class_names = ("bike", "bus", "car", "motor", "person", "rider", "traffic light", "traffic sign", "train", "truck") if torch.is_tensor(targets): targets = torch.Tensor.cpu(targets).detach().numpy() else: targets = np.array(targets) targets = targets.squeeze() image = ToPILImage()(image) image, targets = ToAbsoluteCoords()(image, boxes=targets) targets = targets.astype('int32') draw = ImageDraw.Draw(image) colors = [(0, 0, 0), (0, 0, 255), (255, 0, 0), (0, 100, 100), (100, 0, 100), (100, 100, 0), (0, 0, 100), (0, 255, 0), (255, 165, 0), (255, 255, 0)] # font = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 40) count = 0 print('Labels:', targets) for result in targets: result = result.astype('int32') color = colors[result[-1]] fcolor = tuple(255 - i for i in color) draw.rectangle(list(result[0:4]), outline=color) text = "{0}".format(class_names[result[-1]]) text_size = draw.textsize(text, direction='ltr') c1 = tuple(result[0:2]) c2 = (c1[0] + text_size[0], c1[1] + text_size[1]) draw.rectangle([c1, c2], color, color) draw.text(c1, text, fcolor) count += 1 print('Labels found:', count) image.show()
def show_random_data(nrow=4, ncol=4): traindata = get_data(train_path101) # Collect random samples of each class classes = { label_101_dict[name]: [path for path, label in traindata if label == label_101_dict[name]] for name in label_101_dict } samples = { label: random.sample(classes[label], nrow * ncol) for label in classes } samples = { label: [*map(lambda x: Image.open(x), samples[label])] for label in classes } for label in samples: x = [*map(lambda x: x.resize((128, 128)), samples[label])] x = torch.cat([ToTensor()(x_).unsqueeze(0) for x_ in x], 0) xgrid = ToPILImage()(make_grid(x, nrow=nrow)) xgrid.show()
def Hess_sep_BigGAN_optim(param): lr1 = 10 ** param[0, 0] wd1 = 10 ** param[0, 1] lr2 = 10 ** param[0, 2] wd2 = 10 ** param[0, 3] noise_init = torch.from_numpy(truncated_noise_sample(1, 128)).cuda() class_init = 0.06 * torch.randn(1, 128).cuda() noise_coef = (noise_init @ evc_nois).detach().clone().requires_grad_(True) class_coef = (class_init @ evc_clas).detach().clone().requires_grad_(True) optim1 = Adam([noise_coef], lr=lr1, weight_decay=wd1, betas=(0.9, 0.999)) optim2 = Adam([class_coef], lr=lr2, weight_decay=wd2, betas=(0.9, 0.999)) # torch.optim.lr_scheduler for step in range(300): optim1.zero_grad() optim2.zero_grad() class_vec = class_coef @ evc_clas.T noise_vec = noise_coef @ evc_nois.T fitimg = BGAN.generator(torch.cat((noise_vec, class_vec), dim=1), 0.7) fitimg = torch.clamp((1.0 + fitimg) / 2.0, 0, 1) dsim = alpha * ImDist(fitimg, target_tsr) + L1loss(fitimg, target_tsr) # dsim.backward() optim1.step() optim2.step() if (step + 1) % 10 == 0: print("step%d loss %.2f norm: cls: %.2f nois: %.1f" % (step, dsim.item(), class_vec.norm(), noise_vec.norm())) imcmp = ToPILImage()(make_grid(torch.cat((fitimg, target_tsr)).cpu())) imcmp.show() imcmp.save(join(savedir, "Hsep%06d_%.3f.jpg" % (np.random.randint(1000000), dsim.item()))) return dsim.item() if not torch.isnan(dsim) else 1E6
def show_imgrid(img_tsr, *args, **kwargs): if type(img_tsr) is list: if img_tsr[0].ndim == 4: img_tsr = torch.cat(tuple(img_tsr), dim=0) elif img_tsr[0].ndim == 3: img_tsr = torch.stack(tuple(img_tsr), dim=0) PILimg = ToPILImage()(make_grid(img_tsr.cpu(), *args, **kwargs)) PILimg.show() return PILimg
def denoiseImg(self): if (self.load == 2): model = torch.load('%s/%sF_ALL.pth' % (self.dir, self.typeDir)) elif (self.load == 1): model = torch.load('%s/%sF.pth' % (self.dir, self.typeDir)) else: model = torch.load('%s/%s.pth' % (self.dir, self.typeDir)) model = model.cuda() self.getNext() img = Variable(ToTensor()(self.fake)).view(1, -1, self.fake.size[1], self.fake.size[0]) img = img.cuda() out_img = model(img) out_img = out_img.cpu() out_img = out_img.data[0] for j in xrange(1, self.col): self.getNext() img = Variable(ToTensor()(self.fake)).view(1, -1, self.fake.size[1], self.fake.size[0]) img = img.cuda() img = model(img) img = img.cpu() img = img.data[0] out_img = torch.cat((out_img, img), 2) ans = out_img for i in xrange(1, self.row): self.getNext() img = Variable(ToTensor()(self.fake)).view(1, -1, self.fake.size[1], self.fake.size[0]) img = img.cuda() out_img = model(img) out_img = out_img.cpu() out_img = out_img.data[0] print(out_img.size(0), out_img.size(1), out_img.size(2)) for j in xrange(1, self.col): self.getNext() img = Variable(ToTensor()(self.fake)).view( 1, -1, self.fake.size[1], self.fake.size[0]) img = img.cuda() img = model(img) img = img.cpu() img = img.data[0] out_img = torch.cat((out_img, img), 2) ans = torch.cat((ans, out_img), 1) ans.clamp_(0.0, 1.0) # ans = self.clip(ans); print(ans.size(0), ans.size(1), ans.size(2)) ans = ToPILImage()(ans) ans.show()
def func(self): # img = Image.open('Database/waterloo/distorted_images/gblurConv/00001_1.bmp').convert('RGB') img = Image.open('Database/waterloo/pristine_images/00001.bmp').convert('RGB') img = CenterCrop((224, 224))(img) img.save('outPic/src_pristine.bmp', 'bmp', quality=100) # img.show() # print (img.size) img = Variable(ToTensor()(img)).view(1, -1, img.size[1], img.size[0]) # print(img) model = torch.load('%s/gblurConv.pth' % self.dir) model = model.cuda() input = img.cuda() out_img = model(input) out_img = out_img.cpu().data[0] out_img.clamp_(0.0, 1.0) out_img = ToPILImage()(out_img) # out_img.save('outPic/1_1.bmp', 'bmp', quality=100) out_img.show()
def show_random_data(nrow=4, ncol=4): traindata = get_data(train_path) hotdog = [path for path, label in traindata if label == 1] not_hotdog = [path for path, label in traindata if label == 0] sample_hotdog = random.sample(hotdog, nrow * ncol) sample_not_hotdog = random.sample(not_hotdog, nrow * ncol) sample_hotdog = [*map(lambda x: Image.open(x), sample_hotdog)] sample_not_hotdog = [*map(lambda x: Image.open(x), sample_not_hotdog)] x = [*map(lambda x: x.resize((128, 128)), sample_hotdog)] y = [*map(lambda x: x.resize((128, 128)), sample_not_hotdog)] x = torch.cat([ToTensor()(x_).unsqueeze(0) for x_ in x], 0) y = torch.cat([ToTensor()(y_).unsqueeze(0) for y_ in y], 0) xgrid = ToPILImage()(make_grid(x, nrow=nrow)) ygrid = ToPILImage()(make_grid(y, nrow=nrow)) xgrid.show() ygrid.show()
def denoisePatch(self): if (self.load == 2): model = torch.load('%s/%sF_ALL.pth' % (self.dir, self.typeDir)) elif (self.load == 1): model = torch.load('%s/%sF.pth' % (self.dir, self.typeDir)) else: model = torch.load('%s/%s.pth' % (self.dir, self.typeDir)) model = model.cuda() # img = Image.open('Database/waterloo/pristine_images/00001.bmp').convert('RGB') # img = CenterCrop((self.size, self.size))(img) # img.save('outPic/0.bmp', 'bmp', quality=100) for i in xrange(1, 5): img = Image.open( 'Database/waterloo/distorted_images/%s/00001_%d.bmp' % (self.typeDir, i)).convert('RGB') # img = Image.open('Database/waterloo/pristine_images/00001.bmp').convert('RGB') img = CenterCrop((self.size, self.size))(img) # img.save('outPic/%d_0.bmp' % i, 'bmp', quality=100) img.show() # print (img.size) img = Variable(ToTensor()(img)).view(1, -1, img.size[1], img.size[0]) # print(img) input = img.cuda() out_img = model(input) out_img = out_img.cpu() out_img = out_img.data[0] out_img.clamp_(0.0, 1.0) # out_img = self.clip(out_img) out_img = ToPILImage()(out_img) # out_img.save('outPic/%d.bmp'%i, 'bmp', quality=100) out_img.show()
"ffhq-512-avg-tpurun1", "ffhq-256-config-e-003810", "stylegan2-cat-config-f", "model.ckpt-533504" ]: SGAN = loadStyleGAN2(modelnm + '.pt') shuf_SD = shuffle_state_dict(SGAN.state_dict(), maskfun) torch.save( shuf_SD, join(ckpt_root, modelnm + "_shuffle.pt"), ) feat = torch.randn(5, 512).cuda() G = StyleGAN2_wrapper(SGAN) img = G.visualize(feat) G.StyleGAN.load_state_dict(shuf_SD) img_sf = G.visualize(feat) mtg = ToPILImage()(make_grid(torch.cat((img, img_sf)), nrow=5).cpu()) mtg.show() mtg.save(join(ckpt_root, modelnm + "_shuffle.png")) #%% datadir = r"E:\OneDrive - Washington University in St. Louis\HessNetArchit\StyleGAN2" 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) #%% rndfeat = torch.randn(1, 512).cuda() G_sf.random = False img1 = G_sf.visualize(rndfeat, ).cpu() img2 = G_sf.visualize(rndfeat).cpu() print((img1 - img2).abs().max()) ToPILImage()(img1[0, :].cpu()).show() #%%
def BasinCMA(target_tsr, cmasteps=30, gradsteps=40, finalgrad=500, batch_size=4, basis="all", CMApostAdam=False, RND=None, savedir=savedir, imgnm=""): Record = {"L1cma": [],"dsimcma": [], "L1Adam": [], "dsimAdam": [], "L1refine":[], "dsimrefine":[], "classnorm":[], "noisenorm":[]} # choose the basis vector to use in Adam optimization basisvec = {"all": evc_all, "sep": evc_sep, "none": evc_none}[basis] fixnoise = truncated_noise_sample(1, 128) optim_noise = cma.CMAEvolutionStrategy(fixnoise, 0.4)#0.4) # 0.2 optim_class = cma.CMAEvolutionStrategy(128 * [0.0], 0.2)#0.12) # 0.06 # noise_vec = torch.from_numpy(fixnoise) RND = np.random.randint(1E6) if RND is None else RND # Outer Loop: CMA optimization of initial points for i in tqdm.trange(cmasteps, desc="CMA steps"): class_codes = optim_class.ask() noise_codes = optim_noise.ask() # TODO: boundary handling by projection in code space # Evaluate the cma proposed codes `latent_code` at first. codes_tsr = torch.from_numpy(np.array(class_codes)).float() noise_tsr = torch.from_numpy(np.array(noise_codes)).float() latent_code = torch.cat((noise_tsr, codes_tsr), dim=1).cuda() # this initialize inner loop with torch.no_grad(): imgs = BGAN.generator(latent_code, 0.7) imgs = (imgs + 1.0) / 2.0 dsims = ImDist(imgs, target_tsr).squeeze() L1dsim = (imgs - target_tsr).abs().mean([1, 2, 3]) scores = dsims.detach().cpu().numpy() L1score = L1dsim.detach().cpu().numpy() print("step %d pre-ADAM dsim %.3f L1 %.3f (norm %.2f noise norm %.2f)" % ( i, scores.mean(), L1score.mean(), codes_tsr.norm(dim=1).mean(), noise_tsr.norm(dim=1).mean())) Record["L1cma"].append(L1score) Record["dsimcma"].append(scores) # Inner loop: ADAM optimization from the cma proposed codes `latent_code` batch by batch codes_post = np.zeros_like(np.hstack((noise_codes, class_codes))) scores_post = np.zeros_like(scores) L1score_post = np.zeros_like(L1score) if gradsteps > 0: csr = 0 while csr < len(class_codes): # pbar = tqdm.tqdm(total=len(codes), initial=csr, desc="batchs") csr_end = min(len(class_codes), csr + batch_size) # codes_batch = codes_tsr[csr:csr_end, :].detach().clone().requires_grad_(True) coef_batch = (latent_code[csr:csr_end, :] @ basisvec).detach().clone().requires_grad_(True) optim = Adam([coef_batch], lr=0.05, ) for step in range(gradsteps): # tqdm.trange(gradsteps, desc="ADAM steps"): optim.zero_grad() latent_batch = coef_batch @ basisvec.T imgs = BGAN.generator(latent_batch, 0.7) imgs = (imgs + 1.0) / 2.0 dsims = ImDist(imgs, target_tsr).squeeze() L1dsim = (imgs - target_tsr).abs().mean([1, 2, 3]) loss = (dsims + L1dsim).sum() loss.backward() optim.step() if (step + 1) % 10 == 0: print("step %d dsim %.3f L1 %.3f" % (step, dsims.mean().item(), L1dsim.mean().item(),)) code_batch = (coef_batch @ evc_all.T).detach().cpu().numpy() scores_batch = dsims.detach().cpu().numpy() L1score_batch = L1dsim.detach().cpu().numpy() codes_post[csr:csr_end, :] = code_batch scores_post[csr:csr_end] = scores_batch L1score_post[csr:csr_end] = L1score_batch csr = csr_end # Use the ADAM optimized scores as utility for `latent_code` and do cma update print("step %d post-ADAM dsim %.3f L1 %.3f (norm %.2f, norm %.2f)" % ( i, scores_post.mean(), L1score_post.mean(), np.linalg.norm(codes_post[:, 128:], axis=1).mean(), np.linalg.norm(codes_post[:, :128], axis=1).mean())) else: # if no grad step is performed scores_post = scores L1score_post = L1score codes_post = np.hstack((noise_codes, class_codes)) Record["L1Adam"].append(L1score_post) Record["dsimAdam"].append(scores_post) if CMApostAdam: optim_class.tell(codes_post[:, :128], scores_post + L1score_post) optim_noise.tell(codes_post[:, 128:], scores_post + L1score_post) else: optim_class.tell(class_codes, scores_post + L1score_post) optim_noise.tell(noise_codes, scores_post + L1score_post) # Sort the scores and find the codes with the least scores to be final refined idx = np.argsort((L1score_post + scores_post)) codes_batch = torch.from_numpy(codes_post[idx[:4]]).float().cuda() fit_img = BGAN.generator(codes_batch, 0.7) fit_img = (fit_img + 1) / 2.0 CMAfitimg = ToPILImage()(make_grid(torch.cat((fit_img, target_tsr)).cpu())) CMAfitimg.save(join(savedir, "%s_CMA_final%06d.jpg" % (imgnm, RND))) CMAfitimg.show() # Linear Reparametrization using basisvec coef_batch = (codes_batch @ basisvec).detach().clone().requires_grad_(True) optim = Adam([coef_batch], lr=0.05, ) scheduler = torch.optim.lr_scheduler.StepLR(optim, step_size=200, gamma=0.5) for step in range(finalgrad): # tqdm.trange(gradsteps, desc="ADAM steps"): optim.zero_grad() # latent_code = torch.cat((noise_vec.repeat(codes_batch.shape[0], 1), codes_batch), dim=1).cuda() latent_code = coef_batch @ basisvec.T #evc_all.T imgs = BGAN.generator(latent_code, 0.7) imgs = (imgs + 1.0) / 2.0 dsims = ImDist(imgs, target_tsr).squeeze() L1dsim = (imgs - target_tsr).abs().mean([1, 2, 3]) loss = (dsims + L1dsim).sum() loss.backward() optim.step() scheduler.step() Record["L1refine"].append(L1dsim.detach().cpu().numpy()) Record["dsimrefine"].append(dsims.detach().cpu().numpy()) Record["classnorm"].append(latent_code[:, 128:].norm(dim=1).detach().cpu().numpy()) Record["noisenorm"].append(latent_code[:, :128].norm(dim=1).detach().cpu().numpy()) if (step + 1) % 10 == 0: print("step %d dsim %.3f L1 %.3f (norm %.2f)" % ( step, dsims.mean().item(), L1dsim.mean().item(), latent_code.norm(dim=1).mean().item())) scores_final = dsims.detach().cpu().numpy() L1score_final = L1dsim.detach().cpu().numpy() finalimg = ToPILImage()(make_grid(torch.cat((imgs, target_tsr)).cpu())) finalimg.save(join(savedir, "%srefinefinal%06d.jpg" % (imgnm, RND))) finalimg.show() fig = visualize_optim(Record, titlestr="cmasteps %d gradsteps %d refinesteps %d Hbasis %s, CMApostAdam %d"%(cmasteps, gradsteps, finalgrad, basis, CMApostAdam)) fig.savefig(join(savedir, "%straj_H%s%s_%d_dsim_%.3f_L1_%.3f.jpg" % (imgnm, basis, "_postAdam" if CMApostAdam else "", RND, scores_final.min(), L1score_final.min()))) np.savez(join(savedir, "%soptim_data_%d.npz" % (imgnm, RND)), codes=latent_code.cpu().detach().numpy(), Record=Record, dsims=scores_final, L1dsims=L1score_final) return imgs.cpu().detach().numpy(), latent_code.cpu().detach().numpy(), scores_final, L1score_final, Record
left = min(pos[0], left) up = min(pos[1], up) right = max(pos[0], right) low = max(pos[1], low) # fill with white up and down the mouth for i in range(6): x0, y0, x1, y1 = mp[i][0], mp[i][1], mp[i + 1][0], mp[i + 1][1] mask_draw.polygon([x0, y0, x1, y1, x1, 0, x0, 0], fill="white") for i in range(6, 11): x0, y0, x1, y1 = mp[i][0], mp[i][1], mp[i + 1][0], mp[i + 1][1] mask_draw.polygon([x0, y0, x1, y1, x1, im.height, x0, im.height], fill="white") x0, y0, x1, y1 = mp[11][0], mp[11][1], mp[0][0], mp[0][1] mask_draw.polygon([x0, y0, x1, y1, x1, im.height, x0, im.height], fill="white") # fill with white left and right mask_draw.rectangle([0, 0, left, im.height], fill="white") mask_draw.rectangle([right, 0, im.width, im.height], fill="white") mask = ToTensor()(mask) return mask if __name__ == '__main__': im = Image.open('test.jpg') mask = get_mouth_mask(im) mask = ToPILImage()(mask) mask.show()
CMAfitimg = ToPILImage()(fit_img[-1,:,:,:].cpu()) CMAfitimg.save(join(savedir, "CMA_final%06d.jpg"%RND)) #%% final_gradsteps = 400 codes_batch = torch.from_numpy(codes_post[idx[:4]]).float().cuda() coef_batch = (codes_batch @ evc_all).detach().clone().requires_grad_(True) optim = Adam([coef_batch], lr=0.03, ) scheduler = torch.optim.lr_scheduler.StepLR(optim, step_size=100, gamma=0.5) #% for step in range(final_gradsteps):#tqdm.trange(gradsteps, desc="ADAM steps"): optim.zero_grad() # latent_code = torch.cat((noise_vec.repeat(codes_batch.shape[0], 1), codes_batch), dim=1).cuda() latent_code = coef_batch @ evc_all.T imgs = BGAN.generator(latent_code, 0.7) imgs = (imgs + 1.0) / 2.0 dsims = ImDist(imgs, target_tsr).squeeze() L1dsim = (imgs - target_tsr).abs().mean([1, 2, 3]) loss = (dsims + L1dsim).sum() loss.backward() optim.step() if (step + 1) % 10 ==0: print("step %d dsim %.3f L1 %.3f (norm %.2f)" % ( step, dsims.mean().item(), L1dsim.mean().item(), latent_code.norm(dim=1).mean().item())) scores_final = dsims.detach().cpu().numpy() L1score_final = L1dsim.detach().cpu().numpy() #%% finalimg = ToPILImage()(make_grid(imgs[:,:,:,:].cpu())) finalimg.save(join(savedir, "refinefinal%06d.jpg"%RND)) finalimg.show() #%%
xtar_neg, ytar_neg, stepimgs_neg = find_level_step(BGAN, ImDist, targ_val, reftsr, tan_vec, refimg, iter=20, pos=False) imgrow = torch.cat((torch.flip(stepimgs_neg, (0,)), refimg, stepimgs_pos)).cpu() xticks_row = list(xtar_neg[::-1]) + [0.0] + list(xtar_pos) dsim_row = list(ytar_neg[::-1]) + [0.0] + list(ytar_pos) vecs_row = torch.tensor(xticks_row).cuda().view(-1, 1) @ tan_vec + reftsr xtick_col.append(xticks_row) dsim_col.append(dsim_row) vecs_col.append(vecs_row.cpu().numpy()) img_names.extend("noise_eig%d_lin%.2f.jpg" % (eigid, dist) for dist in tick_labels) # dsim_row) imgall = imgrow if imgall is None else torch.cat((imgall, imgrow)) print(time() - t0) mtg1 = ToPILImage()(make_grid(imgall, nrow=11).cpu()) # 20sec for 13 rows not bad mtg1.show() mtg1.save(join(summary_dir, "noise_space_all_var.jpg")) npimgs = imgall.permute([2, 3, 1, 0]).numpy() for imgi in range(npimgs.shape[-1]): imwrite(join(newimg_dir, img_names[imgi]), np.uint8(npimgs[:, :, :, imgi] * 255)) # %% xtick_arr = np.array(xtick_col) dsim_arr = np.array(dsim_col) vecs_arr = np.array(vecs_col) np.savez(join(summary_dir, "noise_ImDist_root_data.npz"), xtick_arr=xtick_arr, dsim_arr=dsim_arr, vecs_arr=vecs_arr, targ_val=targ_val) # % plt.figure(figsize=[10, 7]) plt.plot(xtick_arr) plt.xlabel("Eigenvalue index") plt.ylabel("L2 deviation from center")
abs(j - centerBlk[1])) # Bind the images in the horizontal direction. transBlkRow[pad[0]:pad[0] + blk_h, j, pad[2]:pad[2] + blk_w] = blk2D # Bind the rows of images in the vertical direction. transImage[:, i, :] = transBlkRow.view(blk_h_pad, -1).t() # Reshape the transformed image into a 2D image. transImage = transImage.view(blk_colCount * blk_w_pad, -1) return transImage.t() if __name__ == "__main__": dataTr = MNIST('../ML_data/', train=True, download=True) kale = Kaleidoscope((1, 2, 1, 1), (2, 2, 2, 2)) ime = kale.KaleidoExpan(dataTr[0][0]) ims = ToPILImage()(ime) ims.show() x = ToTensor()(dataTr[0][0]) x = x.view(1, -1) xForm = kale.KaleidoTransform(x.repeat(6, 1), torch.arange(0, 784).view(28, -1), torch.arange(0, 6).view(2, -1), centerBlk=(0, 1)) imx = ToPILImage()(xForm) #imx.show()
def to_trans(x, path): out = color_transform(x[0].data.max(0)[1]) image = ToPILImage()(out) image.show() root = (path[0]) image.save('results/' + path[0])
(0, )), refimg, stepimgs_pos)).cpu() xticks_row = xtar_neg[::-1] + [0.0] + xtar_pos dsim_row = list(ytar_neg[::-1]) + [0.0] + list(ytar_pos) vecs_row = torch.tensor(xticks_row).cuda().view(-1, 1) @ tan_vec + reftsr xtick_col.append(xticks_row) dsim_col.append(dsim_row) vecs_col.append(vecs_row.cpu().numpy()) img_names.extend("noise_eig%d_lin%.2f.jpg" % (eigid, dist) for dist in np.linspace(-0.4, 0.4, 11)) # dsim_row) imgall = imgrow if imgall is None else torch.cat((imgall, imgrow)) print(time() - t0) #%% mtg1 = ToPILImage()(make_grid(imgall, nrow=11).cpu()) # 20sec for 13 rows not bad mtg1.show() mtg1.save(join(summary_dir, "noise_space_all_var.jpg")) npimgs = imgall.permute([2, 3, 1, 0]).numpy() for imgi in range(npimgs.shape[-1]): imwrite(join(newimg_dir, img_names[imgi]), np.uint8(npimgs[:, :, :, imgi] * 255)) #%% xtick_arr = np.array(xtick_col) dsim_arr = np.array(dsim_col) vecs_arr = np.array(vecs_col) np.savez(join(summary_dir, "ImDist_root_data.npz"), xtick_arr=xtick_arr, dsim_arr=dsim_arr, vecs_arr=vecs_arr, targ_val=targ_val) #%%
def show_img_at_tensor(img_tensor_): img_PIL_ = ToPILImage()(img_tensor_) img_PIL_.show()
if evolspace == "BigGAN_noise": ref_class_vec = space_data[0,threadid]['fix_class_vec'] ref_noise_vec = final_gen_codes.mean(axis=0, keepdims=True) # final_gen_codes[0:1, :] if score_rank_avg: ref_noise_vec = w_avg_code elif evolspace == "BigGAN": ref_vec = final_gen_codes.mean(axis=0, keepdims=True) # final_gen_codes[0:1, :] if score_rank_avg: ref_vec = w_avg_code ref_noise_vec = ref_vec[:, :128] ref_class_vec = ref_vec[:, 128:] ## View image correspond to the reference code ref_vect = torch.from_numpy(np.concatenate((ref_noise_vec, ref_class_vec), axis=1)).float().cuda() refimg = G.visualize(ref_vect).cpu() centimg = ToPILImage()(refimg[0,:,:,:]) centimg.show(title="Center Reference Image") #%% Visualize the Final Generation together with the center reference image. VisFinalGen = True if VisFinalGen: #% If you want to regenerate the images from last generation here. print("Review the last generation codes w.r.t. the center code for manifold.") if evolspace == "BigGAN": imgs_final = G.visualize_batch_np(final_gen_codes[:,:]) elif evolspace == "BigGAN_class": imgs_final = G.visualize_batch_np(np.concatenate((ref_noise_vec.repeat(25,axis=0), final_gen_codes[:,:]), axis=1)) ToPILImage()(make_grid(imgs_final,nrow=5)).show() #G.visualize(torch.from_numpy(np.concatenate((ref_noise_vec.repeat(5,axis=0), final_gen_codes[:5,:]), axis=1)).float().cuda()).cpu() #ToPILImage()(make_grid(imgs.cpu())).show() #%% Compute Hessian decomposition and get the vectors Hess_method = "BP" # "BackwardIter" "ForwardIter" Hess_all = False # Set to False to reduce computation time.
img_data = Image.open(os.path.join(self.path, img)) img_data = self.trans(img_data) labels = img.split(".") axes = np.array(labels[1:5], dtype=np.float32) / 224 category = np.array(labels[5:6], dtype=np.float32) # 拼接列表 target = np.concatenate((axes, category)) return img_data, target if __name__ == '__main__': data = MyDataset(r"E:\Mysoft\ZONG\yellow_minions\datasets") loader = DataLoader(dataset=data, batch_size=5000, shuffle=True) x = data[0][0] print(x) x = ( x * torch.tensor(MyDataset.std, dtype=torch.float32).reshape(3, 1, 1) + torch.tensor(MyDataset.mean, dtype=torch.float32).reshape(3, 1, 1)) x = ToPILImage()(x) print(x) x.show() # data = next(iter(loader))[0] # mean = torch.mean(data,dim=(0,2,3)) # std = torch.std(data, dim=(0,2,3)) # print(mean , std) # a = np.array([0.2,0.3,0.5,0.2]) # b = np.array([1]) # # print(np.concatenate((a,b)))
optim1 = SGD([noise_vec], lr=0.01, weight_decay=0, ) optim2 = SGD([class_vec], lr=0.01, weight_decay=0.005, ) for step in range(200): optim1.zero_grad() optim2.zero_grad() fitimg = BGAN.generator(torch.cat((noise_vec, class_vec), dim=1), 0.7) fitimg = torch.clamp((1.0 + fitimg) / 2.0, 0, 1) dsim = alpha * ImDist(fitimg, target_tsr) + L1loss(fitimg, target_tsr)# dsim.backward() optim1.step() optim2.step() if (step + 1) % 10 ==0: print("step%d loss %.2f norm: cls: %.2f nois: %.1f" % (step, dsim.item(), class_vec.norm(), noise_vec.norm())) #% imcmp = ToPILImage()(make_grid(torch.cat((fitimg, target_tsr)).cpu())) imcmp.show() #%% # current best setting # [-1.5, -3, -3, -2.5, 0.8, 0.6] # [-1.5, -2.5, -3, -3, 0.8, 0.5] def optim_BigGAN(param): lr1 = 10**param[0,0] lr2 = 10**param[0,1] wd1 = 10**param[0,2] wd2 = 10**param[0,3] mom1 = param[0,4] mom2 = param[0,5] noise_init = torch.from_numpy(truncated_noise_sample(1, 128)).cuda() class_init = 0.06 * torch.randn(1, 128).cuda() alpha = 5 class_vec = class_init.detach().clone().cuda().requires_grad_(True)