Top

hypergan.samplers.alphagan_random_walk_sampler module

from hypergan.samplers.base_sampler import BaseSampler
import tensorflow as tf
import numpy as np

class AlphaganRandomWalkSampler(BaseSampler):
    def __init__(self, gan, samples_per_row=8):
        BaseSampler.__init__(self, gan, samples_per_row)
        self.z = None
        self.y = None
        self.x = None
        self.step = 0
        self.steps = 8
        self.target = None

    def _sample(self):
        gan = self.gan
        z_t = gan.uniform_encoder.sample
        inputs_t = gan.inputs.x

        if self.z is None:
            self.z = gan.uniform_encoder.sample.eval()/2
            direct = gan.uniform_encoder.sample.eval()[0]/2
            direct = np.reshape(direct, [1, direct.shape[0]])
            self.direction = np.tile(direct, [self.z.shape[0], 1])
            self.input = gan.session.run(gan.inputs.x)

        if self.step > self.steps:
            self.z = np.minimum(self.z+self.direction, 1)
            self.z = np.maximum(self.z, -1)
            self.direction = gan.uniform_encoder.sample.eval()

            self.step = 0

        percent = float(self.step)/self.steps
        z_interp = self.z + self.direction*percent
        z_interp = np.minimum(z_interp, 1)
        z_interp = np.maximum(z_interp, -1)
        self.step+=1

        g=tf.get_default_graph()
        with g.as_default():
            tf.set_random_seed(1)
            return {
                'generator': gan.session.run(gan.uniform_sample, feed_dict={z_t: z_interp, inputs_t: self.input})
            }

Classes

class AlphaganRandomWalkSampler

class AlphaganRandomWalkSampler(BaseSampler):
    def __init__(self, gan, samples_per_row=8):
        BaseSampler.__init__(self, gan, samples_per_row)
        self.z = None
        self.y = None
        self.x = None
        self.step = 0
        self.steps = 8
        self.target = None

    def _sample(self):
        gan = self.gan
        z_t = gan.uniform_encoder.sample
        inputs_t = gan.inputs.x

        if self.z is None:
            self.z = gan.uniform_encoder.sample.eval()/2
            direct = gan.uniform_encoder.sample.eval()[0]/2
            direct = np.reshape(direct, [1, direct.shape[0]])
            self.direction = np.tile(direct, [self.z.shape[0], 1])
            self.input = gan.session.run(gan.inputs.x)

        if self.step > self.steps:
            self.z = np.minimum(self.z+self.direction, 1)
            self.z = np.maximum(self.z, -1)
            self.direction = gan.uniform_encoder.sample.eval()

            self.step = 0

        percent = float(self.step)/self.steps
        z_interp = self.z + self.direction*percent
        z_interp = np.minimum(z_interp, 1)
        z_interp = np.maximum(z_interp, -1)
        self.step+=1

        g=tf.get_default_graph()
        with g.as_default():
            tf.set_random_seed(1)
            return {
                'generator': gan.session.run(gan.uniform_sample, feed_dict={z_t: z_interp, inputs_t: self.input})
            }

Ancestors (in MRO)

Static methods

def __init__(

self, gan, samples_per_row=8)

Initialize self. See help(type(self)) for accurate signature.

def __init__(self, gan, samples_per_row=8):
    BaseSampler.__init__(self, gan, samples_per_row)
    self.z = None
    self.y = None
    self.x = None
    self.step = 0
    self.steps = 8
    self.target = None

def plot(

self, image, filename, save_sample)

Plot an image.

def plot(self, image, filename, save_sample):
    """ Plot an image."""
    image = np.minimum(image, 1)
    image = np.maximum(image, -1)
    image = np.squeeze(image)
    # Scale to 0..255.
    imin, imax = image.min(), image.max()
    image = (image - imin) * 255. / (imax - imin) + .5
    image = image.astype(np.uint8)
    if save_sample:
        try:
            Image.fromarray(image).save(filename)
        except Exception as e:
            print("Warning: could not sample to ", filename, ".  Please check permissions and make sure the path exists")
            print(e)
    GlobalViewer.update(image)

def sample(

self, path, save_samples)

def sample(self, path, save_samples):
    gan = self.gan
    with gan.session.as_default():
        sample = self._sample()
        data = sample['generator']
        width = min(gan.batch_size(), self.samples_per_row)
        stacks = [np.hstack(data[i*width:i*width+width]) for i in range(gan.batch_size()//width)]
        sample_data = np.vstack(stacks)
        self.plot(sample_data, path, save_samples)
        sample_name = 'generator'
        samples = [[sample_data, sample_name]]
        return [{'image':path, 'label':'sample'} for sample_data, sample_filename in samples]

Instance variables

var step

var steps

var target

var x

var y

var z