文章首發於微信公眾號《與有三學AI》,

有三說GANs【上】

今天我們來說說GAN,這個被譽為新的深度學習的技術。由於內容非常多,我們會分上下兩期。今天這一期是上,我們從以下幾個方向來說。(1)生成式模型與判別式模型。(2)GAN的基本原理。(3)GAN的應用。同時也預告一下下期的內容,(1)GAN的最佳化目標,(2)GAN的模型發展(3)GAN的訓練技巧。

1生成與判別式模型【1】

正式說GAN之前我們先說一下判別式模型和生成式模型。

1。1 判別式模型

判別式模型,即Discriminative Model,又被稱為條件機率模型,它估計的是條件機率分佈(conditional distribution), p(class|context) 。

舉個例子,我們給定(x,y)對,4個樣本。(1,0), (1,0), (2,0), (2, 1),p(y|x)是事件x發生時y的條件機率,它的計算如下:

有三說GANs【上】

1。2 生成式模型

即Generative Model ,生成式模型 ,它估計的是聯合機率分佈(joint probability distribution),p(class, context)=p(class|context)*p(context) 。p(x,y),即事件x與事件y同時發生的機率。同樣以上面的樣本為例,它的計算如下:

有三說GANs【上】

1。3 常見模型

常見的判別式模型有Logistic Regression,Linear Regression,SVM,Traditional Neural Networks

Nearest Neighbor,CRF等。

常見的生成式模型有Naive Bayes,Mixtures of Gaussians, HMMs,Markov Random Fields等。

1。4 比較

判別式模型 ,優點是分類邊界靈活 ,學習簡單,效能較好 ;缺點是不能得到機率分佈 。

生成式模型 ,優點是收斂速度快,可學習分佈,可應對隱變數 ;缺點是學習複雜 ,分類效能較差。

有三說GANs【上】

上面是一個分類例子,可知判別式模型,有清晰的分介面,而生成式模型,有清晰的機率密度分佈。生成式模型,可以轉換為判別式模型,反之則不能。

2 GAN【2】的基本原理

GAN,即Generative adversarial net,它同時包含判別式模型和生成式模型,一個經典的網路結構如下。

有三說GANs【上】

2。1 基本原理

有三說GANs【上】

GAN的原理很簡單,它包括兩個網路,一個生成網路,不斷生成資料分佈。一個判別網路,判斷生成的資料是否為真實資料。上圖是原理展示,黑色虛線是真實分佈,綠色實線是生成模型的學習過程,藍色虛線是判別模型的學習過程,兩者相互對抗,共同學習到最優狀態。

2。2 最佳化目標與求解

下面是它的最佳化目標。

有三說GANs【上】

D是判別器,它的學習目標,是最大化上面的式子,而G是生成器,它的學習目標,是最小化上面的式子。上面問題的求解,透過迭代求解D和G來完成。

要求解上面的式子,等價於求解下面的式子。

有三說GANs【上】

其中D(x)屬於(0,1),上式是alog(y) + blog(1−y)的形式,取得最大值的條件是D(x)=a/(a+b),此時等價於下面式子。

有三說GANs【上】

如果用KL散度來描述,上面的式子等於下面的式子。

有三說GANs【上】

當且僅當pdata(x)=pg(x)時,取得極小值-log4,此時d=0。5,無法分辨真實樣本和假樣本。

GAN從理論上,被證實存在全域性最優解。至於KL散度,大家可以再去補充相關知識,篇幅有限不做贅述。

2。3 如何訓練

有三說GANs【上】

直接從原始論文中擷取虛擬碼了,可見,就是採用判別式模型和生成式模型分別迴圈依次迭代的方法,與CNN一樣,使用梯度下降來最佳化。

2。4 GAN的主要問題

GAN從本質上來說,有與CNN不同的特點,因為GAN的訓練是依次迭代D和G,如果判別器D學的不好,生成器G得不到正確反饋,就無法穩定學習。如果判別器D學的太好,整個loss迅速下降,G就無法繼續學習。

GAN的最佳化需要生成器和判別器達到納什均衡,但是因為判別器D和生成器G是分別訓練的,納什平衡並不一定能達到,這是早期GAN難以訓練的主要原因。另外,最初的損失函式也不是最優的,這些就留待我們的下篇再細講吧,下面欣賞一下GAN的一些精彩的應用。

3 GAN的應用

3。1 資料生成

從GAN到Conditional GAN

GAN的生成式模型可以擬合真實分佈,所以它可以用於偽造資料。DCGAN【3】是第一個用全卷積網路做資料生成的,下面是它的基本結構和生成的資料。

有三說GANs【上】

有三說GANs【上】

輸入100維的噪聲,輸出64*64的影象,從mnist的訓練結果來看,還不錯。筆者也用DCGAN生成過嘴唇表情資料,也是可用的。

但是它的問題是不能控制生成的數字是1還是9,所以後來有了CGAN【4】,即條件GAN,網路結構如下。

有三說GANs【上】

它將標籤資訊encode為一個向量,串接到了D和G的輸入進行訓練,最佳化目標發生了改變。

有三說GANs【上】

與cgan類似,infogan【5】將噪聲z進行了拆解,一是不可壓縮的噪聲z,二是可解釋的隱變數c,可以認為infogan就是無監督的cgan,這樣能夠約束c與生成資料之間的關係,控制一些屬性,比如旋轉等。

有三說GANs【上】

條件GAN的出現,使得控制GAN的輸出有了可能,出現了例如文字生成影象【6】的應用。

有三說GANs【上】

金字塔GAN

原始的GAN生成圖的解析度太小,無法實用,借鑑經典影象中的金字塔演算法,LAPGAN【7】/StackedGAN8【8】各自提出類似的想法,下面是LAPGAN的結構。

有三說GANs【上】

有三說GANs【上】

它有以下特點。

(1)使用殘差逼近,學習相對容易。

(2)逐級獨立訓練提高了網路簡單記憶輸入樣本的難度,減少了每一次 GAN 需要學習的內容,也就從而增大了 GAN 的學習能力和泛化能力。在這個基礎上,nvidia-gan【9】生成了1024解析度的圖片,它的網路結構和生成結果如下。

有三說GANs【上】

有三說GANs【上】

cross domain學習

cross domain的學習,提供了更豐富的資料生成應用。

在傳統的domain adaption中,我們需要學習或者訓練一個domain adaptor,而這個domain adaptor需要用source domain和對應的target domain的訓練圖片來訓練。coGAN【10】/align gan【11】可以在兩個domain不存在對應樣本的情況下學出一個聯合分佈,方法是每一個domain使用一個GAN,並且將高層的語義資訊進行強制權值共享。

有三說GANs【上】

有三說GANs【上】

在這樣的基礎上,有一些很有意義的應用。比如蘋果simGAN【12】用於最佳化模擬資料的方案,此時生成器G的輸入是合成影象,而不是隨機向量,它完美學習到了人工合成圖片(synthetic images)資料分佈到真實圖片(real images)資料分佈的對映。

有三說GANs【上】

下面是生成的結果,很有工程意義。

有三說GANs【上】

一些很酷的應用

下面再說一些很酷的應用,細節不再詳述。creative-gan【13】,用於生成藝術風格的圖片。

有三說GANs【上】

有三說GANs【上】

DesignGan【14】,用於設計T恤。

有三說GANs【上】

有三說GANs【上】

TP-GAN【15】,用於人臉正臉化。

有三說GANs【上】

有三說GANs【上】

還有更多關於影片,音訊的生成【16-19】,請關注我們後續文章和知乎專欄。

3。2 風格遷移

文【20】實現了畫素級別的風格轉換,它的關鍵是提供了兩個域中有相同資料的成對訓練樣本,本質上,是一個CGAN。

有三說GANs【上】

cycle-gan【21】/dual-gan【22】則更勝一籌,不需要配對的資料集,可以實現源域和目標域的相互轉換。

有三說GANs【上】

pairedcycle【23】,將源域和目標域的相互轉換用到化妝和去妝,很有趣的應用。

有三說GANs【上】

文【24】學習了一個數據集到另一個數據集的遷移,可以用於遷移學習,如實現漫畫風格。

有三說GANs【上】

有三說GANs【上】

文【25】實現了動作的遷移。

有三說GANs【上】

文【26】實現了年齡的模擬。

有三說GANs【上】

文【27】提出了一種去雨的演算法,很有實際意義。

有三說GANs【上】

文【28】實現了卡通風格的轉換。

有三說GANs【上】

文【29】實現了字型風格的遷移。

有三說GANs【上】

文【30】實現了去模糊。

有三說GANs【上】

更多風格化相關的應用,請關注配套知乎專欄。

3。3 超分辨重建

srgan【31】是最早使用GAN做超分辨重建的應用,它將輸入從隨機噪聲改為低解析度的圖片,使用了殘差結構和perception loss,有很大的應用價值。

有三說GANs【上】

有三說GANs【上】

超分辨重建可用於小臉的檢測【32】,是個值得關注的方向。

有三說GANs【上】

其實GAN還有很多其他的應用,我們這裡就不一一講述了。感興趣可以參加有三AI的GAN研究小組。

參考文獻

【1】Xue J H, Titterington D M。 Comment on “On Discriminative vs。 Generative Classifiers: A Comparison of Logistic Regression and Naive Bayes”[J]。 Neural Processing Letters, 2008, 28(3):169。

【2】Goodfellow I, Pouget-Abadie J, Mirza M, et al。 Generative adversarial nets[C]//Advances in neural information processing systems。 2014: 2672-2680。

【3】Radford A, Metz L, Chintala S。 Unsupervised representation learning with deep convolutional generative adversarial networks[J]。 arXiv preprint arXiv:1511。06434, 2015。

【4】Mirza M, Osindero S。 Conditional generative adversarial nets[J]。 arXiv preprint arXiv:1411。1784, 2014。

【5】Chen X, Duan Y, Houthooft R, et al。 Infogan: Interpretable representation learning by information maximizing generative adversarial nets[C]//Advances in neural information processing systems。 2016: 2172-2180。

【6】Reed S, Akata Z, Yan X, et al。 Generative adversarial text to image synthesis[J]。 arXiv preprint arXiv:1605。05396, 2016。

【7】Denton E L, Chintala S, Fergus R。 Deep generative image models using a laplacian pyramid of adversarial networks[C]//Advances in neural information processing systems。 2015: 1486-1494。

【8】Huang X, Li Y, Poursaeed O, et al。 Stacked generative adversarial networks[C]//IEEE Conference on Computer Vision and Pattern Recognition (CVPR)。 2017, 2(4)。

【9】Karras T, Aila T, Laine S, et al。 Progressive growing of gans for improved quality, stability, and variation[J]。 arXiv preprint arXiv:1710。10196, 2017。

【10】Mao X, Li Q, Xie H。 AlignGAN: Learning to align cross-domain images with conditional generative adversarial networks[J]。 arXiv preprint arXiv:1707。01400, 2017。

【11】Liu M Y, Tuzel O。 Coupled generative adversarial networks[C]//Advances in neural information processing systems。 2016: 469-477。

【12】Shrivastava A, Pfister T, Tuzel O, et al。 Learning from simulated and unsupervised images through adversarial training[C]//The IEEE Conference on Computer Vision and Pattern Recognition (CVPR)。 2017, 3(4): 6。

【13】Elgammal A, Liu B, Elhoseiny M, et al。 CAN: Creative adversarial networks, generating“ art” by learning about styles and deviating from style norms[J]。 arXiv preprint arXiv:1706。07068, 2017。

【14】Sbai O, Elhoseiny M, Bordes A, et al。 DeSIGN: Design Inspiration from Generative Networks[J]。 arXiv preprint arXiv:1804。00921, 2018。

【15】Huang R, Zhang S, Li T, et al。 Beyond face rotation: Global and local perception gan for photorealistic and identity preserving frontal view synthesis[J]。 arXiv preprint arXiv:1704。04086, 2017。

【16】Creswell A, Bharath A A。 Adversarial training for sketch retrieval[C]//European Conference on Computer Vision。 Springer, Cham, 2016: 798-809。

【17】Tulyakov S, Liu M Y, Yang X, et al。 Mocogan: Decomposing motion and content for video generation[J]。 arXiv preprint arXiv:1707。04993, 2017。

【18】Juvela L, Bollepalli B, Wang X, et al。 Speech waveform synthesis from MFCC sequences with generative adversarial networks[J]。 arXiv preprint arXiv:1804。00920, 2018。

【19】Yu L, Zhang W, Wang J, et al。 SeqGAN: Sequence Generative Adversarial Nets with Policy Gradient[C]//AAAI。 2017: 2852-2858。

【20】Isola P, Zhu J Y, Zhou T, et al。 Image-to-Image Translation with Conditional Adversarial Networks[J]。 2016:5967-5976。

【21】Zhu J Y, Park T, Isola P, et al。 Unpaired image-to-image translation using cycle-consistent adversarial networks[J]。 arXiv preprint, 2017。

【22】Yi Z, Zhang H, Tan P, et al。 Dualgan: Unsupervised dual learning for image-to-image translation[J]。 arXiv preprint, 2017。

【23】Chang H, Lu J, Yu F, et al。 Pairedcyclegan: Asymmetric style transfer for applying and removing makeup[C]//2018 IEEE Conference on Computer Vision and Pattern Recognition (CVPR)。 2018。

【24】Wei L, Zhang S, Gao W, et al。 Person Transfer GAN to Bridge Domain Gap for Person Re-Identification[J]。 arXiv preprint arXiv:1711。08565, 2017。

【25】Joo D, Kim D, Kim J。 Generating a Fusion Image: One‘s Identity and Another’s Shape[J]。 arXiv preprint arXiv:1804。07455, 2018。

【26】Yang H, Huang D, Wang Y, et al。 Learning face age progression: A pyramid architecture of gans[J]。 arXiv preprint arXiv:1711。10352, 2017。

【27】Qian R, Tan R T, Yang W, et al。 Attentive Generative Adversarial Network for Raindrop Removal from A Single Image[C]//Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition。 2018: 2482-2491。

【28】Chen Y, Lai Y K, Liu Y J。 CartoonGAN: Generative Adversarial Networks for Photo Cartoonization[C]//Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition。 2018: 9465-9474。

【29】Azadi S, Fisher M, Kim V, et al。 Multi-content gan for few-shot font style transfer[C]//Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition。 2018, 11: 13。

【30】Kupyn O, Budzan V, Mykhailych M, et al。 DeblurGAN: Blind Motion Deblurring Using Conditional Adversarial Networks[J]。 arXiv preprint arXiv:1711。07064, 2017。

【31】Ledig C, Theis L, Huszár F, et al。 Photo-realistic single image super-resolution using a generative adversarial network[J]。 arXiv preprint, 2017。

【32】Bai Y, Zhang Y, Ding M, et al。 Finding tiny faces in the wild with generative adversarial network[J]。 CVPR。 IEEE, 2018。