{"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":"cp -r ../input/gan-getting-started ./","metadata":{"execution":{"iopub.status.busy":"2022-01-08T16:31:01.206347Z","iopub.execute_input":"2022-01-08T16:31:01.206668Z","iopub.status.idle":"2022-01-08T16:32:05.302669Z","shell.execute_reply.started":"2022-01-08T16:31:01.206596Z","shell.execute_reply":"2022-01-08T16:32:05.301425Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\n\nDATA_DIR = './gan-getting-started'\nprint(os.listdir(DATA_DIR))","metadata":{"gradient":{"editing":false,"execution_count":69,"id":"d9a3464f","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T16:32:05.305482Z","iopub.execute_input":"2022-01-08T16:32:05.30586Z","iopub.status.idle":"2022-01-08T16:32:05.31575Z","shell.execute_reply.started":"2022-01-08T16:32:05.305814Z","shell.execute_reply":"2022-01-08T16:32:05.313376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport sys\nimport shutil\n\n# Get directory name\nmydir= './gan-getting-started/monet_tfrec'\n## Try to remove tree; if failed show an error using try...except on screen\ntry:\n    shutil.rmtree(mydir)\nexcept OSError as e:\n    print (\"Error: %s - %s.\" % (e.filename, e.strerror))","metadata":{"execution":{"iopub.status.busy":"2022-01-08T16:32:11.577334Z","iopub.execute_input":"2022-01-08T16:32:11.57763Z","iopub.status.idle":"2022-01-08T16:32:11.587727Z","shell.execute_reply.started":"2022-01-08T16:32:11.577576Z","shell.execute_reply":"2022-01-08T16:32:11.586141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport sys\nimport shutil\n\n# Get directory name\nmydir= './gan-getting-started/photo_tfrec'\n\n## Try to remove tree; if failed show an error using try...except on screen\ntry:\n    shutil.rmtree(mydir)\nexcept OSError as e:\n    print (\"Error: %s - %s.\" % (e.filename, e.strerror))","metadata":{"execution":{"iopub.status.busy":"2022-01-08T15:45:13.787875Z","iopub.execute_input":"2022-01-08T15:45:13.78897Z","iopub.status.idle":"2022-01-08T15:45:13.843716Z","shell.execute_reply.started":"2022-01-08T15:45:13.788926Z","shell.execute_reply":"2022-01-08T15:45:13.842583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(os.listdir(DATA_DIR)[:10])","metadata":{"gradient":{"editing":false,"execution_count":70,"id":"e4e00c52","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:13.845041Z","iopub.execute_input":"2022-01-08T15:45:13.84609Z","iopub.status.idle":"2022-01-08T15:45:13.853129Z","shell.execute_reply.started":"2022-01-08T15:45:13.846033Z","shell.execute_reply":"2022-01-08T15:45:13.851759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader\nfrom torchvision.datasets import ImageFolder\nimport torchvision.transforms as T","metadata":{"gradient":{"editing":false,"execution_count":71,"id":"57a414f4","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:13.855132Z","iopub.execute_input":"2022-01-08T15:45:13.856039Z","iopub.status.idle":"2022-01-08T15:45:15.755829Z","shell.execute_reply.started":"2022-01-08T15:45:13.855993Z","shell.execute_reply":"2022-01-08T15:45:15.754871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 64\nbatch_size = 128\nstats = (0.5, 0.5, 0.5), (0.5, 0.5, 0.5)","metadata":{"gradient":{"editing":false,"execution_count":72,"id":"d3e00788","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:15.757698Z","iopub.execute_input":"2022-01-08T15:45:15.758005Z","iopub.status.idle":"2022-01-08T15:45:15.763203Z","shell.execute_reply.started":"2022-01-08T15:45:15.757962Z","shell.execute_reply":"2022-01-08T15:45:15.762184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = ImageFolder(DATA_DIR, transform=T.Compose([\n    T.Resize(image_size),\n    T.CenterCrop(image_size),\n    T.ToTensor(),\n    T.Normalize(*stats)]))\n\ntrain_dl = DataLoader(train_ds, batch_size, shuffle=True, num_workers=3, pin_memory=True)","metadata":{"gradient":{"editing":false,"execution_count":73,"id":"870c97b1","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:15.765208Z","iopub.execute_input":"2022-01-08T15:45:15.766038Z","iopub.status.idle":"2022-01-08T15:45:15.825823Z","shell.execute_reply.started":"2022-01-08T15:45:15.765993Z","shell.execute_reply":"2022-01-08T15:45:15.824586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nfrom torchvision.utils import make_grid\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"gradient":{"editing":false,"execution_count":74,"id":"8a0b35e0","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:15.830619Z","iopub.execute_input":"2022-01-08T15:45:15.830897Z","iopub.status.idle":"2022-01-08T15:45:15.837682Z","shell.execute_reply.started":"2022-01-08T15:45:15.830865Z","shell.execute_reply":"2022-01-08T15:45:15.836378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def denorm(img_tensors):\n    return img_tensors * stats[1][0] + stats[0][0]","metadata":{"gradient":{"editing":false,"execution_count":75,"id":"2c64e65f","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:15.839921Z","iopub.execute_input":"2022-01-08T15:45:15.840818Z","iopub.status.idle":"2022-01-08T15:45:15.849015Z","shell.execute_reply.started":"2022-01-08T15:45:15.84077Z","shell.execute_reply":"2022-01-08T15:45:15.847766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def show_images(images, nmax=64):\n    fig, ax = plt.subplots(figsize=(8, 8))\n    ax.set_xticks([]); ax.set_yticks([])\n    ax.imshow(make_grid(denorm(images.detach()[:nmax]), nrow=8).permute(1, 2, 0))\n\ndef show_batch(dl, nmax=64):\n    for images, _ in dl:\n        show_images(images, nmax)\n        break","metadata":{"gradient":{"execution_count":76,"id":"e8933334","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:15.850518Z","iopub.execute_input":"2022-01-08T15:45:15.851024Z","iopub.status.idle":"2022-01-08T15:45:15.862034Z","shell.execute_reply.started":"2022-01-08T15:45:15.850978Z","shell.execute_reply":"2022-01-08T15:45:15.860994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"show_batch(train_dl)","metadata":{"gradient":{"execution_count":77,"id":"7fb56eaa","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:15.863603Z","iopub.execute_input":"2022-01-08T15:45:15.86479Z","iopub.status.idle":"2022-01-08T15:45:20.847362Z","shell.execute_reply.started":"2022-01-08T15:45:15.864745Z","shell.execute_reply":"2022-01-08T15:45:20.846506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_default_device():\n    \"\"\"Pick GPU if available, else CPU\"\"\"\n    if torch.cuda.is_available():\n        return torch.device('cuda')\n    else:\n        return torch.device('cpu')\n    \ndef to_device(data, device):\n    \"\"\"Move tensor(s) to chosen device\"\"\"\n    if isinstance(data, (list,tuple)):\n        return [to_device(x, device) for x in data]\n    return data.to(device, non_blocking=True)\n\nclass DeviceDataLoader():\n    \"\"\"Wrap a dataloader to move data to a device\"\"\"\n    def __init__(self, dl, device):\n        self.dl = dl\n        self.device = device\n        \n    def __iter__(self):\n        \"\"\"Yield a batch of data after moving it to device\"\"\"\n        for b in self.dl: \n            yield to_device(b, self.device)\n\n    def __len__(self):\n        \"\"\"Number of batches\"\"\"\n        return len(self.dl)","metadata":{"gradient":{"execution_count":78,"id":"8eb9181c","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:20.848869Z","iopub.execute_input":"2022-01-08T15:45:20.850811Z","iopub.status.idle":"2022-01-08T15:45:20.863408Z","shell.execute_reply.started":"2022-01-08T15:45:20.850759Z","shell.execute_reply":"2022-01-08T15:45:20.862076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = get_default_device()\ndevice","metadata":{"gradient":{"execution_count":79,"id":"78c767fd","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:20.865902Z","iopub.execute_input":"2022-01-08T15:45:20.867001Z","iopub.status.idle":"2022-01-08T15:45:20.879965Z","shell.execute_reply.started":"2022-01-08T15:45:20.866695Z","shell.execute_reply":"2022-01-08T15:45:20.878805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dl = DeviceDataLoader(train_dl, device)","metadata":{"gradient":{"execution_count":80,"id":"d71856fa","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:20.882279Z","iopub.execute_input":"2022-01-08T15:45:20.88279Z","iopub.status.idle":"2022-01-08T15:45:20.889208Z","shell.execute_reply.started":"2022-01-08T15:45:20.882747Z","shell.execute_reply":"2022-01-08T15:45:20.887952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn","metadata":{"gradient":{"execution_count":81,"id":"50bd86ec","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:20.891786Z","iopub.execute_input":"2022-01-08T15:45:20.8924Z","iopub.status.idle":"2022-01-08T15:45:20.89952Z","shell.execute_reply.started":"2022-01-08T15:45:20.89213Z","shell.execute_reply":"2022-01-08T15:45:20.898695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"discriminator = nn.Sequential(\n    # in: 3 x 64 x 64\n\n    nn.Conv2d(3, 64, kernel_size=4, stride=2, padding=1, bias=False),\n    nn.BatchNorm2d(64),\n    nn.LeakyReLU(0.2, inplace=True),\n    # out: 64 x 32 x 32\n\n    nn.Conv2d(64, 128, kernel_size=4, stride=2, padding=1, bias=False),\n    nn.BatchNorm2d(128),\n    nn.LeakyReLU(0.2, inplace=True),\n    # out: 128 x 16 x 16\n\n    nn.Conv2d(128, 256, kernel_size=4, stride=2, padding=1, bias=False),\n    nn.BatchNorm2d(256),\n    nn.LeakyReLU(0.2, inplace=True),\n    # out: 256 x 8 x 8\n\n    nn.Conv2d(256, 512, kernel_size=4, stride=2, padding=1, bias=False),\n    nn.BatchNorm2d(512),\n    nn.LeakyReLU(0.2, inplace=True),\n    # out: 512 x 4 x 4\n\n    nn.Conv2d(512, 1, kernel_size=4, stride=1, padding=0, bias=False),\n    # out: 1 x 1 x 1\n\n    nn.Flatten(),\n    nn.Sigmoid())","metadata":{"gradient":{"execution_count":82,"id":"22af9baa","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:20.901632Z","iopub.execute_input":"2022-01-08T15:45:20.902049Z","iopub.status.idle":"2022-01-08T15:45:20.973241Z","shell.execute_reply.started":"2022-01-08T15:45:20.902005Z","shell.execute_reply":"2022-01-08T15:45:20.971719Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"discriminator = to_device(discriminator, device)","metadata":{"gradient":{"execution_count":83,"id":"d84307d2","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:20.976549Z","iopub.execute_input":"2022-01-08T15:45:20.977434Z","iopub.status.idle":"2022-01-08T15:45:21.006705Z","shell.execute_reply.started":"2022-01-08T15:45:20.977338Z","shell.execute_reply":"2022-01-08T15:45:21.005641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"latent_size = 128","metadata":{"gradient":{"execution_count":84,"id":"d42eaebe","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:21.012058Z","iopub.execute_input":"2022-01-08T15:45:21.012802Z","iopub.status.idle":"2022-01-08T15:45:21.02135Z","shell.execute_reply.started":"2022-01-08T15:45:21.012751Z","shell.execute_reply":"2022-01-08T15:45:21.019836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generator = nn.Sequential(\n    # in: latent_size x 1 x 1\n\n    nn.ConvTranspose2d(latent_size, 512, kernel_size=4, stride=1, padding=0, bias=False),\n    nn.BatchNorm2d(512),\n    nn.ReLU(True),\n    # out: 512 x 4 x 4\n\n    nn.ConvTranspose2d(512, 256, kernel_size=4, stride=2, padding=1, bias=False),\n    nn.BatchNorm2d(256),\n    nn.ReLU(True),\n    # out: 256 x 8 x 8\n\n    nn.ConvTranspose2d(256, 128, kernel_size=4, stride=2, padding=1, bias=False),\n    nn.BatchNorm2d(128),\n    nn.ReLU(True),\n    # out: 128 x 16 x 16\n\n    nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1, bias=False),\n    nn.BatchNorm2d(64),\n    nn.ReLU(True),\n    # out: 64 x 32 x 32\n\n    nn.ConvTranspose2d(64, 3, kernel_size=4, stride=2, padding=1, bias=False),\n    nn.Tanh()\n    # out: 3 x 64 x 64\n)","metadata":{"gradient":{"execution_count":85,"id":"b9bd7cf2","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:21.024384Z","iopub.execute_input":"2022-01-08T15:45:21.025119Z","iopub.status.idle":"2022-01-08T15:45:21.099564Z","shell.execute_reply.started":"2022-01-08T15:45:21.025073Z","shell.execute_reply":"2022-01-08T15:45:21.098662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xb = torch.randn(batch_size, latent_size, 1, 1) # random latent tensors\nfake_images = generator(xb)\nprint(fake_images.shape)\nshow_images(fake_images)","metadata":{"gradient":{"execution_count":86,"id":"b0bf7429","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:21.101236Z","iopub.execute_input":"2022-01-08T15:45:21.101746Z","iopub.status.idle":"2022-01-08T15:45:22.587689Z","shell.execute_reply.started":"2022-01-08T15:45:21.10168Z","shell.execute_reply":"2022-01-08T15:45:22.586846Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"generator = to_device(generator, device)","metadata":{"gradient":{"execution_count":87,"id":"3a1ecdd2","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:22.589157Z","iopub.execute_input":"2022-01-08T15:45:22.590044Z","iopub.status.idle":"2022-01-08T15:45:22.600337Z","shell.execute_reply.started":"2022-01-08T15:45:22.590002Z","shell.execute_reply":"2022-01-08T15:45:22.598911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_discriminator(real_images, opt_d):\n    # Clear discriminator gradients\n    opt_d.zero_grad()\n\n    # Pass real images through discriminator\n    real_preds = discriminator(real_images)\n    real_targets = torch.ones(real_images.size(0), 1, device=device)\n    real_loss = F.binary_cross_entropy(real_preds, real_targets)\n    real_score = torch.mean(real_preds).item()\n    \n    # Generate fake images\n    latent = torch.randn(batch_size, latent_size, 1, 1, device=device)\n    fake_images = generator(latent)\n\n    # Pass fake images through discriminator\n    fake_targets = torch.zeros(fake_images.size(0), 1, device=device)\n    fake_preds = discriminator(fake_images)\n    fake_loss = F.binary_cross_entropy(fake_preds, fake_targets)\n    fake_score = torch.mean(fake_preds).item()\n\n    # Update discriminator weights\n    loss = real_loss + fake_loss\n    loss.backward()\n    opt_d.step()\n    return loss.item(), real_score, fake_score","metadata":{"gradient":{"execution_count":88,"id":"20928e71","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:22.602155Z","iopub.execute_input":"2022-01-08T15:45:22.602535Z","iopub.status.idle":"2022-01-08T15:45:22.614518Z","shell.execute_reply.started":"2022-01-08T15:45:22.602495Z","shell.execute_reply":"2022-01-08T15:45:22.613266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train_generator(opt_g):\n    # Clear generator gradients\n    opt_g.zero_grad()\n    \n    # Generate fake images\n    latent = torch.randn(batch_size, latent_size, 1, 1, device=device)\n    fake_images = generator(latent)\n    \n    # Try to fool the discriminator\n    preds = discriminator(fake_images)\n    targets = torch.ones(batch_size, 1, device=device)\n    loss = F.binary_cross_entropy(preds, targets)\n    \n    # Update generator weights\n    loss.backward()\n    opt_g.step()\n    \n    return loss.item()","metadata":{"gradient":{"execution_count":89,"id":"2bcc7b3e","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:22.61785Z","iopub.execute_input":"2022-01-08T15:45:22.618399Z","iopub.status.idle":"2022-01-08T15:45:22.62749Z","shell.execute_reply.started":"2022-01-08T15:45:22.61815Z","shell.execute_reply":"2022-01-08T15:45:22.626388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchvision.utils import save_image","metadata":{"gradient":{"execution_count":90,"id":"1ccdaf5f","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:22.629709Z","iopub.execute_input":"2022-01-08T15:45:22.630424Z","iopub.status.idle":"2022-01-08T15:45:22.641055Z","shell.execute_reply.started":"2022-01-08T15:45:22.63037Z","shell.execute_reply":"2022-01-08T15:45:22.639841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_dir = 'generated'\nos.makedirs(sample_dir, exist_ok=True)","metadata":{"gradient":{"execution_count":91,"id":"573f1b24","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:22.643169Z","iopub.execute_input":"2022-01-08T15:45:22.643814Z","iopub.status.idle":"2022-01-08T15:45:22.652026Z","shell.execute_reply.started":"2022-01-08T15:45:22.643769Z","shell.execute_reply":"2022-01-08T15:45:22.650767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def save_samples(index, latent_tensors, show=True):\n    fake_images = generator(latent_tensors)\n    fake_fname = 'generated-images-{0:0=4d}.png'.format(index)\n    save_image(denorm(fake_images), os.path.join(sample_dir, fake_fname), nrow=8)\n   # print('Saving', fake_fname)\n    if show:\n        fig, ax = plt.subplots(figsize=(8, 8))\n        ax.set_xticks([]); ax.set_yticks([])\n        ax.imshow(make_grid(fake_images.cpu().detach(), nrow=8).permute(1, 2, 0))","metadata":{"gradient":{"execution_count":92,"id":"5104bdf6","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:22.659109Z","iopub.execute_input":"2022-01-08T15:45:22.659335Z","iopub.status.idle":"2022-01-08T15:45:22.66772Z","shell.execute_reply.started":"2022-01-08T15:45:22.659306Z","shell.execute_reply":"2022-01-08T15:45:22.666538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fixed_latent = torch.randn(64, latent_size, 1, 1, device=device)","metadata":{"gradient":{"execution_count":93,"id":"cb31c916","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:22.669235Z","iopub.execute_input":"2022-01-08T15:45:22.670178Z","iopub.status.idle":"2022-01-08T15:45:22.679239Z","shell.execute_reply.started":"2022-01-08T15:45:22.670132Z","shell.execute_reply":"2022-01-08T15:45:22.678206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_samples(0, fixed_latent)","metadata":{"gradient":{"execution_count":94,"id":"53ee4703","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:22.680957Z","iopub.execute_input":"2022-01-08T15:45:22.681399Z","iopub.status.idle":"2022-01-08T15:45:28.123648Z","shell.execute_reply.started":"2022-01-08T15:45:22.681352Z","shell.execute_reply":"2022-01-08T15:45:28.122739Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nimport torch.nn.functional as F","metadata":{"gradient":{"execution_count":95,"id":"a2e75bd9","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:28.12519Z","iopub.execute_input":"2022-01-08T15:45:28.125975Z","iopub.status.idle":"2022-01-08T15:45:28.131314Z","shell.execute_reply.started":"2022-01-08T15:45:28.125916Z","shell.execute_reply":"2022-01-08T15:45:28.130112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def fit(epochs, lr, start_idx=1):\n    torch.cuda.empty_cache()\n    \n    # Losses & scores\n    losses_g = []\n    losses_d = []\n    real_scores = []\n    fake_scores = []\n    \n    # Create optimizers\n    opt_d = torch.optim.Adam(discriminator.parameters(), lr=lr, betas=(0.5, 0.999))\n    opt_g = torch.optim.Adam(generator.parameters(), lr=lr, betas=(0.5, 0.999))\n    \n    for epoch in range(epochs):\n        for real_images, _ in tqdm(train_dl):\n            # Train discriminator\n            loss_d, real_score, fake_score = train_discriminator(real_images, opt_d)\n            # Train generator\n            loss_g = train_generator(opt_g)\n            \n        # Record losses & scores\n        losses_g.append(loss_g)\n        losses_d.append(loss_d)\n        real_scores.append(real_score)\n        fake_scores.append(fake_score)\n        \n        # Log losses & scores (last batch)\n        print(\"Epoch [{}/{}], loss_g: {:.4f}, loss_d: {:.4f}, real_score: {:.4f}, fake_score: {:.4f}\".format(\n             epoch+1, epochs, loss_g, loss_d, real_score, fake_score))\n    \n        # Save generated images\n        save_samples(epoch+start_idx, fixed_latent, show=False)\n    \n    return losses_g, losses_d, real_scores, fake_scores","metadata":{"gradient":{"execution_count":96,"id":"4fa06ac5","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:28.133956Z","iopub.execute_input":"2022-01-08T15:45:28.134336Z","iopub.status.idle":"2022-01-08T15:45:28.150747Z","shell.execute_reply.started":"2022-01-08T15:45:28.134281Z","shell.execute_reply":"2022-01-08T15:45:28.147891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr = 0.0002\nepochs = 25","metadata":{"gradient":{"execution_count":105,"id":"6e8592b4","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:28.153269Z","iopub.execute_input":"2022-01-08T15:45:28.15393Z","iopub.status.idle":"2022-01-08T15:45:28.164146Z","shell.execute_reply.started":"2022-01-08T15:45:28.153891Z","shell.execute_reply":"2022-01-08T15:45:28.162804Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc \ngc.collect()","metadata":{"gradient":{"execution_count":106,"id":"4022105c","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:28.165792Z","iopub.execute_input":"2022-01-08T15:45:28.167817Z","iopub.status.idle":"2022-01-08T15:45:28.307311Z","shell.execute_reply.started":"2022-01-08T15:45:28.167773Z","shell.execute_reply":"2022-01-08T15:45:28.305981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"save_samples(0, fixed_latent)","metadata":{"execution":{"iopub.status.busy":"2022-01-08T16:08:01.836222Z","iopub.status.idle":"2022-01-08T16:08:01.837249Z","shell.execute_reply.started":"2022-01-08T16:08:01.836941Z","shell.execute_reply":"2022-01-08T16:08:01.836972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = fit(epochs, lr)","metadata":{"gradient":{"id":"35c71e12","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T15:45:28.30976Z","iopub.execute_input":"2022-01-08T15:45:28.310303Z","iopub.status.idle":"2022-01-08T15:52:06.802656Z","shell.execute_reply.started":"2022-01-08T15:45:28.310093Z","shell.execute_reply":"2022-01-08T15:52:06.80107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from IPython.display import Image","metadata":{"execution":{"iopub.status.busy":"2022-01-08T15:52:06.804708Z","iopub.execute_input":"2022-01-08T15:52:06.804957Z","iopub.status.idle":"2022-01-08T15:52:06.811851Z","shell.execute_reply.started":"2022-01-08T15:52:06.804926Z","shell.execute_reply":"2022-01-08T15:52:06.810578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls generated","metadata":{"execution":{"iopub.status.busy":"2022-01-08T16:00:53.402646Z","iopub.execute_input":"2022-01-08T16:00:53.402974Z","iopub.status.idle":"2022-01-08T16:00:54.18307Z","shell.execute_reply.started":"2022-01-08T16:00:53.402945Z","shell.execute_reply":"2022-01-08T16:00:54.181947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image('generated/generated-images-0025.png')","metadata":{"execution":{"iopub.status.busy":"2022-01-08T16:00:49.077511Z","iopub.execute_input":"2022-01-08T16:00:49.077816Z","iopub.status.idle":"2022-01-08T16:00:49.098329Z","shell.execute_reply.started":"2022-01-08T16:00:49.077785Z","shell.execute_reply":"2022-01-08T16:00:49.097581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr = 0.0002\nepochs = 25","metadata":{"execution":{"iopub.status.busy":"2022-01-08T16:01:18.630943Z","iopub.execute_input":"2022-01-08T16:01:18.631314Z","iopub.status.idle":"2022-01-08T16:01:18.637008Z","shell.execute_reply.started":"2022-01-08T16:01:18.631283Z","shell.execute_reply":"2022-01-08T16:01:18.635529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = fit(epochs, lr)","metadata":{"execution":{"iopub.status.busy":"2022-01-08T15:52:07.84975Z","iopub.execute_input":"2022-01-08T15:52:07.850272Z","iopub.status.idle":"2022-01-08T15:58:41.909556Z","shell.execute_reply.started":"2022-01-08T15:52:07.850211Z","shell.execute_reply":"2022-01-08T15:58:41.906728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = fit(epochs, lr)","metadata":{"execution":{"iopub.status.busy":"2022-01-08T16:01:28.351807Z","iopub.execute_input":"2022-01-08T16:01:28.352309Z","iopub.status.idle":"2022-01-08T16:08:01.802462Z","shell.execute_reply.started":"2022-01-08T16:01:28.352273Z","shell.execute_reply":"2022-01-08T16:08:01.801498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = fit(epochs, lr)","metadata":{"execution":{"iopub.status.busy":"2022-01-08T16:09:16.114649Z","iopub.execute_input":"2022-01-08T16:09:16.114932Z","iopub.status.idle":"2022-01-08T16:15:48.043246Z","shell.execute_reply.started":"2022-01-08T16:09:16.114901Z","shell.execute_reply":"2022-01-08T16:15:48.041665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Image('generated/generated-images-0025.png')","metadata":{"execution":{"iopub.status.busy":"2022-01-08T16:16:05.151736Z","iopub.execute_input":"2022-01-08T16:16:05.152048Z","iopub.status.idle":"2022-01-08T16:16:05.175306Z","shell.execute_reply.started":"2022-01-08T16:16:05.152002Z","shell.execute_reply":"2022-01-08T16:16:05.174288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(generator.state_dict(), 'G.pth')\ntorch.save(discriminator.state_dict(), 'D.pth')","metadata":{"gradient":{"id":"e8737e30","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T16:15:48.046155Z","iopub.execute_input":"2022-01-08T16:15:48.046497Z","iopub.status.idle":"2022-01-08T16:15:48.127464Z","shell.execute_reply.started":"2022-01-08T16:15:48.046452Z","shell.execute_reply":"2022-01-08T16:15:48.126383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import cv2\n# import os\n\n# vid_fname = 'gans_training.avi'\n\n# files = [os.path.join(sample_dir, f) for f in os.listdir(sample_dir) if 'generated' in f]\n# files.sort()\n\n# out = cv2.VideoWriter(vid_fname,cv2.VideoWriter_fourcc(*'MP4V'), 1, (530,530))\n# [out.write(cv2.imread(fname)) for fname in files]\n# out.release()","metadata":{"gradient":{"id":"d71a5e96","kernelId":"c2f0dd13-4efc-4678-9dd3-f02b7c47a68c"},"execution":{"iopub.status.busy":"2022-01-08T16:15:48.129222Z","iopub.execute_input":"2022-01-08T16:15:48.129684Z","iopub.status.idle":"2022-01-08T16:15:48.135901Z","shell.execute_reply.started":"2022-01-08T16:15:48.129636Z","shell.execute_reply":"2022-01-08T16:15:48.134546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import shutil\nshutil.make_archive(\"/kaggle/working/images\", 'zip', \"/kaggle/working/generated\")","metadata":{"execution":{"iopub.status.busy":"2022-01-08T16:22:37.666212Z","iopub.execute_input":"2022-01-08T16:22:37.666477Z","iopub.status.idle":"2022-01-08T16:22:38.162001Z","shell.execute_reply.started":"2022-01-08T16:22:37.666448Z","shell.execute_reply":"2022-01-08T16:22:38.160928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}