{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-06-24T12:42:05.873405Z","iopub.execute_input":"2023-06-24T12:42:05.874464Z","iopub.status.idle":"2023-06-24T12:42:05.885112Z","shell.execute_reply.started":"2023-06-24T12:42:05.874410Z","shell.execute_reply":"2023-06-24T12:42:05.883817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n<h1 style=\"font-family:Times New Roman;font-size:35px;\"> <center> Generative Adversial Netwworks: A Beginner's Guide</center> </h1>\n<p><center style=\"color:#159364; font-family:Times New Roman;font-size:30px\">“Change is the end result of all true learning.” — Leo Buscaglia</center></p>\n","metadata":{}},{"cell_type":"markdown","source":"![GAN](https://raw.githubusercontent.com/106AbdulBasit/Kaggle-Projects/main/GAN/Genrative%20Adversial%20Networks.PNG)","metadata":{}},{"cell_type":"markdown","source":"<span style=\"font-family:Times New Roman; font-size:18px;\">There are basically two neural networks. one is Genrator and other is Discriminator </span>\n\n<span style=\"font-family:Times New Roman; font-size:18px;\"> Genrator Network produce the images through the latent space vector.  Then the discriminator network  try to classify that either the image is real or fake. The process will last until genrator able to trick the discriminator in th training loop </span>\n\n\n<span style=\"font-family:Times New Roman; font-size:18px;\">In this tutorial we will see how to implement the GAN in pytorch on CIFAR data set. </span>","metadata":{}},{"cell_type":"markdown","source":"<h1  id=\"1\" style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           background-color:#FFFFFF;\n           font-size:350%;\n           font-family:Times New Roman;\n           letter-spacing:0.5px\">1 Importing some Libraries and dowmloading the data. </h1>","metadata":{}},{"cell_type":"code","source":"import torch\nimport torchvision\nfrom torchvision.transforms import ToTensor, Normalize, Compose\nfrom torchvision.datasets import CIFAR10\n\ncifar = CIFAR10(root='data', \n              train=True, \n              download=True,\n              transform=Compose([ToTensor(), Normalize(mean=(0.5,0.5,0.5), std=(0.5,0.5,0.5))]))","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:05.887570Z","iopub.execute_input":"2023-06-24T12:42:05.888270Z","iopub.status.idle":"2023-06-24T12:42:06.827352Z","shell.execute_reply.started":"2023-06-24T12:42:05.888230Z","shell.execute_reply":"2023-06-24T12:42:06.826242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"font-family:Times New Roman; font-size:18px;\">See the minimum and maximum value of the Tensors</span>","metadata":{}},{"cell_type":"code","source":"img, label = cifar[0]\nprint('Label: ', label)\nprint(img[:,10:15,10:15])\nprint()\ntorch.min(img), torch.max(img)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:06.828912Z","iopub.execute_input":"2023-06-24T12:42:06.829314Z","iopub.status.idle":"2023-06-24T12:42:06.843478Z","shell.execute_reply.started":"2023-06-24T12:42:06.829261Z","shell.execute_reply":"2023-06-24T12:42:06.842173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"font-family:Times New Roman; font-size:18px;\"> We going to denormalize the input from -1 to 1. It  truns out GAN performs better when we denromalize the inputs . This is known as famous GAN hack. More information can be read out in th following link </span>\n\n[GANHACKS](https://github.com/soumith/ganhacks)","metadata":{}},{"cell_type":"code","source":"def denorm(x):\n    out = (x + 1) / 2\n    return out.clamp(0, 1)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:06.846441Z","iopub.execute_input":"2023-06-24T12:42:06.847220Z","iopub.status.idle":"2023-06-24T12:42:06.852296Z","shell.execute_reply.started":"2023-06-24T12:42:06.847180Z","shell.execute_reply":"2023-06-24T12:42:06.851312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"font-family:Times New Roman; font-size:18px;\">  visualize some of the image </span>","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n%matplotlib inline\n\nimg_norm = denorm(img)\nplt.imshow(img_norm[0])\nprint('Label:', label)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:06.853610Z","iopub.execute_input":"2023-06-24T12:42:06.854498Z","iopub.status.idle":"2023-06-24T12:42:07.074748Z","shell.execute_reply.started":"2023-06-24T12:42:06.854452Z","shell.execute_reply":"2023-06-24T12:42:07.073732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''def imshow(img):\n    img = img / 2 + 0.5     # unnormalize\n    npimg = img.numpy()\n    plt.imshow(np.transpose(npimg, (1, 2, 0)))\n    plt.show()'''","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.076325Z","iopub.execute_input":"2023-06-24T12:42:07.076960Z","iopub.status.idle":"2023-06-24T12:42:07.084197Z","shell.execute_reply.started":"2023-06-24T12:42:07.076920Z","shell.execute_reply":"2023-06-24T12:42:07.083060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1  id=\"2\" style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           background-color:#FFFFFF;\n           font-size:350%;\n           font-family:Times New Roman;\n           letter-spacing:0.5px\"> 2 Setting the Data Loader </h1>","metadata":{}},{"cell_type":"code","source":"from torch.utils.data import DataLoader\n\nbatch_size = 100\ndata_loader = DataLoader(cifar, batch_size, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.085694Z","iopub.execute_input":"2023-06-24T12:42:07.086300Z","iopub.status.idle":"2023-06-24T12:42:07.094996Z","shell.execute_reply.started":"2023-06-24T12:42:07.086260Z","shell.execute_reply":"2023-06-24T12:42:07.094055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for img_batch, label_batch in data_loader:\n    print('first batch')\n    print(img_batch.shape)\n    plt.imshow(img_batch[0][0])\n    print(label_batch)\n    break","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.096663Z","iopub.execute_input":"2023-06-24T12:42:07.097098Z","iopub.status.idle":"2023-06-24T12:42:07.324033Z","shell.execute_reply.started":"2023-06-24T12:42:07.097059Z","shell.execute_reply":"2023-06-24T12:42:07.323097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"font-family:Times New Roman; font-size:18px;\">Check is the gpu is availabe </span>","metadata":{}},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.325395Z","iopub.execute_input":"2023-06-24T12:42:07.325871Z","iopub.status.idle":"2023-06-24T12:42:07.331244Z","shell.execute_reply.started":"2023-06-24T12:42:07.325832Z","shell.execute_reply":"2023-06-24T12:42:07.330030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.336691Z","iopub.execute_input":"2023-06-24T12:42:07.337020Z","iopub.status.idle":"2023-06-24T12:42:07.345061Z","shell.execute_reply.started":"2023-06-24T12:42:07.336991Z","shell.execute_reply":"2023-06-24T12:42:07.343987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"font-family:Times New Roman; font-size:18px;\">This is important to understand </span>\n\n<span style=\"font-family:Times New Roman; font-size:18px;\">We are setting the Image_ size to 3072 becuase  </span>\n\n<span style=\"font-family:Times New Roman; font-size:18px;\">Our Images size is 32 * 32 and it has 3 channels. </span>\n\n<span style=\"font-family:Times New Roman; font-size:18px;\">So 32 * 32 * 3 = 3072</span>","metadata":{}},{"cell_type":"code","source":"image_size = 3072\nhidden_size = 256","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.346991Z","iopub.execute_input":"2023-06-24T12:42:07.347394Z","iopub.status.idle":"2023-06-24T12:42:07.352327Z","shell.execute_reply.started":"2023-06-24T12:42:07.347367Z","shell.execute_reply":"2023-06-24T12:42:07.351341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1  id=\"3\" style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           background-color:#FFFFFF;\n           font-size:350%;\n           font-family:Times New Roman;\n           letter-spacing:0.5px\"> 3  Discriminator Network </h1>\n","metadata":{}},{"cell_type":"markdown","source":"<span style=\"font-family:Times New Roman; font-size:18px;\">The discriminator takes an image as input, and tries to classify it as \"real\" or \"generated\". In this sense, it's like any other neural network. While we can use a CNN for the discriminator, we'll use a simple feedforward network with 3 linear layers to keep things since</span>\n","metadata":{}},{"cell_type":"code","source":"import torch.nn as nn\n\nD = nn.Sequential(\n    nn.Linear(image_size, hidden_size),\n    nn.LeakyReLU(0.2),\n    nn.Linear(hidden_size, hidden_size),\n    nn.LeakyReLU(0.2),\n    nn.Linear(hidden_size, 1),\n    nn.Sigmoid())","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.353844Z","iopub.execute_input":"2023-06-24T12:42:07.354513Z","iopub.status.idle":"2023-06-24T12:42:07.372064Z","shell.execute_reply.started":"2023-06-24T12:42:07.354437Z","shell.execute_reply":"2023-06-24T12:42:07.371212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"font-family:Times New Roman; font-size:18px;\">You can see the discriminator network </span>","metadata":{}},{"cell_type":"code","source":"print(D)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.379034Z","iopub.execute_input":"2023-06-24T12:42:07.379635Z","iopub.status.idle":"2023-06-24T12:42:07.385965Z","shell.execute_reply.started":"2023-06-24T12:42:07.379600Z","shell.execute_reply":"2023-06-24T12:42:07.384994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D.to(device);","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.387397Z","iopub.execute_input":"2023-06-24T12:42:07.387994Z","iopub.status.idle":"2023-06-24T12:42:07.397658Z","shell.execute_reply.started":"2023-06-24T12:42:07.387956Z","shell.execute_reply":"2023-06-24T12:42:07.396897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"latent_size = 64","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.398937Z","iopub.execute_input":"2023-06-24T12:42:07.399645Z","iopub.status.idle":"2023-06-24T12:42:07.405847Z","shell.execute_reply.started":"2023-06-24T12:42:07.399608Z","shell.execute_reply":"2023-06-24T12:42:07.405098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1  id=\"4\" style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           background-color:#FFFFFF;\n           font-size:350%;\n           font-family:Times New Roman;\n           letter-spacing:0.5px\"> 4 Generator Network </h1>","metadata":{}},{"cell_type":"markdown","source":"<span style=\"font-family:Times New Roman; font-size:18px;\">The input to the generator is typically a vector or a matrix which is used as a seed for generating an image. Once again, to keep things simple, we'll use a feedfoward neural network with 3 layers </span>","metadata":{}},{"cell_type":"code","source":"G = nn.Sequential(\n    nn.Linear(latent_size, hidden_size),\n    nn.ReLU(),\n    nn.Linear(hidden_size, hidden_size),\n    nn.ReLU(),\n    nn.Linear(hidden_size, image_size),\n    nn.Tanh())","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.407174Z","iopub.execute_input":"2023-06-24T12:42:07.407829Z","iopub.status.idle":"2023-06-24T12:42:07.423177Z","shell.execute_reply.started":"2023-06-24T12:42:07.407779Z","shell.execute_reply":"2023-06-24T12:42:07.422561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = G(torch.randn(2, latent_size))\ny.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.424407Z","iopub.execute_input":"2023-06-24T12:42:07.425011Z","iopub.status.idle":"2023-06-24T12:42:07.433586Z","shell.execute_reply.started":"2023-06-24T12:42:07.424974Z","shell.execute_reply":"2023-06-24T12:42:07.431656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = G(torch.randn(2, latent_size))\ngen_imgs = denorm(y.reshape((-1, 32,32)).detach())","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.435247Z","iopub.execute_input":"2023-06-24T12:42:07.435700Z","iopub.status.idle":"2023-06-24T12:42:07.443024Z","shell.execute_reply.started":"2023-06-24T12:42:07.435664Z","shell.execute_reply":"2023-06-24T12:42:07.441789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"font-family:Times New Roman; font-size:18px;\">Show some Genrated Images </span>","metadata":{}},{"cell_type":"code","source":"plt.imshow(gen_imgs[0]);","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.444706Z","iopub.execute_input":"2023-06-24T12:42:07.445071Z","iopub.status.idle":"2023-06-24T12:42:07.632497Z","shell.execute_reply.started":"2023-06-24T12:42:07.445035Z","shell.execute_reply":"2023-06-24T12:42:07.631537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(gen_imgs[1]);\n","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.633860Z","iopub.execute_input":"2023-06-24T12:42:07.634621Z","iopub.status.idle":"2023-06-24T12:42:07.939673Z","shell.execute_reply.started":"2023-06-24T12:42:07.634582Z","shell.execute_reply":"2023-06-24T12:42:07.938475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"G.to(device);","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.941301Z","iopub.execute_input":"2023-06-24T12:42:07.941682Z","iopub.status.idle":"2023-06-24T12:42:07.949278Z","shell.execute_reply.started":"2023-06-24T12:42:07.941645Z","shell.execute_reply":"2023-06-24T12:42:07.948201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1  id=\"5\" style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           background-color:#FFFFFF;\n           font-size:350%;\n           font-family:Times New Roman;\n           letter-spacing:0.5px\">  5 Discrimanator Training </h1>\n","metadata":{}},{"cell_type":"code","source":"criterion = nn.BCELoss()\nd_optimizer = torch.optim.Adam(D.parameters(), lr=0.0002)\ng_optimizer = torch.optim.Adam(G.parameters(), lr=0.0002)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.951827Z","iopub.execute_input":"2023-06-24T12:42:07.952915Z","iopub.status.idle":"2023-06-24T12:42:07.959180Z","shell.execute_reply.started":"2023-06-24T12:42:07.952875Z","shell.execute_reply":"2023-06-24T12:42:07.958146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def reset_grad():\n    d_optimizer.zero_grad()\n    g_optimizer.zero_grad()\n\ndef train_discriminator(images):\n    # Create the labels which are later used as input for the BCE loss\n    real_labels = torch.ones(batch_size, 1).to(device)\n    fake_labels = torch.zeros(batch_size, 1).to(device)\n        \n    # Loss for real images\n    outputs = D(images)\n    d_loss_real = criterion(outputs, real_labels)\n    real_score = outputs\n\n    # Loss for fake images\n    z = torch.randn(batch_size, latent_size).to(device)\n    fake_images = G(z)\n    outputs = D(fake_images)\n    d_loss_fake = criterion(outputs, fake_labels)\n    fake_score = outputs\n\n    # Combine losses\n    d_loss = d_loss_real + d_loss_fake\n    # Reset gradients\n    reset_grad()\n    # Compute gradients\n    d_loss.backward()\n    # Adjust the parameters using backprop\n    d_optimizer.step()\n    \n    return d_loss, real_score, fake_score","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.960836Z","iopub.execute_input":"2023-06-24T12:42:07.961345Z","iopub.status.idle":"2023-06-24T12:42:07.971560Z","shell.execute_reply.started":"2023-06-24T12:42:07.961307Z","shell.execute_reply":"2023-06-24T12:42:07.970566Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1  id=\"6\" style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           background-color:#FFFFFF;\n           font-size:350%;\n           font-family:Times New Roman;\n           letter-spacing:0.5px\">  6 Generator Training </h1>\n","metadata":{}},{"cell_type":"code","source":"def train_generator():\n    # Generate fake images and calculate loss\n    z = torch.randn(batch_size, latent_size).to(device)\n    fake_images = G(z)\n    labels = torch.ones(batch_size, 1).to(device)\n    g_loss = criterion(D(fake_images), labels)\n\n    # Backprop and optimize\n    reset_grad()\n    g_loss.backward()\n    g_optimizer.step()\n    return g_loss, fake_images","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.973065Z","iopub.execute_input":"2023-06-24T12:42:07.973580Z","iopub.status.idle":"2023-06-24T12:42:07.981008Z","shell.execute_reply.started":"2023-06-24T12:42:07.973542Z","shell.execute_reply":"2023-06-24T12:42:07.979930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1  id=\"7\" style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           background-color:#FFFFFF;\n           font-size:350%;\n           font-family:Times New Roman;\n           letter-spacing:0.5px\"> Training The Model </h1>","metadata":{}},{"cell_type":"code","source":"import os\n\nsample_dir = 'samples'\nif not os.path.exists(sample_dir):\n    os.makedirs(sample_dir)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.982402Z","iopub.execute_input":"2023-06-24T12:42:07.983459Z","iopub.status.idle":"2023-06-24T12:42:07.995461Z","shell.execute_reply.started":"2023-06-24T12:42:07.983421Z","shell.execute_reply":"2023-06-24T12:42:07.994565Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"font-family:Times New Roman; font-size:18px;\">Save The Batch of Real Images </span>","metadata":{}},{"cell_type":"code","source":"from IPython.display import Image\nfrom torchvision.utils import save_image\n\n# Save some real images\nfor images, _ in data_loader:\n    images = images.reshape(images.size(0), 3, 32, 32)\n    save_image(denorm(images), os.path.join(sample_dir, 'real_images.png'), nrow=10)\n    break\n   \nImage(os.path.join(sample_dir, 'real_images.png'))","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:07.996757Z","iopub.execute_input":"2023-06-24T12:42:07.997209Z","iopub.status.idle":"2023-06-24T12:42:08.077769Z","shell.execute_reply.started":"2023-06-24T12:42:07.997168Z","shell.execute_reply":"2023-06-24T12:42:08.076783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"font-family:Times New Roman; font-size:18px;\">We'll also define a helper function to save a batch of generated images to disk at the end of every epoch. We'll use a fixed set of input vectors to the generator to see how the individual generated images evolve over time as we train the model. </span>","metadata":{}},{"cell_type":"code","source":"sample_vectors = torch.randn(batch_size, latent_size).to(device)\n\ndef save_fake_images(index):\n    fake_images = G(sample_vectors)\n    fake_images = fake_images.reshape(fake_images.size(0), 3, 32, 32)\n    fake_fname = 'fake_images-{0:0=4d}.png'.format(index)\n    print('Saving', fake_fname)\n    save_image(denorm(fake_images), os.path.join(sample_dir, fake_fname), nrow=10)\n    \n# Before training\nsave_fake_images(0)\nImage(os.path.join(sample_dir, 'fake_images-0000.png'))","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:08.079248Z","iopub.execute_input":"2023-06-24T12:42:08.079705Z","iopub.status.idle":"2023-06-24T12:42:08.124244Z","shell.execute_reply.started":"2023-06-24T12:42:08.079670Z","shell.execute_reply":"2023-06-24T12:42:08.123377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nnum_epochs = 10\ntotal_step = len(data_loader)\nd_losses, g_losses, real_scores, fake_scores = [], [], [], []\n\nfor epoch in range(num_epochs):\n    for i, (images, _) in enumerate(data_loader):\n        # Load a batch & transform to vectors\n        images = images.reshape(batch_size,-1).to(device)\n        \n        \n        # Train the discriminator and generator\n        d_loss, real_score, fake_score = train_discriminator(images)\n        g_loss, fake_images = train_generator()\n        \n        # Inspect the losses\n        if (i+1) % 200 == 0:\n            d_losses.append(d_loss.item())\n            g_losses.append(g_loss.item())\n            real_scores.append(real_score.mean().item())\n            fake_scores.append(fake_score.mean().item())\n            print('Epoch [{}/{}], Step [{}/{}], d_loss: {:.4f}, g_loss: {:.4f}, D(x): {:.2f}, D(G(z)): {:.2f}' \n                  .format(epoch, num_epochs, i+1, total_step, d_loss.item(), g_loss.item(), \n                          real_score.mean().item(), fake_score.mean().item()))\n        \n    # Sample and save images\n    save_fake_images(epoch+1)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:42:08.131152Z","iopub.execute_input":"2023-06-24T12:42:08.131541Z","iopub.status.idle":"2023-06-24T12:44:27.218612Z","shell.execute_reply.started":"2023-06-24T12:42:08.131488Z","shell.execute_reply":"2023-06-24T12:44:27.217559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(G.state_dict(), 'G.ckpt')\ntorch.save(D.state_dict(), 'D.ckpt')","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:44:27.220220Z","iopub.execute_input":"2023-06-24T12:44:27.220605Z","iopub.status.idle":"2023-06-24T12:44:27.247884Z","shell.execute_reply.started":"2023-06-24T12:44:27.220565Z","shell.execute_reply":"2023-06-24T12:44:27.246907Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image('./samples/fake_images-0010.png')","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:44:27.249446Z","iopub.execute_input":"2023-06-24T12:44:27.249853Z","iopub.status.idle":"2023-06-24T12:44:27.264442Z","shell.execute_reply.started":"2023-06-24T12:44:27.249814Z","shell.execute_reply":"2023-06-24T12:44:27.263462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1  id=\"8\" style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           background-color:#FFFFFF;\n           font-size:350%;\n           font-family:Times New Roman;\n           letter-spacing:0.5px\">  8 Visualizing The Training Process </h1>","metadata":{}},{"cell_type":"code","source":"Image('./samples/fake_images-0010.png')","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:44:57.674874Z","iopub.execute_input":"2023-06-24T12:44:57.675260Z","iopub.status.idle":"2023-06-24T12:44:57.692076Z","shell.execute_reply.started":"2023-06-24T12:44:57.675226Z","shell.execute_reply":"2023-06-24T12:44:57.691199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image('./samples/fake_images-0009.png')","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:45:26.214041Z","iopub.execute_input":"2023-06-24T12:45:26.214435Z","iopub.status.idle":"2023-06-24T12:45:26.227594Z","shell.execute_reply.started":"2023-06-24T12:45:26.214399Z","shell.execute_reply":"2023-06-24T12:45:26.226705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimport os\nfrom IPython.display import FileLink\n\nvid_fname = 'gans_training.avi'\n\nfiles = [os.path.join(sample_dir, f) for f in os.listdir(sample_dir) if 'fake_images' in f]\nfiles.sort()\n","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:45:38.898577Z","iopub.execute_input":"2023-06-24T12:45:38.898993Z","iopub.status.idle":"2023-06-24T12:45:38.907042Z","shell.execute_reply.started":"2023-06-24T12:45:38.898956Z","shell.execute_reply":"2023-06-24T12:45:38.905673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vid_fname = 'gans_training.avi'\n\nout = cv2.VideoWriter(vid_fname,cv2.VideoWriter_fourcc(*'mp4v'), 8, (342,342))\n\n[out.write(cv2.imread(fname)) for fname in files]\nout.release()\nFileLink('gans_training.avi')","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:45:44.017620Z","iopub.execute_input":"2023-06-24T12:45:44.018009Z","iopub.status.idle":"2023-06-24T12:45:44.085328Z","shell.execute_reply.started":"2023-06-24T12:45:44.017974Z","shell.execute_reply":"2023-06-24T12:45:44.084087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(d_losses)\nprint('\\n')\nprint(g_losses)","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:47:30.647132Z","iopub.execute_input":"2023-06-24T12:47:30.647551Z","iopub.status.idle":"2023-06-24T12:47:30.653895Z","shell.execute_reply.started":"2023-06-24T12:47:30.647494Z","shell.execute_reply":"2023-06-24T12:47:30.652778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<h1  id=\"9\" style=\"color:black;\n           display:fill;\n           border-radius:5px;\n           background-color:#FFFFFF;\n           font-size:350%;\n           font-family:Times New Roman;\n           letter-spacing:0.5px\"> 9 loss and real scores </h1>","metadata":{}},{"cell_type":"code","source":"plt.plot(d_losses, '-')\nplt.plot(g_losses, '-')\nplt.xlabel('epoch')\nplt.ylabel('loss')\nplt.legend(['Discriminator', 'Generator'])\nplt.title('Losses');","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:45:49.549170Z","iopub.execute_input":"2023-06-24T12:45:49.549591Z","iopub.status.idle":"2023-06-24T12:45:49.772379Z","shell.execute_reply.started":"2023-06-24T12:45:49.549552Z","shell.execute_reply":"2023-06-24T12:45:49.771440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(real_scores, '-')\nplt.plot(fake_scores, '-')\nplt.xlabel('epoch')\nplt.ylabel('score')\nplt.legend(['Real Score', 'Fake score'])\nplt.title('Scores');","metadata":{"execution":{"iopub.status.busy":"2023-06-24T12:45:57.526317Z","iopub.execute_input":"2023-06-24T12:45:57.527018Z","iopub.status.idle":"2023-06-24T12:45:57.742391Z","shell.execute_reply.started":"2023-06-24T12:45:57.526971Z","shell.execute_reply":"2023-06-24T12:45:57.741428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<span style=\"font-family:Times New Roman; font-size:18px;\">Refrences </span>\n\n[Jovian   GAN]( https://jovian.ai/aakashns/06-mnist-gan)\n\n[Pytorch Tutorial](https://jovian.ai/outlink?url=https%3A%2F%2Fgithub.com%2Fyunjey%2Fpytorch-tutorial)","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}