Deep Convolutional GAN

Deep Learning for Images with PyTorch

Michal Oleszak

Machine Learning Engineer

Deep Convolutional GAN intuition

  • In discriminator, replace linear layers with convolutions
  • In generator, we can use transposed convolutions
  • Training GANs is often unstable, more adjustments are needed
Deep Learning for Images with PyTorch

DCGAN guidelines

  • Deep Convolutional GAN (DCGAN)
  • DCGAN guidelines:
    • Use only strided convolutions
    • Don't use any linear or pooling layers
    • Use batch normalization
    • Use ReLU activations in the generator (except last layer which uses tanh)
    • Use Leaky ReLU activation in the discriminator

activation graphs

Deep Learning for Images with PyTorch

Strided convolution

Convolution with the stride of 1:

Animation of an unstrided convolution.

nn.Conv2d(..., stride=1)

Convolution with the stride of 2:

Animation of a strided convolution.

nn.Conv2d(..., stride=2)
Deep Learning for Images with PyTorch

Convolutional generator block

def dc_gen_block(
    in_dim, out_dim, kernel_size, stride
):

return nn.Sequential( nn.ConvTranspose2d( in_dim, out_dim, kernel_size, stride=stride, ),
nn.BatchNorm2d(out_dim),
nn.ReLU() )

Generator block consists of:

  • Strided transposed convolution
  • Batch normalization
  • ReLU activation
Deep Learning for Images with PyTorch

Deep Convolutional Generator

class DCGenerator(nn.Module):
    def __init__(self, in_dim, kernel_size=4, stride=2):
        super(Generator, self).__init__()
        self.in_dim = in_dim

self.gen = nn.Sequential( dc_gen_block(in_dim, 1024, kernel_size, stride), dc_gen_block(1024, 512, kernel_size, stride), dc_gen_block(512, 256, kernel_size, stride),
nn.ConvTranspose2d(256, 3, kernel_size, stride=stride),
nn.Tanh() )
def forward(self, x): x = x.view(len(x), self.in_dim, 1, 1) return self.gen(x)
Deep Learning for Images with PyTorch

Convolutional discriminator block

def dc_disc_block(
    in_dim, out_dim, kernel_size, stride
):

return nn.Sequential( nn.Conv2d( in_dim, out_dim, kernel_size, stride=stride, ),
nn.BatchNorm2d(out_dim),
nn.LeakyReLU(0.2), )

Discriminator block consists of:

  • Strided convolution
  • Batch normalization
  • Leaky ReLU activation
Deep Learning for Images with PyTorch

Deep Convolutional Discriminator

class Discriminator(nn.Module):
    def __init__(self, kernel_size=4, stride=2):
        super(Discriminator, self).__init__()

self.disc = nn.Sequential( dc_disc_block(3, 512, kernel_size, stride), dc_disc_block(512, 1024, kernel_size, stride), nn.Conv2d(1024, 1, kernel_size, stride=stride), )
def forward(self, x): x = self.disc(x) return x.view(len(x), -1)
Deep Learning for Images with PyTorch

Let's practice!

Deep Learning for Images with PyTorch

Preparing Video For Download...