GAN 简介

使用 PyTorch 进行图像深度学习

Michal Oleszak

Machine Learning Engineer

生成对抗网络

生成的猫图像。

使用 PyTorch 进行图像深度学习

Pokemon Sprites 数据集

Pokemon Sprites 数据集示例图像

  • 来自 PokeAPI 的 Pokemon Sprites 数据集
  • 含约 1300 张宝可梦风格生物的精灵图
  • 目标:生成新的宝可梦!
使用 PyTorch 进行图像深度学习

GAN 架构

GAN 工作流示意图。

使用 PyTorch 进行图像深度学习

GAN 架构

GAN 工作流示意图。

使用 PyTorch 进行图像深度学习

GAN 架构

GAN 工作流示意图。

使用 PyTorch 进行图像深度学习

GAN 架构

GAN 工作流示意图。

使用 PyTorch 进行图像深度学习

GAN 学习过程

 

 

GAN 工作流示意图。

  • 生成器:学习生成逼真的图像
  • 判别器:学习区分真伪图像
  • 博弈目标推动两者各自提升
  • 最终:生成器应生成逼真图像
使用 PyTorch 进行图像深度学习

基础生成器

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)
  • 定义 Generator
  • 由生成块序列、线性层和 sigmoid 激活组成
    def gen_block(in_dim, out_dim):
      return nn.Sequential(
          nn.Linear(in_dim, out_dim),
          nn.BatchNorm1d(out_dim),
          nn.ReLU(inplace=True)
      )
    
  • 输入依次通过各层
  • 输入:大小为 in_dim 的噪声
  • 输出:大小为 out_dim 的图像
使用 PyTorch 进行图像深度学习

基础判别器

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)
  • 定义 Discriminator
  • 由判别块序列和线性层组成
    def disc_block(in_dim, out_dim):
      return nn.Sequential(
          nn.Linear(in_dim, out_dim),
          nn.LeakyReLU(0.2)
      )
    
  • 输入依次通过各层
  • 输入:大小为 in_dim 的图像
  • 输出:大小为 1 的分类
使用 PyTorch 进行图像深度学习

开始练习!

使用 PyTorch 进行图像深度学习

Preparing Video For Download...