Introduction to GANs

Deep Learning for Images with PyTorch

Michal Oleszak

Machine Learning Engineer

General Adversarial Networks

Generated cat image.

Deep Learning for Images with PyTorch

Pokemon Sprites Dataset

Sample of images from the Pokemon Sprites dataset

  • Pokemon Sprites Dataset from PokeAPI
  • About 1300 sprites of animal-like creatures from a Pokemon video game
  • Goal: Generate new Pokemons!
Deep Learning for Images with PyTorch

GANs architecture

GAN workflow diagram.

Deep Learning for Images with PyTorch

GANs architecture

GAN workflow diagram.

Deep Learning for Images with PyTorch

GANs architecture

GAN workflow diagram.

Deep Learning for Images with PyTorch

GANs architecture

GAN workflow diagram.

Deep Learning for Images with PyTorch

GANs learning process

 

 

GAN workflow diagram.

  • Generator: learns to produce realistic-looking images
  • Discriminator: learns to tell the fakes from real images
  • Conflicting objectives ensure each network gets better at its task
  • In the end, generator should generate realistic images
Deep Learning for Images with PyTorch

Basic Generator

class Generator(nn.Module):
    def __init__(self, in_dim, out_dim):
        super(Generator, self).__init__()

self.generator = nn.Sequential( gen_block(in_dim, 256), gen_block(256, 512), gen_block(512, 1024), nn.Linear(1024, out_dim), nn.Sigmoid(), )
def forward(self, x): return self.generator(x)
  • Define Generator class
  • Sequence of generator blocks, a linear layer, and a sigmoid activation
    def gen_block(in_dim, out_dim):
      return nn.Sequential(
          nn.Linear(in_dim, out_dim),
          nn.BatchNorm1d(out_dim),
          nn.ReLU(inplace=True)
      )
    
  • Pass input through all layers
  • Input: noise of size in_dim
  • Output: image of size out_dim
Deep Learning for Images with PyTorch

Basic Discriminator

class Discriminator(nn.Module):
    def __init__(self, im_dim):
        super(Discriminator, self).__init__()

self.disc = nn.Sequential( disc_block(im_dim, 1024), disc_block(1024, 512), disc_block(512, 256), nn.Linear(256, 1), )
def forward(self, x): return self.disc(x)
  • Define Discriminator class
  • Sequence of discriminator blocks and a linear layer
    def disc_block(in_dim, out_dim):
      return nn.Sequential(
          nn.Linear(in_dim, out_dim),
          nn.LeakyReLU(0.2)
      )
    
  • Pass input through all layers
  • Input: image of size in_dim
  • Output: classification of size 1
Deep Learning for Images with PyTorch

Let's practice!

Deep Learning for Images with PyTorch

Preparing Video For Download...