{"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":"# !pip install py7zr","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:36.951912Z","iopub.execute_input":"2023-04-17T13:35:36.952834Z","iopub.status.idle":"2023-04-17T13:35:36.958316Z","shell.execute_reply.started":"2023-04-17T13:35:36.952779Z","shell.execute_reply":"2023-04-17T13:35:36.956706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport glob\nimport random\nimport py7zr\nimport numpy as np\nimport pandas as pd\nfrom PIL import Image\nfrom pathlib import Path\nimport matplotlib.pyplot as plt\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nfrom torch.utils.data import DataLoader\nfrom torchvision import transforms\nfrom torchvision.transforms import ToTensor, Normalize, Compose","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:36.960676Z","iopub.execute_input":"2023-04-17T13:35:36.961556Z","iopub.status.idle":"2023-04-17T13:35:36.970053Z","shell.execute_reply.started":"2023-04-17T13:35:36.961491Z","shell.execute_reply":"2023-04-17T13:35:36.968841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# work_path = '/kaggle/working'\n# if not os.path.exists(work_path):\n#     os.mkdir(work_path)\n# train_file_path = '/kaggle/input/cifar-10/train.7z'\n# test_file_path = '/kaggle/input/cifar-10/test.7z'\n# archive = py7zr.SevenZipFile(train_file_path, mode='r')\n# archive.extractall(path=work_path)\n# archive = py7zr.SevenZipFile(test_file_path, mode='r')\n# archive.extractall(path=work_path)\n# archive.close()","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:36.972373Z","iopub.execute_input":"2023-04-17T13:35:36.973255Z","iopub.status.idle":"2023-04-17T13:35:36.980581Z","shell.execute_reply.started":"2023-04-17T13:35:36.973216Z","shell.execute_reply":"2023-04-17T13:35:36.979220Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Reading csv file for getting labels\nimg_labels = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')\nimg_labels","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:36.982997Z","iopub.execute_input":"2023-04-17T13:35:36.983750Z","iopub.status.idle":"2023-04-17T13:35:37.014038Z","shell.execute_reply.started":"2023-04-17T13:35:36.983710Z","shell.execute_reply":"2023-04-17T13:35:37.013146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_to_num = dict(zip(list(img_labels['label'].unique()), range(len(img_labels['label'].unique()))))\nnum_to_class = {y: x for x, y in class_to_num.items()}\nclass_to_num","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:37.016437Z","iopub.execute_input":"2023-04-17T13:35:37.017101Z","iopub.status.idle":"2023-04-17T13:35:37.032935Z","shell.execute_reply.started":"2023-04-17T13:35:37.017063Z","shell.execute_reply":"2023-04-17T13:35:37.031813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class get_images():\n    def __init__(self, root, transform=None):\n        self.images = list(Path(root).glob('*.png'))\n        self.transform = transform\n    def __len__(self):\n        return len(self.images)\n    def __getitem__(self, idx):\n        img = self.images[idx]\n        label = class_to_num[img_labels.iloc[idx]['label']]\n        if self.transform:\n            img = self.transform(Image.open(img))\n        return img, label","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:37.036376Z","iopub.execute_input":"2023-04-17T13:35:37.036734Z","iopub.status.idle":"2023-04-17T13:35:37.042816Z","shell.execute_reply.started":"2023-04-17T13:35:37.036699Z","shell.execute_reply":"2023-04-17T13:35:37.041805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize(mean=(0.5,0.5,0.5), std=(0.5,0.5,0.5))\n    ])","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:37.044381Z","iopub.execute_input":"2023-04-17T13:35:37.045113Z","iopub.status.idle":"2023-04-17T13:35:37.053098Z","shell.execute_reply.started":"2023-04-17T13:35:37.045069Z","shell.execute_reply":"2023-04-17T13:35:37.052038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_path = '/kaggle/working/train'\nimages_dataset = get_images(img_path, transform=transform)\nprint(\"normalized image: \", images_dataset[0])","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:37.054728Z","iopub.execute_input":"2023-04-17T13:35:37.055313Z","iopub.status.idle":"2023-04-17T13:35:37.444916Z","shell.execute_reply.started":"2023-04-17T13:35:37.055270Z","shell.execute_reply":"2023-04-17T13:35:37.443672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" # Denormalize the input from -1 to 1\ndef denorm(x):\n    out = (x + 1) / 2\n    return out.clamp(0, 1)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:37.446650Z","iopub.execute_input":"2023-04-17T13:35:37.447278Z","iopub.status.idle":"2023-04-17T13:35:37.452659Z","shell.execute_reply.started":"2023-04-17T13:35:37.447239Z","shell.execute_reply":"2023-04-17T13:35:37.451572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 100\ndata_loader = DataLoader(images_dataset, batch_size, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:37.454197Z","iopub.execute_input":"2023-04-17T13:35:37.454651Z","iopub.status.idle":"2023-04-17T13:35:37.463279Z","shell.execute_reply.started":"2023-04-17T13:35:37.454607Z","shell.execute_reply":"2023-04-17T13:35:37.461840Z"},"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-04-17T13:35:37.468696Z","iopub.execute_input":"2023-04-17T13:35:37.469042Z","iopub.status.idle":"2023-04-17T13:35:37.781918Z","shell.execute_reply.started":"2023-04-17T13:35:37.469015Z","shell.execute_reply":"2023-04-17T13:35:37.780961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:37.783267Z","iopub.execute_input":"2023-04-17T13:35:37.783670Z","iopub.status.idle":"2023-04-17T13:35:37.790363Z","shell.execute_reply.started":"2023-04-17T13:35:37.783631Z","shell.execute_reply":"2023-04-17T13:35:37.789198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 3 * 32 * 32\nhidden_size = 256\n\n# Define the discriminator network\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-04-17T13:35:37.791888Z","iopub.execute_input":"2023-04-17T13:35:37.792638Z","iopub.status.idle":"2023-04-17T13:35:37.810650Z","shell.execute_reply.started":"2023-04-17T13:35:37.792601Z","shell.execute_reply":"2023-04-17T13:35:37.809701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"D.to(device)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:37.812004Z","iopub.execute_input":"2023-04-17T13:35:37.812431Z","iopub.status.idle":"2023-04-17T13:35:37.822501Z","shell.execute_reply.started":"2023-04-17T13:35:37.812395Z","shell.execute_reply":"2023-04-17T13:35:37.821306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"latent_size = 64","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:37.824185Z","iopub.execute_input":"2023-04-17T13:35:37.825782Z","iopub.status.idle":"2023-04-17T13:35:37.830266Z","shell.execute_reply.started":"2023-04-17T13:35:37.825734Z","shell.execute_reply":"2023-04-17T13:35:37.829125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the generator network\nG = 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-04-17T13:35:37.831735Z","iopub.execute_input":"2023-04-17T13:35:37.832412Z","iopub.status.idle":"2023-04-17T13:35:37.847496Z","shell.execute_reply.started":"2023-04-17T13:35:37.832374Z","shell.execute_reply":"2023-04-17T13:35:37.846856Z"},"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())\nplt.imshow(gen_imgs[0])","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:37.848838Z","iopub.execute_input":"2023-04-17T13:35:37.849432Z","iopub.status.idle":"2023-04-17T13:35:38.081823Z","shell.execute_reply.started":"2023-04-17T13:35:37.849397Z","shell.execute_reply":"2023-04-17T13:35:38.080858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"G.to(device)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:38.083300Z","iopub.execute_input":"2023-04-17T13:35:38.084085Z","iopub.status.idle":"2023-04-17T13:35:38.093044Z","shell.execute_reply.started":"2023-04-17T13:35:38.084046Z","shell.execute_reply":"2023-04-17T13:35:38.091819Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.BCELoss()\nd_optimizer = optim.Adam(D.parameters(), lr = 2e-3)\ng_optimizer = optim.Adam(D.parameters(), lr = 2e-3)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:38.096231Z","iopub.execute_input":"2023-04-17T13:35:38.097063Z","iopub.status.idle":"2023-04-17T13:35:38.102848Z","shell.execute_reply.started":"2023-04-17T13:35:38.097035Z","shell.execute_reply":"2023-04-17T13:35:38.101585Z"},"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(data):\n    d_loss = 0\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(data)\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-04-17T13:35:38.104615Z","iopub.execute_input":"2023-04-17T13:35:38.105591Z","iopub.status.idle":"2023-04-17T13:35:38.114742Z","shell.execute_reply.started":"2023-04-17T13:35:38.105546Z","shell.execute_reply":"2023-04-17T13:35:38.113627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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    \n    return g_loss, fake_images               ","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:38.116360Z","iopub.execute_input":"2023-04-17T13:35:38.116786Z","iopub.status.idle":"2023-04-17T13:35:38.125903Z","shell.execute_reply.started":"2023-04-17T13:35:38.116747Z","shell.execute_reply":"2023-04-17T13:35:38.124574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_dir = '/kaggle/working/samples'\nif not os.path.exists(sample_dir):\n    os.makedirs(sample_dir)","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:38.126968Z","iopub.execute_input":"2023-04-17T13:35:38.127213Z","iopub.status.idle":"2023-04-17T13:35:38.137952Z","shell.execute_reply.started":"2023-04-17T13:35:38.127189Z","shell.execute_reply":"2023-04-17T13:35:38.136896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Image as image_display\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_display(os.path.join(sample_dir, 'real_images.png'))","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:38.142938Z","iopub.execute_input":"2023-04-17T13:35:38.143206Z","iopub.status.idle":"2023-04-17T13:35:38.274666Z","shell.execute_reply.started":"2023-04-17T13:35:38.143181Z","shell.execute_reply":"2023-04-17T13:35:38.273527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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_display(os.path.join(sample_dir, 'fake_images-0000.png'))","metadata":{"execution":{"iopub.status.busy":"2023-04-17T13:35:38.276146Z","iopub.execute_input":"2023-04-17T13:35:38.277103Z","iopub.status.idle":"2023-04-17T13:35:38.320948Z","shell.execute_reply.started":"2023-04-17T13:35:38.277068Z","shell.execute_reply":"2023-04-17T13:35:38.319908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_epochs = 20\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        # 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) % 100 == 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+1, 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-04-17T13:35:38.322467Z","iopub.execute_input":"2023-04-17T13:35:38.322875Z","iopub.status.idle":"2023-04-17T13:48:39.880539Z","shell.execute_reply.started":"2023-04-17T13:35:38.322842Z","shell.execute_reply":"2023-04-17T13:48:39.879440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-04-17T13:48:39.882192Z","iopub.execute_input":"2023-04-17T13:48:39.882573Z","iopub.status.idle":"2023-04-17T13:48:40.125495Z","shell.execute_reply.started":"2023-04-17T13:48:39.882534Z","shell.execute_reply":"2023-04-17T13:48:40.124468Z"},"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-04-17T13:48:40.126978Z","iopub.execute_input":"2023-04-17T13:48:40.127319Z","iopub.status.idle":"2023-04-17T13:48:40.371385Z","shell.execute_reply.started":"2023-04-17T13:48:40.127281Z","shell.execute_reply":"2023-04-17T13:48:40.370399Z"},"trusted":true},"execution_count":null,"outputs":[]}]}