├── README.md ├── cifar10cgan.ipynb └── cifar10dcgan.ipynb /README.md: -------------------------------------------------------------------------------- 1 | # gans 2 | Various GANs for playing around 3 | -------------------------------------------------------------------------------- /cifar10dcgan.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 0, 6 | "metadata": { 7 | "colab": { 8 | "autoexec": { 9 | "startup": false, 10 | "wait_interval": 0 11 | } 12 | }, 13 | "colab_type": "code", 14 | "collapsed": true, 15 | "id": "_vA8knol6rGi" 16 | }, 17 | "outputs": [], 18 | "source": [ 19 | "from __future__ import print_function, division\n", 20 | "\n", 21 | "from keras.layers import Input, Dense, Flatten, Dropout, Reshape\n", 22 | "from keras.layers import BatchNormalization, Activation, Conv2D, Conv2DTranspose\n", 23 | "from keras.layers.advanced_activations import LeakyReLU\n", 24 | "from keras.models import Model\n", 25 | "from keras.optimizers import Adam\n", 26 | "\n", 27 | "from keras.datasets import cifar10\n", 28 | "import keras.backend as K\n", 29 | "\n", 30 | "import matplotlib.pyplot as plt\n", 31 | "\n", 32 | "import sys\n", 33 | "import numpy as np\n", 34 | "\n", 35 | "%pylab inline" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 0, 41 | "metadata": { 42 | "colab": { 43 | "autoexec": { 44 | "startup": false, 45 | "wait_interval": 0 46 | } 47 | }, 48 | "colab_type": "code", 49 | "collapsed": true, 50 | "id": "MZRiRjRj6xO4" 51 | }, 52 | "outputs": [], 53 | "source": [ 54 | "def get_generator(input_layer):\n", 55 | " '''\n", 56 | " Requires the input layer as input, outputs the model and the final layer\n", 57 | " '''\n", 58 | " \n", 59 | " hid = Dense(128 * 16 * 16, activation='relu')(input_layer) \n", 60 | " hid = BatchNormalization(momentum=0.9)(hid)\n", 61 | " hid = LeakyReLU(alpha=0.1)(hid)\n", 62 | " hid = Reshape((16, 16, 128))(hid)\n", 63 | "\n", 64 | " hid = Conv2D(128, kernel_size=5, strides=1,padding='same')(hid)\n", 65 | " hid = BatchNormalization(momentum=0.9)(hid) \n", 66 | " #hid = Dropout(0.5)(hid)\n", 67 | " hid = LeakyReLU(alpha=0.1)(hid)\n", 68 | "\n", 69 | " hid = Conv2DTranspose(128, 4, strides=2, padding='same')(hid)\n", 70 | " hid = BatchNormalization(momentum=0.9)(hid)\n", 71 | " hid = LeakyReLU(alpha=0.1)(hid)\n", 72 | "\n", 73 | " hid = Conv2D(128, kernel_size=5, strides=1, padding='same')(hid)\n", 74 | " hid = BatchNormalization(momentum=0.9)(hid)\n", 75 | " #hid = Dropout(0.5)(hid)\n", 76 | " hid = LeakyReLU(alpha=0.1)(hid)\n", 77 | "\n", 78 | " hid = Conv2D(128, kernel_size=5, strides=1, padding='same')(hid)\n", 79 | " hid = BatchNormalization(momentum=0.9)(hid)\n", 80 | " hid = LeakyReLU(alpha=0.1)(hid)\n", 81 | " \n", 82 | " hid = Conv2D(3, kernel_size=5, strides=1, padding=\"same\")(hid)\n", 83 | " out = Activation(\"tanh\")(hid)\n", 84 | "\n", 85 | " model = Model(input_layer, out)\n", 86 | " model.summary()\n", 87 | " \n", 88 | " return model, out\n" 89 | ] 90 | }, 91 | { 92 | "cell_type": "code", 93 | "execution_count": 0, 94 | "metadata": { 95 | "colab": { 96 | "autoexec": { 97 | "startup": false, 98 | "wait_interval": 0 99 | } 100 | }, 101 | "colab_type": "code", 102 | "collapsed": true, 103 | "id": "4yFseSJQ62d2" 104 | }, 105 | "outputs": [], 106 | "source": [ 107 | "def get_discriminator(input_layer):\n", 108 | " '''\n", 109 | " Requires the input layer as input, outputs the model and the final layer\n", 110 | " '''\n", 111 | "\n", 112 | " hid = Conv2D(128, kernel_size=3, strides=1, padding='same')(input_layer)\n", 113 | " hid = BatchNormalization(momentum=0.9)(hid)\n", 114 | " hid = LeakyReLU(alpha=0.1)(hid)\n", 115 | "\n", 116 | " hid = Conv2D(128, kernel_size=4, strides=2, padding='same')(hid)\n", 117 | " hid = BatchNormalization(momentum=0.9)(hid)\n", 118 | " hid = LeakyReLU(alpha=0.1)(hid)\n", 119 | "\n", 120 | " hid = Conv2D(128, kernel_size=4, strides=2, padding='same')(hid)\n", 121 | " hid = BatchNormalization(momentum=0.9)(hid)\n", 122 | " hid = LeakyReLU(alpha=0.1)(hid)\n", 123 | "\n", 124 | " hid = Conv2D(128, kernel_size=4, strides=2, padding='same')(hid)\n", 125 | " hid = BatchNormalization(momentum=0.9)(hid)\n", 126 | " hid = LeakyReLU(alpha=0.1)(hid)\n", 127 | "\n", 128 | " hid = Flatten()(hid)\n", 129 | " hid = Dropout(0.4)(hid)\n", 130 | " out = Dense(1, activation='sigmoid')(hid)\n", 131 | "\n", 132 | " model = Model(input_layer, out)\n", 133 | "\n", 134 | " model.summary()\n", 135 | "\n", 136 | " return model, out" 137 | ] 138 | }, 139 | { 140 | "cell_type": "code", 141 | "execution_count": 0, 142 | "metadata": { 143 | "colab": { 144 | "autoexec": { 145 | "startup": false, 146 | "wait_interval": 0 147 | } 148 | }, 149 | "colab_type": "code", 150 | "collapsed": true, 151 | "id": "uFSdLdMv67Oq" 152 | }, 153 | "outputs": [], 154 | "source": [ 155 | "from keras.preprocessing import image\n", 156 | "\n", 157 | "def generate_noise(n_samples, noise_dim):\n", 158 | " X = np.random.normal(0, 1, size=(n_samples, noise_dim))\n", 159 | " return X\n", 160 | "\n", 161 | "def show_imgs(batchidx):\n", 162 | " noise = generate_noise(9, 100)\n", 163 | " gen_imgs = generator.predict(noise)\n", 164 | "\n", 165 | " fig, axs = plt.subplots(3, 3)\n", 166 | " count = 0\n", 167 | " for i in range(3):\n", 168 | " for j in range(3):\n", 169 | " # Dont scale the images back, let keras handle it\n", 170 | " img = image.array_to_img(gen_imgs[count], scale=True)\n", 171 | " axs[i,j].imshow(img)\n", 172 | " axs[i,j].axis('off')\n", 173 | " count += 1\n", 174 | " plt.show()\n", 175 | " plt.close() " 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 0, 181 | "metadata": { 182 | "colab": { 183 | "autoexec": { 184 | "startup": false, 185 | "wait_interval": 0 186 | } 187 | }, 188 | "colab_type": "code", 189 | "collapsed": true, 190 | "id": "Iha5HiXj7FRg" 191 | }, 192 | "outputs": [], 193 | "source": [ 194 | "# GAN creation\n", 195 | "img_input = Input(shape=(32,32,3))\n", 196 | "discriminator, disc_out = get_discriminator(img_input)\n", 197 | "discriminator.compile(optimizer=Adam(0.0002, 0.5), loss='binary_crossentropy', metrics=['accuracy'])\n", 198 | "\n", 199 | "discriminator.trainable = False\n", 200 | "\n", 201 | "noise_input = Input(shape=(100,))\n", 202 | "generator, gen_out = get_generator(noise_input)\n", 203 | "\n", 204 | "gan_input = Input(shape=(100,))\n", 205 | "x = generator(gan_input)\n", 206 | "gan_out = discriminator(x)\n", 207 | "gan = Model(gan_input, gan_out)\n", 208 | "gan.summary()\n", 209 | "\n", 210 | "gan.compile(optimizer=Adam(0.0002, 0.5), loss='binary_crossentropy')\n" 211 | ] 212 | }, 213 | { 214 | "cell_type": "code", 215 | "execution_count": 0, 216 | "metadata": { 217 | "colab": { 218 | "autoexec": { 219 | "startup": false, 220 | "wait_interval": 0 221 | } 222 | }, 223 | "colab_type": "code", 224 | "collapsed": true, 225 | "id": "x_C473Ji7OtQ" 226 | }, 227 | "outputs": [], 228 | "source": [ 229 | "\n", 230 | "BATCH_SIZE = 16\n", 231 | "\n", 232 | "# # Get training images\n", 233 | "(X_train, y_train), (X_test, _) = cifar10.load_data()\n", 234 | "\n", 235 | "# Select Cars\n", 236 | "X_train = X_train[y_train[:,0]==1]\n", 237 | "print (\"Training shape: {}\".format(X_train.shape))\n", 238 | "\n", 239 | "# Normalize data\n", 240 | "X_train = (X_train - 127.5) / 127.5\n", 241 | " \n", 242 | "num_batches = int(X_train.shape[0]/BATCH_SIZE)" 243 | ] 244 | }, 245 | { 246 | "cell_type": "code", 247 | "execution_count": 0, 248 | "metadata": { 249 | "colab": { 250 | "autoexec": { 251 | "startup": false, 252 | "wait_interval": 0 253 | } 254 | }, 255 | "colab_type": "code", 256 | "collapsed": true, 257 | "id": "jUgkE7bW7ZNK" 258 | }, 259 | "outputs": [], 260 | "source": [ 261 | "N_EPOCHS = 20\n", 262 | "for epoch in range(N_EPOCHS):\n", 263 | "\n", 264 | " cum_d_loss = 0.\n", 265 | " cum_g_loss = 0.\n", 266 | " \n", 267 | " for batch_idx in range(num_batches):\n", 268 | " # Get the next set of real images to be used in this iteration\n", 269 | " images = X_train[batch_idx*BATCH_SIZE : (batch_idx+1)*BATCH_SIZE]\n", 270 | "\n", 271 | " noise_data = generate_noise(BATCH_SIZE, 100)\n", 272 | " generated_images = generator.predict(noise_data)\n", 273 | "\n", 274 | " # Train on soft labels (add noise to labels as well)\n", 275 | " noise_prop = 0.05 # Randomly flip 5% of labels\n", 276 | " \n", 277 | " # Prepare labels for real data\n", 278 | " true_labels = np.zeros((BATCH_SIZE, 1)) + np.random.uniform(low=0.0, high=0.1, size=(BATCH_SIZE, 1))\n", 279 | " flipped_idx = np.random.choice(np.arange(len(true_labels)), size=int(noise_prop*len(true_labels)))\n", 280 | " true_labels[flipped_idx] = 1 - true_labels[flipped_idx]\n", 281 | " \n", 282 | " # Train discriminator on real data\n", 283 | " d_loss_true = discriminator.train_on_batch(images, true_labels)\n", 284 | "\n", 285 | " # Prepare labels for generated data\n", 286 | " gene_labels = np.ones((BATCH_SIZE, 1)) - np.random.uniform(low=0.0, high=0.1, size=(BATCH_SIZE, 1))\n", 287 | " flipped_idx = np.random.choice(np.arange(len(gene_labels)), size=int(noise_prop*len(gene_labels)))\n", 288 | " gene_labels[flipped_idx] = 1 - gene_labels[flipped_idx]\n", 289 | " \n", 290 | " # Train discriminator on generated data\n", 291 | " d_loss_gene = discriminator.train_on_batch(generated_images, gene_labels)\n", 292 | "\n", 293 | " d_loss = 0.5 * np.add(d_loss_true, d_loss_gene)\n", 294 | " cum_d_loss += d_loss\n", 295 | "\n", 296 | " # Train generator\n", 297 | " noise_data = generate_noise(BATCH_SIZE, 100)\n", 298 | " g_loss = gan.train_on_batch(noise_data, np.zeros((BATCH_SIZE, 1)))\n", 299 | " cum_g_loss += g_loss\n", 300 | "\n", 301 | " print(' Epoch: {}, Generator Loss: {}, Discriminator Loss: {}'.format(epoch+1, cum_g_loss/num_batches, cum_d_loss/num_batches))\n", 302 | " show_imgs(\"epoch\" + str(epoch))\n" 303 | ] 304 | }, 305 | { 306 | "cell_type": "code", 307 | "execution_count": 0, 308 | "metadata": { 309 | "colab": { 310 | "autoexec": { 311 | "startup": false, 312 | "wait_interval": 0 313 | } 314 | }, 315 | "colab_type": "code", 316 | "collapsed": true, 317 | "id": "VbrnIMVREqsW" 318 | }, 319 | "outputs": [], 320 | "source": [] 321 | } 322 | ], 323 | "metadata": { 324 | "accelerator": "GPU", 325 | "colab": { 326 | "collapsed_sections": [], 327 | "default_view": {}, 328 | "name": "LatestDCGAN.ipynb", 329 | "provenance": [], 330 | "version": "0.3.2", 331 | "views": {} 332 | }, 333 | "kernelspec": { 334 | "display_name": "Python 2", 335 | "name": "python2" 336 | } 337 | }, 338 | "nbformat": 4, 339 | "nbformat_minor": 0 340 | } 341 | --------------------------------------------------------------------------------