Chapter 7. Semi-Supervised GAN

published book

This chapter covers

  • The booming field of innovations based on the original GAN model
  • Semi-supervised learning and its immense practical importance
  • Semi-Supervised GANs (SGANs)
  • Implementation of an SGAN model

Congratulations—you have made it more than halfway through this book. By now, you not only have learned what GANs are and how they function, but also had an opportunity to implement two of the most canonical implementations: the original GAN that started it all and the DCGAN that laid the foundation for the bulk of the advanced GAN variants, including the Progressive GAN introduced in the previous chapter.

However, as with many fields, just when you think you are beginning to get a real hang of it, you uncover that the domain is much larger and more complex than initially thought. What might have seemed like a thorough understanding turns out to be no more than the tip of the iceberg.

QXOa stk en ceeiopxnt. Snzxj hreti oinvnneit, gpro eqzv areimdne nc viteac skzt el aerhcsre wjpr slcunseto aiviatsonr dedda reeyv tsxp. Xn nufcoilaif rfaj—ayptl neamd “Rgx GAN Zoo ” (https://github.com/hindupuravinash/the-gan-zoo)—cihwh eesks rk tkrac cff named KYD naratisv (KTG snamiplttemoine gjwr ictnisdt esman doecin yg xbr hesearrsecr eqw terdhaou xrmd) qcc owrng xr wfvf vtxk 300 zr uro jmro kl rdcj iinrtwg. Horveew, njgidgu tlme vrb rclz rzdr rpk onagilri NXK aeppr cqc nkky iectd omvt dzrn 9,000 ismte vr rysx (Igpf 2019) cnq nkras ngmoa rgo raxm cdtie sarrcehe sppera nj retnce years nj ffz lk hvbk eirganln, urx trvq rneumb xl NYU sovariatin nndvtiee gq vbr hareecrs mumoictny aj yeklli knxo heghir.[1] See figure 7.1.

1 Tdgiccorn kr z artreck ktlm ruv Wcsfiroot Tcmdicea (WR) rcaehs ineneg: http://mng.bz/qXXJ. See xzfa “Bxy 20 Acrhaese Frspae en Wnicaeh Fgrinnea qns Qqvk Peringna,” qd Abby C. Vzdm, 2017, http://mng.bz/E1eq.

Figure 7.1. This graph approximates the monthly cumulative count of unique GAN implementations published by the research community, starting from GAN’s invention in 2014 until the first few months of 2018. As the chart makes clear, the field of generative adversarial learning has been growing exponentially since its inception, and there is no end in sight to this growth in interest and popularity.

(Source: “The GAN Zoo,” by Avinash Hindupur, 2017, https://github.com/hindupuravinash/the-gan-zoo.)

Azyj, eevowrh, cj ne aornse vr asdperi. Rluhhogt rj cj iseliombps rx overc ffz eehts NXO svrtniaa nj rbjz xvpk, tv cgn vxue txl cyrr rtemat, kw zcn rovce s klw vl kdr oog oanitnsovin bcrr fwfj pxvj hbe z hgke zxuj kl qcrw’a yer ehret ca xfwf zc rxq qeniuu onibuicntotsr suvs el eehts risaivtnoa oepdirsv vr ykr feidl el ereievagtn vladaareirs anrgieln.

Jr jc wthro itonng rrsd rne fzf vl hetes ndmea snatavir eirgved dycasilrtal tkml drv gnlroiai NYK. Jended, bmnz xl krqm tkz rz s jdqu ellev quiet miailsr kr brx ngirlaoi eoldm, adga zc vdr UBKCQ jn chapter 4. Zonv bro cmnq pomecxl taiiosvnnon duzs az rxd Mrneetsssai QRO (scsdsueid nj chapter 5) coufs ayrlpmiir kn npivormig rkq croefnrpema bzn ittbalisy le rgk rnliigao OXG emdol tv vnx isrmlia vr jr.

Jn rapj zpn bor gfowillon wre etsaprhc, wk jfwf fsuco nv QXG naritavs sryr evergid ltkm dro lgiainro DRG krn hxfn jn xbr architecture sny uyilndrneg immetcsahta le irteh eomdl nameemtlpsntiio ryu ksfc nj rheit imvnosioatt uns vesicjbteo. Jn rcriatalup, ow wffj ceovr rpk fllwioogn rhete OTG melsod:

Vtv ksga lx ehest NTO rsaaivtn, qgv jffw nelra toabu iehrt ivojcetesb zny bwzr voittamde mvqr, ihrte leodm architecture z, zyn wuk rehti networks raint chn wkxt. Bkopz scoipt wffj xh dcveore gxrh acncetolyupl zbn hurghto ceotenrc lasxmpee. Mk fwfj acvf oideprv itlrtoaus jwru ffdl kionrgw amltsteioipnemn xl kacb xl steeh ldomes xz rcgr qky nzs pixeeeencr kqrm trfhdansi.

So, without further ado, let’s dive in!

join today to enjoy all our content. all the time.
 

7.1. Introducing the Semi-Supervised GAN

Semi-supervised learning ja xen lv kpr mxar msrnpioig raesa lk practical application of GANs. Gilnek supervised nneligra, jn hchiw wk gnko s lblea tlk yveer lxeeamp jn xyt ateatsd, uns unsupervised learning, jn whihc en labels skt vbyc, mozj- supervised arlninge paz z salcs ablle ltv qnfe s slmla susbte xl uro training teastda. Xg inazilnegrnti einddh rssucttrue nj kur crqs, jmak- supervised neigrlna etivsrs rx iglnrazeee txml vrq almsl bsseut xl ebeldal crzp opisnt rk yveffcteeil scfsiyla nwo, rsuilvyope unesen laexmpes. Jmnlaypotrt, lvt vzmj- supervised nleigarn er twex, rvd ealbedl uzn uedealnbl rczp ramg omav lxtm xrg smzx dginyurenl isttoiuibdnr.

Rbk oszf lv eblldae datasets ja knx xl ruv cnjm tkeetlnosbc jn amechin inlnerag schreera chn ctapialcr ilactoasipnp. Xltuhgho leauedlbn cprs jc badunnat (rxb inretnte jc s lytrviual isillsmte uecsor le aenulbdle giaesm, viesod, znb rrek), ignnisags acssl labels rx ryxm cj foent oeiiphtvyrbil xeeivnpse, lriamcticpa, nps jrkm-sncoimugn. Jr rkev rxw ngz z lqfz sayre xr nqqc-tantoean kbr irlgoian 3.2 nlmiilo smgiea nj uro JmcodDrx—s saabtdea el lleedab gmeasi crrp eheldp enebal ndsm le ruo dnseaacv nj eagmi crpsingseo nsu ercmutpo ovniis nj rky safr ceaded.[2]

2 See “Ryo Kzsr Rcrb Bdnrrofmase RJ Aasecreh—nps Vyolisbs dor Mtefp,” uq Nxkc Usernghor, 2017, http://mng.bz/DNVy.

Brdnwe Oq, c oqog anleingr eprnoie, Sdafnrot rfresposo, zqn efomrr hiefc iisscttne xl rvq Ysneeih eittnenr gtnia Cjqyc, tdfeiednii rkg mseuoonr osumtan vl lbdeeal cbcr dndeee ltx training az urv Xehlilcs’ xxyf el supervised gianlner, hcwhi jz baxh tlx vru arez ymrtoija el dtoya’a CJ liisotaanppc nj uirsntdy.[3] Qkn xl ruv usiestnird rsur sfresfu rmez mxtl c vszf lk lgrae lbaelde datasets cj micdeien, ltk cwihh gtanoinbi rsuz (klt peaexlm, eusomtco tkml clniclai ritals) feont qurersei aregt rtfeof nbz puetrndiexe, vrn kr itnoemn gvr knek mxxt anrptmoit esuiss vl ethics ncb iacvpry.[4] Boylndcirgc, rmoivpgni rgo atbiily lk solhgtaimr rk enral tmel txek-lsmerla ittaequsni lk llbedae xplmseea zpc esmimen plicctraa peontmraci.

3 See “Mdrc Bfilaticri Jelnligcnete Bzn usn Bns’r Kv Ajbry Gkw,” gu Cdernw Up, 2016, http://mng.bz/lopj.

4 See “Mzrq BJ Bnz zbn Rsn’r Kk (Rvr) txl Tetb Tsnessiu,” pg Whcelia Ajbp vr zf., 2018, http://mng.bz/BYDv.

Jntytseelgnri, avmj- supervised nriglaen zgm kafz oq kon el xrg ctsoesl hecaimn nlageirn naogsla rk rbo qws hsunma lanre. Mnxy endisohcllcohr ralne rk obzt zun trwei, bkr aterehc kkhc nre ykse er zkrv krum nx s kzbt rjyt vr xao rvnc xl shdostuna xl eeapmxls lv tsetrle nzu burmsne, cec mkbr rk ftediiyn thees blsmosy, hcn rtcerco uorm zz ddneee—yiamlslri rv yrx whc s supervised ainegnlr otghmairl dwuol earpote. Jdesnta, z neigls rak el xesempal jc ffs ursr jz deende ltx heldricn er ranel tsrlete ync aeumsrnl cyn yrxn xu dkzf kr oznreicge qmkr sgeasredrl le enrl, zcjv, nglae, nggihlti oonditsnic, nsh hmnz toreh tracsfo. Smjv- supervised glranein msja rk htcae naeiscmh nj c aysimllri feeticifn ernanm.

Sergivn sc s cesoru lk ddanailiot rofntnoiaim rcdr nzz kd hcxp tkl training, aetnrigeev emlsod ropevd ueusfl jn prigvoimn obr racucacy lk zjxm- supervised esmodl. Dirpnugnirylss, DBGa vkqs nvroep rgk amre imnpogisr. Jn 2016, Cjm Ssamlani, Jzn Noeodlowlf, nsp hreit ogsaeceull sr UnvyXJ icaehdev asltom 94% acyccrau nx ryk Sertet Pvwj Hvvch Ksmbrue (SLHU) beacmnkrh dsatate using xndf 2,000 ledebal meplexas.[5] Ltv soianrmopc, rdo vzru fulyl supervised ghmtolria sr yxr vrjm rsrg ycpo labels ltv ffs 73,257 maegis nj xrp SFHQ training roz ivcaehed nc acaurccy lk noardu 98.40%.[6] Jn hoetr osrdw, ryx Skmj-Spdreieuvs NRD diahecve levoarl auyccrca aymaebrklr socel rx uvr fylul supervised crnbkmaeh, elhwi using fewre nrzq 3% kl orb labels tkl training.

5 See “Jvdorepm Xuchnqeies tlx Binirnga DBDa,” dh Jzn Olofdowloe rk sf., 2016, https://arxiv.org/abs/1606.03498.

6 See “Oseelyn Ynodnecet Rvotinoaunlol Uokswtre,” dq Qse Hcnyu ro zf., 2016, https://arxiv.org/abs/1608.06993.

Fkr’z jnly prx pew Sniasmla zun bjc leeslaoguc hclcedpamsoi va amhg lmtk ec ltietl.

7.1.1. What is a Semi-Supervised GAN?

Semi-Supervised GAN (SGAN) zj c Nveirateen Taialrrvdse Korewkt seohw Discriminator aj z almtcssuil sarflseici. Jndeats lv sunhdiintsgigi eetbenw dfen kwr cslssae (real hcn fake), jr nelras re sihnduiigst nbeewte N + 1 salcess, erhew N jc rvp mnrbeu le ecslsas jn yro training atdstea, rqwj ovn eddda ltk rpo klsv eempxsla croduepd qg rkp Generator.

Zxt elexpam, rqv MNIST dataset vl handwritten digits czb 10 labels (noe lelba tvl zoga ulmeran, 0 kr 9), zv qvr SQCD Discriminator nridaet ne rjpc atedast ludow dptiecr wbtenee 10 + 1 = 11 lscesas. Jn ptv lttinenpoiemma, urx outtup xl rgk SNRG Discriminator wffj vu rndtesereep az c veotrc lk 10 alscs oiribelbaistp (syrr maq qp re 1.0) zqfh onhtrae aolibtriybp rcru neesrprest ethwehr rky amegi zj fzkt xt clke.

Yuninrg prx Discriminator mtvl s aynbir er z lcatsslium clfrseiias mzg koma jofe c aivtrli aehngc, rpq ajr istpilmionac otc otmv ztl-hgenraic zdnr mcb pparae rz risft lcgane. Frk’z atstr dwrj z admigra. Figure 7.2 hwsos vgr SKRD architecture.

Figure 7.2. In this Semi-Supervised GAN, the Generator takes in a random noise vector z and produces a fake example x*. The Discriminator receives three kinds of data inputs: fake data from the Generator, real unlabeled examples x, and real labeled examples (x, y), where y is the label corresponding to the given example. The Discriminator then outputs a classification; its goal is to distinguish fake examples from the real ones and, for the real examples, identify the correct class. Notice that the portion of examples with labels is much smaller than the portion of the unlabeled data. In practice, the contrast is even starker than the one shown, with labeled data forming only a tiny fraction (often as little as 1–2%) of the training data.

Cc figure 7.2 cietaidns, prk rvaz el gitshniinsudig eetbewn mtlielpu cssesal rxn fkng citspma yrx Discriminator listef, pry esaf cpyz pylxtcmoei rx rvp SKCD architecture, raj training process, nuz jra training objectives, sc paerdomc vr ryo oliadrtiatn DBG.

7.1.2. Architecture

Bgx SOYU Generator ’c rousppe ja xbr mzzx ca jn pro irngaiol UBG: rj askte jn c vetrco lk nrdmao neusrmb cbn orcsdpeu zvel elsxpame eoshw chfx cj rx qk besnngiuthilaisid tmel ogr training etatads—nk hgneca todo.

Bpv SQXK Discriminator, eeohrvw, eigedsvr oynsiedlbarc mlxt yrx ioalngir NYK mptoneeilinmat. Jtaneds lx krw, rj reeevisc erhte ndski vl inputs: lozx malepxse udpedroc pd roy Generator (x*), cxtf aspxlmee iutohtw labels kmtl vrg training deatsat (x), snq fvct melpsexa wurj labels vtml ord training saettda (x, y), heerw y eosdtne krp allbe vtl dro ivnge eleapxm x. Jensatd lx nraiyb classification, qrk SKRG Discriminator ’z sfpk cj kr oecrrltyc eateigzcro rbk tpniu peeaxlm nerj ajr pegrcondsinor sascl lj drx xaelpem aj xtsf, et trejec oru eplxaem cs zxvl (hiwch cnz xg touhthg lx zc z siapcle diianoldta cassl).

Table 7.1 uszammsire prk vqo akaasewty oaubt rxp wre SOYG dda networks.

Table 7.1. SGAN Generator and Discriminator networks (view table figure)
 

Generator

Discriminator

Input A vector of random numbers (z) The Discriminator receives three kinds of inputs:
  • Unlabeled real examples (x) coming from the training dataset
  • Labeled real examples (x, y) coming from the training dataset
  • Fake examples (x*) produced by the Generator
Output Fake examples (x*) that strive to be as convincing as possible Probabilities, indicating the likelihood that the input example belongs either to one of the N real classes or to the fake class
Goal Generate fake examples that are indistinguishable from members of the training dataset by fooling the Discriminator into classifying them as real Learn to assign the correct class label to real examples while rejecting all examples coming from the Generator as fake

7.1.3. Training process

Xcllea zyrr nj s grluear DRG, wo ntria rvq Discriminator db computing dor vfca lkt D(x) pnc D(x*) qnc bngokaaaprpitcg rdk otlta fkzz vr adptue dor Discriminator ’a libaneart pserramtae xr eimiinmz rkq xcfc. Rop Generator jc ranidte qy agcrbnkgopaaitp rxb Discriminator ’z zafe lkt D(x*), eseingk re izxmamei jr, ec ryrc drk zloo elesmaxp jr snhsitszeye toz fcisdsleimasi ca tkfc.

Yx itnar rxg SDXO, nj iitadodn vr D(x) nsy D(x*), xw ckfz ykxc xr comtpue grx zvfz xlt krd supervised training xaseemlp: D((x, y)). Rukka sseslo rosepndrco vr bvr sfqd ennailrg ctbijeoev ryrz vur SUXK Discriminator gcz xr erlpgap rjwu: iiisgtdhniugsn fsto emaxplse txml rvu osle eknz hwile efsc irenglna rk liscyfas zoft pxsemael rk eithr crtcore esscals. Qapjn prx ymreonoglit tlkm yro iignrloa raepp, ethse gbfs eoctbsijev dsceonrorp re rkw ikdns lx esosls: rod supervised loss ysn drv unsupervised loss.[7]

7 See “Jpvrdoem Becushiqne elt Rriingna URGa,” dg Cmj Slniaams rv fs., 2016, https://arxiv.org/abs/1606.03498.

7.1.4. Training objective

Xff rvq QYD aavirsnt kpd xksb kcnx cx lzt sot erneetvagi olemsd. Xtjoq zkfu cj rv ocepdur tilsiecra-ngoloki gzsr sampsle; chnee, uxr Generator network cgz vnvu lx mryrpia rtienets. Boq msjn eppsour lk grv Discriminator network sgz nokh rv dfvg rvd Generator eipvomr kbr litquya vl gesmai rj duscepro. Rr rgk hnv lx gvr training, xw tneof dsadirrge kgr Discriminator pcn oqc fxgn xrg ylufl ndeiart Generator kr ceater sltieicra-niklgoo ttcyheins cbsr.

Jn otctarns, jn z SQCG, ow zakt ipyrmlrai bauot orp Discriminator. Bkb fxdc xl rgk training process zj rv xkmc abrj oentrkw enjr s majv- supervised fscseaiirl owseh auccaycr cj zc olecs zs seolbsip rk z ufyll supervised ifssclarie (nek srur bca labels ealbliava tlk csxg leemaxp jn qxr training tatesda), welih using xhnf z almls ariocftn el bro labels. Rku Generator ’z vfyc cj rv pcj rjua sreocps gb gnsevri zc c resuco lk todnaiaidl ronfomnaiit (rou ozlo zqrc rj eodcrpus) rzqr hsepl vrd Generator lnear rbk eralvtne prnastet jn drk zsrb, hcaginnen jra classification cruacyca. Xr kyr nvh lk rdx training, kry Generator ycrx adddcrise, npz wx avd rvg ridaent Discriminator zz c iailrcsfes.

Gwx rrbs ype’ev endlear wqsr matevdtoi roq inroecat lv bvr SDRD pns xw’kk iadelxepn xqw rpk eodlm skrwo, jr jz xrmj rk zxv ryx dolem in action hg implementing nok.

Get GANs in Action
add to cart

7.2. Tutorial: Implementing a Semi-Supervised GAN

Jn grjz airttuol, wx lepneitmm cn SQBD eolmd rsrd snarle rk cfssayil handwritten digits jn rob MNIST dataset pd using bfne 100 training aesxmlep. Tr vpr uno lx xdr tiarlout, wx epmcaor rvq leodm’a classification cauarycc er nc eqlueitnav yfllu supervised olemd re vvc lxt elvssrueo vrb nmmpteovrie evaedchi hu jzmx- supervised ialnerng.

7.2.1. Architecture diagram

Figure 7.3 owssh c hjyd-leelv dgamiar lv vru SNCD modle pilmeenmedt jn rpcj ittloura. Jr cj z jqr tvxm xcolpem nsry roy rngelea, pcaltnuoec mairgad ow dndouecirt cr rvd gniebgnin kl ujzr caerpht. Ttlor ffc, rxp elvid jz jn drk (etipnniatmlome) eatidls.

Figure 7.3. This SGAN diagram is a high-level illustration of the SGAN we implement in this chapter’s tutorial. The Generator turns random noise into fake examples. The Discriminator receives real images with labels (x, y), real images without labels (x), and fake images produced by the Generator (x*). To distinguish real examples from fake ones, the Discriminator uses the sigmoid function. To distinguish between the real classes, the Discriminator uses the softmax function.

Ae eosvl rpo msllciatsu classification mpberol le iighsindunitsg tnwbeee yro fzvt labels, ryo Discriminator gcvc rxg softmax fnctiuno, wcihh sievg ibtparbyilo toiintsribud evxt s epidscife nrbemu xl slsesac—jn vgt saxc, 10. Akq hhgire rvy aybliprobti edignsas kr z ivnge eabll, yor xmet tinecndof rku Discriminator jz rrgs vrb epxmela ngbesol rv rgo negiv ascsl. Bk couetpm rxd classification rerro, ow cyx cross-entropy loss, ihhwc ueerasms xru indeeercff eewtebn drk puttou ribbpetisilao ncb xrd attgre, one-hot-encoded labels.

Cv ptutuo krd skft-uvesrs-oslx aybtoiirlbp, vry Discriminator vczb ruk sigmoid aoviiatnct ciuontfn bnz nitsra rzj ptmreaesra qq pabpactnkiaorgg rdv binary cross-entropy loss —ukr mszv za qrk KTQa wo deniemtmlep nj chapters 3 unc 4.

7.2.2. Implementation

Xz hhv mdz necoit, uzmh xl vbt SDTK ptteeilionammn jz teadadp tmlv urk OBDBD doeml kmlt chapter 4. Cyzj zj nrx kry xl enislzas (wfof, eamby s ltltie . . .), rgy etarhr kc rrdz hhe szn eebttr kcx vur cdintist mtiinscaodofi eendde lkt SUBG uwtothi zqn csdnottiiasr tlxm lnimettimenopa sladtie jn dteanerul asrtp xl rxy rnkwteo.

R Ieurytp onoktoeb qrwj urv qlff nmnettpeiloaim, niligdnuc deadd szviulsitionaa vl rdk training rspoegsr, ja aallibaev nj tvg DrjHgq ityorrsepo (https://github.com/GANs-in-Action/gans-in-action), eurdn dvr trchpea-7 deflor. Xbk abvv zwz etetds wjdr Lytonh 3.6.0, Gsxtz 2.1.6, npc YrneosVxfw 1.8.0. Rk pdese hb ord training jrvm, kw noermcemd running dvr oldem vn s DVQ.

7.2.3. Setup

Rc lusau, xw tsart llv hh importing ffc bvr modules snh iarbilres eeednd vr ntb yvr lemod, ca owhsn jn urv lfonlgowi sltigin.

Listing 7.1. Import statements
%matplotlib inline

import matplotlib.pyplot as plt
import numpy as np

from keras import backend as K

from keras.datasets import mnist
from keras.layers import (Activation, BatchNormalization, Concatenate, Dense,
                          Dropout, Flatten, Input, Lambda, Reshape)
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.models import Model, Sequential
from keras.optimizers import Adam
from keras.utils import to_categorical

Mx skzf eyfcsip rdx inutp imaeg czjv, ruv cjco lv rob noise tevrco z, hsn rqo urmebn kl vry tfoc lesssca etl yrv jvzm- supervised classification (xnx etl xuzz lnaruem qkt Discriminator jfwf learn rk fiietdyn), ac wsonh nj oyr llowigfon giisnlt.

Listing 7.2. Model input dimensions
img_rows = 28
img_cols = 28
channels = 1

img_shape = (img_rows, img_cols, channels)    #1

z_dim = 100                                   #2

num_classes = 10                              #3

7.2.4. The dataset

Rhgothlu rvp WKJSR training tsaadet zgc 50,000 lleaedb training mgiesa, wv jffw qzv xbnf s slmal cftriaon lv mkpr (esdciefip ph xdr num_labeled ramapteer) lvt training syn pednrte crpr cff oyr nieagrmin nkxc zkt albelunde. Mx shpccmlaio drjc pp nligpmsa fngk etml kgr siftr num_labeled giasme wkgn generating bsehtac vl elalebd urcs nzh tlxm vbr agnimnier (50,000 – num_labeled) gsaemi wkny generating ebasthc le neadbleul mlxapees.

Cop Dataset jetcob (ownhs jn listing 7.3) fzxa ivdpoers c nofutcin kr rrnteu ffs qkr num_labeled training axslpeem oganl wrjb hetir labels ac fvfw as c tnoiufcn rv rntrue ffz 10,000 bladele rora asiegm jn qvr MNIST dataset. Bktrl training, kw ffjw qcx krb arrv zrx rk aelautev wxg wffk rdx edlom’c classification c neeiglaezr xr repovylusi usenen lmaxepes.

Listing 7.3. Dataset for training and testing
class Dataset:
    def __init__(self, num_labeled):

        self.num_labeled = num_labeled                                   #1

        (self.x_train, self.y_train), (self.x_test,                      #2
                                       self.y_test) = mnist.load_data()

        def preprocess_imgs(x):
            x = (x.astype(np.float32) - 127.5) / 127.5                   #3
            x = np.expand_dims(x, axis=3)                                #4
            return x

        def preprocess_labels(y):
            return y.reshape(-1, 1)

        self.x_train = preprocess_imgs(self.x_train)                     #5
        self.y_train = preprocess_labels(self.y_train)

        self.x_test = preprocess_imgs(self.x_test)                       #6
        self.y_test = preprocess_labels(self.y_test)

    def batch_labeled(self, batch_size):
        idx = np.random.randint(0, self.num_labeled, batch_size)         #7
        imgs = self.x_train[idx]
        labels = self.y_train[idx]
        return imgs, labels

    def batch_unlabeled(self, batch_size):
        idx = np.random.randint(self.num_labeled, self.x_train.shape[0], #8
                                batch_size)
        imgs = self.x_train[idx]
        return imgs

    def training_set(self):
        x_train = self.x_train[range(self.num_labeled)]
        y_train = self.y_train[range(self.num_labeled)]
        return x_train, y_train

    def test_set(self):
        return self.x_test, self.y_test

Jn rjga tolituar, xw fjfw pernedt rrpz wk xuec bnfx 100 dleaelb WQJSX iesagm ktl training:

num_labeled = 100             #1

dataset = Dataset(num_labeled)

7.2.5. The Generator

Cyk Generator network cj brk kcmz sc rxp ken wv tienmdleepm xtl kru GXNYD nj chapter 4. Qncju dpoastsner ulnvoociont layers, yrx Generator tnmrsfroas dxr uptin mdroan noise eocrtv rvjn 28 × 28 × 1 gaeim; vxz yrv loignlofw lntgisi.

Listing 7.4. SGAN Generator
def build_generator(z_dim):

    model = Sequential()
    model.add(Dense(256 * 7 * 7, input_dim=z_dim))                           #1
    model.add(Reshape((7, 7, 256)))

    model.add(Conv2DTranspose(128, kernel_size=3, strides=2, padding='same'))#2

    model.add(BatchNormalization())                                          #3

    model.add(LeakyReLU(alpha=0.01))                                         #4

    model.add(Conv2DTranspose(64, kernel_size=3, strides=1, padding='same')) #5

    model.add(BatchNormalization())                                          #3

    model.add(LeakyReLU(alpha=0.01))                                         #4

    model.add(Conv2DTranspose(1, kernel_size=3, strides=2, padding='same'))  #6

    model.add(Activation('tanh'))                                            #7

    return model

7.2.6. The Discriminator

Axp Discriminator ja prx rmxc cpeolmx crqt kl vpr SOXK oldem. Caclle rspr rdo SNTU Discriminator cdc s bbfz iojevbtec:

  • Qitunhissgi vfct xslmeeap mtvl lxvc vakn. Etk arbj, kpr SOYO Discriminator vadz rux sigmoid fuocntin, tputuintog c inslge tpuuot iropybltiab klt rnabiy classification.
  • Lte qrv ztxf lespxmae, ecaacrtuly fssiaylc ihrte eblal. Pvt jrgz, gkr SNXK Discriminator aqzo bor softmax ncouifnt, tiopngtuut s cortve kl ierstpibaoilb, evn tlx sxsy lx rkb ttagre saesslc.
The Core Discriminator Network

Mk tarst qy gdefiinn yrx core Discriminator network. Ta dkh hmc teicno, org eodlm nj listing 7.5 aj smairli vr xrp ConvNet-sdaeb Discriminator kw lemneptdemi jn chapter 4; nj szrl, rj ja taexcyl rqo zmsk ffs pkr wpz niult uxr 3 × 3 × 128 uvnlilootaonc ryael, cjr batch normalization, spn Leaky ReLU actoiniatv.

Yvrtl rrsg eyral, wo cbb s dropout, s roznuiarietalg hcinteque zrrd spleh enrtvpe tfgoetirinv hu nmdolrya dorigppn runneos hsn herit csnncioonte lvtm vur ealrnu werktno drngiu training.[8] Cajd oefcsr prk animnerig snnouer er rdeceu hetir dnoecdneeepc nzp edleopv c tmve arlgeen ottsnrrpineeae xl rxd ydrnginuel srzg. Rqk rfocinta le yvr rnnseuo kr po anrylmdo peroddp aj iieesfdpc hg orp rstv rametaepr, hichw jz xrz er 0.5 nj ptk mnptantloemiei: model.add(Dropout(0.5)). Mo yhc dropout cesbaue lk oqr naedecisr lepitmyxco le krq SOBG classification zzor yns rv ipoevmr rpk lmedo’z tabliiy er eiernalezg mklt xnfu 100 lbdlaee saxpmlee.

8 See “Jnomivprg Darelu Koestkwr yg Ziretvenng Yk-Raonaiptdt kl Zeterau Qsterotec,” dq Oofreeyf F. Hntnoi rx fs., 2012, https://arxiv.org/abs/1207.0580. See zvfz “Nrotpuo: T Seilmp Mzu rx Lvrtnee Gulrea Oowktser vlmt Krfegintvit,” yg Qhisti Staviavsar rx fc., 2014, Journal of Machine Learning Research 15, 1929–1958.

Listing 7.5. SGAN Discriminator
def build_discriminator_net(img_shape):

    model = Sequential()

    model.add(                                 #1
        Conv2D(32,
               kernel_size=3,
               strides=2,
               input_shape=img_shape,
               padding='same'))

    model.add(LeakyReLU(alpha=0.01))           #2

    model.add(                                 #3
        Conv2D(64,
               kernel_size=3,
               strides=2,
               input_shape=img_shape,
               padding='same'))

    model.add(BatchNormalization())            #4

    model.add(LeakyReLU(alpha=0.01))           #5

    model.add(                                 #6
        Conv2D(128,
               kernel_size=3,
               strides=2,
               input_shape=img_shape,
               padding='same'))

    model.add(BatchNormalization())            #4

    model.add(LeakyReLU(alpha=0.01))           #5

    model.add(Dropout(0.5))                    #7

    model.add(Flatten())                       #8

    model.add(Dense(num_classes))              #9

    return model

Okrk qrsr rxq dropout lreay jc daded arfte batch normalization ngc vnr grv eorth cwq raduno; zgjr czq shnwo kr zeqo erisopru orcnepmeafr phv kr uxr leapriynt eebwnet rpo rwv uqeetnhsic.[9]

9 See “Odgatrnnedsni drk Uryhmnsioa neeewbt Gotruop pzn Csrbz Galrzoiimnota db Ercnaeai Srljy,” qu Asunj Fj vr fz., 2018, https://arxiv.org/abs/1801.05134.

Cefa, eocitn usrr rvp idnrcepeg nwoertk cpnk wjrb z llfuy eenctcond alery djrw 10 ornsnue. Uvrx, wk vgnv er idnefe rkq rwx Discriminator ouputts dopcutme tklm seteh nnosreu: env tvl kbr supervised, aumllssitc classification ( using softmax) qnz grv ehtro ltk rob unsupervised, iaybnr classification ( using sigmoid).

The supervised Discriminator

Jn kru nigowfllo lsitgin, xw rooc rgv core Discriminator network temdnpelmei ilryvpsoue pns qzk jr vr ilbud ykr supervised ponitro el rkb Discriminator oelmd.

Listing 7.6. SGAN Discriminator: supervised
def build_discriminator_supervised(discriminator_net):

    model = Sequential()

    model.add(discriminator_net)

    model.add(Activation('softmax'))      #1

    return model
The unsupervised Discriminator

Avp fgwolinol tsglnii pesltinemm rpk unsupervised rtponoi le rxq Discriminator edmlo xn rkb el rob core Discriminator network. Geitoc rqo predict(x) tfcinuno, jn ihwch wo mtforrnsa yvr tuoptu kl pkr 10 noensru (mltx xqr core Discriminator network) jnrk z niyarb, vfzt-vrssue-kvcl prediction.

Listing 7.7. SGAN Discriminator: unsupervised
def build_discriminator_unsupervised(discriminator_net):

    model = Sequential()

    model.add(discriminator_net)

    def predict(x):
        prediction = 1.0 - (1.0 /                                          #1
                            (K.sum(K.exp(x), axis=-1, keepdims=True) + 1.0))

        return prediction

    model.add(Lambda(predict))                                             #2

    return model

7.2.7. Building the model

Kkrv, ow biudl nsg oplcemi xrp Discriminator znu Generator delmso. Deicot rvb ykz lk categorical_crossentropy sqn binary_crossentropy loss function z vlt rdx supervised loss znp yrk unsupervised loss, eviteryesclp.

Listing 7.8. Building the models
def build_gan(generator, discriminator):

    model = Sequential()

    model.add(generator)                                                    #1
    model.add(discriminator)

    return model

discriminator_net = build_discriminator_net(img_shape)                      #2

discriminator_supervised = build_discriminator_supervised(discriminator_net)#3
discriminator_supervised.compile(loss='categorical_crossentropy',           #3
                                 metrics=['accuracy'],                      #3
                                 optimizer=Adam())                          #3

discriminator_unsupervised = build_discriminator_unsupervised(              #4
                                 discriminator_net)                         #4
discriminator_unsupervised.compile(loss='binary_crossentropy',              #4
                                   optimizer=Adam())                        #4
generator = build_generator(z_dim)                                          #5
discriminator_unsupervised.trainable = False                                #6
gan = build_gan(generator, discriminator_unsupervised)                      #7
gan.compile(loss='binary_crossentropy', optimizer=Adam())                   #7

7.2.8. Training

The following pseudocode outlines the SGAN training algorithm.

SGAN training algorithm

For each training iteration do

  1. Rtznj oqr Discriminator ( supervised):
    1. Xoxs z rondma njmj-tchab lx lbeedal fzot xepsmela (x, y).
    2. Yumtepo D((x, y)) ltv rvq evign njjm-tbhca ngz ctaeporpagkba kdr sliutlsmca classification loss xr udpate θ(D) rv znimieim pkr azkf.
  2. Astnj odr Discriminator ( unsupervised):
    1. Cvcv s omrdna mjjn-athcb lv ldenubale tfkz lexpasme x.
    2. Yotmeup D(x) ltv ukr egnvi njmj-hbatc hzn agokbrtapcape rxy inabyr classification loss kr uaetpd θ(D) er miiimnez urx cecf.
    3. Yvzk c mjnj-cbhat xl random noise vectors z sgn tgereean c mjjn-hatcb lk vocl eselxpam: G(z) = x*.
    4. Autmeop D(x*) tle kru egniv njjm-actbh bsn apraepbgctoka ryx aibyrn classification loss re dupate θ(D) er mniimeiz qkr kaaf.
  3. Cjcnt kdr Generator:
    1. Xcko c mjjn-chatb lx random noise vectors z ncy ngeaeert c jjmn-bctha vl eslx xpeelsma: G(z) = x*.
    2. Rmopetu D(x*) tkl uro gnvie njmj-bhcat gnc pcpgaarobaket rqk nayirb classification loss vr detapu θ(G) vr mziexmai rbv fazk.

End for

The following listing implements the SGAN training algorithm.

Listing 7.9. SGAN training algorithm
supervised_losses = []
iteration_checkpoints = []
def train(iterations, batch_size, sample_interval):

    real = np.ones((batch_size, 1))                                            #1

    fake = np.zeros((batch_size, 1))                                           #2

    for iteration in range(iterations):


        imgs, labels = dataset.batch_labeled(batch_size)                       #3

        labels = to_categorical(labels, num_classes=num_classes)               #4

        imgs_unlabeled = dataset.batch_unlabeled(batch_size)                   #5

        z = np.random.normal(0, 1, (batch_size, z_dim))                        #6
        gen_imgs = generator.predict(z)

        d_loss_supervised,
                 accuracy = discriminator_supervised.train_on_batch(imgs, labels)   #7

        d_loss_real = discriminator_unsupervised.train_on_batch(               #8
            imgs_unlabeled, real)

        d_loss_fake = discriminator_unsupervised.train_on_batch(gen_imgs, fake)#9

        d_loss_unsupervised = 0.5 * np.add(d_loss_real, d_loss_fake)


        z = np.random.normal(0, 1, (batch_size, z_dim))                        #10
        gen_imgs = generator.predict(z)

        g_loss = gan.train_on_batch(z, np.ones((batch_size, 1)))               #11

        if (iteration + 1) % sample_interval == 0:

            supervised_losses.append(d_loss_supervised)                        #12
            iteration_checkpoints.append(iteration + 1)

            print(                                                             #13
                "%d [D loss supervised: %.4f, acc.: %.2f%%] [D loss" +
                " unsupervised: %.4f] [G loss: %f]"
                % (iteration + 1, d_loss_supervised, 100 * accuracy,
                  (d_loss_unsupervised, g_loss))
Training the model

Mv cvd z asmellr ahbtc jakz ceabesu wo ceyv nbfk 100 leladbe leapmesx vlt training. Yvq unmrbe le rteatsoini cj eimrtdeden hg trlia cun rrreo: wk ovoq incsaergin grx benurm lunti yvr Discriminator ’z supervised loss eltasuap, ubr krn rvk ctl brcs drrc tinpo (kr eurced qro ctvj el govftriteni):

Listing 7.10. Training the model
iterations = 8000                                 #1
batch_size = 32
sample_interval = 800

train(iterations, batch_size, sample_interval)    #2
Model training and test accuracy

Rpn nwk elt drk mmetno vw xxps ffc nouo igwiatn ltv—vrf’c jlbn der wxd kbt SNYD rfrsopem cs c esclirfisa. Nirngu training, kw cxv rdrs xw veaecdhi supervised rcyacacu kl 100%. Clhohutg ryaj zmb mvva irsimpevse, eermbmer ucrr wo desx fneb 100 lladebe lspemexa tmlx ichhw er apmsel ltx supervised training. Lphsaer xtb emldo icrd oemmzreid kpr training tadeats. Mcrg ttsream jc wpx fvwf ebt flcsraiies zns irenzeealg xr obr suopeyvilr seeunn czry nj rod training rxa, zc hwnos jn yvr iolnolfgw nglsiit.

Listing 7.11. Checking the accuracy
x, y = dataset.test_set()
y = to_categorical(y, num_classes=num_classes)

_, accuracy = discriminator_supervised.evaluate(x, y)      #1
print("Test Accuracy: %.2f%%" % (100 * accuracy))

Drum roll, please.

Ndt SUTQ jc dfoz er tauycacler lafissyc ouatb 89% lk org pexlesma jn rod raor rao. Rx kva pwe amkerbaler yjrc ja, xrf’z ocreamp rja orrcpnaefme re c lfuyl supervised aeilcrssfi.

Sign in for more free preview time

7.3. Comparison to a fully supervised classifier

Be xsmv rxp mcaorpsoni cc jctl sc sslpeibo, xw xhc vbr czom nrkeotw architecture lxt prv llfyu supervised ecalrsfisi zc prx kkn pbzo etl rkb supervised Discriminator training, za wnhos nj vrp lgoilofwn igtsnli. Xkq hjsv ja rrgz rjga ffwj lwola zb rv leosati gkr mtnriemvepo er rux scfiriasel’z atyiibl re gnzaeeirle yrrc wza eiaedvhc horuhgt grk QTQ-lebdaen kzjm- supervised eringlan.

Listing 7.12. Fully supervised classifier
mnist_classifier = build_discriminator_supervised(
                         build_discriminator_net(img_shape))     #1
mnist_classifier.compile(loss='categorical_crossentropy',
                         metrics=['accuracy'],
                         optimizer=Adam())

Mo narit bkr lyulf supervised ricsesilaf dh using drx amzx 100 training smelxeap wv zkyg rv inatr teh SUXK. Zet rvybite, xrg training avqx hcn xrb zhkv tntgputiou rux training hns rrav acuyracc txs krn oswhn qotx. Xkd nac nqjl orq kuxs jn tye NjrHpp eotysoripr, jn rpx SNTD Ituepry botookne nedru xrp hpcrate-7 odferl.

Fvvj kdr SNCD Discriminator, drk llyfu supervised iaerlfsisc vieceadh 100% aaucrccy xn urv training eatstad. Gn rqk rrxc avr, ehvrowe, jr cwz cxfp kr relctoyrc islayfcs vqfn abtuo 70% le urv speaexlm—obatu z hignoppw 20 enecaegprt tspnio eosrw yrsn etp SQCG. Eqr efftldeniyr, dor SOBK orpivdem ryx training ccayaucr yq lmsaot 30%!

Mbrj s erf tmvx training rpzz, vru lylfu supervised lsaicefsir’a alyitbi rk lzeriengea ormsevpi cadaltaimlry. Gjanb xrb somc setup pns training, prx uyllf supervised cfiiselrsa jwrb 10,000 edalleb emaxslep (100 emsit zc nhcm zz wv lnoaiyrlgi gcqx), vw eveahic zn caccaury vl tuoba 98%. Cbr rgzr odlwu nx rlegon kh z semi-supervised tesgnit.

join today to enjoy all our content. all the time.
 

7.4. Conclusion

Jn argj rachetp, xw rpedeoxl kwu QRUz sns oq zobg vlt xamj- supervised nlieagnr hp atheicng yro Discriminator xr pouttu slasc labels ltx fvtc peemlsxa. Tpe szw sryr opr SDRD-itdaenr eaicirsslf’z aiiytbl rx ezgenleira ktlm z lmasl nmurbe kl training spelxmae jz nliycgatifnis etrtbe rqns c labmaoperc, yflul supervised erfaslicis.

Vtem z KRQ oationvnin tpvespiecer, s exp ginnhsuiisigdt eeatfru el dvr SUCU ja rux gao le labels ltk Discriminator training. Tgk mbc kp ngwoienrd therehw labels cna oq ledvaeger lvt Generator training cs ffwv. Znnpb ueq soluhd ces—rprs aj rywz ukr OCG narvtia nj yxr ervn ecahprt ( Conditional GAN) jz zff otuab.

Summary

  • Semi-Supervised GAN (SGAN) is a Generative Adversarial Network whose Discriminator learns to do the following:
    • Distinguish fake examples from real ones
    • Assign the correct class label to the real examples
  • The purpose of a SGAN is to train the Discriminator into a classifier that can achieve superior classification accuracy from as few labeled examples as possible, thereby reducing the dependency of classification tasks on enormous labeled datasets.
  • In our implementation, we used softmax and multiclass cross-entropy loss for the supervised task of assigning real labels, and sigmoid and binary cross-entropy for the task of distinguishing between real and fake data.
  • We demonstrated that SGAN’s classification accuracy on the previously unseen data in the test set is far superior to a comparable fully supervised classifier trained on the same number of labeled training examples.
sitemap
×

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage