From 37a4c097259a7d1f7c2b6b302a5e1883f435495e Mon Sep 17 00:00:00 2001 From: Ankur <34418414+Ankuraxz@users.noreply.github.com> Date: Thu, 19 Mar 2020 20:32:36 +0530 Subject: [PATCH 1/6] Create Image_GAN.md --- tensorflow_gan/Image_GAN/Image_GAN.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 tensorflow_gan/Image_GAN/Image_GAN.md diff --git a/tensorflow_gan/Image_GAN/Image_GAN.md b/tensorflow_gan/Image_GAN/Image_GAN.md new file mode 100644 index 00000000..611089fc --- /dev/null +++ b/tensorflow_gan/Image_GAN/Image_GAN.md @@ -0,0 +1,4 @@ +In response to Issue "Colab Tutorial Not TF 2.x Compatible #18" on tensorflow/gan +https://github.com/tensorflow/gan/issues/18 + +Collab Tutorial using tf 2.x From 85352e7fb79cd068cc2532f01f5ffc443d26d7ef Mon Sep 17 00:00:00 2001 From: Ankur <34418414+Ankuraxz@users.noreply.github.com> Date: Thu, 19 Mar 2020 20:44:46 +0530 Subject: [PATCH 2/6] COLAB TUTORIAL UTILIZING tf.2x --- Collab_tutorial_on_GAN_using_tf2_x.ipynb | 334 +++++++++++++++++++++++ 1 file changed, 334 insertions(+) create mode 100644 Collab_tutorial_on_GAN_using_tf2_x.ipynb diff --git a/Collab_tutorial_on_GAN_using_tf2_x.ipynb b/Collab_tutorial_on_GAN_using_tf2_x.ipynb new file mode 100644 index 00000000..52cf78dc --- /dev/null +++ b/Collab_tutorial_on_GAN_using_tf2_x.ipynb @@ -0,0 +1,334 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Collab_tutorial_on_GAN_using_tf2.x.ipynb", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "code", + "metadata": { + "id": "h8-7MxIETkG4", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 36 + }, + "outputId": "c5b2bb65-bf61-43ec-adbe-908c16741db9" + }, + "source": [ + "# Setting up\n", + "from __future__ import absolute_import, division, print_function, unicode_literals\n", + "try:\n", + " # tf_version only exist in colab\n", + " %tensorflow_version 2.x\n", + "\n", + "except Exception:\n", + " pass\n" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "TensorFlow 2.x selected.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6MD16UIYVt7n", + "colab_type": "text" + }, + "source": [ + "# SETUP" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "A5gKsgrGUZ39", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import tensorflow as tf\n", + "tf.random.set_seed(7)\n", + "import numpy as np\n", + "np.random.seed(7)\n", + "import matplotlib.pyplot as plt\n", + "import os\n", + "import time" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "J0iuziuiVwkA", + "colab_type": "text" + }, + "source": [ + "# HYPERPARAMETERS\n", + "\n", + "X_Train is the array of images, the user wants to use as training data. Labels and test data aren't needed" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ZlWkvpA0UcK5", + "colab_type": "code", + "colab": {} + }, + "source": [ + "EPOCHS = 60\n", + "BATCH_SIZE = 256\n", + "NO_OF_BATCHES = int(X_Train.shape[0]/BATCH_SIZE)\n", + "HALF_BATCH_SIZE = 128\n", + "NOISE_DIM = 100\n", + "# Customised ADAM optimizer\n", + "adam = tf.keras.optimizers.Adam(lr=2e-4,beta_1=0.5) # lr: Learning Rate" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YEnk012DV_Et", + "colab_type": "text" + }, + "source": [ + "# Making up models for GAN" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ewWCDClQUhGo", + "colab_type": "code", + "colab": {} + }, + "source": [ + "from tensorflow.keras.layers import *\n", + "from tensorflow.keras.layers import LeakyReLU\n", + "from tensorflow.keras.models import Sequential, Model" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gMbuIfzhWC6K", + "colab_type": "text" + }, + "source": [ + "# GENERATOR " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "oXt9-uPMUjUo", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Generator\n", + "generator = Sequential()\n", + "generator.add(Dense(256,input_shape=(NOISE_DIM,)))\n", + "generator.add(LeakyReLU(0.2))\n", + "generator.add(Dense(512))\n", + "generator.add(LeakyReLU(0.2))\n", + "generator.add(Dense(1024))\n", + "generator.add(LeakyReLU(0.2))\n", + "generator.add(Dense(784,activation='tanh'))\n", + "\n", + "# Compile\n", + "generator.compile(loss='binary_crossentropy',optimizer=adam)\n", + "\n", + "generator.summary()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RZownvPRWGT-", + "colab_type": "text" + }, + "source": [ + "# DISCRIMINATOR" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "KAhiXuXWUnyT", + "colab_type": "code", + "colab": {} + }, + "source": [ + "#Discriminator\n", + "discriminator = Sequential()\n", + "discriminator.add(Dense(512,input_shape=(784,)))\n", + "discriminator.add(LeakyReLU(0.2))\n", + "discriminator.add(Dense(256))\n", + "discriminator.add(LeakyReLU(0.2))\n", + "discriminator.add(Dense(1,activation='sigmoid'))\n", + "\n", + "discriminator.compile(loss='binary_crossentropy',optimizer=adam)\n", + "discriminator.summary()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JfnJ1IVIWJt2", + "colab_type": "text" + }, + "source": [ + "# Combined Model" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "VKzNOfNIUpxa", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Combined Model (Geneerator + Discriminator) -> Functional API\n", + "discriminator.trainable = False\n", + "gan_input = Input(shape=(NOISE_DIM,))\n", + "generator_output = generator(gan_input)\n", + "gan_output = discriminator(generator_output)\n", + "\n", + "model = Model(gan_input,gan_output)\n", + "model.compile(loss='binary_crossentropy',optimizer=adam)\n", + "\n", + "model.summary()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "tWMncFAzUs_D", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def showImgs(epoch):\n", + " noise = np.random.normal(0,1,size=(100,NOISE_DIM)) # Random Noise\n", + " generated_imgs = generator.predict(noise)\n", + " generated_imgs = generated_imgs.reshape(-1,28,28)\n", + " \n", + " #Display the Images\n", + " plt.figure(figsize=(10,10))\n", + " for i in range(100):\n", + " plt.subplot(10,10,i+1)\n", + " plt.imshow(generated_imgs[i],cmap='gray',interpolation='nearest')\n", + " plt.axis(\"off\")\n", + " plt.tight_layout()\n", + " plt.show()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "21WSSthiWPuo", + "colab_type": "text" + }, + "source": [ + "# Training" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "TTUSWup6U59U", + "colab_type": "code", + "colab": {} + }, + "source": [ + "d_losses = []\n", + "g_losses = []\n", + "\n", + "# Training Loop\n", + "for epoch in range(EPOCHS+1): \n", + " epoch_d_loss = 0.0\n", + " epoch_g_loss = 0.0\n", + " \n", + " # Mini Batch\n", + " for step in range(NO_OF_BATCHES):\n", + " idx = np.random.randint(0,X_Train.shape[0],HALF_BATCH_SIZE)\n", + " real_imgs = X_Train[idx]\n", + " \n", + " # generate fake images assuming generator is frozen\n", + " noise = np.random.normal(0,1,size=(HALF_BATCH_SIZE,NOISE_DIM))\n", + " fake_imgs = generator.predict(noise)\n", + " \n", + " # Labels\n", + " real_y = np.ones((HALF_BATCH_SIZE,1))*0.9\n", + " fake_y = np.zeros((HALF_BATCH_SIZE,1))\n", + " \n", + " #Train on Real and Fake Images\n", + " d_real_loss = discriminator.train_on_batch(real_imgs,real_y)\n", + " d_fake_loss = discriminator.train_on_batch(fake_imgs,fake_y)\n", + " \n", + " d_loss = 0.5*d_real_loss + 0.5*d_fake_loss\n", + " epoch_d_loss += d_loss\n", + " \n", + " # Train Generator\n", + " noise = np.random.normal(0,1,size=(BATCH_SIZE,NOISE_DIM))\n", + " real_y = np.ones((BATCH_SIZE,1))\n", + " g_loss = model.train_on_batch(noise,real_y)\n", + " epoch_g_loss += g_loss\n", + " \n", + " d_losses.append(epoch_d_loss)\n", + " g_losses.append(epoch_g_loss)\n", + " \n", + " print(\"Epoch %d D Loss %.4f G loss %0.4f \"%((epoch+1),epoch_d_loss,epoch_g_loss))\n", + " if (epoch%5)==0: #printing images after every 5 epocs\n", + " #generator.save(\"model/gen_{0}.h5\".format(epoch)) # Saving the model\n", + " showImgs(epoch) # Show the image transformation progress at every step" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "tP85ZHAvVksH", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From 5f38a6f1da23386e45d53162baab15ec34804789 Mon Sep 17 00:00:00 2001 From: Ankur <34418414+Ankuraxz@users.noreply.github.com> Date: Fri, 20 Mar 2020 16:35:49 +0530 Subject: [PATCH 3/6] Delete Image_GAN.md --- tensorflow_gan/Image_GAN/Image_GAN.md | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 tensorflow_gan/Image_GAN/Image_GAN.md diff --git a/tensorflow_gan/Image_GAN/Image_GAN.md b/tensorflow_gan/Image_GAN/Image_GAN.md deleted file mode 100644 index 611089fc..00000000 --- a/tensorflow_gan/Image_GAN/Image_GAN.md +++ /dev/null @@ -1,4 +0,0 @@ -In response to Issue "Colab Tutorial Not TF 2.x Compatible #18" on tensorflow/gan -https://github.com/tensorflow/gan/issues/18 - -Collab Tutorial using tf 2.x From 6df5f198b2df0c96de151bf4e9e3228b40718430 Mon Sep 17 00:00:00 2001 From: Ankur <34418414+Ankuraxz@users.noreply.github.com> Date: Fri, 20 Mar 2020 16:36:39 +0530 Subject: [PATCH 4/6] Tf2.x_Collab_Tutorial_on_GAN In response to Issue "Colab Tutorial Not TF 2.x Compatible #18" on tensorflow/gan https://github.com/tensorflow/gan/issues/18 Collab Tutorial using tf 2.x --- .../Collab_tutorial_on_GAN_using_tf2_x.ipynb | 334 ++++++++++++++++++ 1 file changed, 334 insertions(+) create mode 100644 tensorflow_gan/Collab_tutorial_on_GAN_using_tf2_x.ipynb diff --git a/tensorflow_gan/Collab_tutorial_on_GAN_using_tf2_x.ipynb b/tensorflow_gan/Collab_tutorial_on_GAN_using_tf2_x.ipynb new file mode 100644 index 00000000..52cf78dc --- /dev/null +++ b/tensorflow_gan/Collab_tutorial_on_GAN_using_tf2_x.ipynb @@ -0,0 +1,334 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Collab_tutorial_on_GAN_using_tf2.x.ipynb", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "code", + "metadata": { + "id": "h8-7MxIETkG4", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 36 + }, + "outputId": "c5b2bb65-bf61-43ec-adbe-908c16741db9" + }, + "source": [ + "# Setting up\n", + "from __future__ import absolute_import, division, print_function, unicode_literals\n", + "try:\n", + " # tf_version only exist in colab\n", + " %tensorflow_version 2.x\n", + "\n", + "except Exception:\n", + " pass\n" + ], + "execution_count": 1, + "outputs": [ + { + "output_type": "stream", + "text": [ + "TensorFlow 2.x selected.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6MD16UIYVt7n", + "colab_type": "text" + }, + "source": [ + "# SETUP" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "A5gKsgrGUZ39", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import tensorflow as tf\n", + "tf.random.set_seed(7)\n", + "import numpy as np\n", + "np.random.seed(7)\n", + "import matplotlib.pyplot as plt\n", + "import os\n", + "import time" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "J0iuziuiVwkA", + "colab_type": "text" + }, + "source": [ + "# HYPERPARAMETERS\n", + "\n", + "X_Train is the array of images, the user wants to use as training data. Labels and test data aren't needed" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ZlWkvpA0UcK5", + "colab_type": "code", + "colab": {} + }, + "source": [ + "EPOCHS = 60\n", + "BATCH_SIZE = 256\n", + "NO_OF_BATCHES = int(X_Train.shape[0]/BATCH_SIZE)\n", + "HALF_BATCH_SIZE = 128\n", + "NOISE_DIM = 100\n", + "# Customised ADAM optimizer\n", + "adam = tf.keras.optimizers.Adam(lr=2e-4,beta_1=0.5) # lr: Learning Rate" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YEnk012DV_Et", + "colab_type": "text" + }, + "source": [ + "# Making up models for GAN" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ewWCDClQUhGo", + "colab_type": "code", + "colab": {} + }, + "source": [ + "from tensorflow.keras.layers import *\n", + "from tensorflow.keras.layers import LeakyReLU\n", + "from tensorflow.keras.models import Sequential, Model" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gMbuIfzhWC6K", + "colab_type": "text" + }, + "source": [ + "# GENERATOR " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "oXt9-uPMUjUo", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Generator\n", + "generator = Sequential()\n", + "generator.add(Dense(256,input_shape=(NOISE_DIM,)))\n", + "generator.add(LeakyReLU(0.2))\n", + "generator.add(Dense(512))\n", + "generator.add(LeakyReLU(0.2))\n", + "generator.add(Dense(1024))\n", + "generator.add(LeakyReLU(0.2))\n", + "generator.add(Dense(784,activation='tanh'))\n", + "\n", + "# Compile\n", + "generator.compile(loss='binary_crossentropy',optimizer=adam)\n", + "\n", + "generator.summary()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RZownvPRWGT-", + "colab_type": "text" + }, + "source": [ + "# DISCRIMINATOR" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "KAhiXuXWUnyT", + "colab_type": "code", + "colab": {} + }, + "source": [ + "#Discriminator\n", + "discriminator = Sequential()\n", + "discriminator.add(Dense(512,input_shape=(784,)))\n", + "discriminator.add(LeakyReLU(0.2))\n", + "discriminator.add(Dense(256))\n", + "discriminator.add(LeakyReLU(0.2))\n", + "discriminator.add(Dense(1,activation='sigmoid'))\n", + "\n", + "discriminator.compile(loss='binary_crossentropy',optimizer=adam)\n", + "discriminator.summary()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JfnJ1IVIWJt2", + "colab_type": "text" + }, + "source": [ + "# Combined Model" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "VKzNOfNIUpxa", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Combined Model (Geneerator + Discriminator) -> Functional API\n", + "discriminator.trainable = False\n", + "gan_input = Input(shape=(NOISE_DIM,))\n", + "generator_output = generator(gan_input)\n", + "gan_output = discriminator(generator_output)\n", + "\n", + "model = Model(gan_input,gan_output)\n", + "model.compile(loss='binary_crossentropy',optimizer=adam)\n", + "\n", + "model.summary()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "tWMncFAzUs_D", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def showImgs(epoch):\n", + " noise = np.random.normal(0,1,size=(100,NOISE_DIM)) # Random Noise\n", + " generated_imgs = generator.predict(noise)\n", + " generated_imgs = generated_imgs.reshape(-1,28,28)\n", + " \n", + " #Display the Images\n", + " plt.figure(figsize=(10,10))\n", + " for i in range(100):\n", + " plt.subplot(10,10,i+1)\n", + " plt.imshow(generated_imgs[i],cmap='gray',interpolation='nearest')\n", + " plt.axis(\"off\")\n", + " plt.tight_layout()\n", + " plt.show()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "21WSSthiWPuo", + "colab_type": "text" + }, + "source": [ + "# Training" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "TTUSWup6U59U", + "colab_type": "code", + "colab": {} + }, + "source": [ + "d_losses = []\n", + "g_losses = []\n", + "\n", + "# Training Loop\n", + "for epoch in range(EPOCHS+1): \n", + " epoch_d_loss = 0.0\n", + " epoch_g_loss = 0.0\n", + " \n", + " # Mini Batch\n", + " for step in range(NO_OF_BATCHES):\n", + " idx = np.random.randint(0,X_Train.shape[0],HALF_BATCH_SIZE)\n", + " real_imgs = X_Train[idx]\n", + " \n", + " # generate fake images assuming generator is frozen\n", + " noise = np.random.normal(0,1,size=(HALF_BATCH_SIZE,NOISE_DIM))\n", + " fake_imgs = generator.predict(noise)\n", + " \n", + " # Labels\n", + " real_y = np.ones((HALF_BATCH_SIZE,1))*0.9\n", + " fake_y = np.zeros((HALF_BATCH_SIZE,1))\n", + " \n", + " #Train on Real and Fake Images\n", + " d_real_loss = discriminator.train_on_batch(real_imgs,real_y)\n", + " d_fake_loss = discriminator.train_on_batch(fake_imgs,fake_y)\n", + " \n", + " d_loss = 0.5*d_real_loss + 0.5*d_fake_loss\n", + " epoch_d_loss += d_loss\n", + " \n", + " # Train Generator\n", + " noise = np.random.normal(0,1,size=(BATCH_SIZE,NOISE_DIM))\n", + " real_y = np.ones((BATCH_SIZE,1))\n", + " g_loss = model.train_on_batch(noise,real_y)\n", + " epoch_g_loss += g_loss\n", + " \n", + " d_losses.append(epoch_d_loss)\n", + " g_losses.append(epoch_g_loss)\n", + " \n", + " print(\"Epoch %d D Loss %.4f G loss %0.4f \"%((epoch+1),epoch_d_loss,epoch_g_loss))\n", + " if (epoch%5)==0: #printing images after every 5 epocs\n", + " #generator.save(\"model/gen_{0}.h5\".format(epoch)) # Saving the model\n", + " showImgs(epoch) # Show the image transformation progress at every step" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "tP85ZHAvVksH", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file From f4b566bc85993468ed375ede73eab435ec3103b0 Mon Sep 17 00:00:00 2001 From: Ankur <34418414+Ankuraxz@users.noreply.github.com> Date: Fri, 20 Mar 2020 17:03:24 +0530 Subject: [PATCH 5/6] Delete Collab_tutorial_on_GAN_using_tf2_x.ipynb --- Collab_tutorial_on_GAN_using_tf2_x.ipynb | 334 ----------------------- 1 file changed, 334 deletions(-) delete mode 100644 Collab_tutorial_on_GAN_using_tf2_x.ipynb diff --git a/Collab_tutorial_on_GAN_using_tf2_x.ipynb b/Collab_tutorial_on_GAN_using_tf2_x.ipynb deleted file mode 100644 index 52cf78dc..00000000 --- a/Collab_tutorial_on_GAN_using_tf2_x.ipynb +++ /dev/null @@ -1,334 +0,0 @@ -{ - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "Collab_tutorial_on_GAN_using_tf2.x.ipynb", - "provenance": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - } - }, - "cells": [ - { - "cell_type": "code", - "metadata": { - "id": "h8-7MxIETkG4", - "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 36 - }, - "outputId": "c5b2bb65-bf61-43ec-adbe-908c16741db9" - }, - "source": [ - "# Setting up\n", - "from __future__ import absolute_import, division, print_function, unicode_literals\n", - "try:\n", - " # tf_version only exist in colab\n", - " %tensorflow_version 2.x\n", - "\n", - "except Exception:\n", - " pass\n" - ], - "execution_count": 1, - "outputs": [ - { - "output_type": "stream", - "text": [ - "TensorFlow 2.x selected.\n" - ], - "name": "stdout" - } - ] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "6MD16UIYVt7n", - "colab_type": "text" - }, - "source": [ - "# SETUP" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "A5gKsgrGUZ39", - "colab_type": "code", - "colab": {} - }, - "source": [ - "import tensorflow as tf\n", - "tf.random.set_seed(7)\n", - "import numpy as np\n", - "np.random.seed(7)\n", - "import matplotlib.pyplot as plt\n", - "import os\n", - "import time" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "J0iuziuiVwkA", - "colab_type": "text" - }, - "source": [ - "# HYPERPARAMETERS\n", - "\n", - "X_Train is the array of images, the user wants to use as training data. Labels and test data aren't needed" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "ZlWkvpA0UcK5", - "colab_type": "code", - "colab": {} - }, - "source": [ - "EPOCHS = 60\n", - "BATCH_SIZE = 256\n", - "NO_OF_BATCHES = int(X_Train.shape[0]/BATCH_SIZE)\n", - "HALF_BATCH_SIZE = 128\n", - "NOISE_DIM = 100\n", - "# Customised ADAM optimizer\n", - "adam = tf.keras.optimizers.Adam(lr=2e-4,beta_1=0.5) # lr: Learning Rate" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "YEnk012DV_Et", - "colab_type": "text" - }, - "source": [ - "# Making up models for GAN" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "ewWCDClQUhGo", - "colab_type": "code", - "colab": {} - }, - "source": [ - "from tensorflow.keras.layers import *\n", - "from tensorflow.keras.layers import LeakyReLU\n", - "from tensorflow.keras.models import Sequential, Model" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "gMbuIfzhWC6K", - "colab_type": "text" - }, - "source": [ - "# GENERATOR " - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "oXt9-uPMUjUo", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Generator\n", - "generator = Sequential()\n", - "generator.add(Dense(256,input_shape=(NOISE_DIM,)))\n", - "generator.add(LeakyReLU(0.2))\n", - "generator.add(Dense(512))\n", - "generator.add(LeakyReLU(0.2))\n", - "generator.add(Dense(1024))\n", - "generator.add(LeakyReLU(0.2))\n", - "generator.add(Dense(784,activation='tanh'))\n", - "\n", - "# Compile\n", - "generator.compile(loss='binary_crossentropy',optimizer=adam)\n", - "\n", - "generator.summary()" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "RZownvPRWGT-", - "colab_type": "text" - }, - "source": [ - "# DISCRIMINATOR" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "KAhiXuXWUnyT", - "colab_type": "code", - "colab": {} - }, - "source": [ - "#Discriminator\n", - "discriminator = Sequential()\n", - "discriminator.add(Dense(512,input_shape=(784,)))\n", - "discriminator.add(LeakyReLU(0.2))\n", - "discriminator.add(Dense(256))\n", - "discriminator.add(LeakyReLU(0.2))\n", - "discriminator.add(Dense(1,activation='sigmoid'))\n", - "\n", - "discriminator.compile(loss='binary_crossentropy',optimizer=adam)\n", - "discriminator.summary()" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "JfnJ1IVIWJt2", - "colab_type": "text" - }, - "source": [ - "# Combined Model" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "VKzNOfNIUpxa", - "colab_type": "code", - "colab": {} - }, - "source": [ - "# Combined Model (Geneerator + Discriminator) -> Functional API\n", - "discriminator.trainable = False\n", - "gan_input = Input(shape=(NOISE_DIM,))\n", - "generator_output = generator(gan_input)\n", - "gan_output = discriminator(generator_output)\n", - "\n", - "model = Model(gan_input,gan_output)\n", - "model.compile(loss='binary_crossentropy',optimizer=adam)\n", - "\n", - "model.summary()" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "tWMncFAzUs_D", - "colab_type": "code", - "colab": {} - }, - "source": [ - "def showImgs(epoch):\n", - " noise = np.random.normal(0,1,size=(100,NOISE_DIM)) # Random Noise\n", - " generated_imgs = generator.predict(noise)\n", - " generated_imgs = generated_imgs.reshape(-1,28,28)\n", - " \n", - " #Display the Images\n", - " plt.figure(figsize=(10,10))\n", - " for i in range(100):\n", - " plt.subplot(10,10,i+1)\n", - " plt.imshow(generated_imgs[i],cmap='gray',interpolation='nearest')\n", - " plt.axis(\"off\")\n", - " plt.tight_layout()\n", - " plt.show()" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "markdown", - "metadata": { - "id": "21WSSthiWPuo", - "colab_type": "text" - }, - "source": [ - "# Training" - ] - }, - { - "cell_type": "code", - "metadata": { - "id": "TTUSWup6U59U", - "colab_type": "code", - "colab": {} - }, - "source": [ - "d_losses = []\n", - "g_losses = []\n", - "\n", - "# Training Loop\n", - "for epoch in range(EPOCHS+1): \n", - " epoch_d_loss = 0.0\n", - " epoch_g_loss = 0.0\n", - " \n", - " # Mini Batch\n", - " for step in range(NO_OF_BATCHES):\n", - " idx = np.random.randint(0,X_Train.shape[0],HALF_BATCH_SIZE)\n", - " real_imgs = X_Train[idx]\n", - " \n", - " # generate fake images assuming generator is frozen\n", - " noise = np.random.normal(0,1,size=(HALF_BATCH_SIZE,NOISE_DIM))\n", - " fake_imgs = generator.predict(noise)\n", - " \n", - " # Labels\n", - " real_y = np.ones((HALF_BATCH_SIZE,1))*0.9\n", - " fake_y = np.zeros((HALF_BATCH_SIZE,1))\n", - " \n", - " #Train on Real and Fake Images\n", - " d_real_loss = discriminator.train_on_batch(real_imgs,real_y)\n", - " d_fake_loss = discriminator.train_on_batch(fake_imgs,fake_y)\n", - " \n", - " d_loss = 0.5*d_real_loss + 0.5*d_fake_loss\n", - " epoch_d_loss += d_loss\n", - " \n", - " # Train Generator\n", - " noise = np.random.normal(0,1,size=(BATCH_SIZE,NOISE_DIM))\n", - " real_y = np.ones((BATCH_SIZE,1))\n", - " g_loss = model.train_on_batch(noise,real_y)\n", - " epoch_g_loss += g_loss\n", - " \n", - " d_losses.append(epoch_d_loss)\n", - " g_losses.append(epoch_g_loss)\n", - " \n", - " print(\"Epoch %d D Loss %.4f G loss %0.4f \"%((epoch+1),epoch_d_loss,epoch_g_loss))\n", - " if (epoch%5)==0: #printing images after every 5 epocs\n", - " #generator.save(\"model/gen_{0}.h5\".format(epoch)) # Saving the model\n", - " showImgs(epoch) # Show the image transformation progress at every step" - ], - "execution_count": 0, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "tP85ZHAvVksH", - "colab_type": "code", - "colab": {} - }, - "source": [ - "" - ], - "execution_count": 0, - "outputs": [] - } - ] -} \ No newline at end of file From b956c7d571539fd1053b3df3dddddbcbd27be65c Mon Sep 17 00:00:00 2001 From: Ankur <34418414+Ankuraxz@users.noreply.github.com> Date: Fri, 20 Mar 2020 17:06:56 +0530 Subject: [PATCH 6/6] Image GAN utilising tf2.x This general model can be applied to any kind of Image dataset, like Handwritten digits, Cifar10, Fashion-MNIST etc. Find more about datasets here: https://www.tensorflow.org/api_docs/python/tf/keras/datasets --- .../Collab_tutorial_on_GAN_using_tf2_x.ipynb | 360 ++++++++++++++++++ 1 file changed, 360 insertions(+) create mode 100644 tensorflow_gan/examples/colab_notebooks/Collab_tutorial_on_GAN_using_tf2_x.ipynb diff --git a/tensorflow_gan/examples/colab_notebooks/Collab_tutorial_on_GAN_using_tf2_x.ipynb b/tensorflow_gan/examples/colab_notebooks/Collab_tutorial_on_GAN_using_tf2_x.ipynb new file mode 100644 index 00000000..136d8a46 --- /dev/null +++ b/tensorflow_gan/examples/colab_notebooks/Collab_tutorial_on_GAN_using_tf2_x.ipynb @@ -0,0 +1,360 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Collab_tutorial_on_GAN_using_tf2_x.ipynb", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "code", + "metadata": { + "id": "h8-7MxIETkG4", + "colab_type": "code", + "outputId": "c5b2bb65-bf61-43ec-adbe-908c16741db9", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 36 + } + }, + "source": [ + "# Copyright 2018 The TensorFlow GAN Authors. All Rights Reserved.\n", + "#\n", + "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", + "# you may not use this file except in compliance with the License.\n", + "# You may obtain a copy of the License at\n", + "#\n", + "# http://www.apache.org/licenses/LICENSE-2.0\n", + "#\n", + "# Unless required by applicable law or agreed to in writing, software\n", + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", + "# See the License for the specific language governing permissions and\n", + "# limitations under the License.\n", + "# ==============================================================================" + ], + "execution_count": 0, + "outputs": [ + { + "output_type": "stream", + "text": [ + "TensorFlow 2.x selected.\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-GaC1zBqtPSG", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Setting up\n", + "from __future__ import absolute_import, division, print_function, unicode_literals\n", + "try:\n", + " # tf_version only exist in colab\n", + " %tensorflow_version 2.x\n", + "\n", + "except Exception:\n", + " pass" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6MD16UIYVt7n", + "colab_type": "text" + }, + "source": [ + "# SETUP" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "A5gKsgrGUZ39", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import tensorflow as tf\n", + "tf.random.set_seed(7)\n", + "import numpy as np\n", + "np.random.seed(7)\n", + "import matplotlib.pyplot as plt\n", + "import os\n", + "import time" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "J0iuziuiVwkA", + "colab_type": "text" + }, + "source": [ + "# HYPERPARAMETERS\n", + "\n", + "X_Train is the array of images, the user wants to use as training data. Labels and test data aren't needed" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ZlWkvpA0UcK5", + "colab_type": "code", + "colab": {} + }, + "source": [ + "EPOCHS = 60\n", + "BATCH_SIZE = 256\n", + "NO_OF_BATCHES = int(X_Train.shape[0]/BATCH_SIZE)\n", + "HALF_BATCH_SIZE = 128\n", + "NOISE_DIM = 100\n", + "# Customised ADAM optimizer\n", + "adam = tf.keras.optimizers.Adam(lr=2e-4,beta_1=0.5) # lr: Learning Rate" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YEnk012DV_Et", + "colab_type": "text" + }, + "source": [ + "# Making up models for GAN" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ewWCDClQUhGo", + "colab_type": "code", + "colab": {} + }, + "source": [ + "from tensorflow.keras.layers import *\n", + "from tensorflow.keras.layers import LeakyReLU\n", + "from tensorflow.keras.models import Sequential, Model" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gMbuIfzhWC6K", + "colab_type": "text" + }, + "source": [ + "# GENERATOR " + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "oXt9-uPMUjUo", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Generator\n", + "generator = Sequential()\n", + "generator.add(Dense(256,input_shape=(NOISE_DIM,)))\n", + "generator.add(LeakyReLU(0.2))\n", + "generator.add(Dense(512))\n", + "generator.add(LeakyReLU(0.2))\n", + "generator.add(Dense(1024))\n", + "generator.add(LeakyReLU(0.2))\n", + "generator.add(Dense(784,activation='tanh'))\n", + "\n", + "# Compile\n", + "generator.compile(loss='binary_crossentropy',optimizer=adam)\n", + "\n", + "generator.summary()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RZownvPRWGT-", + "colab_type": "text" + }, + "source": [ + "# DISCRIMINATOR" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "KAhiXuXWUnyT", + "colab_type": "code", + "colab": {} + }, + "source": [ + "#Discriminator\n", + "discriminator = Sequential()\n", + "discriminator.add(Dense(512,input_shape=(784,)))\n", + "discriminator.add(LeakyReLU(0.2))\n", + "discriminator.add(Dense(256))\n", + "discriminator.add(LeakyReLU(0.2))\n", + "discriminator.add(Dense(1,activation='sigmoid'))\n", + "\n", + "discriminator.compile(loss='binary_crossentropy',optimizer=adam)\n", + "discriminator.summary()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JfnJ1IVIWJt2", + "colab_type": "text" + }, + "source": [ + "# Combined Model" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "VKzNOfNIUpxa", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Combined Model (Geneerator + Discriminator) -> Functional API\n", + "discriminator.trainable = False\n", + "gan_input = Input(shape=(NOISE_DIM,))\n", + "generator_output = generator(gan_input)\n", + "gan_output = discriminator(generator_output)\n", + "\n", + "model = Model(gan_input,gan_output)\n", + "model.compile(loss='binary_crossentropy',optimizer=adam)\n", + "\n", + "model.summary()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "tWMncFAzUs_D", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def showImgs(epoch):\n", + " noise = np.random.normal(0,1,size=(100,NOISE_DIM)) # Random Noise\n", + " generated_imgs = generator.predict(noise)\n", + " generated_imgs = generated_imgs.reshape(-1,28,28)\n", + " \n", + " #Display the Images\n", + " plt.figure(figsize=(10,10))\n", + " for i in range(100):\n", + " plt.subplot(10,10,i+1)\n", + " plt.imshow(generated_imgs[i],cmap='gray',interpolation='nearest')\n", + " plt.axis(\"off\")\n", + " plt.tight_layout()\n", + " plt.show()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "21WSSthiWPuo", + "colab_type": "text" + }, + "source": [ + "# Training" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "TTUSWup6U59U", + "colab_type": "code", + "colab": {} + }, + "source": [ + "d_losses = []\n", + "g_losses = []\n", + "\n", + "# Training Loop\n", + "for epoch in range(EPOCHS+1): \n", + " epoch_d_loss = 0.0\n", + " epoch_g_loss = 0.0\n", + " \n", + " # Mini Batch\n", + " for step in range(NO_OF_BATCHES):\n", + " idx = np.random.randint(0,X_Train.shape[0],HALF_BATCH_SIZE)\n", + " real_imgs = X_Train[idx]\n", + " \n", + " # generate fake images assuming generator is frozen\n", + " noise = np.random.normal(0,1,size=(HALF_BATCH_SIZE,NOISE_DIM))\n", + " fake_imgs = generator.predict(noise)\n", + " \n", + " # Labels\n", + " real_y = np.ones((HALF_BATCH_SIZE,1))*0.9\n", + " fake_y = np.zeros((HALF_BATCH_SIZE,1))\n", + " \n", + " #Train on Real and Fake Images\n", + " d_real_loss = discriminator.train_on_batch(real_imgs,real_y)\n", + " d_fake_loss = discriminator.train_on_batch(fake_imgs,fake_y)\n", + " \n", + " d_loss = 0.5*d_real_loss + 0.5*d_fake_loss\n", + " epoch_d_loss += d_loss\n", + " \n", + " # Train Generator\n", + " noise = np.random.normal(0,1,size=(BATCH_SIZE,NOISE_DIM))\n", + " real_y = np.ones((BATCH_SIZE,1))\n", + " g_loss = model.train_on_batch(noise,real_y)\n", + " epoch_g_loss += g_loss\n", + " \n", + " d_losses.append(epoch_d_loss)\n", + " g_losses.append(epoch_g_loss)\n", + " \n", + " print(\"Epoch %d D Loss %.4f G loss %0.4f \"%((epoch+1),epoch_d_loss,epoch_g_loss))\n", + " if (epoch%5)==0: #printing images after every 5 epocs\n", + " #generator.save(\"model/gen_{0}.h5\".format(epoch)) # Saving the model\n", + " showImgs(epoch) # Show the image transformation progress at every step" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "tP85ZHAvVksH", + "colab_type": "code", + "colab": {} + }, + "source": [ + "" + ], + "execution_count": 0, + "outputs": [] + } + ] +} \ No newline at end of file