{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.7.10","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30132,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# notebook 20 GAN MNIST\nThis notebook is heavily based on [\"Generative Adversarial Network(GAN) using Keras\"](https://medium.datadriveninvestor.com/generative-adversarial-network-gan-using-keras-ce1c05cfdfd3) by Renu Khandelwal.","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nfrom tensorflow import keras\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Dense, Dropout, Input\nfrom tensorflow.keras.models import Model,Sequential\nfrom tensorflow.keras.datasets import mnist\nfrom tqdm import tqdm\nfrom tensorflow.keras.layers import LeakyReLU\nfrom tensorflow.keras.optimizers import Adam","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-11-30T14:13:08.525915Z","iopub.execute_input":"2024-11-30T14:13:08.526184Z","iopub.status.idle":"2024-11-30T14:13:13.435629Z","shell.execute_reply.started":"2024-11-30T14:13:08.526113Z","shell.execute_reply":"2024-11-30T14:13:13.434837Z"},"trusted":true,"_kg_hide-input":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def load_data():\n    (x_train, y_train), (x_test, y_test) = mnist.load_data()\n    x_train = (x_train.astype(np.float32) - 127.5)/127.5\n    \n    # convert shape of x_train from (60000, 28, 28) to (60000, 784) \n    # 784 columns per row\n    x_train = x_train.reshape(60000, 784)\n    return (x_train, y_train, x_test, y_test)\n\n(X_train, y_train,X_test, y_test)=load_data()\nprint(X_train.shape)","metadata":{"execution":{"iopub.status.busy":"2024-11-30T14:13:13.437136Z","iopub.execute_input":"2024-11-30T14:13:13.437371Z","iopub.status.idle":"2024-11-30T14:13:13.876714Z","shell.execute_reply.started":"2024-11-30T14:13:13.437344Z","shell.execute_reply":"2024-11-30T14:13:13.875868Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def adam_optimizer():\n    return Adam(lr=0.0002, beta_1=0.5)","metadata":{"execution":{"iopub.status.busy":"2024-11-30T14:13:13.877659Z","iopub.execute_input":"2024-11-30T14:13:13.877850Z","iopub.status.idle":"2024-11-30T14:13:13.881636Z","shell.execute_reply.started":"2024-11-30T14:13:13.877819Z","shell.execute_reply":"2024-11-30T14:13:13.880826Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def create_generator():\n    generator=Sequential()\n    generator.add(Dense(units=256,input_dim=100))\n    generator.add(LeakyReLU(0.2))\n    \n    generator.add(Dense(units=512))\n    generator.add(LeakyReLU(0.2))\n    \n    generator.add(Dense(units=1024))\n    generator.add(LeakyReLU(0.2))\n    \n    generator.add(Dense(units=784, activation='tanh'))\n    \n    generator.compile(loss='binary_crossentropy', optimizer=adam_optimizer())\n    return generator\ng=create_generator()\ng.summary()","metadata":{"execution":{"iopub.status.busy":"2024-11-30T14:13:13.883281Z","iopub.execute_input":"2024-11-30T14:13:13.883631Z","iopub.status.idle":"2024-11-30T14:13:16.145330Z","shell.execute_reply.started":"2024-11-30T14:13:13.883599Z","shell.execute_reply":"2024-11-30T14:13:16.144691Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def create_discriminator():\n    discriminator=Sequential()\n    discriminator.add(Dense(units=1024,input_dim=784))\n    discriminator.add(LeakyReLU(0.2))\n    discriminator.add(Dropout(0.3))\n       \n    \n    discriminator.add(Dense(units=512))\n    discriminator.add(LeakyReLU(0.2))\n    discriminator.add(Dropout(0.3))\n       \n    discriminator.add(Dense(units=256))\n    discriminator.add(LeakyReLU(0.2))\n    \n    discriminator.add(Dense(units=1, activation='sigmoid'))\n    \n    discriminator.compile(loss='binary_crossentropy', optimizer=adam_optimizer())\n    return discriminator\nd =create_discriminator()\nd.summary()","metadata":{"execution":{"iopub.status.busy":"2024-11-30T14:13:16.146256Z","iopub.execute_input":"2024-11-30T14:13:16.146470Z","iopub.status.idle":"2024-11-30T14:13:16.195735Z","shell.execute_reply.started":"2024-11-30T14:13:16.146444Z","shell.execute_reply":"2024-11-30T14:13:16.195082Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def create_gan(discriminator, generator):\n    discriminator.trainable=False\n    gan_input = Input(shape=(100,))\n    x = generator(gan_input)\n    gan_output= discriminator(x)\n    gan= Model(inputs=gan_input, outputs=gan_output)\n    gan.compile(loss='binary_crossentropy', optimizer='adam')\n    return gan\n\ngan = create_gan(d,g)\ngan.summary()","metadata":{"execution":{"iopub.status.busy":"2024-11-30T14:13:16.196609Z","iopub.execute_input":"2024-11-30T14:13:16.196802Z","iopub.status.idle":"2024-11-30T14:13:16.239627Z","shell.execute_reply.started":"2024-11-30T14:13:16.196778Z","shell.execute_reply":"2024-11-30T14:13:16.238980Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def plot_generated_images(epoch, generator, examples=100, dim=(10,10), figsize=(10,10)):\n    noise= np.random.normal(loc=0, scale=1, size=[examples, 100])\n    generated_images = generator.predict(noise)\n    generated_images = generated_images.reshape(100,28,28)\n    plt.figure(figsize=figsize)\n    for i in range(generated_images.shape[0]):\n        plt.subplot(dim[0], dim[1], i+1)\n        plt.imshow(generated_images[i], interpolation='nearest')\n        plt.axis('off')\n    plt.tight_layout()\n    plt.savefig('gan_generated_image %d.png' %epoch)","metadata":{"execution":{"iopub.status.busy":"2024-11-30T14:13:16.240557Z","iopub.execute_input":"2024-11-30T14:13:16.240742Z","iopub.status.idle":"2024-11-30T14:13:16.246071Z","shell.execute_reply.started":"2024-11-30T14:13:16.240721Z","shell.execute_reply":"2024-11-30T14:13:16.245194Z"},"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def training(epochs=1, batch_size=128):\n    \n    #Loading the data\n    (X_train, y_train, X_test, y_test) = load_data()\n    batch_count = X_train.shape[0] / batch_size\n    \n    # Creating GAN\n    generator= create_generator()\n    discriminator= create_discriminator()\n    gan = create_gan(discriminator, generator)\n    \n    for e in range(1,epochs+1 ):\n        #print(\"Epoch %d\" %e)\n        for _ in range(batch_size):\n        #generate  random noise as an input  to  initialize the  generator\n            noise= np.random.normal(0,1, [batch_size, 100])\n            \n            # Generate fake MNIST images from noised input\n            generated_images = generator.predict(noise)\n            \n            # Get a random set of  real images\n            image_batch =X_train[np.random.randint(low=0,high=X_train.shape[0],size=batch_size)]\n            \n            #Construct different batches of  real and fake data \n            X= np.concatenate([image_batch, generated_images])\n            \n            # Labels for generated and real data\n            y_dis=np.zeros(2*batch_size)\n            y_dis[:batch_size]=0.9\n            \n            #Pre train discriminator on  fake and real data  before starting the gan. \n            discriminator.trainable=True\n            discriminator.train_on_batch(X, y_dis)\n            \n            #Tricking the noised input of the Generator as real data\n            noise= np.random.normal(0,1, [batch_size, 100])\n            y_gen = np.ones(batch_size)\n            \n            # During the training of gan, \n            # the weights of discriminator should be fixed. \n            #We can enforce that by setting the trainable flag\n            discriminator.trainable=False\n            \n            #training  the GAN by alternating the training of the Discriminator \n            #and training the chained GAN model with Discriminator’s weights freezed.\n            gan.train_on_batch(noise, y_gen)\n            \n        if e == 1 or e % 50 == 0:\n            print(\"Epoch\",e)\n            plot_generated_images(e, generator)\n            \ntraining(400,128)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2024-11-30T14:13:16.246988Z","iopub.execute_input":"2024-11-30T14:13:16.247170Z"}},"outputs":[],"execution_count":null}]}