$30 off During Our Annual Pro Sale. View Details »

【深度學習】07 生成模式和 GAN

【深度學習】07 生成模式和 GAN

2022 政大應數「數學軟體應用」深度學習課程 07

[email protected]
PRO

May 23, 2022
Tweet

Other Decks in Technology

Transcript

  1. 政⼤應數。數學軟體應⽤
    蔡炎龍
    ⽣成模式和 GAN
    政治⼤學應⽤數學系
    深度學習入⾨
    07.

    View Slide

  2. ⽣成模式和特徵截取
    20.

    View Slide

  3. 數學軟體應⽤ 451
    ⽣成模式 Generative Models
    Generative
    Models
    為何要討論這個呢?

    View Slide

  4. 數學軟體應⽤ 452
    討論⽣成模式⼀定要引費曼的話
    What I cannot create,
    I do not understand.”

    —Richard Feynman

    View Slide

  5. 數學軟體應⽤ 453
    ⽣成模式
    G

    ⽣成器
    ⽣出圖、照片、⽂字
    z
    latent vector
    特徵向 /本徵向

    View Slide

  6. 數學軟體應⽤ 454
    輸入的特徵向 基本上有兩種作法
    生成器
    Noise
    生出圖、照片、文字
    亂亂做!
    (保證生出「正確格式」的東⻄就好)
    ⽅法⼀
    隨機輸入⼀
    堆數字。
    在 GAN 裡, 這是最常⽤的⽅法!

    View Slide

  7. 數學軟體應⽤ 455
    輸入的特徵向 基本上有兩種作法
    Encoder
    ⽅法⼆ 先想辦法做個
    「好的」特徵
    向 出來!
    特徵向量

    View Slide

  8. 數學軟體應⽤ 456
    Autoencoder
    ⽅法⼆看來有點神奇, 怎麼能訓練⼀個
    截取特徵向 的函數的? 這裡介紹⼀個
    常⽤⼿法叫⾃編碼器 (Autoencoder)。
    Autoencoder

    View Slide

  9. 數學軟體應⽤ 457
    Autoencoder
    encoder decoder
    z
    latent vector
    x
    m >> k
    k維
    x
    m維
    於是, z 可以取代 x (或者說 z 是 x 的
    一個 presentation)
    Autoencoder 是輸入什
    麼, 就輸出什麼的函數。
    感覺很奇怪, 原來是中間我
    們會⽤⼀層比較⼩的 維
    神經元。
    這⼀層的輸出就是我們準
    備當特徵向 的。
    k

    View Slide

  10. 數學軟體應⽤ 458
    Autoencoder
    encoder z
    x
    以後我們基本上可以
    ⽤比較⼩的 z 來取代
    比較⼤的 x。
    特徵向
    就這麼找
    出來了。

    View Slide

  11. 數學軟體應⽤ 459
    Autoencoder
    另⼀邊就可以當⽣成
    器, 輸入⼀個特徵向
    , 就⽣出⼀個我們要
    的圖或任何東⻄。
    假設我們有隻兔⼦
    的特徵向 , 改⼀
    點點會不會⽣出⼀
    隻很像的兔⼦?
    decoder
    z x

    View Slide

  12. 數學軟體應⽤ 460
    Autoencoder
    答案是不太⾏
    隨便⽣兩個 latent vectors, 數學
    上距離很近, 但⽣出來的東⻄不⼀
    定有什麼關係。
    白話文是 z 差不多就是亂數, 我們無以掌控。

    View Slide

  13. 數學軟體應⽤ 461
    VAE (Variational AutoEncoder)
    所以有了 VAE
    Variational AutoEncoder

    View Slide

  14. 數學軟體應⽤ 462
    VAE (Variational AutoEncoder)
    我們想辦法找每個數的平均
    值和變異數 (or 標準差)
    神秘編碼 laten vector 每個數字是符合某常態分布的, 這樣我們容易掌控!
    這要怎麼
    做到?

    View Slide

  15. 數學軟體應⽤ 463
    VAE (Variational AutoEncoder)
    encoder z
    x
    k維
    m維
    μ
    σ
    在 encoder 後做點手腳。
    laten vector
    其實這層還有
    加 noise
    其實就是要函數
    學習機去學平均
    值和變異數!

    View Slide

  16. 數學軟體應⽤ 464
    Deepfake 可以⽤ Autoencoder 就做出來
    encoder
    A
    decoder
    A
    encoder
    B
    decoder
    B
    ⽤ autoencoder 就
    可以做到 deepfake

    View Slide

  17. 數學軟體應⽤ 465
    Deepfake 可以⽤ Autoencoder 就做出來
    encoder
    A
    decoder
    B
    ⽤ encoder A 做出
    的 latent vector, 送
    進 decoder B 之
    中。

    View Slide

  18. ⽣成對抗網路 GAN
    21.

    View Slide

  19. 數學軟體應⽤ 467
    LeCun 認為 GAN 是深度學習最有潛⼒的 model
    There are many interesting recent development
    in deep learning…
    The most important one, in my opinion, is
    adversarial training (also called GAN for
    Generative Adversarial Networks).

    —Yan LeCun (楊⽴昆), 2016

    View Slide

  20. 數學軟體應⽤ 468
    GAN 的架構
    ⽣成器
    generator
    Noise
    鑑別器
    discriminator
    GAN 是兩個神經網路,
    ⼀個叫⽣成器、⼀個叫
    鑑別器, 相互對抗!
    G
    D
    z
    G(z)
    x
    1
    真的
    0
    假的
    or

    View Slide

  21. 數學軟體應⽤ 469
    GAN 就是⽣成器、鑑別器⼤對抗!
    希望
    接近 1
    希望
    接近 0
    接近 1
    ⽣成器 G 鑑別器 D

    View Slide

  22. 數學軟體應⽤ 470
    GAN 的著名應⽤
    接下來介紹幾
    個 GAN 的著
    名應⽤!

    View Slide

  23. 數學軟體應⽤ 471
    iGAN ⼈⼈都是⼤畫家!
    人人都是大畫家!
    https://youtu.be/9c4z6YsBGQ0
    朱俊彦等人 (ECCV 2016)
    “Generative Visual Manipulation on the Natural Image Manifold”
    iGAN
    https://arxiv.org/abs/1609.03552

    View Slide

  24. 數學軟體應⽤ 472
    Progressive GAN ⽣成明星照片
    Karras-Aila-Laine-Lehtinen
    NVIDIA 團隊很有名的⽂章
    當初⽤現在基本上沒有在⽤的 Theano, Python 2, ⽽
    且只有單 GPU
    Progressive Growing of GANs for Improved
    Quality, Stability, and Variation
    Karras 等 NVIDIA 團隊 (ICLR 2018)
    “Progressive Growing of GANs for Improved Quality, Stability, and Variation”
    Progressive GAN
    https://arxiv.org/abs/1710.10196

    View Slide

  25. 數學軟體應⽤ 473
    Progressive GAN ⽣成明星照片
    這攏係假ㄟ啦 (1024x1024 明星照)

    View Slide

  26. 數學軟體應⽤ 474
    然後越來越誇張 (StyleGAN2)
    https://thispersondoesnotexist.com/

    View Slide

  27. 數學軟體應⽤ 475
    This X Does Not Exist
    AI ⽣出各種不
    同的 X!

    View Slide

  28. 數學軟體應⽤ 476
    ⼈臉變迪⼠尼風格!
    https://bit.ly/colab_toonify
    魏澤⼈老師整理成 Colab Notebook:

    View Slide

  29. 數學軟體應⽤ 477
    Pix2pix: 簡單畫畫就產⽣很真實的相片
    * 來自 Isola, 朱俊彦等人的原始論文 (2017)
    Pix2pix 把衛星圖變地圖。
    Isola, 朱俊彦等人 (CVPR 2017)
    “Image-to-Image Translation with Conditional Adversarial Networks”
    Pix2Pix
    https://arxiv.org/abs/1611.07004

    View Slide

  30. 數學軟體應⽤ 478
    Pix2pix: 簡單畫畫就產⽣很真實的相片
    ⽣成器
    generator
    G
    鑑別器
    discriminator
    D
    Real
    注意輸入輸出
    需同時送進鑑
    別器。

    View Slide

  31. 數學軟體應⽤ 479
    Pix2pix: 簡單畫畫就產⽣很真實的相片
    * 來自 Isola, 朱俊彦等人的原始論文 (2017)
    Pix2pix 隨手畫畫變街景。
    可以訓練
    ⾃駕⾞!

    View Slide

  32. 數學軟體應⽤ 480
    Pix2pix: 簡單畫畫就產⽣很真實的相片
    * Christopher Hesse 依原論文做出
    Pix2pix 線上版。
    https://affinelayer.com/pixsrv/
    ⾃⼰試試看,
    可不可以⽣出
    隻貓來!

    View Slide

  33. 數學軟體應⽤ 481
    CycleGAN: 讓⼈驚呆的魔法
    朱俊彦等人 (ICCV 2017)
    “Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks”
    CycleGAN
    https://arxiv.org/abs/1703.10593

    View Slide

  34. 數學軟體應⽤ 482
    CycleGAN: 讓⼈驚呆的魔法
    G

    ⽣成器
    F

    ⽣成器
    Domain
    A
    Domain
    B
    鑑別器
    B
    鑑別器
    A
    CycleGAN 資料「不需要」配對!
    於是有無限可能...
    比如 “Cycle
    GAN” 般的機
    器翻譯!

    View Slide

  35. 數學軟體應⽤ 483
    CycleGAN: 讓⼈驚呆的魔法
    全世界的⼈都驚呆了的⾺變斑⾺。
    https://youtu.be/9reHvktowLY

    View Slide

  36. 數學軟體應⽤ 484
    CycleGAN: 讓⼈驚呆的魔法
    CycleGAN 作者群幽⾃⼰⼀默的失敗例⼦。

    View Slide

  37. 數學軟體應⽤ 485
    CycleGAN: 讓⼈驚呆的魔法
    Goodfellow 也關注的館長變陳沂 (魏澤⼈老師)。
    https://youtu.be/Fea4kZq0oFQ

    View Slide

  38. AI 新進發展: 尋找良好的
    representation
    22.

    View Slide

  39. 數學軟體應⽤ 487
    表⽰向
    新進 AI 的發展, 很多是
    在追尋良好的表⽰向 !

    View Slide

  40. 數學軟體應⽤ 488
    也就是訓練產⽣表⽰向 的函數!
    f
    表⽰向 可以想成是電
    腦的「記憶」或是「理
    解」。我們真正要做的
    事 (比如分類), ⽤表⽰向
    可能會更簡單順利!
    等等, 這不就是 feature engineering 嗎?

    View Slide

  41. 數學軟體應⽤ 489
    還記得有⼈說...
    Deep learning 不太需要做 feature engineering。
    f
    ⽤海 的資料讓電腦看著辦...
    這位就「有⼈」

    View Slide

  42. 數學軟體應⽤ 490
    Feature Engineering
    神經網路某⼀隱藏層可以當
    ⼀種 feature engineering
    也就是說, 我們可以把神經網路在做「⾃動的」feature engineering

    View Slide

  43. 數學軟體應⽤ 491
    Feature Engineering
    事實上可以想每⼀個隱藏層都
    在做 feature engineering。

    View Slide

  44. 數學軟體應⽤ 492
    Representation Learning
    神經網路就是⾃動的在做原始資料新的 representation。

    View Slide

  45. 數學軟體應⽤ 493
    ⽂字我們做過類似的, 影像呢?
    以後公司⾨禁就直
    接⽤⼈臉辨識!
    假設我們要做⼈臉辨識。

    View Slide

  46. 數學軟體應⽤ 494
    感覺挺容易的, 就給每個同仁⼀個編號
    1 2 3 4

    View Slide

  47. 數學軟體應⽤ 495
    就學這個函數
    f
    輸出
    輸入
    3

    View Slide

  48. 數學軟體應⽤ 496
    問題1
    圖形辨識⼀個類別
    ⼤約要 1000 張才
    能訓練!
    我們總不能叫每位同仁都來個 1000 張照片...

    View Slide

  49. 數學軟體應⽤ 497
    問題2
    我是新加入的, 要
    新訓練嗎?

    View Slide

  50. 數學軟體應⽤ 498
    這和⼈類不太⼀樣...
    ⼈好像不需要幾千
    張照片才能辨識...

    View Slide

  51. 數學軟體應⽤ 499
    Meta Learning
    有沒有可能教電腦
    「怎麼學習」? 學
    會了⼩數據也可以
    訓練。

    View Slide

  52. 數學軟體應⽤ 500
    如果可以找到這樣的函數...
    f
    ̂
    y
    1
    ̂
    y
    2
    ̂
    y
    n
    於是她就有個代表向 ̂
    y = [ ̂
    y
    1
    , ̂
    y
    2
    , …, ̂
    y
    n]

    View Slide

  53. 數學軟體應⽤ 501
    每個⼈就有個「代表向 」
    假設 是公司內四位同仁的照片。
    x
    1
    , x
    2
    , x
    3
    , x
    4
    f(x
    1
    )
    f(x
    2
    )
    f(x
    3
    )
    f(x
    4
    )
    f
    ̂
    y
    1
    ̂
    y
    2
    ̂
    y
    n
    看和哪個
    距離最⼩!

    View Slide

  54. 數學軟體應⽤ 502
    於是種種問題就解決了!
    比⽅說有新⼈來了, 我們就⽤訓練好
    的這個神經網路做她的代表向 。
    f
    ̂
    y
    1
    ̂
    y
    2
    ̂
    y
    n

    View Slide

  55. 數學軟體應⽤ 503
    還有個立即的好處
    我們可以規定, 什麼才叫「夠像」。就是定義⼀個數 , 如果
    τ
    d(f(x), f(x
    i
    )) < τ
    就判定是這個⼈。所以, 我們也可以知道, 這個⼈判斷這個⼈
    不是公司內部的⼈。

    View Slide

  56. 數學軟體應⽤ 504
    但是訓練資料難以準備...
    我怎麼知道什
    麼是代表她最
    好的向 ?

    View Slide

  57. 數學軟體應⽤ 505
    ⾃動特徵擷取機
    神經網路可以想成每個隱藏層在
    做「⾃動特徵擷取」。
    所以某個隱藏層的輸出, 可以看
    成原資料的代表向 !

    View Slide

  58. 數學軟體應⽤ 506
    從⽂字的 Word Embedding 得到的靈感...
    CNN
    ̂
    y
    1
    ̂
    y
    2
    ̂
    y
    n
    Dense Output
    (Softmax)
    砍掉最後⼀
    層就可以!
    做「正常」的⼈臉辨識, 然後砍掉最後⼀層!

    View Slide

  59. 數學軟體應⽤ 507
    也可以直接就訓練判斷是否為同⼀個⼈
    CNN
    ̂
    y
    1
    ̂
    y
    2
    ̂
    y
    n
    砍掉最後⼀
    層就可以!
    CNN
    ̂
    y
    1
    ̂
    y
    2
    ̂
    y
    n
    0

    View Slide

  60. 數學軟體應⽤ 508
    更好的是⽤ Triplet Loss
    CNN
    ̂
    y
    1
    ̂
    y
    2
    ̂
    y
    n
    CNN
    ̂
    y
    1
    ̂
    y
    2
    ̂
    y
    n
    CNN
    ̂
    y
    1
    ̂
    y
    2
    ̂
    y
    n
    越近越好
    越遠越好
    注意訓練時還是需要做 labeling

    View Slide

  61. 數學軟體應⽤ 509
    更好的是⽤ Triplet Loss
    F. Schroff, D. Kalenichenko, J. Philbin (Google). FaceNet: A
    Unified Embedding for Face Recognition and Clustering. arXiv
    preprint arXiv:1503.03832.
    CNN
    ̂
    y
    1
    ̂
    y
    2
    ̂
    y
    n
    CNN
    ̂
    y
    1
    ̂
    y
    2
    ̂
    y
    n
    Positive Sample Negative Sample

    View Slide

  62. 數學軟體應⽤ 510
    更⼀般化就是 Contrastive Learning
    f
    θq
    g
    θk
    Target 的部份 Sample 的部份
    q k
    神經網路可以⼀樣、也可以不⼀樣。
    要有 negative samples 是為預防 collapse 的發⽣。

    View Slide

  63. 數學軟體應⽤ 511
    Contrastive Learning
    sim(q, k)
    定義⼀個「相似度」函數。 可以是距離函
    數, 甚⾄⼤家
    更常⽤內積。
    ∥q − k∥2
    ⟨q, k⟩
    τ
    1 2
    例⼦

    View Slide

  64. 數學軟體應⽤ 512
    Contrastive Learning
    ℒ(θ) = − log
    esim(q,k+)
    ∑ esim(q,k)

    View Slide

  65. 數學軟體應⽤ 513
    我可以完全不做 labeling 嗎?
    以上的想法, 我們可以⽤較少的 labeling (⼤約 1%), 就可以訓練出很
    好的 model。但是, 我可以不做 labeling 就來訓練嗎? 可以的, 就是把
    ⼀張圖做各種惡搞, 不是, 轉換。也就是⼤家稱為 augmentation 的動
    作。
    旋轉 裁切 調亮度、顏⾊

    View Slide

  66. 數學軟體應⽤ 514
    Self-Supervised Learning
    Contrastive learning 去學 representation 時, 我們只⽤少
    labeling, 甚⾄不⽤ labeling。不管如何, 我們並沒有告訴神經網路, 任
    何⼀個 representation 是什麼, 因此, 這是非監督式的學習。
    但認真想想, 電腦其實是「⾃⼰訓練⾃⼰」, 所以 Yann LeCun 說這應
    該叫...
    self-supervised learning
    LeCun

    View Slide

  67. 數學軟體應⽤ 515
    Self-Supervised Learning
    We believe that self-supervised learning is one
    of the most promising ways to build such
    background knowledge and approximate a
    form of common sense in AI systems.

    —Yan LeCun (楊⽴昆)/Ishan Misra, 2021

    Self-supervised Learning: The Dark Matter of Intelligence
    https://ai.facebook.com/blog/self-supervised-
    learning-the-dark-matter-of-intelligence/

    View Slide

  68. 數學軟體應⽤ 516
    Non-Contrastive Learning
    實作的時候, 會發現為了 negative samples, 有些技術上的問題 (雖然
    也不算很⼤), 比如會⽤到很多記憶體、計算資源。於是⼤家想, 我們可
    以不要 negative samples, 但是⼜不會 collapse 嗎?
    f
    θq
    g
    θk
    q
    k
    x
    x+
    P
    φ
    預測

    View Slide

  69. 數學軟體應⽤ 517
    推薦範例
    http://bit.ly/yenlung
    colab08 ⽤ DeepFace 神速打造⼈臉辨識.ipynb
    Deep-Learning-Basics

    View Slide