Top

hypergan.gans.autoencoder_gan module

import importlib
import json
import numpy as np
import os
import sys
import time
import uuid
import copy

from hypergan.discriminators import *
from hypergan.encoders import *
from hypergan.generators import *
from hypergan.inputs import *
from hypergan.samplers import *
from hypergan.trainers import *

import hyperchamber as hc
from hyperchamber import Config
from hypergan.ops import TensorflowOps
import tensorflow as tf
import hypergan as hg

from hypergan.gan_component import ValidationException, GANComponent
from .standard_gan import StandardGAN
from .base_gan import BaseGAN

class AutoencoderGAN(StandardGAN):
    """ 
    """

    def required(self):
        return "generator".split()

    def create(self):
        config = self.config

        d2 = dict(config.discriminator)
        d2['class'] = self.ops.lookup("class:hypergan.discriminators.pyramid_discriminator.PyramidDiscriminator")
        self.encoder = self.create_component(d2)
        self.encoder.ops.describe("encoder")
        self.encoder.create(self.inputs.x)
        self.encoder.z = tf.zeros(0)
        self.trainer = self.create_component(config.trainer)

        StandardGAN.create(self)
        cycloss = tf.reduce_mean(tf.abs(self.inputs.x-self.generator.sample))
        cycloss_lambda = config.cycloss_lambda or 10
        self.loss.sample[1] *= config.g_lambda or 1
        self.loss.sample[1] += cycloss*cycloss_lambda
        self.trainer.create()

        self.session.run(tf.global_variables_initializer())

Classes

class AutoencoderGAN

class AutoencoderGAN(StandardGAN):
    """ 
    """

    def required(self):
        return "generator".split()

    def create(self):
        config = self.config

        d2 = dict(config.discriminator)
        d2['class'] = self.ops.lookup("class:hypergan.discriminators.pyramid_discriminator.PyramidDiscriminator")
        self.encoder = self.create_component(d2)
        self.encoder.ops.describe("encoder")
        self.encoder.create(self.inputs.x)
        self.encoder.z = tf.zeros(0)
        self.trainer = self.create_component(config.trainer)

        StandardGAN.create(self)
        cycloss = tf.reduce_mean(tf.abs(self.inputs.x-self.generator.sample))
        cycloss_lambda = config.cycloss_lambda or 10
        self.loss.sample[1] *= config.g_lambda or 1
        self.loss.sample[1] += cycloss*cycloss_lambda
        self.trainer.create()

        self.session.run(tf.global_variables_initializer())

Ancestors (in MRO)

  • AutoencoderGAN
  • hypergan.gans.standard_gan.StandardGAN
  • hypergan.gans.base_gan.BaseGAN
  • hypergan.gan_component.GANComponent
  • builtins.object

Static methods

def __init__(

self, *args, **kwargs)

Initialized a new GAN.

def __init__(self, *args, **kwargs):
    BaseGAN.__init__(self, *args, **kwargs)
    self.discriminator = None
    self.encoder = None
    self.generator = None
    self.loss = None
    self.trainer = None
    self.session = None

def batch_size(

self)

def batch_size(self):
    if self._batch_size:
        return self._batch_size
    if self.inputs == None:
        raise ValidationException("gan.batch_size() requested but no inputs provided")
    return self.ops.shape(self.inputs.x)[0]

def biases(

self)

Biases of the GAN component.

def biases(self):
    """
        Biases of the GAN component.
    """
    return self.ops.biases

def channels(

self)

def channels(self):
    if self._channels:
        return self._channels
    if self.inputs == None:
        raise ValidationException("gan.channels() requested but no inputs provided")
    return self.ops.shape(self.inputs.x)[-1]

def create(

self)

def create(self):
    config = self.config
    d2 = dict(config.discriminator)
    d2['class'] = self.ops.lookup("class:hypergan.discriminators.pyramid_discriminator.PyramidDiscriminator")
    self.encoder = self.create_component(d2)
    self.encoder.ops.describe("encoder")
    self.encoder.create(self.inputs.x)
    self.encoder.z = tf.zeros(0)
    self.trainer = self.create_component(config.trainer)
    StandardGAN.create(self)
    cycloss = tf.reduce_mean(tf.abs(self.inputs.x-self.generator.sample))
    cycloss_lambda = config.cycloss_lambda or 10
    self.loss.sample[1] *= config.g_lambda or 1
    self.loss.sample[1] += cycloss*cycloss_lambda
    self.trainer.create()
    self.session.run(tf.global_variables_initializer())

def create_component(

self, defn, *args, **kw_args)

def create_component(self, defn, *args, **kw_args):
    if defn == None:
        return None
    if defn['class'] == None:
        raise ValidationException("Component definition is missing '" + name + "'")
    gan_component = defn['class'](self, defn, *args, **kw_args)
    self.components.append(gan_component)
    return gan_component

def create_ops(

self, config)

Create the ops object as self.ops. Also looks up config

def create_ops(self, config):
    """
    Create the ops object as `self.ops`.  Also looks up config
    """
    if self.gan is None:
        return
    if self.gan.ops_backend is None:
        return
    self.ops = self.gan.ops_backend(config=self.config, device=self.gan.device)
    self.config = self.gan.ops.lookup(config)

def fully_connected_from_list(

self, nets)

def fully_connected_from_list(self, nets):
    results = []
    ops = self.ops
    for net, net2 in nets:
        net = ops.concat([net, net2], axis=3)
        shape = ops.shape(net)
        bs = shape[0]
        net = ops.reshape(net, [bs, -1])
        features = ops.shape(net)[1]
        net = ops.linear(net, features)
        #net = self.layer_regularizer(net)
        net = ops.lookup('lrelu')(net)
        #net = ops.linear(net, features)
        net = ops.reshape(net, shape)
        results.append(net)
    return results

def get_config_value(

self, symbol)

def get_config_value(self, symbol):
    if symbol in self.config:
        config = hc.Config(hc.lookup_functions(self.config[symbol]))
        return config
    return None

def height(

self)

def height(self):
    if self._height:
        return self._height
    if self.inputs == None:
        raise ValidationException("gan.height() requested but no inputs provided")
    return self.ops.shape(self.inputs.x)[1]

def layer_regularizer(

self, net)

def layer_regularizer(self, net):
    symbol = self.config.layer_regularizer
    op = self.gan.ops.lookup(symbol)
    if op:
        net = op(self, net)
    return net

def load(

self, save_file)

def load(self, save_file):
    save_file = os.path.expanduser(save_file)
    if os.path.isfile(save_file) or os.path.isfile(save_file + ".index" ):
        print("[hypergan] |= Loading network from "+ save_file)
        dir = os.path.dirname(save_file)
        print("[hypergan] |= Loading checkpoint from "+ dir)
        ckpt = tf.train.get_checkpoint_state(os.path.expanduser(dir))
        if ckpt and ckpt.model_checkpoint_path:
            saver = tf.train.Saver()
            saver.restore(self.session, save_file)
            loadedFromSave = True
            return True
        else:
            return False
    else:
        return False

def permute(

self, nets, k)

def permute(self, nets, k):
    return list(itertools.permutations(nets, k))

def relation_layer(

self, net)

def relation_layer(self, net):
    ops = self.ops
    #hack
    shape = ops.shape(net)
    input_size = shape[1]*shape[2]*shape[3]
    netlist = self.split_by_width_height(net)
    permutations = self.permute(netlist, 2)
    permutations = self.fully_connected_from_list(permutations)
    net = ops.concat(permutations, axis=3)
    #hack
    bs = ops.shape(net)[0]
    net = ops.reshape(net, [bs, -1])
    net = ops.linear(net, input_size)
    net = ops.reshape(net, shape)
    return net

def required(

self)

Return a list of required config strings and a ValidationException will be thrown if any are missing.

Example: python class MyComponent(GANComponent): def required(self): "learn rate is required" ["learn_rate"]

def required(self):
    return "generator".split()

def reuse(

self, net)

def reuse(self, net):
    self.ops.reuse()
    net = self.build(net)
    self.ops.stop_reuse()
    return net

def save(

self, save_file)

def save(self, save_file):
    print("[hypergan] Saving network to ", save_file)
    os.makedirs(os.path.expanduser(os.path.dirname(save_file)), exist_ok=True)
    saver = tf.train.Saver()
    saver.save(self.session, save_file)

def split_batch(

self, net, count=2)

Discriminators return stacked results (on axis 0).

This splits the results. Returns [d_real, d_fake]

def split_batch(self, net, count=2):
    """ 
    Discriminators return stacked results (on axis 0).  
    
    This splits the results.  Returns [d_real, d_fake]
    """
    ops = self.ops or self.gan.ops
    s = ops.shape(net)
    bs = s[0]
    nets = []
    net = ops.reshape(net, [bs, -1])
    start = [0 for x in ops.shape(net)]
    for i in range(count):
        size = [bs//count] + [x for x in ops.shape(net)[1:]]
        nets.append(ops.slice(net, start, size))
        start[0] += bs//count
    return nets

def split_by_width_height(

self, net)

def split_by_width_height(self, net):
    elems = []
    ops = self.gan.ops
    shape = ops.shape(net)
    bs = shape[0]
    height = shape[1]
    width = shape[2]
    for i in range(width):
        for j in range(height):
            elems.append(ops.slice(net, [0, i, j, 0], [bs, 1, 1, -1]))
    return elems

def step(

self, feed_dict={})

def step(self, feed_dict={}):
    if not self.created:
        self.create()
    if self.trainer == None:
        raise ValidationException("gan.trainer is missing.  Cannot train.")
    return self.trainer.step(feed_dict)

def validate(

self)

Validates a GANComponent. Return an array of error messages. Empty array [] means success.

def validate(self):
    """
    Validates a GANComponent.  Return an array of error messages. Empty array `[]` means success.
    """
    errors = []
    required = self.required()
    for argument in required:
        if(self.config.__getattr__(argument) == None):
            errors.append("`"+argument+"` required")
    if(self.gan is None):
        errors.append("GANComponent constructed without GAN")
    return errors

def variables(

self)

All variables associated with this component.

def variables(self):
    """
        All variables associated with this component.
    """
    return self.ops.variables()

def weights(

self)

The weights of the GAN component.

def weights(self):
    """
        The weights of the GAN component.
    """
    return self.ops.weights

def width(

self)

def width(self):
    if self._width:
        return self._width
    if self.inputs == None:
        raise ValidationException("gan.width() requested but no inputs provided")
    return self.ops.shape(self.inputs.x)[2]