ae = Autoencoder(shape, DIM, FOLDER, BATCH_SIZE, CONV) if AE_LOAD: ae.load_weights("autoencoder") else: if "x_train" in locals(): ae.train(AE_STEPS, x_train, x_test, lr=0.003) else: ae.train_iter(AE_STEPS, train_loader, test_loader, lr=0.003) # Prepare the Latent Space Model if not 'x_test' in locals(): # print ("EVENTUALLY CHANGE THIS BACK AS WELL!!!") x_test = np.load("faces.npy") # x_test = unload(test_loader) encodings = ae.encode(x_test) if MODEL == 'transporter': model = Transporter(encodings, DISTR, FOLDER, BATCH_SIZE_GEN) elif MODEL == 'generator': model = Generator(encodings, DISTR, FOLDER, BATCH_SIZE_GEN) # I Could try L2 Loss instead of L1? else: raise NotImplementedError # Train the Latent Space Model if GEN_LOAD: model.load_weights(MODEL) else: model.train(STEPS, lr=0.001) # I should try adjusting the learning rate? #model.train(STEPS//2, lr=0.0003) #model.train(STEPS//2, lr=0.0001)
from tqdm import tqdm from autoencoder import Autoencoder device = torch.device("cuda" if torch.cuda.is_available() else "cpu") AUTOENCODER_FILENAME = 'trained_models/autoencoder.to' from image_loader import ImageDataset dataset = ImageDataset(return_hashes=True) data_loader = DataLoader(dataset, batch_size=1, shuffle=True, num_workers=4) autoencoder = Autoencoder() autoencoder.load_state_dict(torch.load(AUTOENCODER_FILENAME)) autoencoder.eval() with torch.no_grad(): for sample in tqdm(data_loader): image, hash = sample hash = hash[0] output = autoencoder.decode( autoencoder.encode(image.to(device).unsqueeze(0))) result = torch.zeros((3, 128, 256)) result[:, :, :128] = image.cpu() result[:, :, 128:] = output utils.save_image(result, 'data/test/{:s}.jpg'.format(hash))
import numpy as np from autoencoder import Autoencoder, LATENT_CODE_SIZE from config import * device = torch.device("cuda" if torch.cuda.is_available() else "cpu") from image_loader import ImageDataset dataset = ImageDataset(quality=(1, 2)) BATCH_SIZE = 256 data_loader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=8) autoencoder = Autoencoder(is_variational=USE_VARIATIONAL_AUTOENCODER) autoencoder.load_state_dict(torch.load(AUTOENCODER_FILENAME)) autoencoder.eval() latent_codes = np.zeros((len(dataset), LATENT_CODE_SIZE), dtype=np.float32) position = 0 with torch.no_grad(): for batch in tqdm(data_loader): current = autoencoder.encode(batch.to(device)) latent_codes[position:position + current.shape[0], :] = current.cpu().numpy() position += current.shape[0] np.save('data/latent_codes.npy', latent_codes)
loss += torch.sum(S * kernel_val) loss = torch.sqrt(loss) gmmn_optimizer.zero_grad() loss.backward() gmmn_optimizer.step() return loss # training loop for ep in range(N_GEN_EPOCHS): avg_loss = 0 for idx, (x, _) in enumerate(train_loader): x = x.view(x.size()[0], -1) with torch.no_grad(): x = Variable(x).to(device) encoded_x = encoder_net.encode(x) # uniform random noise between [-1, 1] random_noise = torch.rand((BATCH_SIZE, NOISE_SIZE)) * 2 - 1 loss = train_one_step(encoded_x, random_noise) avg_loss += loss.item() avg_loss /= (idx + 1) print("GMMN Training: Epoch - [%3d] complete, average loss - [%.4f]" % (ep, avg_loss)) torch.save(gmm_net.state_dict(), GMMN_SAVE_PATH)
for i in range(n_layers): vis_func = None if i == 0 else neuron_fn # create autoencoder for the next layer auto = Autoencoder(shapes[i], shapes[i + 1], rf_shape=rf_shapes[i], vis_func=vis_func, hid_func=neuron_fn) deep.autos.append(auto) # train the autoencoder using SGD auto.auto_sgd(data, deep, test_images, n_epochs=n_epochs, rate=rates[i]) # hidden layer activations become training data for next layer data = auto.encode(data) plt.figure(99) plt.clf() recons = deep.reconstruct(test_images) show_recons(test_images, recons) print "recons error", rms(test_images - recons, axis=1).mean() deep.auto_sgd(train_images, test_images, rate=0.3, n_epochs=30) print "recons error", rms(test_images - recons, axis=1).mean() # --- train classifier with backprop deep.train_classifier(train, test) print "mean error", deep.test(test).mean() # --- train with backprop
for i in range(n_layers): savename = "sigmoid-auto-%d.npz" % i if not os.path.exists(savename): auto = Autoencoder( shapes[i], shapes[i+1], rf_shape=rf_shapes[i], vis_func=funcs[i], hid_func=funcs[i+1]) deep.autos.append(auto) auto.auto_sgd(data, deep, test_images, noise=0.1, n_epochs=n_epochs, rate=rates[i]) auto.to_file(savename) else: auto = FileObject.from_file(savename) assert type(auto) is Autoencoder deep.autos.append(auto) data = auto.encode(data) plt.figure(99) plt.clf() recons = deep.reconstruct(test_images) show_recons(test_images, recons) print "recons error", rms(test_images - recons, axis=1).mean() # deep.auto_sgd(train_images, test_images, rate=0.3, n_epochs=30) # print "recons error", rms(test_images - recons, axis=1).mean() # --- train classifier with backprop savename = "sigmoid-classifier-hinge.npz" if not os.path.exists(savename): deep.train_classifier(train, test)
SAMPLE_SIZE = 400 indices = [int(i / SAMPLE_SIZE * len(dataset)) for i in range(SAMPLE_SIZE)] dataset.hashes = [dataset.hashes[i] for i in indices] data_loader = DataLoader(dataset, batch_size=1, shuffle=True, num_workers=4) autoencoder = Autoencoder(is_variational=USE_VARIATIONAL_AUTOENCODER) autoencoder.load_state_dict(torch.load(AUTOENCODER_FILENAME)) autoencoder.eval() STEPS = 5 with torch.no_grad(): for sample in tqdm(data_loader): image, hash = sample hash = hash[0] latent_code = autoencoder.encode(image.to(device)).unsqueeze(0) result = torch.zeros((3, 128, 128 * (STEPS + 1))) result[:, :, :128] = image.cpu() for i in range(STEPS): output = autoencoder.decode(latent_code * (1.0 - i / (STEPS - 1))) result[:, :, 128 * (i + 1):128 * (i + 2)] = output result = (torch.clamp(result, 0, 1).numpy() * 255).astype( np.uint8).transpose((1, 2, 0)) io.imsave('data/test/{:s}.jpg'.format(hash), result, quality=99)
class VCCA(): def __init__(self, epochs, batch_size, ZDIMS, input_dim): self.SEED = 1 self.BATCH_SIZE = batch_size self.LOG_INTERVAL = 10 self.EPOCHS = epochs self.ZDIMS = ZDIMS self.input_dim = input_dim torch.manual_seed(self.SEED) def loss_function(self, recon_x1, recon_x2, x1, x2, mu, logvar) -> Variable: # how well do input x and output recon_x agree? BCE1 = F.binary_cross_entropy(recon_x1, x1.view(-1, self.input_dim)) BCE2 = F.binary_cross_entropy(recon_x2, x2.view(-1, self.input_dim)) # KLD is Kullback–Leibler divergence -- how much does one learned # distribution deviate from another, in this specific case the # learned distribution from the unit Gaussian # - D_{KL} = 0.5 * sum(1 + log(sigma^2) - mu^2 - sigma^2) # note the negative D_{KL} in appendix B of the paper KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp()) # Normalise by same number of elements as in reconstruction KLD /= self.BATCH_SIZE * self.input_dim # BCE tries to make our reconstruction as accurate as possible # KLD tries to push the distributions as close as possible to unit Gaussian return BCE1 + KLD + BCE2 def train(self, epoch): # toggle model to train mode self.model.train() train_loss = 0 for batch_idx, (data1, data2) in enumerate(self.train_loader): data1 = Variable(data1).float() data2 = Variable(data2).float() self.optimizer.zero_grad() recon_batch1, recon_batch2, mu, log_var = self.model(data1, data2) # calculate scalar loss loss = self.loss_function(recon_batch1, recon_batch2, data1, data2, mu, log_var) # calculate the gradient of the loss w.r.t. the graph leaves # i.e. input variables -- by the power of pytorch! loss.backward() train_loss += loss.data self.optimizer.step() print('====> Epoch: {} Average loss: {:.4f}'.format( epoch, train_loss / len(self.train_loader.dataset))) def fit(self, x_view, y_view): data1 = x_view data2 = y_view self.train_loader = torch.utils.data.DataLoader( ConcatDataset(data1, data2), batch_size=self.BATCH_SIZE, shuffle=True) self.model = Autoencoder(self.ZDIMS, self.input_dim) self.optimizer = optim.Adam(self.model.parameters(), lr=0.0001) for epoch in range(1, self.EPOCHS + 1): # train(model,epoch,train_loader,optimizer,input_dim) self.train(epoch) #est(epoch) self.model.eval() def transform(self, x_view, y_view): mu, log_var = self.model.encode(x_view.view(-1, self.input_dim)) z1 = self.model.reparameterize(mu, log_var) z2 = self.model.reparameterize(mu, log_var) return z1, z2