def test_metrics(self): with self.test_session(): graph = mock_gan() gan = mock_gan() gan.create() loss = BoundaryEquilibriumLoss(gan, loss_config) d_loss, g_loss = loss.create() metrics = loss.metrics self.assertTrue(metrics['k'] != None)
def test_create(self): with self.test_session(): graph = mock_gan() gan = mock_gan() gan.create() loss = LambGanLoss(gan, loss_config) d_loss, g_loss = loss.create() d_shape = gan.ops.shape(d_loss) g_shape = gan.ops.shape(g_loss) self.assertEqual(d_shape, []) self.assertEqual(g_shape, [])
def test_batch_norm(self): with self.test_session(): config['layer_regularizer'] = 'batch_norm' gan = mock_gan() generator = ResizableGenerator(config=config, gan=gan, input=gan.latent.sample) generator.layer_regularizer(tf.constant(1, shape=[1,1,1,1], dtype=tf.float32)) self.assertNotEqual(len(generator.variables()), 0)
def test_train(self): with self.test_session(): gan = mock_gan() args = hc.Config({"size": "1", "steps": 1, "method": "train", "save_every": -1}) cli = hg.CLI(gan, args) cli.train() self.assertEqual(cli.gan, gan)
def test_run(self): with self.test_session(): gan = mock_gan() args = hc.Config({"size": "1"}) cli = hg.CLI(gan, args) cli.run() self.assertEqual(cli.gan, gan)
def test_initial_depth(self): with self.test_session(): gan = mock_gan() generator = ResizableGenerator(config=config, gan=gan, input=gan.latent.sample) print(generator.depths()) depths = generator.depths() self.assertEqual(depths[-1], 2)
def test_cli(self): with self.test_session(): gan = mock_gan() args = { } cli = hg.CLI(gan, args) self.assertEqual(cli.gan, gan)
def test_config(self): with self.test_session(): gan = mock_gan() gan.create() sampler = BatchSampler(gan) self.assertEqual(sampler._sample()['generator'].shape[-1], 1)
def test_metric(self): with self.test_session(): gan = mock_gan() loss = SupervisedLoss(gan, loss_config) d_loss, g_loss = loss.create() metrics = loss.metrics self.assertTrue(metrics['d_class_loss'] != None)
def test_create_component(self): with self.test_session(): gan = mock_gan() distribution = gan.create_component(gan.config.latent) self.assertEqual( type(distribution), hg.distributions.uniform_distribution.UniformDistribution)
def test_metrics(self): with self.test_session(): gan = mock_gan() loss = BoundaryEquilibriumLoss(gan, loss_config) d_loss, g_loss = loss.create() metrics = loss.metrics self.assertTrue(metrics['k'] != None)
def test_config(self): with self.test_session(): gan = mock_gan(batch_size=32) gan.latent = UniformDistribution(gan, {'z':2, 'min': -1, 'max': 1, 'projections':['identity']}) sampler = GridSampler(gan) self.assertEqual(sampler._sample()['generator'].shape[-1], 1)
def test_adds_supervised_loss(self): with self.test_session(): gan = mock_gan(y=2) args = hc.Config({"size": "1", "steps": 1, "method": "train", "save_every": -1, "classloss": True}) cli = hg.CLI(gan, args) cli.add_supervised_loss() self.assertEqual(type(cli.gan.loss), MultiComponent) self.assertEqual(type(cli.gan.loss.components[0]), SupervisedLoss)
def test_create(self): with self.test_session(): gan = mock_gan() loss = SupervisedLoss(gan, loss_config) d_loss, g_loss = loss.create() d_shape = gan.ops.shape(d_loss) self.assertEqual(d_shape, [1]) self.assertEqual(g_loss, None)
def test_categories(self): with self.test_session(): gan = mock_gan() encoder = CategoryEncoder(gan, config) gan.encoder = encoder gan.encoder.create() gan.create() self.assertEqual( gan.ops.shape(encoder.sample)[1] // 2, len(encoder.categories))
def test_config(self): with self.test_session(): gan = mock_gan(batch_size=32) gan.latent = UniformDistribution(gan, {'z':2, 'min': -1, 'max': 1, 'projections':['identity']}) gan.latent.create() gan.create() sampler = GridSampler(gan) self.assertEqual(sampler._sample()['generator'].shape[-1], 1)
def test_create(self): with self.test_session(): gan = mock_gan() loss = LeastSquaresLoss(gan, loss_config) d_loss, g_loss = loss.create() d_shape = gan.ops.shape(d_loss) g_shape = gan.ops.shape(g_loss) self.assertEqual(sum(d_shape), 0) self.assertEqual(sum(g_shape), 0)
def test_output_string(self): with self.test_session(): gan = mock_gan() config = {'d_learn_rate': 1e-3, 'g_learn_rate': 1e-3, 'd_trainer': 'rmsprop', 'g_trainer': 'adam'} trainer = AlternatingTrainer(gan, config) c = tf.constant(1) self.assertTrue('d_loss' in trainer.output_string({'d_loss':c})) self.assertTrue('g_loss' in trainer.output_string({'g_loss':c})) self.assertEqual(len(trainer.output_variables({'a': c, 'b': c})), 2)
def test_create(self): with self.test_session(): gan = mock_gan() loss = LambGanLoss(gan, loss_config) d_loss, g_loss = loss.create() d_shape = gan.ops.shape(d_loss) g_shape = gan.ops.shape(g_loss) self.assertEqual(d_shape, []) self.assertEqual(g_shape, [])
def test_output_string(self): with self.test_session(): gan = mock_gan() gan.create() config = {'d_learn_rate': 1e-3, 'g_learn_rate': 1e-3, 'd_trainer': 'rmsprop', 'g_trainer': 'adam'} trainer = AlternatingTrainer(gan, config) c = tf.constant(1) self.assertTrue('d_loss' in trainer.output_string({'d_loss':c})) self.assertTrue('g_loss' in trainer.output_string({'g_loss':c})) self.assertEqual(len(trainer.output_variables({'a': c, 'b': c})), 2)
def test_create(self): with self.test_session(): gan = mock_gan() gan.create() loss = BoundaryEquilibriumLoss(gan, loss_config) d_loss, g_loss = loss.create() d_shape = gan.ops.shape(d_loss) g_shape = gan.ops.shape(g_loss) self.assertEqual(sum(d_shape), 0) self.assertEqual(sum(g_shape), 0)
def test_new(self): with self.test_session(): try: os.remove('test.json') except Exception: pass gan = mock_gan() args = hc.Config({"size": "1", "steps": 1, "method": "train", "directory": "test"}) cli = hg.CLI(gan, args) cli.new() self.assertTrue(os.path.isfile('test.json'))
def test_config(self): with self.test_session(): config = { 'd_learn_rate': 1e-3, 'g_learn_rate': 1e-3, 'd_trainer': 'rmsprop', 'g_trainer': 'adam' } gan = mock_gan() trainer = AlternatingTrainer(gan, config) self.assertEqual(trainer.config.d_learn_rate, 1e-3)
def test_train(self): with self.test_session(): gan = mock_gan() args = hc.Config({ "size": "1", "steps": 1, "method": "train", "save_every": -1 }) cli = hg.CLI(gan, args) cli.train() self.assertEqual(cli.gan, gan)
def test_create(self): return None #disabled for now with self.test_session(): remove_d_config = hg.Configuration.default() remove_d_config['discriminator'] = None remove_d_config['loss'] = None remove_d_config['trainer'] = None gan = mock_gan(config = remove_d_config) discriminator = AutoencoderDiscriminator(gan, config) gan.encoder = gan.create_component(gan.config.encoder) gan.generator = gan.create_component(gan.config.generator) self.assertEqual(int(net.get_shape()[1]), 32)
def test_sample(self): with self.test_session(): gan = mock_gan() mock_sample = tf.constant(1., shape=[1,1]) multi = MultiComponent(combine='concat', components=[ MockLoss(gan, sample=mock_sample), MockLoss(gan, sample=mock_sample) ]) gan.encoder = multi self.assertEqual(gan.ops.shape(multi.sample), [1,2])
def test_create(self): return None #disabled for now with self.test_session(): remove_d_config = hg.Configuration.default() remove_d_config['discriminator'] = None remove_d_config['loss'] = None remove_d_config['trainer'] = None gan = mock_gan(config=remove_d_config) discriminator = AutoencoderDiscriminator(gan, config) gan.encoder = gan.create_component(gan.config.encoder) gan.generator = gan.create_component(gan.config.generator) self.assertEqual(int(net.get_shape()[1]), 32)
def test_safe_new(self): with self.test_session(): try: os.remove('test.json') except Exception: pass gan = mock_gan() args = hc.Config({"size": "1", "steps": 1, "method": "train", "directory": "test"}) cli = hg.CLI(gan, args) cli.new() with self.assertRaises(ValidationException): cli.new()
def test_proxy_methods(self): with self.test_session(): gan = mock_gan() mock_sample = tf.constant(1., shape=[1, 1]) multi = MultiComponent(combine='concat', components=[ MockLoss(gan, sample=mock_sample), MockLoss(gan, sample=mock_sample) ]) multi.proxy() self.assertEqual(multi.proxy_called, [True, True])
def test_proxy_methods(self): with self.test_session(): gan = mock_gan() mock_sample = tf.constant(1., shape=[1,1]) multi = MultiComponent(combine='concat', components=[ MockLoss(gan, sample=mock_sample), MockLoss(gan, sample=mock_sample) ]) multi.proxy() self.assertEqual(multi.proxy_called, [True, True])
def test_sample(self): with self.test_session(): gan = mock_gan() mock_sample = tf.constant(1., shape=[1, 1]) multi = MultiComponent(combine='concat', components=[ MockLoss(gan, sample=mock_sample), MockLoss(gan, sample=mock_sample) ]) gan.encoder = multi self.assertEqual(gan.ops.shape(multi.sample), [1, 2])
def test_combine_dict(self): with self.test_session(): gan = mock_gan() ops = gan.ops mock_sample = tf.constant(1., shape=[1]) multi = MultiComponent(combine='add', components=[ MockLoss(gan, sample={"a": "b"}), MockLoss(gan, sample={"b": "c"}) ]) self.assertEqual(len(multi.sample), 2) self.assertEqual(multi.sample['a'], 'b') self.assertEqual(multi.sample['b'], 'c')
def test_sample_loss(self): with self.test_session(): gan = mock_gan() ops = gan.ops mock_sample = tf.constant(1., shape=[1]) multi = MultiComponent(combine='add', components=[ MockLoss(gan, sample=[mock_sample, None]), MockLoss(gan, sample=[mock_sample, mock_sample]) ]) self.assertEqual(len(multi.sample), 2) self.assertEqual(ops.shape(multi.sample[0]), [1]) self.assertEqual(ops.shape(multi.sample[1]), [1])
def test_combine_dict(self): with self.test_session(): gan = mock_gan() ops = gan.ops mock_sample = tf.constant(1., shape=[1]) multi = MultiComponent(combine='add', components=[ MockLoss(gan, sample={"a":"b"}), MockLoss(gan, sample={"b":"c"}) ]) self.assertEqual(len(multi.sample), 2) self.assertEqual(multi.sample['a'], 'b') self.assertEqual(multi.sample['b'], 'c')
def test_sample(self): with self.test_session(): gan = mock_gan() args = hc.Config({ "size": "1", "steps": 1, "method": "train", "save_every": -1 }) gan.create() cli = hg.CLI(gan, args) cli.sample('/tmp/test-sample.png') self.assertEqual(cli.gan, gan)
def test_adds_supervised_loss(self): with self.test_session(): gan = mock_gan(y=2) args = hc.Config({ "size": "1", "steps": 1, "method": "train", "save_every": -1, "classloss": True }) cli = hg.CLI(gan, args) cli.add_supervised_loss() self.assertEqual(type(cli.gan.loss), MultiComponent) self.assertEqual(type(cli.gan.loss.components[0]), SupervisedLoss)
def test_new(self): with self.test_session(): try: os.remove('test.json') except Exception: pass gan = mock_gan() args = hc.Config({ "size": "1", "steps": 1, "method": "train", "directory": "test" }) cli = hg.CLI(gan, args) cli.new() self.assertTrue(os.path.isfile('test.json'))
def test_sample_loss(self): with self.test_session(): gan = mock_gan() ops = gan.ops mock_sample = tf.constant(1., shape=[1]) multi = MultiComponent(combine='add', components=[ MockLoss(gan, sample=[mock_sample, None]), MockLoss( gan, sample=[mock_sample, mock_sample]) ]) self.assertEqual(len(multi.sample), 2) self.assertEqual(ops.shape(multi.sample[0]), [1]) self.assertEqual(ops.shape(multi.sample[1]), [1])
def test_safe_new(self): with self.test_session(): try: os.remove('test.json') except Exception: pass gan = mock_gan() args = hc.Config({ "size": "1", "steps": 1, "method": "train", "directory": "test" }) cli = hg.CLI(gan, args) cli.new() with self.assertRaises(ValidationException): cli.new()
def test_config(self): with self.test_session(): gan = mock_gan() sampler = BatchSampler(gan) self.assertEqual(sampler._sample()['generator'].shape[-1], 1)
def test_config(self): with self.test_session(): loss = LambGanLoss(mock_gan(), loss_config) self.assertTrue(loss.config.test)
def test_create_component(self): with self.test_session(): gan = mock_gan() distribution = gan.create_component(gan.config.latent) self.assertEqual(type(distribution), hg.distributions.uniform_distribution.UniformDistribution)
def test_config(self): with self.test_session(): loss = LeastSquaresLoss(mock_gan(), loss_config) self.assertTrue(loss.config.test)
def test_config(self): with self.test_session(): loss = BoundaryEquilibriumLoss(mock_gan(), loss_config) self.assertTrue(loss.config.test)
def test_validate(self): with self.assertRaises(ValidationException): gan = mock_gan() AlternatingTrainer(gan, {})
def test_config(self): with self.test_session(): config = {'d_learn_rate': 1e-3, 'g_learn_rate': 1e-3, 'd_trainer': 'rmsprop', 'g_trainer': 'adam'} gan = mock_gan() trainer = AlternatingTrainer(gan, config) self.assertEqual(trainer.config.d_learn_rate, 1e-3)
def test_hg_gan(self): self.assertIs(type(mock_gan()), hg.gans.standard_gan.StandardGAN)
def test_config(self): with self.test_session(): loss = SupervisedLoss(mock_gan(), loss_config) self.assertTrue(loss.config.test)
def test_can_create_alphagan(self): config = hg.Configuration.load('alpha-default.json') self.assertIs(type(mock_gan(config=config)), hg.gans.alpha_gan.AlphaGAN)
def test_cli(self): with self.test_session(): gan = mock_gan() args = {} cli = hg.CLI(gan, args) self.assertEqual(cli.gan, gan)
def test_config(self): with self.test_session(): return None # disable for now gan = mock_gan() discriminator = PyramidDiscriminator(gan, config) self.assertEqual(discriminator.config.activation, tf.nn.tanh)
def test_can_create_default(self): config = hg.Configuration.load('default.json') self.assertIs(type(mock_gan(config=config)), hg.gans.standard_gan.StandardGAN)