def generate(model): # Prepare a input truncation = 0.4 batch_size = 10 class_vector = one_hot_from_names(['vase'], batch_size=batch_size) noise_vector = truncated_noise_sample(truncation=truncation, batch_size=batch_size) # All in tensors noise_vector = torch.from_numpy(noise_vector) class_vector = torch.from_numpy(class_vector) # If you have a GPU, put everything on cuda noise_vector = noise_vector.to('cuda') class_vector = class_vector.to('cuda') model.to('cuda') # Generate an image with torch.no_grad(): output = model(noise_vector, class_vector, truncation) # If you have a GPU put back on CPU output = output.to('cpu') # If you have a sixtel compatible terminal you can display the images in the terminal # (see https://github.com/saitoha/libsixel for details) # display_in_terminal(output) # Save results as png images save_as_images(output, file_name='output/output')
def biggan(inp: List[str], metadata): truncation = 0.4 try: class_vector = one_hot_from_names(inp, batch_size=len(inp)) noise_vector = truncated_noise_sample(truncation=truncation, batch_size=len(inp)) noise_vector = torch.from_numpy(noise_vector) class_vector = torch.from_numpy(class_vector) with torch.no_grad(): output = modelBG(noise_vector, class_vector, truncation) except: inp = ['cat'] class_vector = torch.from_numpy(one_hot_from_names(inp, batch_size=len(inp))) noise_vector = torch.from_numpy(truncated_noise_sample(truncation=truncation, batch_size=len(inp))) with torch.no_grad(): output = modelBG(noise_vector, class_vector, truncation) return convert_to_images(output)[0]
def recover_latent(trial): trueZ = truncated_noise_sample(truncation=truncation, batch_size=batches) noise = truncated_noise_sample(truncation=maxNoise, batch_size=batches) class_vec = one_hot_from_names(['fountain'], batch_size=batches) z = torch.from_numpy(trueZ + noise) print(z) ##print('diff:\n', z-trueZ) opt = optim.Adam([z.requires_grad_()]) with torch.no_grad(): trueZImg = model(torch.from_numpy(trueZ), torch.from_numpy(class_vec), truncation).requires_grad_() zImg = model(z, torch.from_numpy(class_vec), truncation).requires_grad_() zImg0 = zImg.clone() i = 0 while (i < 5): lf = nn.MSELoss() loss = lf(zImg, trueZImg) loss.backward() opt.step() opt.zero_grad() i += 1 print(i, ': ImageMSE: ', mse_loss(zImg, trueZImg), '\sVecMSE: ', mse_loss(z, torch.from_numpy(trueZ))) zImg = model(z, torch.from_numpy(class_vec), truncation).requires_grad_() ##with torch.no_grad(): ##zImg = model(torch.from_numpy(z, class_vec, truncation) trial = 1 #Save Images saveOriginal = 'output/' + str(trial) + '_original' saveNoisy = 'output/' + str(trial) + '_noisy' saveFixed = 'output/' + str(trial) + '_fixed' ensure_dir(saveOriginal) save_as_images(trueZImg, saveOriginal) ensure_dir(saveNoisy) save_as_images(zImg0, saveNoisy) ensure_dir(saveFixed) save_as_images(zImg, saveFixed) #Save vectors saveOriginal = 'output/' + str(trial) + 'originalVec_.pt' saveNoisy = 'output/' + str(trial) + '_noisyVec.pt' saveFixed = 'output/' + str(trial) + '_fixedVec.pt' ensure_dir(saveOriginal) torch.save(trueZImg, saveOriginal) ensure_dir(saveNoisy) torch.save(zImg0, saveNoisy) ensure_dir(saveFixed) torch.save(zImg, saveFixed)
def set_output_class(self, class_id): if isinstance(class_id, int): self.v_class = torch.from_numpy(one_hot_from_int([class_id])).to( self.device) self.outclass = f"class{class_id}" elif isinstance(class_id, str): self.outclass = class_id.replace(" ", "_") self.v_class = torch.from_numpy(one_hot_from_names([class_id])).to( self.device) else: raise RuntimeError(f"Unknown class identifier {class_id}")
def main(): args = parser.parse_args() import logging logging.basicConfig(level=logging.INFO) # Load pre-trained model tokenizer (vocabulary) global model model = BigGAN.from_pretrained(args.model_dir).to('cuda') label_str = args.labels.strip() labels = [l.replace('_', ' ') for l in label_str.split(',') if len(l)>0] class_base_vecs = one_hot_from_names(labels) label_alt_str = args.labels_alt.strip() labels_alt = [l.replace('_', ' ') for l in label_alt_str.split(',') if len(l)>0] print(labels, labels_alt) if len(labels_alt) > 0: assert len(labels_alt) == len(labels) c1 = one_hot_from_names(labels_alt) class_base_vecs = args.mixture_prop * class_base_vecs + (1-args.mixture_prop) * c1 outs = [] labels = [] for _ in trange(0, args.n_samples // args.batch_size): # Prepare a input cls = np.random.randint(0, class_base_vecs.shape[0], size=(args.batch_size,)) class_vector = class_base_vecs[cls] noise_vector = truncated_noise_sample( truncation=args.truncation, batch_size=args.batch_size) outs.append(gen_image( noise_vector, class_vector, args.crop_ratio, args.truncation)) labels.append(cls) outs = np.concatenate(outs) labels = np.concatenate(labels) np.savez(args.dump_dir+'.npz', args=vars(args), samples=outs, labels=labels) Image.fromarray(tile_images(outs[:81])).save(args.dump_dir+'.samples-81.png') Image.fromarray(tile_images(outs[:16])).save(args.dump_dir+'.samples-16.png')
def generate_image(dense_class_vector=None, name=None, noise_seed_vector=None, truncation=0.4, gan_model=None, pretrained_gan_model_name='biggan-deep-128'): """ Utility function to generate an image (numpy uint8 array) from either: - a name (string): converted in an associated ImageNet class and then a dense class embedding using BigGAN's internal ImageNet class embeddings. - a dense_class_vector (torch.Tensor with 128 elements): used as a replacement of BigGAN internal ImageNet class embeddings. Other args: - noise_seed_vector: a vector used to control the seed (seed set to the sum of the vector elements) - truncation: a float between 0 and 1 to control image quality/diversity tradeoff (see BigGAN paper) - gan_model: a BigGAN model from pytorch_pretrained_biggan library. If None a model is instanciated from a pretrained model name given by `pretrained_gan_model_name` List of possible names: https://github.com/huggingface/pytorch-pretrained-BigGAN#models - pretrained_gan_model_name: shortcut name of the GAN model to instantiate if no gan_model is provided. Default to 'biggan-deep-128' """ seed = int(noise_seed_vector.sum().item() ) if noise_seed_vector is not None else None noise_vector = truncated_noise_sample(truncation=truncation, batch_size=1, seed=seed) noise_vector = torch.from_numpy(noise_vector) if gan_model is None: gan_model = BigGAN.from_pretrained(pretrained_gan_model_name) if name is not None: class_vector = one_hot_from_names([name], batch_size=1) class_vector = torch.from_numpy(class_vector) dense_class_vector = gan_model.embeddings(class_vector) # input_vector = torch.cat([noise_vector, gan_class_vect.unsqueeze(0)], dim=1) # dense_class_vector = torch.matmul(class_vector, gan.embeddings.weight.t()) else: dense_class_vector = dense_class_vector.view(1, 128) input_vector = torch.cat([noise_vector, dense_class_vector], dim=1) # Generate an image with torch.no_grad(): output = gan_model.generator(input_vector, truncation) output = output.cpu().numpy() output = output.transpose((0, 2, 3, 1)) output = ((output + 1.0) / 2.0) * 256 output.clip(0, 255, out=output) output = np.asarray(np.uint8(output[0]), dtype=np.uint8) return output
def generate_image(thing="mushroom", model_name="biggan-deep-512", truncation=0.4): "Generate an image of *thing* from the model, save it and return the path" if model_name in ["waifu", "celeb"]: return generate_waifu(model_name, truncation) global img_i model = get_model(model_name) # Prepare a input class_vector = one_hot_from_names([thing], batch_size=1) noise_vector = truncated_noise_sample(truncation=truncation, batch_size=1) # All in tensors noise_vector = torch.from_numpy(noise_vector) class_vector = torch.from_numpy(class_vector) # If you have a GPU, put everything on cuda noise_vector = noise_vector.to('cuda') class_vector = class_vector.to('cuda') model.to('cuda') # Generate an image with torch.no_grad(): output = model(noise_vector, class_vector, truncation) # If you have a GPU put back on CPU output = output.to('cpu') img = convert_to_images(output) out = img[0] file_name = f"images/{img_i}.png" img_i += 1 os.system("mkdir -p images/") out.save(file_name, 'png') print( f"Generated an image of {thing} in file {file_name} with model {model_name}" ) return file_name
import torch from pytorch_pretrained_biggan import (BigGAN, one_hot_from_names, truncated_noise_sample, save_as_images, display_in_terminal) # OPTIONAL: if you want to have more information on what's happening, activate the logger as follows import logging logging.basicConfig(level=logging.INFO) # Load pre-trained model tokenizer (vocabulary) model = BigGAN.from_pretrained('biggan-deep-512') # Prepare a input truncation = 0.4 class_vector = one_hot_from_names(['soap bubble', 'coffee', 'mushroom'], batch_size=3) noise_vector = truncated_noise_sample(truncation=truncation, batch_size=3) # All in tensors noise_vector = torch.from_numpy(noise_vector) class_vector = torch.from_numpy(class_vector) # # If you have a GPU, put everything on cuda # noise_vector = noise_vector.to('cuda') # class_vector = class_vector.to('cuda') # model.to('cuda') # Generate an image with torch.no_grad(): output = model(noise_vector, class_vector, truncation) # If you have a GPU put back on CPU # output = output.to('cpu')
display_in_terminal) import numpy as np import json try: import Image except ImportError: from PIL import Image # Load pre-trained model model = BigGAN.from_pretrained('biggan-deep-256') truncation = 0.4 # List of classes to interpolate between classes = ['soup bowl', 'wolf'] class_vector = torch.from_numpy( one_hot_from_names(classes, batch_size=len(classes))) # Defining a nonlinear spacing function to ease in/out of categories def spacing_func(y, branch): return (1 + branch * y * np.sqrt((2 - np.power(y, 2)).clip(0))) / 2 p_range = np.concatenate( (spacing_func(np.linspace(1, np.sqrt(2), 10, endpoint=False), -1), spacing_func(np.linspace(np.sqrt(2), 1, 10, endpoint=False), 1))) # Creating mixed category vectors nIm = len(classes) * len(p_range) interp_class = torch.Tensor(nIm, class_vector.shape[1]) i = 0
from pytorch_pretrained_biggan import (BigGAN, one_hot_from_names, truncated_noise_sample, save_as_images, display_in_terminal) # OPTIONAL: if you want to have more information on what's happening, activate the logger as follows import logging from PIL import Image logging.basicConfig(level=logging.INFO) # Load pre-trained model tokenizer (vocabulary) model = BigGAN.from_pretrained('./BigGAN/model') # Prepare a input truncation = 0.4 class_vector = one_hot_from_names(['lakeshore'], batch_size=1) noise_vector = truncated_noise_sample(truncation=truncation, batch_size=1) # All in tensors noise_vector = torch.from_numpy(noise_vector) class_vector = torch.from_numpy(class_vector) # If you have a GPU, put everything on cuda # noise_vector = noise_vector.to('cuda') # class_vector = class_vector.to('cuda') # model.to('cuda') # Generate an image with torch.no_grad(): output = model(noise_vector, class_vector, truncation)
import nltk nltk.data.path.append('./nltk_data/') # OPTIONAL: if you want to have more information on what's happening, activate the logger as follows import logging logging.basicConfig(level=logging.INFO) # Load pre-trained model tokenizer (vocabulary) #model = BigGAN.from_pretrained('biggan-deep-256') model = BigGAN.from_pretrained('biggan-deep-128') #model = BigGAN.from_pretrained('biggan-deep-512') # Prepare a input truncation = 0.4 class_vector = one_hot_from_names(['agama','night snake'], batch_size=1) noise_vector = truncated_noise_sample(truncation=truncation, batch_size=1) # All in tensors noise_vector = torch.from_numpy(noise_vector) class_vector = torch.from_numpy(class_vector) # If you have a GPU, put everything on cuda # noise_vector = noise_vector.to('cuda') # class_vector = class_vector.to('cuda') # model.to('cuda') # Generate an image with torch.no_grad(): output = model(noise_vector, class_vector, truncation)
#parameters n_clusters = 3 #make saved_data directory if needed Path('./saved_data').mkdir(parents=True, exist_ok=True) #list for storing each class' results C_T_classes = [] T_LOO_acc_classes = [] Qm_LOO_acc_classes = [] for name in classes: print('Testing class \'{0:s}\'...'.format(name)) #get one hot encoding of the class class_vec = one_hot_from_names([name], batch_size=1) img_class = np.argmax(class_vec, axis=1) imgnet_id = class_2_imgnet[img_class][0] #Make a loader for this class class_id = train_set.class_to_idx['n0' + str(imgnet_id)] class_idx = np.arange(len( train_set.targets))[np.array(train_set.targets) == class_id] class_sampler = SubsetRandomSampler(class_idx) class_loader = DataLoader(train_set, batch_size=64, shuffle=False, sampler=class_sampler) #now val loader class_idx_val = np.arange(len(
model = BigGAN.from_pretrained('biggan-deep-256') # Prepare a input batches = 1 truncation = 1 classnam = str(input('Enter desired class:\n')) numpics = int(input('Enter desired number of pictures:\n')) large_batches = True if str(input('Large Batches?\n')) == "y" else False jitter = True if str(input('Jitter?\n')) == "y" else False if jitter: super_jitter = True if str(input('SuperJitter?\n')) == "y" else False if super_jitter: minJ = float(input('MinJitter:\n')) class_vector = one_hot_from_names([classnam], batch_size=batches) noise_vector = truncated_noise_sample(truncation=truncation, batch_size=batches) ### All in tensors nv = torch.from_numpy(noise_vector) class_vector = torch.from_numpy(class_vector) ## ##torch.save(nv, 'Experiments/nv.pt') ##torch.save(class_vector, 'Experiments/cv.pt') ##class_vector = torch.load('Experiments/cv.pt') #0? ##nv2 = nv.cos().abs()-.5 ##nv3 = nv2.cos().abs()-.5 ##nv4 = nv3.cos().abs()-.5
def vase_vector(batch_size): return one_hot_from_names(['vase'], batch_size=batch_size)
output = model.generator(input_vecs[csr:csr_end, :].float().cuda(), truncation).cpu() # imgs = convert_to_images(output.cpu()) # imgs = [np.array(img).astype(np.float64) / 255 * scale for img in imgs] imgs = convert_to_images_np(output, scale) imgs_all.extend(imgs) csr = csr_end return imgs_all if __name__ == "__main__": # %% # Prepare a input batch_size = 3 truncation = 0.5 class_vector = one_hot_from_names(['soap bubble', 'coffee', 'mushroom'], batch_size=batch_size) noise_vector = truncated_noise_sample(truncation=truncation, batch_size=batch_size) #noise_vector = truncated_noise_sample(truncation=truncation, batch_size=1) # All in tensors #noise_vector = torch.from_numpy(np.ones([3, 128]).astype(np.float32)) # noise_vector = torch.from_numpy(noise_vector) class_vector = torch.from_numpy(class_vector) # If you have a GPU, put everything on cuda noise_vector = noise_vector.to('cuda') class_vector = class_vector.to('cuda') model.to('cuda') # Generate an image with torch.no_grad(): output = model(noise_vector, class_vector, truncation)
def reconstruct_batch_celebA(target, filter, n_pixels, G, num_samples, z_dim=128, img_dim=128, n_channels=3, n_iter=1000, threshold=0.05, truncation=1.0): bs_ = 1 G.to('cuda') class_vector = one_hot_from_names(['dog'], batch_size=bs_) class_vector = torch.from_numpy(class_vector) class_vec = class_vector.to('cuda') y = torch.FloatTensor(target).cuda() A = torch.FloatTensor(filter).cuda() z = torch.FloatTensor( truncated_noise_sample(truncation=truncation, batch_size=bs_)).cuda() z_param = torch.nn.Parameter(z) #batch_y = y.unsqueeze(0).repeat(z.shape[0],1,1) complete_zs = [] #np.array([]) #torch.nn.Parameter() lr = 1e-2 # optim = torch.optim.SGD([z_param], lr=lr, momentum=0.9) optim = torch.optim.Adam([z_param], lr=lr) step = 0 last_size = num_samples sampled = [] while last_size > 0: # print(batch_y.shape) if (step > 100000) or z_param.shape[0] == 0: print('restarting with ', last_size, ' left') step = 0 z = torch.FloatTensor( truncated_noise_sample(truncation=truncation, batch_size=bs_)).cuda() z_param = torch.nn.Parameter(z) # optim = torch.optim.SGD([z_param], lr=lr, momentum=0.9) optim = torch.optim.Adam([z_param], lr=lr) # batch_y = y.unsqueeze(0).repeat(z.shape[0],1,1) step += 1 optim.zero_grad() x_hat = G(z_param, class_vec, truncation).view(z_param.size()[0], n_channels, img_dim, img_dim) y_hat = x_hat * A loss = i_se_3d(y_hat, y) / (n_pixels * n_channels) loss_filt = loss[loss.data > threshold] loss_val = loss_filt.data.cpu().numpy() print(loss_val) if loss_filt.shape[0] > 0: loss_mean = loss_filt.mean() loss_mean.backward() optim.step() z_completed = z_param[loss.data <= threshold] if z_completed.size()[0] != 0: remaining = last_size last_size -= z_completed.shape[0] min_len = min(remaining, z_completed.shape[0]) sampled.append( x_hat[loss.data <= threshold].data.cpu().numpy()[:min_len]) complete_zs.append( z_completed.data.cpu().numpy()[:min_len].reshape( min_len, z_dim, 1, 1)) # z = z_param.data[loss.data > threshold] z[loss.data <= threshold] = torch.FloatTensor( truncated_noise_sample( truncation=truncation, batch_size=z_completed.shape[0])).cuda() # also cutoff those that have anything larger than truncation level... cutoffs = torch.sum(torch.abs(z) > 1, dim=1) > 0 z[cutoffs] = torch.FloatTensor( truncated_noise_sample( truncation=truncation, batch_size=torch.sum(cutoffs).item())).cuda() z_param = torch.nn.Parameter(z) # optim = torch.optim.SGD([z_param], lr=lr, momentum=0.9) optim = torch.optim.Adam([z_param], lr=lr) #if z_param.shape[0] > 0: # batch_y = y.unsqueeze(0).repeat(z_param.shape[0],1,1) #optim = torch.optim.SGD([z_param], lr=1, momentum=0.9) complete_zs = np.concatenate(complete_zs, axis=0) final_sample = np.concatenate(sampled, axis=0) unmasked = torch.from_numpy(final_sample).cuda() * (1 - A) return complete_zs, final_sample, unmasked.data.cpu().numpy()
# Prepare a input truncation = 0.4 # class_vector = one_hot_from_names(['teapot', 'coffeepot', 'car_wheel'], batch_size=3) # # class_vector = one_hot_from_names(['running_shoe', 'running_shoe', 'running_shoe'], batch_size=3) # class_vector = one_hot_from_names(['car_wheel', 'car_wheel', 'car_wheel'], batch_size=3) # class_vector = one_hot_from_names(['pitcher', 'pitcher', 'pitcher'], batch_size=3) # class_vector = one_hot_from_names(['running_shoe', 'chair', 'car_wheel'], batch_size=3) # class_vector = one_hot_from_names(['king_penguin', 'running_shoe', 'backpack'], batch_size=3) # # class_vector = one_hot_from_names(['bench', 'bench', 'bench'], batch_size=3) # noise_vector = truncated_noise_sample(truncation=truncation, batch_size=3, seed=838383) ## Random Walk Walk_steps = 5 class_vector1 = one_hot_from_names(['husky'], batch_size=1) class_vector2 = one_hot_from_names(['banana'], batch_size=1) class_vector3 = one_hot_from_names(['car_wheel'], batch_size=1) class_vector4 = one_hot_from_names(['bicycle'], batch_size=1) class_vector5 = one_hot_from_names(['bench'], batch_size=1) class_vector = (class_vector1 + class_vector2 + class_vector3 + class_vector4 + class_vector5)/7 print("hot size", class_vector.shape) exit(0) noise_vector = truncated_noise_sample(truncation=truncation, batch_size=Walk_steps, seed=3442) # All in tensors print("noise_vector", noise_vector) noise_vector = torch.from_numpy(noise_vector) # noise_vector =torch.randn(3, 128)
# Load pre-trained model tokenizer (vocabulary) model = BigGAN.from_pretrained('biggan-deep-256') use_VGG = False if use_VGG: print("load VGG") VGG = models.__dict__["vgg16"](pretrained=True) # VGG.features = torch.nn.DataParallel(VGG.features) VGG.cuda() print("finish loading VGG") # Prepare a input truncation = 0.4 class_vector = one_hot_from_names(['teapot', 'coffeepot', 'car_wheel'], batch_size=3) # class_vector = one_hot_from_names(['running_shoe', 'running_shoe', 'running_shoe'], batch_size=3) class_vector = one_hot_from_names(['car_wheel', 'car_wheel', 'car_wheel'], batch_size=3) class_vector = one_hot_from_names(['pitcher', 'pitcher', 'pitcher'], batch_size=3) class_vector = one_hot_from_names(['running_shoe', 'chair', 'car_wheel'], batch_size=3) class_vector = one_hot_from_names(['king_penguin', 'running_shoe', 'backpack'], batch_size=3) # class_vector = one_hot_from_names(['bench', 'bench', 'bench'], batch_size=3) noise_vector = truncated_noise_sample(truncation=truncation, batch_size=3, seed=838383) # All in tensors