def test_1d(self): bn = False params = dict() params['optimization'] = dict() params['optimization']['epoch'] = 1 params['summary_every'] = 4 params['save_every'] = 5 params['print_every'] = 3 params['net'] = dict() params['net']['shape'] = [16, 1] # Shape of the image params['net']['generator'] = dict() params['net']['generator']['latent_dim'] = 100 params['net']['generator']['full'] = [2 * 8] params['net']['generator']['nfilter'] = [2, 32, 32, 1] params['net']['generator']['batch_norm'] = [bn, bn, bn] params['net']['generator']['shape'] = [[5], [5], [5], [5]] params['net']['generator']['stride'] = [1, 2, 1, 1] params['net']['generator']['data_size'] = 1 params['net']['discriminator'] = dict() params['net']['discriminator']['full'] = [32] params['net']['discriminator']['nfilter'] = [16, 32, 32, 32] params['net']['discriminator']['batch_norm'] = [bn, bn, bn, bn] params['net']['discriminator']['shape'] = [[5], [5], [5], [3]] params['net']['discriminator']['stride'] = [2, 2, 2, 1] params['net']['discriminator']['data_size'] = 1 X = np.random.rand(101, 16) dataset = Dataset(X) wgan = GANsystem(WGAN, params) wgan.train(dataset) img = wgan.generate(2) assert (len(img) == 2) assert (img.shape[1:] == (16, 1)) img = wgan.generate(500) assert (len(img) == 500)
def test_generate(self): params = dict() params['optimization'] = dict() params['optimization']['epoch'] = 1 params['summary_every'] = 4 params['save_every'] = 5 params['print_every'] = 3 params['net']= dict() params['net']['generator'] = dict() params['net']['generator']['latent_dim'] = 5 X = np.random.rand(101,16,16) dataset = Dataset(X) wgan = GANsystem(WGAN, params) wgan.train(dataset) img = wgan.generate(2) img = wgan.generate(500) assert(len(img)==500) z = np.random.randn(133,5) img1 = wgan.generate(z=z) img2 = np.zeros(shape=img1.shape) for i in range(133): img2[i] = wgan.generate(z=np.reshape(z[i], [1,5])) np.testing.assert_almost_equal(img1, img2, decimal=6)
def test_gansystem(self): params = dict() params['optimization'] = dict() params['optimization']['epoch'] = 1 params['summary_every'] = 4 params['save_every'] = 5 params['print_every'] = 3 X = np.random.rand(101,16,16) dataset = Dataset(X) wgan = GANsystem(WGAN, params) wgan.train(dataset) img = wgan.generate(2) img = wgan.generate(500) assert(len(img)==500)
def test_patchupscalegan1dborder(self): bn = False params = dict() params['optimization'] = dict() params['optimization']['epoch'] = 1 params['summary_every'] = 4 params['save_every'] = 5 params['print_every'] = 3 params['net'] = dict() params['net']['shape'] = [8, 2] params['net']['generator'] = dict() params['net']['generator']['latent_dim'] = 8 params['net']['generator']['full'] = [16] params['net']['generator']['nfilter'] = [8, 32, 1] params['net']['generator']['batch_norm'] = [bn, bn] params['net']['generator']['shape'] = [[3], [3], [3]] params['net']['generator']['stride'] = [1, 1, 1] params['net']['generator']['data_size'] = 1 params['net']['generator']['borders'] = dict() params['net']['generator']['borders']['width_full'] = None params['net']['generator']['borders']['nfilter'] = [4, 1] params['net']['generator']['borders']['batch_norm'] = [bn, bn] params['net']['generator']['borders']['shape'] = [[5], [3]] params['net']['generator']['borders']['stride'] = [2, 2] params['net']['generator']['borders']['data_size'] = 1 params['net']['generator']['borders']['width_full'] = 2 params['net']['discriminator'] = dict() params['net']['discriminator']['full'] = [32] params['net']['discriminator']['nfilter'] = [16, 32] params['net']['discriminator']['batch_norm'] = [bn, bn] params['net']['discriminator']['shape'] = [[5], [3]] params['net']['discriminator']['stride'] = [2, 2] params['net']['discriminator']['data_size'] = 1 params['net']['upscaling'] = 2 X = np.random.rand(101, 8, 2) dataset = Dataset(X) wgan = GANsystem(UpscalePatchWGAN, params) wgan.train(dataset) borders = np.random.rand(500, 8, 1) X_smooth = np.random.rand(500, 8, 1) print(borders[:2].shape) img = wgan.generate(N=2, X_smooth=X_smooth[:2], borders=borders[:2]) assert (len(img) == 2) assert (img.shape[1:] == (8, 1)) img = wgan.generate(N=500, X_smooth=X_smooth[:500], borders=borders[:500]) assert (len(img) == 500)
def test_patchupscalegan3d(self): bn = False params = dict() params['optimization'] = dict() params['optimization']['epoch'] = 1 params['summary_every'] = 4 params['save_every'] = 5 params['print_every'] = 3 params['net'] = dict() params['net']['shape'] = [8, 8, 8, 8] params['net']['generator'] = dict() params['net']['generator']['latent_dim'] = 8 * 8 * 8 params['net']['generator']['full'] = [] params['net']['generator']['nfilter'] = [8, 32, 1] params['net']['generator']['batch_norm'] = [bn, bn] params['net']['generator']['shape'] = [[3, 3, 3], [3, 3, 3], [3, 3, 3]] params['net']['generator']['stride'] = [1, 1, 1] params['net']['generator']['data_size'] = 3 params['net']['discriminator'] = dict() params['net']['discriminator']['full'] = [32] params['net']['discriminator']['nfilter'] = [16, 32] params['net']['discriminator']['batch_norm'] = [bn, bn] params['net']['discriminator']['shape'] = [[3, 3, 3], [3, 3, 3]] params['net']['discriminator']['stride'] = [2, 2] params['net']['discriminator']['data_size'] = 3 params['net']['upsampling'] = 2 X = np.random.rand(101, 8, 8, 8, 8) dataset = Dataset(X) class UgradedGAN(UpscalePatchWGAN, CosmoWGAN): pass wgan = GANsystem(UgradedGAN, params) wgan.train(dataset) borders = np.random.rand(500, 8, 8, 8, 7) X_down = np.random.rand(500, 4, 4, 4, 1) img = wgan.generate(N=2, X_down=X_down[:2], borders=borders[:2]) assert (len(img) == 2) assert (img.shape[1:] == (8, 8, 8, 1)) img = wgan.generate(N=500, X_down=X_down[:500], borders=borders[:500]) assert (len(img) == 500)
def test_lapgan2d(self): bn = False params = dict() params['optimization'] = dict() params['optimization']['epoch'] = 1 params['summary_every'] = 4 params['save_every'] = 5 params['print_every'] = 3 params['net'] = dict() params['net']['shape'] = [16, 16, 1] params['net']['generator'] = dict() params['net']['generator']['latent_dim'] = 16 * 16 params['net']['generator']['full'] = [] params['net']['generator']['nfilter'] = [8, 32, 1] params['net']['generator']['batch_norm'] = [bn, bn] params['net']['generator']['shape'] = [[5, 5], [5, 5], [5, 5]] params['net']['generator']['stride'] = [1, 1, 1] params['net']['generator']['data_size'] = 2 params['net']['discriminator'] = dict() params['net']['discriminator']['full'] = [32] params['net']['discriminator']['nfilter'] = [16, 32] params['net']['discriminator']['batch_norm'] = [bn, bn] params['net']['discriminator']['shape'] = [[5, 5], [3, 3]] params['net']['discriminator']['stride'] = [2, 2] params['net']['discriminator']['data_size'] = 2 params['net']['upscaling'] = 2 X = np.random.rand(101, 16, 16) dataset = Dataset(X) wgan = GANsystem(LapWGAN, params) wgan.train(dataset) X_down = np.random.rand(500, 8, 8, 1) img = wgan.generate(N=2, X_down=X_down[:2]) assert (len(img) == 2) assert (img.shape[1:] == (16, 16, 1)) img = wgan.generate(N=500, X_down=X_down[:500]) assert (len(img) == 500)
params_optimization['discriminator'] = dict() params_optimization['discriminator']['optimizer'] = 'adam' params_optimization['discriminator']['kwargs'] = {'beta1': 0.5, 'beta2': 0.9} params_optimization['discriminator']['learning_rate'] = 1e-4 # all parameters params = dict() params['net'] = dict() # All the parameters for the model params['net']['generator'] = params_generator params['net']['discriminator'] = params_discriminator params['net']['prior_distribution'] = 'gaussian' params['net']['shape'] = [128, 128, 1] # Shape of the image params['net']['gamma_gp'] = 10 # Gradient penalty params['net']['fs'] = 16000 // downscale params['net']['loss_type'] = 'wasserstein' params['optimization'] = params_optimization params['summary_every'] = 100 # Tensorboard summaries every ** iterations params['print_every'] = 50 # Console summaries every ** iterations params['save_every'] = 1000 # Save the model every ** iterations params['summary_dir'] = os.path.join(global_path, name + '_summary/') params['save_dir'] = os.path.join(global_path, name + '_checkpoints/') params['Nstats'] = 500 resume, params = utils.test_resume(True, params) params['optimization']['epoch'] = 10000 wgan = GANsystem(SpectrogramGAN, params) wgan.train(dataset, resume=resume)
def test_conditionalwgan(self): bn = False ns = 16 # Parameters for the generator params_generator = dict() params_generator['latent_dim'] = 126 params_generator['stride'] = [1, 2, 1, 1] params_generator['nfilter'] = [32, 64, 32, 1] params_generator['shape'] = [[5, 5], [5, 5], [5, 5], [5, 5]] params_generator['batch_norm'] = [bn, bn, bn] params_generator['full'] = [8 * 8 * 32] params_generator['non_lin'] = tf.nn.relu params_generator['in_conv_shape'] = [8, 8] # Parameters for the discriminator params_discriminator = dict() params_discriminator['stride'] = [2, 2, 1] params_discriminator['nfilter'] = [16, 64, 32] params_discriminator['shape'] = [[5, 5], [5, 5], [3, 3]] params_discriminator['batch_norm'] = [bn, bn, bn] params_discriminator['full'] = [512, 128, 32] params_discriminator['minibatch_reg'] = False # Optimization parameters d_opt = dict() d_opt['optimizer'] = "rmsprop" d_opt['learning_rate'] = 3e-5 params_optimization = dict() params_optimization['n_critic'] = 5 params_optimization['batch_size'] = 8 params_optimization['epoch'] = 1 # all parameters params = dict() params['net'] = dict() # All the parameters for the model params['net']['generator'] = params_generator params['net']['discriminator'] = params_discriminator params['net']['shape'] = [ns, ns, 1] # Shape of the image params['net']['gamma_gp'] = 10 # Gradient penalty # Conditional params params['net']['prior_normalization'] = False params['net']['cond_params'] = 2 params['net']['init_range'] = [[0, 1], [0, 1]] params['net']['prior_distribution'] = "gaussian_length" params['net']['final_range'] = [ 0.1 * np.sqrt(params_generator['latent_dim']), 1 * np.sqrt(params_generator['latent_dim']) ] params['optimization'] = params_optimization params['summary_every'] = 4 params['save_every'] = 5 params['print_every'] = 3 X = np.random.rand(101, ns, ns) parameters = np.random.rand(101, 2) dataset = Dataset_parameters(X, parameters) wgan = GANsystem(ConditionalParamWGAN, params) wgan.train(dataset) img = wgan.generate(N=16, **{ 'z': wgan.net.sample_latent(bs=16, params=np.random.rand( 16, 2)) }) assert (img.shape[0] == 16 and img.shape[1] == ns and img.shape[2] == ns and img.shape[3] == 1)