Top

hypergan.samplers.grid_sampler module

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


class GridSampler(BaseSampler):
    def __init__(self, gan, samples_per_row=8):
        BaseSampler.__init__(self, gan, samples_per_row)

    def _sample(self):
        gan = self.gan
        z_t = gan.encoder.z
        #This isn't doing any gridlike stuff.  Need to feed this into feed dict(also check size)
        y = np.linspace(0,1, 6)

        z = np.mgrid[-0.999:0.999:0.6, -0.999:0.999:0.26].reshape(2,-1).T

        return {
            'generator': gan.session.run(gan.generator.sample, feed_dict={z_t: z})
        }

Classes

class GridSampler

class GridSampler(BaseSampler):
    def __init__(self, gan, samples_per_row=8):
        BaseSampler.__init__(self, gan, samples_per_row)

    def _sample(self):
        gan = self.gan
        z_t = gan.encoder.z
        #This isn't doing any gridlike stuff.  Need to feed this into feed dict(also check size)
        y = np.linspace(0,1, 6)

        z = np.mgrid[-0.999:0.999:0.6, -0.999:0.999:0.26].reshape(2,-1).T

        return {
            'generator': gan.session.run(gan.generator.sample, feed_dict={z_t: z})
        }

Ancestors (in MRO)

  • GridSampler
  • hypergan.samplers.base_sampler.BaseSampler
  • builtins.object

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)

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]