Skip to content

Instantly share code, notes, and snippets.

@BarclayII
Last active July 22, 2017 22:57
Show Gist options
  • Save BarclayII/ffedd35326727b73bfafa7afff9722f4 to your computer and use it in GitHub Desktop.
Save BarclayII/ffedd35326727b73bfafa7afff9722f4 to your computer and use it in GitHub Desktop.
GAN generating audio. Probably does not work
import tensorflow as TF
import modeltf as model
import numpy as NP
import numpy.random as RNG
import h5py
import argparse
from timer import Timer
parser = argparse.ArgumentParser()
parser.add_argument('--critic_iter', default=5, type=int)
parser.add_argument('--cnn', action='store_true')
parser.add_argument('modelname', type=str)
args = parser.parse_args()
print args.modelname
batch_size = 32
if args.cnn:
g = model.Conv1DGenerator([
(128, 33, 4),
(256, 33, 4),
(512, 33, 5),
# (1024, 33, 2),
# (2048, 33, 5),
])
z = TF.placeholder(TF.float32, shape=(None, None))
z_fixed = RNG.randn(batch_size, 100)
else:
g = model.RNNGenerator()
z = TF.placeholder(TF.float32, shape=(None, None, None))
z_fixed = RNG.randn(batch_size, 8000 // 200, 100)
d = model.Conv1DDiscriminator([
(128, 33, 10),
(256, 33, 5),
(512, 33, 4),
# (1024, 33, 2),
# (2048, 33, 2),
])
x_real = TF.placeholder(TF.float32, shape=(None, None))
x_fake = g.generate(batch_size=batch_size, length=8000)
loss_d = TF.reduce_mean(d.compare(x_real, x_fake))
loss_g = score_fake = TF.reduce_mean(-d.discriminate(x_fake))
score_real = TF.reduce_mean(d.discriminate(x_real))
x = g.generate(z=z)
opt_g = TF.train.AdamOptimizer()
opt_d = TF.train.AdamOptimizer()
train_g = opt_g.apply_gradients(opt_g.compute_gradients(loss_g, var_list=g.model.trainable_weights))
train_d = opt_d.apply_gradients(opt_d.compute_gradients(loss_d, var_list=d.model.trainable_weights))
dataset = h5py.File('dataset.h5')
data = dataset['data']
nsamples = data.shape[0]
n_train_samples = nsamples // 10 * 9
def _dataloader(batch_size, data, lower, upper):
epoch = 1
batch = 0
idx = RNG.permutation(range(lower, upper))
cur = 0
while True:
indices = []
for i in range(batch_size):
if cur == len(idx):
cur = 0
idx = RNG.permutation(range(lower, upper))
epoch += 1
batch = 0
indices.append(idx[cur])
cur += 1
sample = data[sorted(indices)]
yield epoch, batch, NP.array(sample)
batch += 1
dataloader = _dataloader(batch_size, data, 0, n_train_samples)
dataloader_val = _dataloader(batch_size, data, n_train_samples, nsamples)
i = 0
epoch = 1
if __name__ == '__main__':
s = TF.Session()
s.run(TF.global_variables_initializer())
x_gen = s.run(x, feed_dict={z: z_fixed})
assert x_gen.shape[0] == batch_size
assert x_gen.shape[1] == 8000
while True:
_epoch = epoch
i += 1
for _ in range(args.critic_iter):
with Timer.new('load', print_=False):
epoch, batch_id, real_data = dataloader.next()
with Timer.new('train_d', print_=False):
_, loss, c_real, c_fake = s.run([train_d, loss_d, score_real, score_fake], feed_dict={x_real: real_data})
print 'D', epoch, batch_id, loss, c_real, c_fake, Timer.get('load'), Timer.get('train_d')
_, _, real_data = dataloader_val.next()
loss, c_real, c_fake = s.run([loss_d, score_real, score_fake], feed_dict={x_real: real_data})
print 'D-valid', loss, c_real, c_fake
with Timer.new('train_g', print_=False):
_, loss, c_fake = s.run([train_g, loss_g, score_fake])
print 'G', epoch, loss, c_fake, Timer.get('train_g')
if i % 1000 == 0:
print 'Saving...'
x_gen = s.run(x, feed_dict={z: z_fixed})
NP.save('%s%05d.npy' % (args.modelname, i), x_gen)
from __future__ import division
import tensorflow as TF
import keras.backend as K
import keras.layers as KL
import keras.models as KM
import utiltf as util
class RNNGenerator(util.Component):
def __init__(self, frame_size=200, noise_size=100, state_size=100):
super(RNNGenerator, self).__init__()
self._frame_size = frame_size
self._noise_size = noise_size
_z = KL.Input(shape=(None, noise_size))
_lstm = KL.Bidirectional(KL.LSTM(units=state_size, activation='tanh', return_sequences=True))(_z)
_x = KL.TimeDistributed(KL.Dense(frame_size, activation='tanh'))(_lstm)
_x = KL.Lambda(lambda x: K.reshape(x, (K.shape(x)[0], K.shape(x)[1] * K.shape(x)[2])))(_x)
self.model = KM.Model(inputs=_z, outputs=_x)
def generate(self, batch_size=None, length=None, z=None):
if z is None:
_len = length // self._frame_size
_z0 = K.random_normal((batch_size, 1, self._noise_size))
_z1 = K.random_normal((batch_size, 1, self._noise_size))
z = K.concatenate([_z0, K.zeros((batch_size, _len - 2, self._noise_size)), _z1], axis=1)
else:
batch_size = K.shape(z)[0]
length = K.shape(z)[1] * self._frame_size
return self.model(z)
class Conv1DGenerator(util.Component):
def __init__(self, config, noise_size=100):
super(Conv1DGenerator, self).__init__()
_x = KL.Input(shape=(None,))
_x1 = KL.Lambda(lambda x: K.expand_dims(K.expand_dims(x, 1), 3))(_x)
self._multiplier = 1
self._noise_size = noise_size
for num_filters, filter_size, filter_stride in config:
_x1 = KL.Conv2DTranspose(filters=num_filters, kernel_size=(1, filter_size), strides=(1, filter_stride), padding='same', activation='relu')(_x1)
self._multiplier *= filter_stride
_x1 = KL.Conv2DTranspose(filters=1, kernel_size=(1, 1), strides=(1, 1), padding='same', activation='tanh')(_x1)
_pooled = KL.Lambda(lambda x: x[:, 0, :, 0])(_x1)
self.model = KM.Model(inputs=_x, outputs=_pooled)
def generate(self, batch_size=None, length=None, z=None):
if z is None:
z = K.random_normal((batch_size, self._noise_size))
return self.model(z)
class Conv1DDiscriminator(util.Component):
def __init__(self, config):
super(Conv1DDiscriminator, self).__init__()
_x = KL.Input(shape=(None,))
_x1 = KL.Lambda(lambda x: K.expand_dims(x, 2))(_x)
for num_filters, filter_size, filter_stride in config:
_x1 = KL.Conv1D(filters=num_filters, kernel_size=filter_size, strides=filter_stride, padding='same', activation='relu')(_x1)
_pooled = KL.GlobalAvgPool1D()(_x1)
_d = KL.Dense(1)(_pooled)
self.model = KM.Model(inputs=_x, outputs=_d)
def discriminate(self, x):
return self.model(x)[:, 0]
def compare(self, x_real, x_fake, grad_penalty=True, lambda_=10):
d_real = self.discriminate(x_real)
d_fake = self.discriminate(x_fake)
loss = d_fake - d_real
if grad_penalty:
eps = K.random_uniform([K.shape(x_real)[0], 1])
x_inter = eps * x_real + (1 - eps) * x_fake
d_inter = self.discriminate(x_inter)
grads = K.gradients(d_inter, x_inter)[0]
grad_norms = K.sqrt(K.sum(K.square(grads), axis=1))
penalty = K.square(grad_norms - 1)
loss += lambda_ * penalty
return K.mean(loss)
import time
class Timer(object):
timers = {}
def __init__(self, name, print_=False):
self.start = self.end = 0
self.name = name
self.print_ = print_
@classmethod
def new(cls, name, print_=False):
cls.timers[name] = Timer(name, print_)
return cls.timers[name]
def __enter__(self):
self.start = time.time()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.end = time.time()
if self.print_:
print '%s: %.6fs' % (self.name, self.end - self.start)
@classmethod
def get(cls, name):
return ((cls.timers[name].end - cls.timers[name].start)
if name in cls.timers else 0)
@classmethod
def reset(cls):
cls.timers = {}
class Component(object):
def save(self, path):
self.model.save_weights(path)
def load(self, path):
self.model.load_weights(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment