def build_model( experiment_name, img_size, n_hidden=128, recon_depth=9, recon_vs_gan_weight=5e-5, real_vs_gen_weight=0.5, discriminate_sample_z=True, discriminate_ae_recon=True, wgain=1.0, wdecay=1e-5, bn_noise_std=0.0, ): if n_hidden != 128: experiment_name += "_nhidden%i" % n_hidden experiment_name += "_reconganweight%.1e" % recon_vs_gan_weight if recon_depth > 0: experiment_name += "_recondepth%i" % recon_depth if not np.isclose(real_vs_gen_weight, 0.5): experiment_name += "_realgenweight%.2f" % real_vs_gen_weight if not discriminate_sample_z: experiment_name += "_nodissamplez" if not discriminate_ae_recon: experiment_name += "_nodisaerecon" if not np.isclose(wgain, 1.0): experiment_name += "_wgain%.1e" % wgain if not np.isclose(wdecay, 1e-5): experiment_name += "_wdecay%.1e" % wdecay if not np.isclose(bn_noise_std, 0.0): experiment_name += "_bnnoise%.2f" % bn_noise_std # Setup network if img_size == 32: encoder, decoder, discriminator = architectures.img32x32(wgain=wgain, wdecay=wdecay, bn_noise_std=bn_noise_std) elif img_size == 64: encoder, decoder, discriminator = architectures.img64x64(wgain=wgain, wdecay=wdecay, bn_noise_std=bn_noise_std) else: raise ValueError("no architecture for img_size %i" % img_size) latent_encoder = architectures.vae_latent_encoder(n_hidden) model = aegan.AEGAN( encoder=encoder, latent_encoder=latent_encoder, decoder=decoder, discriminator=discriminator, recon_depth=recon_depth, discriminate_sample_z=discriminate_sample_z, discriminate_ae_recon=discriminate_ae_recon, recon_vs_gan_weight=recon_vs_gan_weight, real_vs_gen_weight=real_vs_gen_weight, ) return model, experiment_name
def build_model(experiment_name, img_size, n_hidden=128, recon_depth=9, recon_vs_gan_weight=5e-5, real_vs_gen_weight=0.5, discriminate_sample_z=True, discriminate_ae_recon=True, wgain=1.0, wdecay=1e-5, bn_noise_std=0.0): if n_hidden != 128: experiment_name += '_nhidden%i' % n_hidden experiment_name += '_reconganweight%.1e' % recon_vs_gan_weight if recon_depth > 0: experiment_name += '_recondepth%i' % recon_depth if not np.isclose(real_vs_gen_weight, 0.5): experiment_name += '_realgenweight%.2f' % real_vs_gen_weight if not discriminate_sample_z: experiment_name += '_nodissamplez' if not discriminate_ae_recon: experiment_name += '_nodisaerecon' if not np.isclose(wgain, 1.0): experiment_name += '_wgain%.1e' % wgain if not np.isclose(wdecay, 1e-5): experiment_name += '_wdecay%.1e' % wdecay if not np.isclose(bn_noise_std, 0.0): experiment_name += '_bnnoise%.2f' % bn_noise_std # Setup network if img_size == 32: encoder, decoder, discriminator = architectures.img32x32( wgain=wgain, wdecay=wdecay, bn_noise_std=bn_noise_std) elif img_size == 64: encoder, decoder, discriminator = architectures.img64x64( wgain=wgain, wdecay=wdecay, bn_noise_std=bn_noise_std) else: raise ValueError('no architecture for img_size %i' % img_size) latent_encoder = architectures.vae_latent_encoder(n_hidden) model = aegan.AEGAN( encoder=encoder, latent_encoder=latent_encoder, decoder=decoder, discriminator=discriminator, recon_depth=recon_depth, discriminate_sample_z=discriminate_sample_z, discriminate_ae_recon=discriminate_ae_recon, recon_vs_gan_weight=recon_vs_gan_weight, real_vs_gen_weight=real_vs_gen_weight, ) return model, experiment_name
def run(): n_hidden = 64 ae_kind = 'variational' lr_start = 0.01 lr_stop = 0.0001 lr_gamma = 0.75 n_epochs = 150 epoch_size = 250 batch_size = 64 experiment_name = 'mnist_ae' experiment_name += '_nhidden%i' % n_hidden out_dir = os.path.join('out', experiment_name) arch_path = os.path.join(out_dir, 'arch.pickle') start_arch_path = arch_path start_arch_path = None print('experiment_name', experiment_name) print('start_arch_path', start_arch_path) print('arch_path', arch_path) # Setup network if start_arch_path is None: print('Creating new model') encoder, decoder, _ = architectures.mnist() if ae_kind == 'variational': latent_encoder = architectures.vae_latent_encoder(n_hidden) elif ae_kind == 'adversarial': latent_encoder = architectures.aae_latent_encoder(n_hidden) else: print('Starting from %s' % start_arch_path) with open(start_arch_path, 'rb') as f: decoder, discriminator = pickle.load(f) model = ae.Autoencoder( encoder=encoder, latent_encoder=latent_encoder, decoder=decoder, ) model.recon_error = ae.GaussianNegLogLikelihood() # Fetch dataset dataset = dp.dataset.MNIST() x_train, y_train, x_test, y_test = dataset.arrays() x_train = mnist_transform(x_train) x_test = mnist_transform(x_test) # Prepare network inputs train_input = dp.Input(x_train, batch_size, epoch_size) test_input = dp.Input(x_test, batch_size) # Plotting n_examples = 64 batch = test_input.batches().next() original_x = batch['x'] original_x = np.array(original_x)[:n_examples] samples_z = np.random.normal(size=(n_examples, n_hidden)) samples_z = (samples_z).astype(dp.float_) # Train network learn_rule = dp.RMSProp() trainer = dp.GradientDescent(model, train_input, learn_rule) annealer = dp.GammaAnnealer(lr_start, lr_stop, n_epochs, gamma=lr_gamma) try: recon_video = Video(os.path.join(out_dir, 'convergence_recon.mp4')) sample_video = Video(os.path.join(out_dir, 'convergence_samples.mp4')) sp.misc.imsave(os.path.join(out_dir, 'examples.png'), dp.misc.img_tile(mnist_inverse_transform(original_x))) for e in range(n_epochs): model.phase = 'train' model.setup(**train_input.shapes) learn_rule.learn_rate = annealer.value(e) / batch_size loss = trainer.train_epoch() model.phase = 'test' original_z = model.encode(original_x) recon_x = model.decode(original_z) samples_x = model.decode(samples_z) recon_x = mnist_inverse_transform(recon_x) samples_x = mnist_inverse_transform(model.decode(samples_z)) recon_video.append(dp.misc.img_tile(recon_x)) sample_video.append(dp.misc.img_tile(samples_x)) likelihood = model.likelihood(test_input) print('epoch %i Train loss:%.4f Test likelihood:%.4f' % (e, np.mean(loss), np.mean(likelihood))) except KeyboardInterrupt: pass print('Saving model to disk') with open(arch_path, 'wb') as f: pickle.dump((decoder, discriminator), f) model.phase = 'test' n_examples = 100 samples_z = np.random.normal(size=(n_examples, n_hidden)).astype(dp.float_) output.samples(model, samples_z, out_dir, mnist_inverse_transform) output.walk(model, samples_z, out_dir, mnist_inverse_transform)
def run(): experiment_name = 'celeba-ae' img_size = 64 epoch_size = 250 batch_size = 64 n_hidden = 128 n_augment = int(6e5) train_feed, test_feed = dataset.celeba.feeds( img_size, split='test', batch_size=batch_size, epoch_size=epoch_size, n_augment=n_augment, ) experiment_name += '_nhidden%i' % n_hidden out_dir = os.path.join('out', experiment_name) arch_path = os.path.join(out_dir, 'arch.pickle') start_arch_path = arch_path start_arch_path = None print('experiment_name', experiment_name) print('start_arch_path', start_arch_path) print('arch_path', arch_path) ae_kind = 'variational' # Setup network if start_arch_path is None: print('Creating new model') encoder, decoder, _ = architectures.img64x64() if ae_kind == 'variational': latent_encoder = architectures.vae_latent_encoder(n_hidden) elif ae_kind == 'adversarial': latent_encoder = architectures.aae_latent_encoder(n_hidden) else: print('Starting from %s' % start_arch_path) with open(start_arch_path, 'rb') as f: encoder, latent_encoder, decoder = pickle.load(f) model = ae.Autoencoder( encoder=encoder, latent_encoder=latent_encoder, decoder=decoder, ) model.recon_error = ae.NLLNormal() # Plotting n_examples = 64 original_x, = test_feed.batches().next() original_x = np.array(original_x)[:n_examples] samples_z = np.random.normal(size=(n_examples, n_hidden)) samples_z = (samples_z).astype(dp.float_) n_epochs = 250 lr_start = 0.025 lr_stop = 0.00001 lr_gamma = 0.75 # Train network learn_rule = dp.RMSProp() trainer = dp.GradientDescent(model, train_feed, learn_rule) annealer = dp.GammaAnnealer(lr_start, lr_stop, n_epochs, gamma=lr_gamma) try: recon_video = Video(os.path.join(out_dir, 'convergence_recon.mp4')) sample_video = Video(os.path.join(out_dir, 'convergence_samples.mp4')) sp.misc.imsave(os.path.join(out_dir, 'examples.png'), dp.misc.img_tile(img_inverse_transform(original_x))) for e in range(n_epochs): model.phase = 'train' model.setup(*train_feed.shapes) learn_rule.learn_rate = annealer.value(e) / batch_size loss = trainer.train_epoch() model.phase = 'test' original_z = model.encode(original_x) recon_x = model.decode(original_z) samples_x = model.decode(samples_z) recon_x = img_inverse_transform(recon_x) samples_x = img_inverse_transform(model.decode(samples_z)) recon_video.append(dp.misc.img_tile(recon_x)) sample_video.append(dp.misc.img_tile(samples_x)) likelihood = model.likelihood(test_feed) print( 'epoch %i Train loss:%.4f Test likelihood:%.4f Learn Rate:%.4f' % (e, np.mean(loss), np.mean(likelihood), learn_rule.learn_rate)) except KeyboardInterrupt: pass print('Saving model to disk') with open(arch_path, 'wb') as f: pickle.dump((encoder, latent_encoder, decoder), f) model.phase = 'test' n_examples = 100 test_feed.reset() original_x = np.array(test_feed.batches().next()[0])[:n_examples] samples_z = np.random.normal(size=(n_examples, n_hidden)) output.samples(model, samples_z, out_dir, img_inverse_transform) output.reconstructions(model, original_x, out_dir, img_inverse_transform) original_z = model.encode(original_x) output.walk(model, original_z, out_dir, img_inverse_transform)
def run(): n_hidden = 64 ae_kind = 'variational' lr_start = 0.01 lr_stop = 0.0001 lr_gamma = 0.75 n_epochs = 150 epoch_size = 250 batch_size = 64 experiment_name = 'mnist_ae' experiment_name += '_nhidden%i' % n_hidden out_dir = os.path.join('out', experiment_name) arch_path = os.path.join(out_dir, 'arch.pickle') start_arch_path = arch_path start_arch_path = None print('experiment_name', experiment_name) print('start_arch_path', start_arch_path) print('arch_path', arch_path) # Setup network if start_arch_path is None: print('Creating new model') encoder, decoder, _ = architectures.mnist() if ae_kind == 'variational': latent_encoder = architectures.vae_latent_encoder(n_hidden) elif ae_kind == 'adversarial': latent_encoder = architectures.aae_latent_encoder(n_hidden) else: print('Starting from %s' % start_arch_path) with open(start_arch_path, 'rb') as f: decoder, discriminator = pickle.load(f) model = ae.Autoencoder( encoder=encoder, latent_encoder=latent_encoder, decoder=decoder, ) model.recon_error = ae.NLLNormal() # Fetch dataset dataset = dp.dataset.MNIST() x_train, y_train, x_test, y_test = dataset.arrays() x_train = mnist_transform(x_train) x_test = mnist_transform(x_test) # Prepare network feeds train_feed = dp.Feed(x_train, batch_size, epoch_size) test_feed = dp.Feed(x_test, batch_size) # Plotting n_examples = 64 original_x, = test_feed.batches().next() original_x = np.array(original_x)[:n_examples] samples_z = np.random.normal(size=(n_examples, n_hidden)) samples_z = (samples_z).astype(dp.float_) # Train network learn_rule = dp.RMSProp() trainer = dp.GradientDescent(model, train_feed, learn_rule) annealer = dp.GammaAnnealer(lr_start, lr_stop, n_epochs, gamma=lr_gamma) try: recon_video = Video(os.path.join(out_dir, 'convergence_recon.mp4')) sample_video = Video(os.path.join(out_dir, 'convergence_samples.mp4')) sp.misc.imsave(os.path.join(out_dir, 'examples.png'), dp.misc.img_tile(mnist_inverse_transform(original_x))) for e in range(n_epochs): model.phase = 'train' model.setup(*train_feed.shapes) learn_rule.learn_rate = annealer.value(e) / batch_size loss = trainer.train_epoch() model.phase = 'test' original_z = model.encode(original_x) recon_x = model.decode(original_z) samples_x = model.decode(samples_z) recon_x = mnist_inverse_transform(recon_x) samples_x = mnist_inverse_transform(model.decode(samples_z)) recon_video.append(dp.misc.img_tile(recon_x)) sample_video.append(dp.misc.img_tile(samples_x)) likelihood = model.likelihood(test_feed) print('epoch %i Train loss:%.4f Test likelihood:%.4f' % (e, np.mean(loss), np.mean(likelihood))) except KeyboardInterrupt: pass print('Saving model to disk') with open(arch_path, 'wb') as f: pickle.dump((decoder, discriminator), f) model.phase = 'test' n_examples = 100 samples_z = np.random.normal(size=(n_examples, n_hidden)).astype(dp.float_) output.samples(model, samples_z, out_dir, mnist_inverse_transform) output.walk(model, samples_z, out_dir, mnist_inverse_transform)