From d36c8c88fc435aa4372e6c5564f0ff12fc7120e9 Mon Sep 17 00:00:00 2001 From: nunzip Date: Fri, 8 Mar 2019 01:34:10 +0000 Subject: add variable dropout --- cgan.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'cgan.py') diff --git a/cgan.py b/cgan.py index 6406244..68bb2cc 100755 --- a/cgan.py +++ b/cgan.py @@ -15,7 +15,7 @@ from tqdm import tqdm import numpy as np class CGAN(): - def __init__(self, dense_layers = 3): + def __init__(self, dense_layers = 3, dropout=0.4): # Input shape self.img_rows = 28 self.img_cols = 28 @@ -24,6 +24,7 @@ class CGAN(): self.num_classes = 10 self.latent_dim = 100 self.dense_layers = dense_layers + self.dropout = dropout optimizer = Adam(0.0002, 0.5) @@ -87,10 +88,10 @@ class CGAN(): model.add(LeakyReLU(alpha=0.2)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) - model.add(Dropout(0.4)) + model.add(Dropout(self.dropout)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) - model.add(Dropout(0.4)) + model.add(Dropout(self.dropout)) model.add(Dense(1, activation='sigmoid')) #model.summary() -- cgit v1.2.3-54-g00ecf From 240be62b504dbdcb0cadcaf313d111eefe9ceea0 Mon Sep 17 00:00:00 2001 From: nunzip Date: Fri, 8 Mar 2019 12:17:11 +0000 Subject: Fix issue with produced labels --- cdcgan.py | 6 +++--- cgan.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'cgan.py') diff --git a/cdcgan.py b/cdcgan.py index dba6737..8d59a03 100755 --- a/cdcgan.py +++ b/cdcgan.py @@ -229,9 +229,9 @@ class CDCGAN(): labels_val = np.zeros(5000).reshape(-1, 1) for i in range(10): - labels_train[i*5500:] = i - labels_test[i*1000:] = i - labels_val[i*500:] = i + labels_train[i*5500:-1] = i + labels_test[i*1000:-1] = i + labels_val[i*500:-1] = i train_data = self.generator.predict([noise_train, labels_train]) test_data = self.generator.predict([noise_test, labels_test]) diff --git a/cgan.py b/cgan.py index 68bb2cc..d27b11b 100755 --- a/cgan.py +++ b/cgan.py @@ -204,9 +204,9 @@ class CGAN(): labels_val = np.zeros(5000).reshape(-1, 1) for i in range(10): - labels_train[i*5500:] = i - labels_test[i*1000:] = i - labels_val[i*500:] = i + labels_train[i*5500:-1] = i + labels_test[i*1000:-1] = i + labels_val[i*500:-1] = i train_data = self.generator.predict([noise_train, labels_train]) test_data = self.generator.predict([noise_test, labels_test]) -- cgit v1.2.3-54-g00ecf From 791ff2ad0e32bf1c916f6d4119f0e5eb1ab9a35c Mon Sep 17 00:00:00 2001 From: nunzip Date: Sat, 9 Mar 2019 16:22:31 +0000 Subject: Produce variable output gen --- cgan.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'cgan.py') diff --git a/cgan.py b/cgan.py index d27b11b..0e3dacd 100755 --- a/cgan.py +++ b/cgan.py @@ -194,19 +194,23 @@ class CGAN(): fig.savefig("images/%d.png" % epoch) plt.close() - def generate_data(self): - noise_train = np.random.normal(0, 1, (55000, 100)) - noise_test = np.random.normal(0, 1, (10000, 100)) - noise_val = np.random.normal(0, 1, (5000, 100)) - - labels_train = np.zeros(55000).reshape(-1, 1) - labels_test = np.zeros(10000).reshape(-1, 1) - labels_val = np.zeros(5000).reshape(-1, 1) - + def generate_data(self, split): + train_size = int((55000*100/split)-55000) + val_size = int(train_size/11) + test_size = 2*val_size + + noise_train = np.random.normal(0, 1, (train_size, 100)) + noise_test = np.random.normal(0, 1, (test_size, 100)) + noise_val = np.random.normal(0, 1, (val_size, 100)) + + labels_train = np.zeros(train_size).reshape(-1, 1) + labels_test = np.zeros(test_size).reshape(-1, 1) + labels_val = np.zeros(val_size).reshape(-1, 1) + for i in range(10): - labels_train[i*5500:-1] = i - labels_test[i*1000:-1] = i - labels_val[i*500:-1] = i + labels_train[i*int(train_size/10):-1] = i + labels_test[i*int(test_size/10):-1] = i + labels_val[i*int(val_size/10):-1] = i train_data = self.generator.predict([noise_train, labels_train]) test_data = self.generator.predict([noise_test, labels_test]) -- cgit v1.2.3-54-g00ecf From 4f96cbcc673f46d8fdd5bc7bc253aaeb2aa2af88 Mon Sep 17 00:00:00 2001 From: nunzip Date: Sun, 10 Mar 2019 18:15:14 +0000 Subject: Rewrite generate data --- cgan.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'cgan.py') diff --git a/cgan.py b/cgan.py index 0e3dacd..a34a0e3 100755 --- a/cgan.py +++ b/cgan.py @@ -194,21 +194,23 @@ class CGAN(): fig.savefig("images/%d.png" % epoch) plt.close() - def generate_data(self, split): - train_size = int((55000*100/split)-55000) - val_size = int(train_size/11) + def generate_data(self, output_train = 55000): + # with this output_train you specify how much training data you want. the other two variables produce validation + # and testing data in proportions equal to the ones of MNIST dataset + + val_size = int(output_train/11) test_size = 2*val_size - noise_train = np.random.normal(0, 1, (train_size, 100)) + noise_train = np.random.normal(0, 1, (output_train, 100)) noise_test = np.random.normal(0, 1, (test_size, 100)) noise_val = np.random.normal(0, 1, (val_size, 100)) - labels_train = np.zeros(train_size).reshape(-1, 1) + labels_train = np.zeros(output_train).reshape(-1, 1) labels_test = np.zeros(test_size).reshape(-1, 1) labels_val = np.zeros(val_size).reshape(-1, 1) for i in range(10): - labels_train[i*int(train_size/10):-1] = i + labels_train[i*int(output_train/10):-1] = i labels_test[i*int(test_size/10):-1] = i labels_val[i*int(val_size/10):-1] = i -- cgit v1.2.3-54-g00ecf From 79d666afdf6517ea15bfc9b882f7e4e77bff295b Mon Sep 17 00:00:00 2001 From: nunzip Date: Wed, 13 Mar 2019 17:25:54 +0000 Subject: Try GD --- cgan.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'cgan.py') diff --git a/cgan.py b/cgan.py index a34a0e3..d579e33 100755 --- a/cgan.py +++ b/cgan.py @@ -141,6 +141,7 @@ class CGAN(): gen_imgs = self.generator.predict([noise, labels]) # Train the discriminator + d_loss_real = self.discriminator.train_on_batch([imgs, labels], valid*smooth_real) d_loss_fake = self.discriminator.train_on_batch([gen_imgs, labels], valid*smooth_fake) d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) @@ -152,7 +153,10 @@ class CGAN(): # Condition on labels sampled_labels = np.random.randint(0, 10, batch_size).reshape(-1, 1) # Train the generator - g_loss = self.combined.train_on_batch([noise, sampled_labels], valid) + if epoch % 3 == 0 + g_loss = self.combined.train_on_batch([noise, sampled_labels], valid) + else: + g_loss = 0 # Plot the progress #print ("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100*d_loss[1], g_loss)) -- cgit v1.2.3-54-g00ecf From 1fb7afb062fb50e0be0f32d3c8969e5ec1e72314 Mon Sep 17 00:00:00 2001 From: nunzip Date: Wed, 13 Mar 2019 17:31:21 +0000 Subject: Fix gd attempt --- cgan.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'cgan.py') diff --git a/cgan.py b/cgan.py index d579e33..b68e4ab 100755 --- a/cgan.py +++ b/cgan.py @@ -108,7 +108,7 @@ class CGAN(): return Model([img, label], validity) - def train(self, epochs, batch_size=128, sample_interval=50, graph=False, smooth_real=1, smooth_fake=0): + def train(self, epochs, batch_size=128, sample_interval=50, graph=False, smooth_real=1, smooth_fake=0, gdstep=1): # Load the dataset (X_train, y_train), (_, _) = mnist.load_data() @@ -153,7 +153,7 @@ class CGAN(): # Condition on labels sampled_labels = np.random.randint(0, 10, batch_size).reshape(-1, 1) # Train the generator - if epoch % 3 == 0 + if epoch % gdstep == 0: g_loss = self.combined.train_on_batch([noise, sampled_labels], valid) else: g_loss = 0 -- cgit v1.2.3-54-g00ecf