Пример #1
0
 def __init__(self):
     self.mnist = None
     #self.model = CPPNVAE()
     self.model = CPPNVAE(batch_size=1, x_dim=s, y_dim=s, c_dim=3)
     # load previously trained model
     self.model.load_model('save')
     self.z = self.generate_z()
Пример #2
0
def gen(args):
    tf_filepath = "/data/ai-datasets/201-FFHQ/tfrecords/face256.tfrecords"
    img = get_input_data(tf_filepath, seq_len, args.batch_size, 10)

    learning_rate = args.learning_rate
    learning_rate_d = args.learning_rate_d
    learning_rate_vae = args.learning_rate_vae
    batch_size = args.batch_size
    training_epochs = args.training_epochs
    display_step = args.display_step
    checkpoint_step = args.checkpoint_step # save training results every check point step
    beta1 = args.beta1
    keep_prob = args.keep_prob
    dirname = 'save'
    if not os.path.exists(dirname):
        os.makedirs(dirname)

    with open(os.path.join(dirname, 'config.pkl'), 'w') as f:
        cPickle.dump(args, f)

    x_dim = s
    y_dim = s
    z_dim = 32
    scale = args.scale
    cppn = CPPNVAE(batch_size=batch_size, 
            learning_rate = learning_rate, 
            learning_rate_d = learning_rate_d, 
            learning_rate_vae = learning_rate_vae, 
            beta1 = beta1, 
            keep_prob = keep_prob,
            x_dim = x_dim,
            y_dim = y_dim,
            c_dim = 3, 
            net_depth_g = 3
            )

    # load previously trained model if appilcable
    ckpt = tf.train.get_checkpoint_state(dirname)
    cppn.load_model(dirname)

    batch_images = cppn.sess.run(img)/255.
    batch_images = np.reshape(batch_images, [batch_size, 256, 256, 3])
    batch_images = np.array([cv2.resize(img, (s, s)) for img in batch_images])

    
    #fake = cppn.generate(x_dim=int(x_dim*scale), y_dim=(y_dim*scale), scale=scale)
    print('scale: ', scale)
    fake = cppn.generate(x_dim=x_dim, y_dim=y_dim, scale=scale)
    im = Image.fromarray(np.uint8(fake[0] * 255))
    outpath = "graph/00_pred.png" 
    im.save(outpath)
 def __init__(self):
     self.mnist = None
     self.model = CPPNVAE()
     self.model.load_model('save')
     self.z = self.generate_z()
class Sampler():
    def __init__(self):
        self.mnist = None
        self.model = CPPNVAE()
        self.model.load_model('save')
        self.z = self.generate_z()

    def get_random_mnist(self, with_label=False):
        if self.mnist == None:
            self.mnist = read_data_sets()
        if with_label == True:
            data, label = self.mnist.next_batch(1, with_label)
            return data[0], label[0]
        return self.mnist.next_batch(1)[0]

    def get_random_specific_mnist(self, label=2):
        m, l = self.get_random_mnist(with_label=True)
        for i in range(100):
            if l == label:
                break
            m, l = self.get_random_mnist(with_label=True)
        return m

    def generate_random_label(self, label):
        m = self.get_random_specific_mnist(label)
        self.show_image(m)
        self.show_image_from_z(self.encode(m))

    def generate_z(self):
        z = np.random.normal(size=self.model.z_dim).astype(np.float32)
        return z

    def encode(self, mnist_data):
        new_shape = [1] + list(mnist_data.shape)
        return self.model.encode(np.reshape(mnist_data, new_shape))

    def generate(self, z=None, x_dim=512, y_dim=512, scale=8.0):
        if z is None:
            z = self.generate_z()
        else:
            z = np.reshape(z, (1, self.model.z_dim))
        self.z = z
        return self.model.generate(z, x_dim, y_dim, scale)[0]

    def show_image(self, image_data):
        '''
    image_data is a tensor, in [height width depth]
    image_data is NOT the PIL.Image class
    '''
        plt.subplot(1, 1, 1)
        y_dim = image_data.shape[0]
        x_dim = image_data.shape[1]
        c_dim = self.model.c_dim
        if c_dim > 1:
            plt.imshow(image_data, interpolation='nearest')
        else:
            plt.imshow(image_data.reshape(y_dim, x_dim),
                       cmap='Greys',
                       interpolation='nearest')
        plt.axis('off')
        plt.show()

    def show_image_from_z(self, z):
        self.show_image(self.generate(z))

    def save_png(self, image_data, filename, specific_size=None):
        img_data = np.array(1 - image_data)
        y_dim = image_data.shape[0]
        x_dim = image_data.shape[1]
        c_dim = self.model.c_dim
        if c_dim > 1:
            img_data = np.array(img_data.reshape(
                (y_dim, x_dim, c_dim)) * 255.0,
                                dtype=np.uint8)
        else:
            img_data = np.array(img_data.reshape((y_dim, x_dim)) * 255.0,
                                dtype=np.uint8)
        im = Image.fromarray(img_data)
        if specific_size != None:
            im = im.resize(specific_size)
        im.save(filename)

    def to_image(self, image_data):
        # convert to PIL.Image format from np array (0, 1)
        img_data = np.array(1 - image_data)
        y_dim = image_data.shape[0]
        x_dim = image_data.shape[1]
        c_dim = self.model.c_dim
        if c_dim > 1:
            img_data = np.array(img_data.reshape(
                (y_dim, x_dim, c_dim)) * 255.0,
                                dtype=np.uint8)
        else:
            img_data = np.array(img_data.reshape((y_dim, x_dim)) * 255.0,
                                dtype=np.uint8)
        im = Image.fromarray(img_data)
        return im

    def morph(self,
              z1,
              z2,
              n_total_frame=10,
              x_dim=512,
              y_dim=512,
              scale=8.0,
              sinusoid=False):
        '''
    returns a list of img_data to represent morph between z1 and z2
    default to linear morph, but can try sinusoid for more time near the anchor pts
    n_total_frame must be >= 2, since by definition there's one frame for z1 and z2
    '''
        delta_z = 1.0 / (n_total_frame - 1)
        diff_z = (z2 - z1)
        img_data_array = []
        for i in range(n_total_frame):
            percentage = delta_z * float(i)
            factor = percentage
            if sinusoid == True:
                factor = np.sin(percentage * np.pi / 2)
            z = z1 + diff_z * factor
            print "processing image ", i
            img_data_array.append(self.generate(z, x_dim, y_dim, scale))
        return img_data_array

    def save_anim_gif(self, img_data_array, filename, duration=0.1):
        '''
    this saves an animated gif given a list of img_data (numpy arrays)
    '''
        images = []
        for i in range(len(img_data_array)):
            images.append(self.to_image(img_data_array[i]))
        writeGif(filename, images, duration=duration)
Пример #5
0
 def __init__(self):
   self.mnist = None
   self.model = CPPNVAE()
   self.z = self.generate_z()
Пример #6
0
class Sampler():
  def __init__(self):
    self.mnist = None
    self.model = CPPNVAE()
    self.z = self.generate_z()
  def load_model(self):
    self.model.load_model('save')
  def get_random_mnist(self, with_label = False):
    if self.mnist == None:
      self.mnist = read_data_sets()
    if with_label == True:
      data, label = self.mnist.next_batch(1, with_label)
      return data[0], label[0]
    return self.mnist.next_batch(1)[0]
  def get_random_specific_mnist(self, label = 2):
    m, l = self.get_random_mnist(with_label = True)
    for i in range(100):
      if l == label:
        break
      m, l = self.get_random_mnist(with_label = True)
    return m
  def generate_random_label(self, label):
    m = self.get_random_specific_mnist(label)
    self.show_image(m)
    self.show_image_from_z(self.encode(m))
  def generate_z(self):
    z = np.random.normal(size=self.model.z_dim).astype(np.float32)
    return z
  def encode(self, mnist_data):
    new_shape = [1]+list(mnist_data.shape)
    return self.model.encode(np.reshape(mnist_data, new_shape))
  def generate(self, z=None, x_dim=512, y_dim=512, scale = 8.0):
    if z is None:
      z = self.generate_z()
    else:
      z = np.reshape(z, (1, self.model.z_dim))
    self.z = z
    return self.model.generate(z, x_dim, y_dim, scale)[0]
  def show_image(self, image_data):
    '''
    image_data is a tensor, in [height width depth]
    image_data is NOT the PIL.Image class
    '''
    plt.subplot(1, 1, 1)
    y_dim = image_data.shape[0]
    x_dim = image_data.shape[1]
    c_dim = self.model.c_dim
    if c_dim > 1:
      plt.imshow(image_data, interpolation='nearest')
    else:
      plt.imshow(image_data.reshape(y_dim, x_dim), cmap='Greys', interpolation='nearest')
    plt.axis('off')
    plt.show()
  def show_image_from_z(self, z):
    self.show_image(self.generate(z))
  def save_png(self, image_data, filename, specific_size = None):
    img_data = np.array(1-image_data)
    y_dim = image_data.shape[0]
    x_dim = image_data.shape[1]
    c_dim = self.model.c_dim
    if c_dim > 1:
      img_data = np.array(img_data.reshape((y_dim, x_dim, c_dim))*255.0, dtype=np.uint8)
    else:
      img_data = np.array(img_data.reshape((y_dim, x_dim))*255.0, dtype=np.uint8)
    im = Image.fromarray(img_data)
    if specific_size != None:
      im = im.resize(specific_size)
    im.save(filename)
  def to_image(self, image_data):
    # convert to PIL.Image format from np array (0, 1)
    img_data = np.array(1-image_data)
    y_dim = image_data.shape[0]
    x_dim = image_data.shape[1]
    c_dim = self.model.c_dim
    if c_dim > 1:
      img_data = np.array(img_data.reshape((y_dim, x_dim, c_dim))*255.0, dtype=np.uint8)
    else:
      img_data = np.array(img_data.reshape((y_dim, x_dim))*255.0, dtype=np.uint8)
    im = Image.fromarray(img_data)
    return im
  def morph(self, z1, z2, n_total_frame = 10, x_dim = 512, y_dim = 512, scale = 8.0, sinusoid = False):
    '''
    returns a list of img_data to represent morph between z1 and z2
    default to linear morph, but can try sinusoid for more time near the anchor pts
    n_total_frame must be >= 2, since by definition there's one frame for z1 and z2
    '''
    delta_z = 1.0 / (n_total_frame-1)
    diff_z = (z2-z1)
    img_data_array = []
    for i in range(n_total_frame):
      percentage = delta_z*float(i)
      factor = percentage
      if sinusoid == True:
        factor = np.sin(percentage*np.pi/2)
      z = z1 + diff_z*factor
      print "processing image ", i
      img_data_array.append(self.generate(z, x_dim, y_dim, scale))
    return img_data_array
  def save_anim_gif(self, img_data_array, filename, duration = 0.1):
    '''
    this saves an animated gif given a list of img_data (numpy arrays)
    '''
    images = []
    for i in range(len(img_data_array)):
      images.append(self.to_image(img_data_array[i]))
    writeGif(filename, images, duration = duration)
Пример #7
0
def train(args):

  learning_rate_g = args.learning_rate_g
  learning_rate_d = args.learning_rate_d
  learning_rate_vae = args.learning_rate_vae
  batch_size = args.batch_size
  training_epochs = args.training_epochs
  display_step = args.display_step
  checkpoint_step = args.checkpoint_step # save training results every check point step
  beta1 = args.beta1
  keep_prob = args.keep_prob
  dirname = 'save'
  if not os.path.exists(dirname):
    os.makedirs(dirname)

  with open(os.path.join(dirname, 'config.pkl'), 'w') as f:
    cPickle.dump(args, f)

  mnist = read_data_sets()
  n_samples = mnist.num_examples

  cppnvae = CPPNVAE(batch_size=batch_size, learning_rate_g = learning_rate_g, learning_rate_d = learning_rate_d, learning_rate_vae = learning_rate_vae, beta1 = beta1, keep_prob = keep_prob)

  # load previously trained model if appilcable
  ckpt = tf.train.get_checkpoint_state(dirname)
  if ckpt:
    cppnvae.load_model(dirname)

  counter = 0

  # Training cycle
  for epoch in range(training_epochs):
    avg_d_loss = 0.
    avg_d_loss_real = 0.
    avg_d_loss_fake = 0.
    avg_q_loss = 0.
    avg_vae_loss = 0.
    avg_d_real_accuracy = 0.
    avg_d_fake_accuracy = 0.
    avg_g_accuracy = 0.
    mnist.shuffle_data()
    total_batch = int(n_samples / batch_size)
    # Loop over all batches
    for i in range(total_batch):
      batch_images, batch_labels = mnist.next_batch(batch_size, with_label = True) # obtain training labels

      d_loss, g_loss, vae_loss, n_operations, d_real_accuracy, d_fake_accuracy, g_accuracy, d_loss_real, d_loss_fake = cppnvae.partial_train(batch_images, batch_labels)

      assert( vae_loss < 1000000 ) # make sure it is not NaN or Inf
      assert( d_loss < 1000000 ) # make sure it is not NaN or Inf
      assert( g_loss < 1000000 ) # make sure it is not NaN or Inf
      assert( d_loss_real < 1000000 ) # make sure it is not NaN or Inf
      assert( d_loss_fake < 1000000 ) # make sure it is not NaN or Inf
      assert( d_real_accuracy < 1000000 ) # make sure it is not NaN or Inf
      assert( d_fake_accuracy < 1000000 ) # make sure it is not NaN or Inf
      assert( g_accuracy < 1000000 ) # make sure it is not NaN or Inf

      # Display logs per epoch step
      if (counter+1) % display_step == 0:
        print "Sample:", '%d' % ((i+1)*batch_size), " Epoch:", '%d' % (epoch), \
              "d_loss=", "{:.4f}".format(d_loss), \
              "d_real=", "{:.4f}".format(d_loss_real), \
              "d_fake=", "{:.4f}".format(d_loss_fake), \
              "g_loss=", "{:.4f}".format(g_loss), \
              "vae_loss=", "{:.4f}".format(vae_loss), \
              "d_real_accuracy=", "{:.2f}".format(d_real_accuracy), \
              "d_fake_accuracy=", "{:.2f}".format(d_fake_accuracy), \
              "g_accuracy=", "{:.2f}".format(g_accuracy), \
              "n_op=", '%d' % (n_operations)
      counter += 1
      # Compute average loss
      avg_d_loss += d_loss / n_samples * batch_size
      avg_d_loss_real += d_loss_real / n_samples * batch_size
      avg_d_loss_fake += d_loss_fake / n_samples * batch_size
      avg_q_loss += g_loss / n_samples * batch_size
      avg_vae_loss += vae_loss / n_samples * batch_size
      avg_d_real_accuracy += d_real_accuracy / n_samples * batch_size
      avg_d_fake_accuracy += d_fake_accuracy / n_samples * batch_size
      avg_g_accuracy += g_accuracy / n_samples * batch_size

    # Display logs per epoch step
    if epoch >= 0:
      print "Epoch:", '%04d' % (epoch), \
            "avg_d_loss=", "{:.6f}".format(avg_d_loss), \
            "avg_d_real=", "{:.6f}".format(avg_d_loss_real), \
            "avg_d_fake=", "{:.6f}".format(avg_d_loss_fake), \
            "avg_q_loss=", "{:.6f}".format(avg_q_loss), \
            "d_real_accuracy=", "{:.2f}".format(avg_d_real_accuracy), \
            "d_fake_accuracy=", "{:.2f}".format(avg_d_fake_accuracy), \
            "g_accuracy=", "{:.2f}".format(avg_g_accuracy), \
            "avg_vae_loss=", "{:.6f}".format(avg_vae_loss)

    # save model
    if epoch >= 0 and epoch % checkpoint_step == 0:
      checkpoint_path = os.path.join('save', 'model.ckpt')
      cppnvae.save_model(checkpoint_path, epoch)
      print "model saved to {}".format(checkpoint_path)

  # save model one last time, under zero label to denote finish.
  cppnvae.save_model(checkpoint_path, 0)
 def __init__(self):
   self.cifar = None
   self.model = CPPNVAE()
   self.reload_model()
   self.z = self.generate_z()
import os
from glob import glob
import tensorflow as tf
import numpy as np
import tensorflow_wav
from model import CPPNVAE

model = CPPNVAE(batch_size=1)
model.load_model('save')
print("Loaded")

data = glob(os.path.join("./training", "*.wav"))
sample_file = data[0]
sample = tensorflow_wav.get_wav(sample_file)


def new_z():
    return np.random.normal(size=(1, 32)).astype(np.float32)


print("Generating")

bitrate = 4096
t_dims = [bitrate, bitrate * 2]
scales = [8.0, 16.0, 24.0]
for t_dim in t_dims:
    for scale in scales:
        wavdata = [
            model.generate(new_z(), t_dim=t_dim, scale=scale)
            for i in range(10)
        ]
Пример #10
0
def train(args):

  learning_rate = args.learning_rate
  learning_rate_d = args.learning_rate_d
  learning_rate_vae = args.learning_rate_vae
  batch_size = args.batch_size
  training_epochs = args.training_epochs
  display_step = args.display_step
  checkpoint_step = args.checkpoint_step # save training results every check point step
  beta1 = args.beta1
  keep_prob = args.keep_prob
  dirname = 'save'
  if not os.path.exists(dirname):
    os.makedirs(dirname)


  cppnvae = CPPNVAE(batch_size=batch_size, learning_rate = learning_rate, learning_rate_d = learning_rate_d, learning_rate_vae = learning_rate_vae, beta1 = beta1, keep_prob = keep_prob)

  # load previously trained model if appilcabl
  ckpt = tf.train.get_checkpoint_state(dirname)
  if ckpt:
    cppnvae.load_model(dirname)

  counter = 0

  # Training cycle
  for epoch in range(training_epochs):
    batch_files = glob(os.path.join("./training", "*.wav"))
    np.random.shuffle(batch_files)
    avg_d_loss = 0.
    avg_q_loss = 0.
    avg_vae_loss = 0.
    def get_wav_content(files):
        for filee in files:
            print("Yielding ", filee)
            try:
                yield tensorflow_wav.get_wav(filee)
            except Exception as e:
                print("Could not load ", filee, e)


    for filee in get_wav_content(batch_files):
      data = filee["data"]
      print("min", np.min(data), np.max(data))

      n_samples = len(data)
      samples_per_batch=batch_size * cppnvae.t_dim
      total_batch = int(n_samples / samples_per_batch)
        


      try:

        # Loop over all batches
        for i in range(total_batch):
          batch_audio =data[(i*samples_per_batch):((i+1)*samples_per_batch)]
          batch_audio = np.reshape(batch_audio, (batch_size, cppnvae.t_dim, 1))
          batch_audio = np.dot(np.array(batch_audio, np.float32), 1.0/32767)
  
          d_loss, g_loss, vae_loss, n_operations = cppnvae.partial_train(batch_audio)
  
  
          # Display logs per epoch step
          if (counter+1) % display_step == 0:
            print("Sample:", '%d' % ((i+1)*batch_size), " Epoch:", '%d' % (epoch), \
                  "d_loss=", "{:.4f}".format(d_loss), \
                  "g_loss=", "{:.4f}".format(g_loss), \
                  "vae_loss=", "{:.4f}".format(vae_loss), \
                  "n_op=", '%d' % (n_operations))
  
          assert( vae_loss < 1000000 ) # make sure it is not NaN or Inf
          assert( d_loss < 1000000 ) # make sure it is not NaN or Inf
          assert( g_loss < 1000000 ) # make sure it is not NaN or Inf
          counter += 1
          # Compute average loss
          avg_d_loss += d_loss / n_samples * batch_size
          avg_q_loss += g_loss / n_samples * batch_size
          avg_vae_loss += vae_loss / n_samples * batch_size
          
          if(counter % 30 == 0):
            # save model
            checkpoint_path = os.path.join('save', 'model.ckpt')
            cppnvae.save_model(checkpoint_path, epoch)
            print("model saved to {}".format(checkpoint_path))


      except:
          print("Oh shit we diverged. Reloading and retrying different file")
          # load previously trained model if appilcabl
          ckpt = tf.train.get_checkpoint_state(dirname)
          if ckpt:
            cppnvae.load_model(dirname)

      finally:
          print("Next Song")
    # Display logs per epoch step
    if epoch >= 0:
      print("Epoch:", '%04d' % (epoch), \
            "avg_d_loss=", "{:.6f}".format(avg_d_loss), \
            "avg_q_loss=", "{:.6f}".format(avg_q_loss), \
            "avg_vae_loss=", "{:.6f}".format(avg_vae_loss))


  # save model one last time, under zero label to denote finish.
  cppnvae.save_model(checkpoint_path, 0)
 def __init__(self):
     self.cifar = None
     self.model = CPPNVAE()
     self.reload_model()
     self.z = self.generate_z()