def __init__(self, gan, config, discriminator=None, generator=None): GANComponent.__init__(self, gan, config) self.metrics = {} self.sample = None self.ops = None self.discriminator = discriminator self.generator = generator
def before_step(self, step, feed_dict, depth=0): if self.gan.steps != 1: return defn = self.config.encoder.copy() klass = GANComponent.lookup_function(None, defn['class']) encode = klass(self.gan, defn).cuda() defn = self.config.optimizer.copy() klass = GANComponent.lookup_function(None, defn['class']) del defn["class"] self.optimizer = klass( list(encode.parameters()) + list(self.gan.generator.parameters()), **defn) for i in range(self.config.steps or 1000): self.optimizer.zero_grad() inp = self.gan.inputs.next() e = encode(inp) fake = self.gan.generator(e) loss = ((inp - fake)**2).mean() loss.backward() for p in (list(self.gan.g_parameters()) + list(encode.parameters())): p.requires_grad = True #move = torch_grad(outputs=loss, inputs=list(self.gan.g_parameters())+list(encode.parameters()), retain_graph=True, create_graph=True) #print(list(self.gan.g_parameters())+list(encode.parameters())) self.optimizer.step() if self.config.verbose: print("[autoencode]", i, "loss", loss.item()) if self.config.info and (i % 100) == 0: print("[autoencode]", i, "loss", loss.item())
def __init__(self, config=None, inputs=None, device='/gpu:0', ops_config=None, ops_backend=TensorflowOps, batch_size=None, width=None, height=None, channels=None): """ Initialized a new GAN.""" self.inputs = inputs self.device = device self.ops_backend = ops_backend self.ops_config = ops_config self.created = False self.components = [] self._batch_size = batch_size self._width = width self._height = height self._channels = channels if config == None: config = hg.Configuration.default() # A GAN as a component has a parent of itself # gan.gan.gan.gan.gan.gan GANComponent.__init__(self, self, config)
def __init__(self, gan, config, d_vars=None, g_vars=None, loss=None): GANComponent.__init__(self, gan, config) self.create_called = False self.current_step = 0 self.g_vars = g_vars self.d_vars = d_vars self.loss = loss
def __init__(self, gan, config, d_vars=None, g_vars=None, loss=None, name="BaseTrainer"): self.current_step = 0 self.g_vars = g_vars self.d_vars = d_vars self.loss = loss self.d_shake = None self.g_shake = None self.train_hooks = [] for hook_config in (config.hooks or []): hook_config = hc.lookup_functions(hook_config.copy()) defn = { k: v for k, v in hook_config.items() if k in inspect.getargspec(hook_config['class']).args } defn['gan'] = gan defn['config'] = hook_config defn['trainer'] = self hook = hook_config["class"](**defn) losses = hook.losses() if losses[0] is not None: self.loss.sample[0] += losses[0] if losses[1] is not None: self.loss.sample[1] += losses[1] self.train_hooks.append(hook) GANComponent.__init__(self, gan, config, name=name)
def __init__(self, gan, config, d_vars=None, g_vars=None, name="BaseTrainer"): self.current_step = 0 self.g_vars = g_vars self.d_vars = d_vars self.train_hooks = [] GANComponent.__init__(self, gan, config, name=name)
def __init__(self, config=None, inputs=None, device='/gpu:0', ops_config=None, ops_backend=TensorflowOps, graph=None, batch_size=None, width=None, height=None, channels=None, debug=None, session=None, name="hypergan"): """ Initialized a new GAN.""" self.inputs = inputs self.device = device self.ops_backend = ops_backend self.ops_config = ops_config self.components = [] self._batch_size = batch_size self._width = width self._height = height self._channels = channels self.debug = debug self.name = name self.session = session self.skip_connections = SkipConnections() self.destroy = False if graph is None: graph = tf.get_default_graph() self.graph = graph if config == None: config = hg.Configuration.default() if debug and not isinstance(self.session, tf_debug.LocalCLIDebugWrapperSession): self.session = tf_debug.LocalCLIDebugWrapperSession(self.session) self.session.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan) else: tfconfig = tf.ConfigProto(allow_soft_placement=True) #tfconfig = tf.ConfigProto(log_device_placement=True) tfconfig.gpu_options.allow_growth = True with tf.device(self.device): self.session = self.session or tf.Session(config=tfconfig, graph=graph) self.global_step = tf.Variable(0, trainable=False, name='global_step') self.steps = tf.Variable(0, trainable=False, name='global_step') self.increment_step = tf.assign(self.steps, self.steps + 1) # A GAN as a component has a parent of itself # gan.gan.gan.gan.gan.gan GANComponent.__init__(self, self, config, name=self.name) self.ops.debug = debug
def __init__(self, gan, config, name="BaseGenerator", input=None, reuse=False): self.input = input self.name = name GANComponent.__init__(self, gan, config, name=name, reuse=reuse)
def __init__(self, gan, config, name=None, input=None, reuse=None, features=None): self.input = input self.name = name self.features = features GANComponent.__init__(self, gan, config, name=name, reuse=reuse)
def __init__(self, gan, config, discriminator=None, generator=None, x=None, split=2, d_fake=None, d_real=None, reuse=False, name="BaseLoss"): self.sample = None self.ops = None self.reuse=reuse self.x = x self.d_fake = d_fake self.d_real = d_real self.discriminator = discriminator or gan.discriminator self.generator = generator self.split = split GANComponent.__init__(self, gan, config, name=name)
def __init__(self, gan, config, name=None, input=None, reuse=None, features=None, weights=None, biases=None): GANComponent.__init__(self, gan, config) self.input = input self.name = name self.features = features
def __init__(self, gan, config, name=None, input=None, reuse=None, x=None, g=None): self.input = input self.name = name self.x = x self.g = g GANComponent.__init__(self, gan, config, name=name, reuse=reuse)
def before_step(self, step, feed_dict, depth=0): defn = self.config.optimizer.copy() klass = GANComponent.lookup_function(None, defn['class']) del defn["class"] self.optimizer = klass(self.gan.generator.parameters(), **defn) for i in range(self.config.steps or 1): self.optimizer.zero_grad() fake = self.gan.discriminator(self.gan.generator(self.gan.latent.instance)).mean() real = self.gan.discriminator(self.gan.inputs.sample).mean() loss = self.gan.loss.forward_adversarial_norm(real, fake) if loss == 0.0: if self.config.verbose: print("[match support] No loss") break move = torch_grad(outputs=loss, inputs=self.gan.g_parameters(), retain_graph=True, create_graph=True) if self.config.regularize: move = torch_grad(outputs=(loss+sum([m.abs().sum() for m in move])), inputs=self.gan.g_parameters(), retain_graph=True, create_graph=True) for p, g in zip(self.gan.g_parameters(), move): if p._grad is not None: p._grad.copy_(g) else: pass #print("Missing g") self.optimizer.step() if self.config.verbose: print("[match support]", i, "loss", loss.item()) if self.config.loss_threshold and loss < self.config.loss_threshold: if self.config.info: print("[match support] loss_threshold steps", i, "loss", loss.item()) break if self.config.info and i == self.config.steps-1: print("[match support] loss_threshold steps", i, "loss", loss.item())
def create_optimizer(self, name="optimizer"): defn = getattr(self.config, name) or self.config.optimizer defn = defn.copy() klass = GANComponent.lookup_function(None, defn['class']) del defn["class"] optimizer = self.trainable_gan.create_optimizer(klass, defn) return optimizer
def __init__(self, gan, config): BaseDistribution.__init__(self, gan, config) klass = GANComponent.lookup_function(None, self.config['source']) self.source = klass(gan, config) self.current_channels = config["z"] self.current_width = 1 self.current_height = 1 self.current_input_size = config["z"] self.z = self.source.z
def next(self): sample = self.source.next() if self.z_var is None: self.z_var = Variable(sample, requires_grad=True).cuda() defn = self.config.optimizer.copy() klass = GANComponent.lookup_function(None, defn['class']) del defn["class"] self.optimizer = klass([self.z_var], **defn) z = self.z_var z.data.copy_(sample) z.grad = torch.zeros_like(z) for i in range(self.config.steps or 1): self.optimizer.zero_grad() fake = self.gan.discriminator(self.gan.generator( self.hardtanh(z))).mean() real = self.gan.discriminator(self.gan.inputs.sample).mean() loss = self.gan.loss.forward_adversarial_norm(real, fake) if loss == 0.0: if self.config.verbose: print("[optimize distribution] No loss") break z_move = torch_grad(outputs=loss, inputs=z, retain_graph=True, create_graph=True) z_change = z_move[0].abs().mean() if self.config.z_change_threshold or self.config.verbose: if i == 0: first_z_change = z_change z._grad.copy_(z_move[0]) self.optimizer.step() if self.config.verbose: print("[optimize distribution]", i, "loss", loss.item(), "mean movement", (z - sample).abs().mean().item(), (z_change / first_z_change).item()) if self.config.z_change_threshold and z_change / first_z_change < self.config.z_change_threshold: if self.config.info: print("[optimize distribution] z_change_threshold steps", i, "loss", loss.item(), "mean movement", (z - sample).abs().mean().item(), (z_change / first_z_change).item()) break if self.config.loss_threshold and loss < self.config.loss_threshold: if self.config.info: print("[optimize distribution] loss_threshold steps", i, "loss", loss.item(), "mean movement", (z - sample).abs().mean().item(), (z_change / first_z_change).item()) break if self.config.info and i == self.config.steps - 1: print("[optimize distribution] steps_threshold steps", i, "loss", loss.item(), "mean movement", (z - sample).abs().mean().item()) self.instance = z return z
def __init__(self, config=None, inputs=None, device='/gpu:0', ops_config=None, ops_backend=TensorflowOps, graph=None, batch_size=None, width=None, height=None, channels=None, debug=None, session=None, name="hypergan"): """ Initialized a new GAN.""" self.inputs = inputs self.device = device self.ops_backend = ops_backend self.ops_config = ops_config self.components = [] self._batch_size = batch_size self._width = width self._height = height self._channels = channels self.debug = debug self.name = name self.session = session self.skip_connections = SkipConnections() self.destroy = False if graph is None: graph = tf.get_default_graph() self.graph = graph if config == None: config = hg.Configuration.default() if debug and not isinstance(self.session, tf_debug.LocalCLIDebugWrapperSession): self.session = tf_debug.LocalCLIDebugWrapperSession(self.session) self.session.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan) else: tfconfig = tf.ConfigProto(allow_soft_placement=True) #tfconfig = tf.ConfigProto(log_device_placement=True) tfconfig.gpu_options.allow_growth=True with tf.device(self.device): self.session = self.session or tf.Session(config=tfconfig, graph=graph) self.global_step = tf.Variable(0, trainable=False, name='global_step') self.steps = tf.Variable(0, trainable=False, name='global_step') self.increment_step = tf.assign(self.steps, self.steps+1) # A GAN as a component has a parent of itself # gan.gan.gan.gan.gan.gan GANComponent.__init__(self, self, config, name=self.name) self.ops.debug = debug
def __init__(self, gan, config, discriminator=None, generator=None, x=None, split=2, d_fake=None, d_real=None, reuse=False, name="BaseLoss"): self.sample = None self.ops = None self.reuse = reuse self.x = x self.d_fake = d_fake self.d_real = d_real self.discriminator = discriminator self.generator = generator self.split = split GANComponent.__init__(self, gan, config, name=name)
def __init__(self, gan, config): BaseDistribution.__init__(self, gan, config) klass = GANComponent.lookup_function(None, self.config['source']['class']) self.source = klass(gan, config['source']) self.current_channels = config['source']["z"] self.current_width = 1 self.current_height = 1 self.current_input_size = config['source']["z"] self.z = self.source.z self.hardtanh = torch.nn.Hardtanh() self.relu = torch.nn.ReLU() self.z_var = None
def test_relation_layer(self): component = GANComponent(gan=gan, config={'test': True}) with self.test_session(): constant = tf.zeros([1, 2, 2, 1]) split = component.split_by_width_height(constant) self.assertEqual(len(split), 4) permute = component.permute(split, 2) self.assertEqual(len(permute), 12) rel_layer = component.relation_layer(constant) self.assertEqual(gan.ops.shape(rel_layer), [1, 2, 2, 1]) constant = tf.zeros([1, 4, 4, 1]) split = component.split_by_width_height(constant) self.assertEqual(len(split), 16) permute = component.permute(split, 2) self.assertEqual(len(permute), 240) rel_layer = component.relation_layer(constant) self.assertEqual(gan.ops.shape(rel_layer), [1, 4, 4, 1])
def __init__(self, gan, config, input=None): GANComponent.__init__(self, gan, config) self.input = input
def test_missing_gan(self): with self.assertRaises(ValidationException): GANComponent(config={}, gan=None)
def test_proxy_methods(self): component = GANComponent(gan=gan, config={'test': True}) with self.test_session(): self.assertEqual(component.weights(), []) self.assertEqual(component.biases(), []) self.assertEqual(component.variables(), [])
import tensorflow as tf import hyperchamber as hc import numpy as np from hypergan.gan_component import ValidationException from hypergan.ops import TensorflowOps from hypergan.gan_component import GANComponent from hypergan.multi_component import MultiComponent import hypergan as hg from unittest.mock import MagicMock gan = hg.GAN() component = GANComponent(gan=gan, config={'test': True}) class GanComponentTest(tf.test.TestCase): def test_config(self): with self.test_session(): self.assertEqual(component.config.test, True) def test_validate(self): with self.test_session(): self.assertEqual(component.validate(), []) def test_gan(self): with self.test_session(): self.assertEqual(component.gan, gan) def test_ops(self): with self.test_session(): self.assertEqual(type(component.ops), TensorflowOps)
def create_input(input_config): klass = GANComponent.lookup_function(None, input_config['class']) return klass(input_config)
def __init__(self, gan, config, trainable_gan): self.current_step = 0 self.train_hooks = gan.hooks self.trainable_gan = trainable_gan GANComponent.__init__(self, gan, config)
def __init__(self, gan, config, g=None, x=None, name=None, input=None, reuse=None, features=[], skip_connections=[]): self.x = x self.g = g GANComponent.__init__(self, gan, config, name=name, reuse=reuse)
def create_input(self, blank=False, rank=None): klass = GANComponent.lookup_function(None, self.input_config['class']) self.input_config["blank"]=blank self.input_config["rank"]=rank return klass(self.input_config)