hypergan.samplers.debug_sampler module
from hypergan.samplers.base_sampler import BaseSampler
from hypergan.samplers.began_sampler import BeganSampler
from hypergan.samplers.batch_sampler import BatchSampler
from hypergan.samplers.static_batch_sampler import StaticBatchSampler
from hypergan.samplers.random_walk_sampler import RandomWalkSampler
import tensorflow as tf
import numpy as np
import hypergan as hg
from hypergan.losses.boundary_equilibrium_loss import BoundaryEquilibriumLoss
class DebugSampler(BaseSampler):
def __init__(self, gan, samples_per_row=8):
BaseSampler.__init__(self, gan, samples_per_row)
self.samplers = [
StaticBatchSampler(gan, samples_per_row),
BatchSampler(gan, samples_per_row),
RandomWalkSampler(gan, samples_per_row)
]
if gan.config.loss['class'] == BoundaryEquilibriumLoss:
self.samplers += [BeganSampler(gan, samples_per_row)]
print("GANLOSS", gan.loss.__class__.__name__)
def _sample(self):
samples = [sampler._sample()['generator'] for sampler in self.samplers]
all_samples = np.vstack(samples)
return {
'generator':all_samples
}
Classes
class DebugSampler
class DebugSampler(BaseSampler):
def __init__(self, gan, samples_per_row=8):
BaseSampler.__init__(self, gan, samples_per_row)
self.samplers = [
StaticBatchSampler(gan, samples_per_row),
BatchSampler(gan, samples_per_row),
RandomWalkSampler(gan, samples_per_row)
]
if gan.config.loss['class'] == BoundaryEquilibriumLoss:
self.samplers += [BeganSampler(gan, samples_per_row)]
print("GANLOSS", gan.loss.__class__.__name__)
def _sample(self):
samples = [sampler._sample()['generator'] for sampler in self.samplers]
all_samples = np.vstack(samples)
return {
'generator':all_samples
}
Ancestors (in MRO)
- DebugSampler
- 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)
self.samplers = [
StaticBatchSampler(gan, samples_per_row),
BatchSampler(gan, samples_per_row),
RandomWalkSampler(gan, samples_per_row)
]
if gan.config.loss['class'] == BoundaryEquilibriumLoss:
self.samplers += [BeganSampler(gan, samples_per_row)]
print("GANLOSS", gan.loss.__class__.__name__)
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(np.shape(data)[0]//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 samplers