import torch
import torch.autograd as autograd
import torchvision.datasets as datasets
import torchvision.transforms as transforms

def dataloader(root, train_batch_size, test_batch_size, num_workers):

    transform_train = transforms.Compose([
        transforms.RandomResizedCrop(32, scale=(0.7, 1.0), ratio=(1.0,1.0)),
        transforms.ColorJitter(
                brightness=0.1*torch.randn(1),
                contrast=0.1*torch.randn(1),
                saturation=0.1*torch.randn(1),
                hue=0.1*torch.randn(1)),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
    ])


    transform_test = transforms.Compose([
        transforms.CenterCrop(32),
        transforms.ToTensor(),
        transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5)),
    ])

    trainset = datasets.CIFAR10(root =root, train = True, transform = transform_train, download = True)
    trainloader = torch.utils.data.DataLoader(trainset, batch_size = train_batch_size, shuffle = True, num_workers = num_workers)

    testset = datasets.CIFAR10(root = root, train = False, transform = transform_test, download = False)
    testloader = torch.utils.data.DataLoader(testset, batch_size = test_batch_size, shuffle = False, num_workers = num_workers)

    return trainloader, testloader


def calc_gradient_penalty(batch_size, netD, real_data, fake_data):
    DIM = 32
    LAMBDA = 10
    alpha = torch.rand(batch_size, 1)
    alpha = alpha.expand(batch_size, int(real_data.nelement()/batch_size)).contiguous()
    alpha = alpha.view(batch_size, 3, DIM, DIM)
    alpha = alpha.cuda()
    
    fake_data = fake_data.view(batch_size, 3, DIM, DIM)
    interpolates = alpha * real_data.detach() + ((1 - alpha) * fake_data.detach())

    interpolates = interpolates.cuda()
    interpolates.requires_grad_(True)

    disc_interpolates, _ = netD(interpolates)

    gradients = autograd.grad(outputs=disc_interpolates, inputs=interpolates,
                              grad_outputs=torch.ones(disc_interpolates.size()).cuda(),
                              create_graph=True, retain_graph=True, only_inputs=True)[0]

    gradients = gradients.view(gradients.size(0), -1)                              
    gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean() * LAMBDA
    return gradient_penalty


import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import os

def plot(samples):
    fig = plt.figure(figsize=(10, 10))
    gs = gridspec.GridSpec(10, 10)
    gs.update(wspace=0.02, hspace=0.02)

    for i, sample in enumerate(samples):
        ax = plt.subplot(gs[i])
        plt.axis('off')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.set_aspect('equal')
        plt.imshow(sample)
    return fig
f10507c4d1397839f7b2b603923b52d19f0bddd5
<mask token>


def dataloader(root, train_batch_size, test_batch_size, num_workers):
    transform_train = transforms.Compose([transforms.RandomResizedCrop(32,
        scale=(0.7, 1.0), ratio=(1.0, 1.0)), transforms.ColorJitter(
        brightness=0.1 * torch.randn(1), contrast=0.1 * torch.randn(1),
        saturation=0.1 * torch.randn(1), hue=0.1 * torch.randn(1)),
        transforms.RandomHorizontalFlip(), transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
    transform_test = transforms.Compose([transforms.CenterCrop(32),
        transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 
        0.5, 0.5))])
    trainset = datasets.CIFAR10(root=root, train=True, transform=
        transform_train, download=True)
    trainloader = torch.utils.data.DataLoader(trainset, batch_size=
        train_batch_size, shuffle=True, num_workers=num_workers)
    testset = datasets.CIFAR10(root=root, train=False, transform=
        transform_test, download=False)
    testloader = torch.utils.data.DataLoader(testset, batch_size=
        test_batch_size, shuffle=False, num_workers=num_workers)
    return trainloader, testloader


<mask token>


def plot(samples):
    fig = plt.figure(figsize=(10, 10))
    gs = gridspec.GridSpec(10, 10)
    gs.update(wspace=0.02, hspace=0.02)
    for i, sample in enumerate(samples):
        ax = plt.subplot(gs[i])
        plt.axis('off')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.set_aspect('equal')
        plt.imshow(sample)
    return fig

<mask token>


def dataloader(root, train_batch_size, test_batch_size, num_workers):
    transform_train = transforms.Compose([transforms.RandomResizedCrop(32,
        scale=(0.7, 1.0), ratio=(1.0, 1.0)), transforms.ColorJitter(
        brightness=0.1 * torch.randn(1), contrast=0.1 * torch.randn(1),
        saturation=0.1 * torch.randn(1), hue=0.1 * torch.randn(1)),
        transforms.RandomHorizontalFlip(), transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
    transform_test = transforms.Compose([transforms.CenterCrop(32),
        transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 
        0.5, 0.5))])
    trainset = datasets.CIFAR10(root=root, train=True, transform=
        transform_train, download=True)
    trainloader = torch.utils.data.DataLoader(trainset, batch_size=
        train_batch_size, shuffle=True, num_workers=num_workers)
    testset = datasets.CIFAR10(root=root, train=False, transform=
        transform_test, download=False)
    testloader = torch.utils.data.DataLoader(testset, batch_size=
        test_batch_size, shuffle=False, num_workers=num_workers)
    return trainloader, testloader


def calc_gradient_penalty(batch_size, netD, real_data, fake_data):
    DIM = 32
    LAMBDA = 10
    alpha = torch.rand(batch_size, 1)
    alpha = alpha.expand(batch_size, int(real_data.nelement() / batch_size)
        ).contiguous()
    alpha = alpha.view(batch_size, 3, DIM, DIM)
    alpha = alpha.cuda()
    fake_data = fake_data.view(batch_size, 3, DIM, DIM)
    interpolates = alpha * real_data.detach() + (1 - alpha) * fake_data.detach(
        )
    interpolates = interpolates.cuda()
    interpolates.requires_grad_(True)
    disc_interpolates, _ = netD(interpolates)
    gradients = autograd.grad(outputs=disc_interpolates, inputs=
        interpolates, grad_outputs=torch.ones(disc_interpolates.size()).
        cuda(), create_graph=True, retain_graph=True, only_inputs=True)[0]
    gradients = gradients.view(gradients.size(0), -1)
    gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean() * LAMBDA
    return gradient_penalty


<mask token>


def plot(samples):
    fig = plt.figure(figsize=(10, 10))
    gs = gridspec.GridSpec(10, 10)
    gs.update(wspace=0.02, hspace=0.02)
    for i, sample in enumerate(samples):
        ax = plt.subplot(gs[i])
        plt.axis('off')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.set_aspect('equal')
        plt.imshow(sample)
    return fig

<mask token>


def dataloader(root, train_batch_size, test_batch_size, num_workers):
    transform_train = transforms.Compose([transforms.RandomResizedCrop(32,
        scale=(0.7, 1.0), ratio=(1.0, 1.0)), transforms.ColorJitter(
        brightness=0.1 * torch.randn(1), contrast=0.1 * torch.randn(1),
        saturation=0.1 * torch.randn(1), hue=0.1 * torch.randn(1)),
        transforms.RandomHorizontalFlip(), transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
    transform_test = transforms.Compose([transforms.CenterCrop(32),
        transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 
        0.5, 0.5))])
    trainset = datasets.CIFAR10(root=root, train=True, transform=
        transform_train, download=True)
    trainloader = torch.utils.data.DataLoader(trainset, batch_size=
        train_batch_size, shuffle=True, num_workers=num_workers)
    testset = datasets.CIFAR10(root=root, train=False, transform=
        transform_test, download=False)
    testloader = torch.utils.data.DataLoader(testset, batch_size=
        test_batch_size, shuffle=False, num_workers=num_workers)
    return trainloader, testloader


def calc_gradient_penalty(batch_size, netD, real_data, fake_data):
    DIM = 32
    LAMBDA = 10
    alpha = torch.rand(batch_size, 1)
    alpha = alpha.expand(batch_size, int(real_data.nelement() / batch_size)
        ).contiguous()
    alpha = alpha.view(batch_size, 3, DIM, DIM)
    alpha = alpha.cuda()
    fake_data = fake_data.view(batch_size, 3, DIM, DIM)
    interpolates = alpha * real_data.detach() + (1 - alpha) * fake_data.detach(
        )
    interpolates = interpolates.cuda()
    interpolates.requires_grad_(True)
    disc_interpolates, _ = netD(interpolates)
    gradients = autograd.grad(outputs=disc_interpolates, inputs=
        interpolates, grad_outputs=torch.ones(disc_interpolates.size()).
        cuda(), create_graph=True, retain_graph=True, only_inputs=True)[0]
    gradients = gradients.view(gradients.size(0), -1)
    gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean() * LAMBDA
    return gradient_penalty


<mask token>
matplotlib.use('Agg')
<mask token>


def plot(samples):
    fig = plt.figure(figsize=(10, 10))
    gs = gridspec.GridSpec(10, 10)
    gs.update(wspace=0.02, hspace=0.02)
    for i, sample in enumerate(samples):
        ax = plt.subplot(gs[i])
        plt.axis('off')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.set_aspect('equal')
        plt.imshow(sample)
    return fig

import torch
import torch.autograd as autograd
import torchvision.datasets as datasets
import torchvision.transforms as transforms


def dataloader(root, train_batch_size, test_batch_size, num_workers):
    transform_train = transforms.Compose([transforms.RandomResizedCrop(32,
        scale=(0.7, 1.0), ratio=(1.0, 1.0)), transforms.ColorJitter(
        brightness=0.1 * torch.randn(1), contrast=0.1 * torch.randn(1),
        saturation=0.1 * torch.randn(1), hue=0.1 * torch.randn(1)),
        transforms.RandomHorizontalFlip(), transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])
    transform_test = transforms.Compose([transforms.CenterCrop(32),
        transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 
        0.5, 0.5))])
    trainset = datasets.CIFAR10(root=root, train=True, transform=
        transform_train, download=True)
    trainloader = torch.utils.data.DataLoader(trainset, batch_size=
        train_batch_size, shuffle=True, num_workers=num_workers)
    testset = datasets.CIFAR10(root=root, train=False, transform=
        transform_test, download=False)
    testloader = torch.utils.data.DataLoader(testset, batch_size=
        test_batch_size, shuffle=False, num_workers=num_workers)
    return trainloader, testloader


def calc_gradient_penalty(batch_size, netD, real_data, fake_data):
    DIM = 32
    LAMBDA = 10
    alpha = torch.rand(batch_size, 1)
    alpha = alpha.expand(batch_size, int(real_data.nelement() / batch_size)
        ).contiguous()
    alpha = alpha.view(batch_size, 3, DIM, DIM)
    alpha = alpha.cuda()
    fake_data = fake_data.view(batch_size, 3, DIM, DIM)
    interpolates = alpha * real_data.detach() + (1 - alpha) * fake_data.detach(
        )
    interpolates = interpolates.cuda()
    interpolates.requires_grad_(True)
    disc_interpolates, _ = netD(interpolates)
    gradients = autograd.grad(outputs=disc_interpolates, inputs=
        interpolates, grad_outputs=torch.ones(disc_interpolates.size()).
        cuda(), create_graph=True, retain_graph=True, only_inputs=True)[0]
    gradients = gradients.view(gradients.size(0), -1)
    gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean() * LAMBDA
    return gradient_penalty


import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import os


def plot(samples):
    fig = plt.figure(figsize=(10, 10))
    gs = gridspec.GridSpec(10, 10)
    gs.update(wspace=0.02, hspace=0.02)
    for i, sample in enumerate(samples):
        ax = plt.subplot(gs[i])
        plt.axis('off')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.set_aspect('equal')
        plt.imshow(sample)
    return fig

import torch
import torch.autograd as autograd
import torchvision.datasets as datasets
import torchvision.transforms as transforms

def dataloader(root, train_batch_size, test_batch_size, num_workers):

    transform_train = transforms.Compose([
        transforms.RandomResizedCrop(32, scale=(0.7, 1.0), ratio=(1.0,1.0)),
        transforms.ColorJitter(
                brightness=0.1*torch.randn(1),
                contrast=0.1*torch.randn(1),
                saturation=0.1*torch.randn(1),
                hue=0.1*torch.randn(1)),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
    ])


    transform_test = transforms.Compose([
        transforms.CenterCrop(32),
        transforms.ToTensor(),
        transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5)),
    ])

    trainset = datasets.CIFAR10(root =root, train = True, transform = transform_train, download = True)
    trainloader = torch.utils.data.DataLoader(trainset, batch_size = train_batch_size, shuffle = True, num_workers = num_workers)

    testset = datasets.CIFAR10(root = root, train = False, transform = transform_test, download = False)
    testloader = torch.utils.data.DataLoader(testset, batch_size = test_batch_size, shuffle = False, num_workers = num_workers)

    return trainloader, testloader


def calc_gradient_penalty(batch_size, netD, real_data, fake_data):
    DIM = 32
    LAMBDA = 10
    alpha = torch.rand(batch_size, 1)
    alpha = alpha.expand(batch_size, int(real_data.nelement()/batch_size)).contiguous()
    alpha = alpha.view(batch_size, 3, DIM, DIM)
    alpha = alpha.cuda()
    
    fake_data = fake_data.view(batch_size, 3, DIM, DIM)
    interpolates = alpha * real_data.detach() + ((1 - alpha) * fake_data.detach())

    interpolates = interpolates.cuda()
    interpolates.requires_grad_(True)

    disc_interpolates, _ = netD(interpolates)

    gradients = autograd.grad(outputs=disc_interpolates, inputs=interpolates,
                              grad_outputs=torch.ones(disc_interpolates.size()).cuda(),
                              create_graph=True, retain_graph=True, only_inputs=True)[0]

    gradients = gradients.view(gradients.size(0), -1)                              
    gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean() * LAMBDA
    return gradient_penalty


import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import os

def plot(samples):
    fig = plt.figure(figsize=(10, 10))
    gs = gridspec.GridSpec(10, 10)
    gs.update(wspace=0.02, hspace=0.02)

    for i, sample in enumerate(samples):
        ax = plt.subplot(gs[i])
        plt.axis('off')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.set_aspect('equal')
        plt.imshow(sample)
    return fig