GAN 入門

使用 PyTorch 進行影像深度學習

Michal Oleszak

Machine Learning Engineer

生成式對抗網路概觀

生成的貓圖片。

使用 PyTorch 進行影像深度學習

Pokemon Sprites 資料集

Pokemon Sprites 資料集的影像樣本

  • 來自 PokeAPI 的 Pokemon Sprites 資料集
  • 約 1300 張寶可夢遊戲中動物風格角色的 Sprites
  • 目標:生成新的寶可夢!
使用 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...