hypergan.samplers.aligned_sampler module
from hypergan.samplers.base_sampler import BaseSampler
import tensorflow as tf
import numpy as np
class AlignedSampler(BaseSampler):
def __init__(self, gan):
BaseSampler.__init__(self, gan)
self.xa_v = None
self.xb_v = None
self.created = False
def sample(self, path, sample_to_file):
gan = self.gan
cyca = gan.cyca
cycb = gan.cycb
xa_t = gan.inputs.xa
xba_t = gan.xba
xab_t = gan.xab
xb_t = gan.inputs.xb
sess = gan.session
config = gan.config
if(not self.created):
self.xa_v, self.xb_v = sess.run([xa_t, xb_t])
self.created = True
xab_v, xba_v, samplea, sampleb = sess.run([xab_t, xba_t, cyca, cycb], {xa_t: self.xa_v, xb_t: self.xb_v})
stacks = []
bs = gan.batch_size() // 2
width = 5
for i in range(1):
stacks.append([self.xa_v[i*width+width+j] for j in range(width)])
for i in range(1):
stacks.append([xab_v[i*width+width+j] for j in range(width)])
for i in range(1):
stacks.append([samplea[i*width+width+j] for j in range(width)])
for i in range(1):
stacks.append([self.xb_v[i*width+width+j] for j in range(width)])
for i in range(1):
stacks.append([xba_v[i*width+width+j] for j in range(width)])
for i in range(1):
stacks.append([sampleb[i*width+width+j] for j in range(width)])
#[print(np.shape(s)) for s in stacks]
images = np.vstack([np.hstack(s) for s in stacks])
self.plot(images, path, sample_to_file)
return [{'image': path, 'label': 'tiled x sample'}]
Classes
class AlignedSampler
class AlignedSampler(BaseSampler):
def __init__(self, gan):
BaseSampler.__init__(self, gan)
self.xa_v = None
self.xb_v = None
self.created = False
def sample(self, path, sample_to_file):
gan = self.gan
cyca = gan.cyca
cycb = gan.cycb
xa_t = gan.inputs.xa
xba_t = gan.xba
xab_t = gan.xab
xb_t = gan.inputs.xb
sess = gan.session
config = gan.config
if(not self.created):
self.xa_v, self.xb_v = sess.run([xa_t, xb_t])
self.created = True
xab_v, xba_v, samplea, sampleb = sess.run([xab_t, xba_t, cyca, cycb], {xa_t: self.xa_v, xb_t: self.xb_v})
stacks = []
bs = gan.batch_size() // 2
width = 5
for i in range(1):
stacks.append([self.xa_v[i*width+width+j] for j in range(width)])
for i in range(1):
stacks.append([xab_v[i*width+width+j] for j in range(width)])
for i in range(1):
stacks.append([samplea[i*width+width+j] for j in range(width)])
for i in range(1):
stacks.append([self.xb_v[i*width+width+j] for j in range(width)])
for i in range(1):
stacks.append([xba_v[i*width+width+j] for j in range(width)])
for i in range(1):
stacks.append([sampleb[i*width+width+j] for j in range(width)])
#[print(np.shape(s)) for s in stacks]
images = np.vstack([np.hstack(s) for s in stacks])
self.plot(images, path, sample_to_file)
return [{'image': path, 'label': 'tiled x sample'}]
Ancestors (in MRO)
- AlignedSampler
- hypergan.samplers.base_sampler.BaseSampler
- builtins.object
Static methods
def __init__(
self, gan)
Initialize self. See help(type(self)) for accurate signature.
def __init__(self, gan):
BaseSampler.__init__(self, gan)
self.xa_v = None
self.xb_v = None
self.created = False
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, sample_to_file)
def sample(self, path, sample_to_file):
gan = self.gan
cyca = gan.cyca
cycb = gan.cycb
xa_t = gan.inputs.xa
xba_t = gan.xba
xab_t = gan.xab
xb_t = gan.inputs.xb
sess = gan.session
config = gan.config
if(not self.created):
self.xa_v, self.xb_v = sess.run([xa_t, xb_t])
self.created = True
xab_v, xba_v, samplea, sampleb = sess.run([xab_t, xba_t, cyca, cycb], {xa_t: self.xa_v, xb_t: self.xb_v})
stacks = []
bs = gan.batch_size() // 2
width = 5
for i in range(1):
stacks.append([self.xa_v[i*width+width+j] for j in range(width)])
for i in range(1):
stacks.append([xab_v[i*width+width+j] for j in range(width)])
for i in range(1):
stacks.append([samplea[i*width+width+j] for j in range(width)])
for i in range(1):
stacks.append([self.xb_v[i*width+width+j] for j in range(width)])
for i in range(1):
stacks.append([xba_v[i*width+width+j] for j in range(width)])
for i in range(1):
stacks.append([sampleb[i*width+width+j] for j in range(width)])
#[print(np.shape(s)) for s in stacks]
images = np.vstack([np.hstack(s) for s in stacks])
self.plot(images, path, sample_to_file)
return [{'image': path, 'label': 'tiled x sample'}]
Instance variables
var created
var xa_v
var xb_v