Progressive growing of GANs
Progressive growing of GANs
Inspired from NVIDIA’s 2017 paper “PROGRESSIVE GROWING OF GANS FOR IMPROVED QUALITY, STABILITY, AND VARIATION”: https://arxiv.org/pdf/1710.10196.pdf
The github page is here : https://github.com/tkarras/progressive_growing_of_gans
CelebA dataset
The celebA dataset can be downloaded on the project's home page. Here we’ll assume this was done and the pictures are all in a directory.
In[]:=
celebADirectory="/home/grondilu/Images/img_align_celeba";
We don’t import the images all in one go as that would take too much time. Instead, we create a list of symbolic file representations.
In[]:=
celebAFiles=File/@FileNames["*.jpg",celebADirectory];
Here is a random sample :
In[]:=
ImageCollage[Import/@RandomChoice[celebAFiles,9]]
Out[]=
These images are not square, but that’s ok, when we encode them we’ll squeeze them. In fact, the default NetEncoder does it for images anyway (notice the dimensions of the output):
In[]:=
Print@NetEncoder["Image"]
NetEncoder
Also the default size for an image is 128x128, which is probably large enough for this experiment as I don’t have any NVIDIA GPU.
Network structure
In the paper they use a latent space of 512 dimensions.
In[]:=
latentSpaceSize=512;
The discriminator will have to scale the input image down. This can be done with the input NetEncoder :
In[]:=
NetEncoder[{"Image",{32,32}}][Import@RandomChoice@celebAFiles]//NetDecoder["Image"]
Out[]=
In[]:=
gan=NetGANOperator[{NetChain[{ReshapeLayer[{latentSpaceSize,1,1}],{ConvolutionLayer[latentSpaceSize,4,PaddingSize3],ParametricRampLayer[],ConvolutionLayer[latentSpaceSize,3,PaddingSize1],ParametricRampLayer[]},LinearLayer[{3,4,4}]},"Input"->latentSpaceSize,"Output"->NetDecoder["Image"]],NetChain[{{ConvolutionLayer[latentSpaceSize,3,PaddingSize1],ParametricRampLayer[],ConvolutionLayer[latentSpaceSize,3,PaddingSize1],ParametricRampLayer[]},LinearLayer[{}]},"Input"->NetEncoder[{"Image",{4,4}}]]}]
Out[]=
NetGANOperator
In[]:=
Length@celebAFiles
Out[]=
202599
In[]:=
images=Import/@RandomChoice[celebAFiles,10000];
In[]:=
dataset=<|"Sample"->images,"Latent"->Array[NumericArray[Array[RandomReal,latentSpaceSize],"Real32"]&,Length[images]]|>;
In[]:=
NetTrain[gan,dataset,TrainingUpdateSchedule{"Discriminator","Generator"}]
Out[]=
NetGANOperator
In[]:=
trained=%
Out[]=
NetGANOperator
In[]:=
NetExtract[trained,"Generator"]@Array[RandomReal,latentSpaceSize]
Out[]=
In[]:=
NetDecoder["Image"]/@NetEncoder[{"Image",{4,4}}]/@RandomChoice[images,10]
Out[]=
,
,
,
,
,
,
,
,
,
