데이터과학과 2021320303 정지원

노션 페이지: https://stop1one.notion.site/HW4-686f5b392493438abcc433e660a21488?pvs=4

1. Learn a GAN (deconv) with 64x64

latent_dims = 100
num_epochs = 40
batch_size = 512
learning_rate = 2e-4
use_gpu = True
class Generator(nn.Module):
    def __init__(self, ld, d=128):
        super(Generator, self).__init__()
        self.deconv1 = nn.ConvTranspose2d(ld, d*8, 4, 1, 0)
        self.deconv1_bn = nn.BatchNorm2d(d*8)
        self.deconv2 = nn.ConvTranspose2d(d*8, d*4, 4, 2, 1)
        self.deconv2_bn = nn.BatchNorm2d(d*4)
        self.deconv3 = nn.ConvTranspose2d(d*4, d*2, 4, 2, 1)
        self.deconv3_bn = nn.BatchNorm2d(d*2)
        self.deconv4 = nn.ConvTranspose2d(d*2, d, 4, 2, 1)
        self.deconv4_bn = nn.BatchNorm2d(d)
        self.deconv5 = nn.ConvTranspose2d(d, 1, 4, 2, 1)

    def forward(self, input):
        # x = F.relu(self.deconv1(input))
        x = F.relu(self.deconv1_bn(self.deconv1(input)))
        x = F.relu(self.deconv2_bn(self.deconv2(x)))
        x = F.relu(self.deconv3_bn(self.deconv3(x)))
        x = F.relu(self.deconv4_bn(self.deconv4(x)))
        x = torch.tanh(self.deconv5(x))

        return x

class Discriminator(nn.Module):
    def __init__(self, d=128):
        super(Discriminator, self).__init__()
        self.conv1 = nn.Conv2d(1, d, 4, 2, 1) # 32 32 d
        self.conv2 = nn.Conv2d(d, d*2, 4, 2, 1) # 16 16 2d
        self.conv2_bn = nn.BatchNorm2d(d*2)
        self.conv3 = nn.Conv2d(d*2, d*4, 4, 2, 1) # 8 8 4d
        self.conv3_bn = nn.BatchNorm2d(d*4)
        self.conv4 = nn.Conv2d(d*4, d*8, 4, 2, 1) # 4 4 8d
        self.conv4_bn = nn.BatchNorm2d(d*8)
        self.conv5 = nn.Conv2d(d*8, 1, 4, 1, 0) # 1 1 1

    def forward(self, input):
        x = F.leaky_relu(self.conv1(input), 0.2)
        x = F.leaky_relu(self.conv2_bn(self.conv2(x)), 0.2)
        x = F.leaky_relu(self.conv3_bn(self.conv3(x)), 0.2)
        x = F.leaky_relu(self.conv4_bn(self.conv4(x)), 0.2)
        x = torch.sigmoid(self.conv5(x))

        return x

Learning Curve

Untitled

Change hyperparameters; learning rate, batch_size, latent_dims

Visualize Generated Samples

Untitled

Interpolation

Untitled

Visualize generated images using the same latent vectors across epochs

p1.gif

2. Learn a GAN (deconv) with 28x28

latent_dims = 100
num_epochs = 100
batch_size = 512
learning_rate = 2e-4
use_gpu = True
class Generator(nn.Module):
    def __init__(self, ld, d=32):
        super(Generator, self).__init__()
        self.deconv1 = nn.ConvTranspose2d(ld, d*2, 7, 1, 0)
        self.deconv1_bn = nn.BatchNorm2d(d*2)
        self.deconv2 = nn.ConvTranspose2d(d*2, d, 4, 2, 1)
        self.deconv2_bn = nn.BatchNorm2d(d)
        self.deconv3 = nn.ConvTranspose2d(d, 1, 4, 2, 1)

    def forward(self, input):
        x = F.relu(self.deconv1(input))
        x = F.relu(self.deconv1_bn(self.deconv1(input)))
        x = F.relu(self.deconv2_bn(self.deconv2(x)))
        x = torch.tanh(self.deconv3(x))

        return x

class Discriminator(nn.Module):
    def __init__(self, d=32):
        super(Discriminator, self).__init__()
        self.conv1 = nn.Conv2d(1, d, 3, 2, 1) # 14 x 14 x d
        self.conv2 = nn.Conv2d(d, d*2, 3, 2, 1) # 7 x 7 x 2d
        self.conv2_bn = nn.BatchNorm2d(d*2)
        self.conv3 = nn.Conv2d(d*2, 1, 7, 1, 0) # 1 x 1 x 1

    def forward(self, input):
        x = F.leaky_relu(self.conv1(input), 0.2)
        x = F.leaky_relu(self.conv2_bn(self.conv2(x)), 0.2)
        x = torch.sigmoid(self.conv3(x))

        return x