lecture16 GAN cont
lecture16 GAN cont
* Unless otherwise noted, all material posted for this course are copyright of the
instructor, and cannot be reused or reposted without the instructor’s written permission.
Housekeeping Items
Random
noise
Generator and Discriminator learn from the competition with each other.
Ian Goodfellow et al., “Generative Adversarial Nets”, NIPS 2014
Majid Komeili, Carleton University 3
Training the Discriminator
𝑛
Discriminator
BCE with labels for
𝐷𝜃 real (1) and fake (0)
G(z)
▪ Draw 𝑧 (1) , 𝑧 (2) ,…, 𝑧 (𝑛) (random noise) and generate n fake samples.
▪ Update the generator by gradient descent using:
1
𝜙 𝑛𝑒𝑤 = 𝜙 𝑜𝑙𝑑 − ∇𝜙 σ𝑛𝑖=1 −𝑙𝑜𝑔 𝐷𝜃 (𝐺𝜙 𝑧 (𝑖) ) Modified generator loss
𝑛
Carleton University 6
Issue with BCE loss
▪ Generally, the task of the discriminator is easier than the generator.
▪ If the discriminator becomes too strong relative to the generator, the BCE
loss will be saturated (flat regions).
▪ Consequently, there will be no gradient signal.
Discriminator/Critic Generator
𝑛 𝑛
1 1
GAN min
𝐷 𝑛
−𝑙𝑜𝑔 𝐷 𝑥 (𝑖) − 𝑙𝑜𝑔 1 − 𝐷(𝐺(𝑧 (𝑖) )) m𝑖𝑛
𝐺 𝑛
−𝑙𝑜𝑔 𝐷(𝐺 𝑥 (𝑖) )
𝑖=1 𝑖=1
𝑛 𝑛
1 1
WGAN m𝑎𝑥
𝑐 𝑛
𝑐 𝑥 (𝑖) − 𝑐 𝐺 𝑧 (𝑖) m𝑎𝑥
𝐺 𝑛
𝑐 𝐺 𝑧 (𝑖)
𝑖=1 𝑖=1
Carleton University 9
Conditional Generation
• Specify which class we want the Generator to generate images.
Random 0 otherwise
noise
Discriminator input
0
1 Matrix of full of zeros
Indicate 0 0
1
the class 0 0
0
0 0
Matrix of full of ones
0
0
RGB image
eyeglasses
Random
noise
Classifier
Generator (Pre-trained eyeglass
detector )
https://arxiv.org/pdf/1907.10786.pdf
𝑧0
Random
noise
Classifier
Generator (Pre-trained eyeglass P(eyeglasses)=0.01
detector )
https://arxiv.org/pdf/1907.10786.pdf
𝑧1
Random
noise
Classifier
Generator (Pre-trained eyeglass P(eyeglasses)=0.4
detector )
Backprop but do not update Backprop but do not update
Update z
using
backprop
https://arxiv.org/pdf/1907.10786.pdf
𝑧2
Random
noise
Classifier
Generator (Pre-trained eyeglass P(eyeglasses)=0.8
detector )
Backprop but do not update Backprop but do not update
Update z
using
backprop
https://arxiv.org/pdf/1907.10786.pdf
𝑧3
Random
noise
Classifier
Generator (Pre-trained eyeglass P(eyeglasses)=0.99
detector )
Backprop but do not update Backprop but do not update
Update z
using
backprop
https://arxiv.org/pdf/1907.10786.pdf
▪ Diversity
▪ Range of images the generator can generate.
Isola, Phillip, et al. "Image-to-image translation with conditional adversarial networks." 2017.
Image-to-image Translation
• Using naive loss functions often produced blurry images. → Use GAN
Isola, Phillip, et al. "Image-to-image translation with conditional adversarial networks." 2017.
Min Max 𝔼𝑦∈𝒟 𝑙𝑜𝑔 𝐷𝜃 (𝑦) + 𝔼𝑧 𝑙𝑜𝑔 1 − 𝐷𝜃 (𝐺𝜙 (𝑧)) y: real image
𝜙 𝜃 z: noise
G(z): fake image
Training dataset: images y.
cGAN loss function: Min Max 𝔼𝑥,𝑦 𝑙𝑜𝑔 𝐷𝜃 (𝑥, 𝑦) + 𝔼𝑥,𝑧 𝑙𝑜𝑔 1 − 𝐷𝜃 (𝐺𝜙 (𝑥, 𝑧))
𝜙 𝜃
x: source image
y: real target image
z: noise
As seen in the original paper:
G(x,z): fake target image
Training dataset: pairs of x,y images.
• cGANs were first proposed in Mirza, Mehdi, and Simon Osindero. "Conditional
generative adversarial nets." arXiv preprint arXiv:1411.1784 (2014).
▪ Generator tries to minimize the
function against an adversarial
• Pix2Pix is a cGAN proposed in Isola, Phillip, et al. "Image-to-image translation with discriminator that tries to maximize it.
conditional adversarial networks." 2017.
▪ Two Discriminators:
▪ 𝐷𝑦 : Discriminates y from G(x)
▪ 𝐷𝑥 : Discriminates x from F(y)
▪ Final loss:
▪ Hours to zebra
▪ Orange to Apple