Invert a pre-trained GAN model (includes code for training a GAN on celebA)
Generative adversarial networks (GANs) learn a deep generative model that is able to synthesise novel, high-dimensional data samples. New data samples are synthesised by passing latent samples, drawn from a chosen prior distribution, through the generative model. Once trained, the latent space exhibits interesting properties, that may be useful for down stream tasks such as classification or retrieval. Unfortunately, GANs do not offer an "inverse model", a mapping from data space back to latent space, making it difficult to infer a latent representation for a given data sample. In this paper, we introduce a technique, inversion, to project data samples, specifically images, to the latent space using a pre-trained GAN. Using our proposed inversion technique, we are able to identify which attributes of a dataset a trained GAN is able to model and quantify GAN performance, based on a reconstruction loss. We demonstrate how our proposed inversion technique may be used to quantitatively compare performance of various GAN models trained on three image datasets. We provide code for all of our experiments, https://github.com/ToniCreswell/InvertingGAN.READ FULL TEXT VIEW PDF
Generative adversarial networks (GANs) learn to synthesise new samples f...
In the years since Goodfellow et al. introduced Generative Adversarial
In this paper, we introduce Random Path Generative Adversarial Network
Inferring the latent variable generating a given test sample is a challe...
Conditional generation refers to the process of sampling from an unknown...
We present an application of deep generative models in the context of
A food recipe is an ordered set of instructions for preparing a particul...
Invert a pre-trained GAN model (includes code for training a GAN on celebA)
Generative adversarial networks (GANs) [13, 8] are a class of generative model which are able to synthesise novel, realistic looking images of faces, digits and street numbers . GANs involve two networks: a generator, , and a discriminator, . The generator,
, is trained to generate synthetic images, taking a random vector,, drawn from a prior distribution,
, as input. The prior is often chosen to be a normal or uniform distribution.
Radford et al.  demonstrated that generative adversarial networks (GANs) learn a “rich linear structure”, meaning that algebraic operations in -space often lead to semantically meaningful synthetic samples in image space. Since images represented in -space are often meaningful, direct access to a for a given image, may be useful for discriminative tasks such as retrieval or classification. Recently, it has also become desirable to be able to access -space in order to manipulate original images . Thus, there are many reasons we may wish to invert the generator.
Typically, inversion is achieved by finding a vector which when passed through the generator produces an image that is very similar to the target image. If no suitable exists, this may be an indicator that the generator is unable to model either the whole image or certain attributes of the image. We give a concrete example in Section VI-B. Therefore, inverting the generator, additionally, provides interesting insights to highlight what a trained GAN has learned.
Mapping an image, from image space, , to -space is non-trivial, as it requires inversion of the generator, which is often a many layered, non-linear model [13, 8, 3]. Dumoulin et al.  (ALI) and Donahue et al. (BiGAN)  proposed learning a third, decoder network alongside the generator and discriminator to map image samples back to
-space. Collectively, they demonstrated results on MNIST, ImageNet, CIFAR-10 and SVHN and CelebA. However, reconstructions of inversions are often poor. Specifically, reconstructions of inverted MNIST digits using methods of Donahue et al., often fail to preserve style and character class. Recently, Li et al.  proposed method to improve reconstructions, however drawbacks to these approaches [10, 6, 7] include the need to train a third network which increases the number of parameters that have to be learned, increasing the chances of over-fitting. The need to train an extra network, along side the GAN, also means that inversion cannot be performed on pre-trained networks.
A more serious concern, when employing a decoder model to perform inversion, is that its value as a diagnostic tool for evaluating GANs is hindered. GANs suffer from several pathologies including over-fitting, that we may be able to detect using inversion. If an additional encoder model is trained to perform inversion [10, 6, 7, 11], the encoder itself may over-fit, thus not portraying the true nature of a trained GAN. Since our approach does not involve training an additional encoder model, we may use our approach for “trouble shooting” and evaluating different pre-trained GAN models.
In this paper, we make the following contributions:
We propose a novel approach to invert the generator of any pre-trained GAN, provided that the computational graph for the generator network is available (Section II).
We demonstrate that, we are able to infer a -space representation for a target image, such that when passed through the GAN, it produces a sample visually similar to the target image (Section VI).
Additionally, we show that batches of samples can be inferred from batches of image samples, which improves the efficiency of the inversion process by allowing multiple images to be inverted in parallel (Section II-A).
We begin, by describing our proposed inversion technique.
For a target image, we want to infer the -space representation, , which when passed through the trained generator produces an image very similar to . We refer to the process of inferring from as inversion. This can be formulated as a minimisation problem:
Provided that the computational graph for is known, can be calculated via gradient descent methods, taking the gradient of w.r.t. . This is detailed in Algorithm 1.
Provided that the generator is deterministic, each value maps to a single image, . A single value cannot map to multiple images. However, it is possible that a single value may map to several representations, particularly if the generator has collapsed . This suggests that there may be multiple possible values to describe a single image. This is very different to a discriminative model, where multiple images, may often be described by the same representation vector , particularly when a discriminative model learns representations tolerant to variations.
The approach described in Algorithm 1 is similar in spirit to that of Mahendran et al. , however instead of inverting a representation to obtain the image that was responsible for it, we invert an image to discover the latent representation that generated it.
Algorithm 1 shows how we can invert a single data sample, however it may not be efficient to invert single images at a time, rather, a more practical approach is to invert many images at once. We will now show that we are able to invert batches of examples.
Let z, z be a batch of samples of . This will map to a batch of image samples x, x. For each pair , , a loss , may be calculated. The update for would then be
If reconstruction loss is calculated over a batch, then the batch reconstruction loss would be , and the update would be:
Each reconstruction loss depends only on , so depends only on , which means , for all . This shows that is updated only by reconstruction loss , and the other losses do not contribute to the update of , meaning that it is valid to perform updates on batches.
A GAN is trained to generate samples from a where the distribution over is a chosen prior distribution, . is often a multivariate Gaussian or uniform distribution. If is a multivariate uniform distribution, , then after updating , it can be clipped to be between . This ensures that
lies in the probable regions of. If
is a multivariate Gaussian Distribution,, regularisation terms may be added to the cost function, penalising samples that have statistics that are not consistent with .
If is a vector of length and each of the elements in
are drawn independently and from identical distributions, we may be able to add a regularisation term to the loss function. For example, ifis a multivariate Gaussian distribution, then elements in a single are independent and identically drawn from a Gaussian distribution. Therefore we may calculate the likelihood of an encoding, z, under a multivariate Gaussian distribution by evaluating:
where is the element in a latent vector z and
is the probability density function of a (univariate) Gaussian, which may be calculated analytically. Our new loss function may be given by:
by minimising this loss function (Equation 5), we encourage to come from the same distribution as the prior.
In this paper, we build on our own work222Creswell, Antonia, and Anil Anthony Bharath. ”Inverting The Generator Of A Generative Adversarial Network.” arXiv preprint arXiv:1611.05644 (2016). This paper was previously accepted at the NIPS Workshop on Adversarial Training, which was made available as a non archival pre-print only. We have augmented the paper, by performing additional experiments on a shoe dataset  and CelebA, as well as repeating experiments on the Omniglot dataset using the DCGAN model proposed by Radford et al.  rather than our own network . In addition to proposing a novel approach for mapping data samples to their corresponding latent representation, we show how our approach may be used to quantitatively and qualitatively compare models.
Our approach to inferring from is similar to the previous work of Zhu et al. , however we make several additional contributions.
Specifically, Zhu et al.  calculate reconstruction loss, by comparing the features of and extracted from layers of AlexNet, a CNN trained on natural scenes. This approach is likely to fail if generated samples are not of natural scenes (e.g. Omniglot handwritten characters). Our approach considers pixel-wise loss, providing an approach that is generic to the dataset. Further, if our intention is to use the inversion to better understand the GAN model, it is essential not to incorporate information from other pre-trained networks in the inversion process.
An alternative class of inversion methods involves training a separate encoding network to learn a mapping from image samples, to latent samples . Li et al , Donahue et al.  and Dumoulin et al.  propose learning the encoder along side the GAN. Unfortunately, training an additional encoder network may encourage over-fitting, resulting in poor image reconstruction. Further, this approach may not be applied to pre-trained models.
On the other hand, Luo et al.333Luo et al. cite our pre-print , train an encoding network after the GAN has been trained, which means that their approach may be applied to pre-trained models. One concern about the approach of Luo et al. , is that it may not be an accurate reflection of what the GAN has learned, since the learned decoder may over-fit to the examples it is trained on. For this reason, the approach of Luo et al.  may not be suitable for inverting image samples that come from a different distribution to the training data. Evidence of over-fitting may be found in Luo et al. -Figure 2, where “original” image samples being inverted are synthesised samples, rather than samples from a test set data; in other words Luo et al.  show results (Figure 2) for inverting synthesised samples, rather than real image samples from a test set.
In contrast to Luo et al. , we demonstrate our inversion approach on data samples drawn from test sets of real data samples. To make inversion more challenging, in the case of the Omniglot dataset, we invert image samples that come from a different distribution to the training data. We invert image samples from the Omniglot handwritten characters dataset that come from a different set of alphabets to the set used to train the (Omniglot) GAN (Figure 4). Our results will show that, using our approach, we are still able to recover a latent encoding that captures most features of the test data samples.
In this section we discuss the training and architecture details of several different GAN models, trained on different datasets, which we will use for our inversion experiments detailed in Section V. We show results on total of trained models (Sections VI and VII).
The Omniglot dataset  consists of characters from different alphabets, where each alphabet has at least different characters. The Omniglot dataset has a background dataset, used for training and a test dataset. The background set consists of characters from writing systems, while the test dataset consists of characters from the other . Note, characters in the training and testing dataset come from different writing systems. We train both a DCGAN  and a WGAN  using a latent representation of dimension, . The WGAN 
is a variant of the GAN that is easier to train and less likely to suffer from mode collapse; mode collapse is where synthesised samples look similar to each other. All GANs are trained with additive noise whose standard deviation decays during training. Figure 1 shows Omniglot samples synthesised using the trained models. Though it is clear from Figure 1(a), that the GAN has collapsed, because the generator is synthesising similar samples for different latent codes, it is less clear to what extent the WGAN (Figure 1(b)) may have collapsed or over-fit. It is also unclear from Figure 1(b) what representative power, the (latent space of the) WGAN has. Results in sections VI and VII will provide more insight into the representations learned by these models.
The shoes dataset  consists of c. examples of shoes in RGB colour, from different categories and over different subcategories. The images are of dimensions . We leave samples out for testing and use the rest for training. We train two GANs using the DCGAN  architecture. We train one DCGAN with full sized images and the second we train on images. The networks were trained according to the setup described by Radford et al. , using a multivariate Gaussian prior. We also train a WGAN  on full sized images. All GANs are trained with additive noise whose standard deviation decays during training . Figure 3 shows samples randomly synthesised using the DCGAN models trained on shoes. The samples look quite realistic, but again, they do not tell us much about the representations learned by the GANs.
The CelebA dataset consists of celebrity faces, in RGB colour. The images are of dimensions pixels. We leave samples out for testing and use the rest for training. We train three models, a DCGAN and WGAN trained with decaying noise  and a DCGAN trained without noise. The networks are trained according to the setup described by Radford et al. . Figure 2(c) shows examples of faces synthesised with and without noise. It is clear from Figure 2(c)(a) that the GAN trained without noise has collapsed, synthesising similar examples for different latent codes. The WGAN produces the sharpest and most varied samples. However, these samples do not provide sufficient information about the representation power of the models.
To obtain latent representations, for a given image we apply our proposed inversion technique to a batch of randomly selected test images, . To invert a batch of image samples, we minimised the cost function described by Equation (5). In most of our experiments we use , unless stated otherwise, and update candidate
using an RMSprop optimiser, with a learning of rate.
A valid inversion process should map a target image sample, to a , such that when is passed through the generative part of the GAN, it produces an image, , that is close to the target image, . However, the quality of the reconstruction, depends heavily on the latent representation that the generative model has learned. In the case where a generative model is only able to represent some attributes of the target image, , the reconstruction, may only partially reconstruct .
Thus, the purpose of our experiments is two fold:
To demonstrate qualitatively, through reconstruction, (), that for most well trained GANs, our inversion process is able to recover a latent code, , that captures most of the important features of a target image (Section VI).
The Omniglot inversions are particularly challenging, as we are trying to find a set of ’s for a set of characters, , from alphabets that were not in the training data. The inversion process will involve finding representations for data samples from alphabets that it has not seen before, using information about alphabets that it has seen. The original and reconstructed samples are shown in Figure 4.
In our previous work,444https://arxiv.org/abs/1611.05644 we showed that given the “correct” architecture, we are able to find latent representations that lead to excellent reconstructions. However, here we focus on evaluating standard models  and we are particularly interested in detecting (and quantifying) where models fail, especially since visual inspection of synthesised samples may not be sufficient to detect model failure.
It is clear from Figure 1 that the GAN has over-fit, however it was less clear whether or not the WGAN has, since samples appeared to be more varied. By attempting to perform inversion, we can see that the WGAN has indeed over-fit, as it is only able to partially reconstruct the target data samples. In the next section (Section VII), we quantitatively compare the extent to which the GAN and WGAN trained on the Omniglot dataset have over-fit.
In Figure 5 we compare shoe reconstructions using a DCGAN trained on low and high resolution images. By comparing reconstructions in Figures 5 (b) and (c) (particularly the blue shoe on the top row) we see that the lower resolution model has failed to capture some structural details, while the higher resolution model has not. This suggests that the model trained on higher resolution images is able to capture more structural details than the model trained on lower resolution images. Using our inversion technique to make comparisons between models is just one example of how inversion may also be used to “trouble shoot” and identify which features of a dataset our models are not capturing.
Additionally, we may observe that while the GAN trained on higher resolution images preserves more structure than the GAN trained on lower resolution images, it still misses certain details. For example the reconstructed red shoes do not have laces (top left Figure 5
(b,c)) laces. This suggests that the representation is not able to distinguish shoes with laces from those without. This may be important when designing representations for image retrieval, where a retrieval system using this representation may be able to consistently retrieve red shoes, but less consistently retrieve red shoes with laces. This is another illustration of how a good inversion technique may be used to better understand what representation is learned by a GAN.
Figure 5(d) shows reconstructions using a WGAN trained on low resolution images. We see that the WGAN is better able to model the blue shoe, and some ability to model the ankle strap, compared to the GAN trained on higher resolution images. It is however, difficult to asses from reconstructions, which model best represents the data. In the next Section (VII), we show how our inversion approach may be used to quantitatively compare these models, and determine which learns a better (latent) representation for the data.
Finally, we found that while regularisation of the latent space may not always improve reconstruction fidelity, it can be helpful for ensuring that latent encodings, , found through inversion, correspond to images, that look more like shoes. Our results in Figure 5 were achieved using .
Figure 6 shows reconstructions using three different GAN models. Training GANs can be very challenging, and so various modifications may be made to their training to make them easier to train. Two examples of modifications are (1) adding corruption to the data samples during training  and (2) a reformulation of the cost function to use the Wasserstein distance. While these techniques are known to make training more stable, and perhaps also prevent other pathologies found in GANs for example, mode collapse, we are interested to compare the (latent) representations learned by these models.
The most faithful reconstructions appear to be those from the WGAN Figure 6 (b). This will be confirmed quantitatively in the next section. By observing reconstruction results across all models in Figure 6, it is apparent that all three models fail to capture a particular mode of the data; all three models fail to represent profile views of faces.
Failing to represent a mode in the data is commonly referred to a “mode dropping”, and is just one of three common problems exhibited by GANs. For completeness, common problems exhibited by trained GANs include the following: (1) mode collapse, this is where similar image samples are synthesised for different inputs, (2) mode dropping (more precisely), this is where the GAN only captures certain regions of high density in the data generating distribution and (3) training sample memorisation, this is where the GAN memorises and reproduces samples seen in the training data. If a model exhibits these symptoms, we say that it has over-fit, however these symptoms are often difficult to detect.
|[1pt/5pt] High Res.||-||0.016||-|
If a GAN is trained well and exhibits none of the above three problems, it should be possible to preform inversion to find suitable representations for most test samples using our technique.
However, if a GAN does exhibit any of the three problems listed above, inversion becomes challenging, since certain regions of high density in the data generating distribution may not be represented by the GAN. Thus, we may compare GAN models, by evaluating reconstruction error using our proposed inversion process. A high reconstruction error, in this case mean squared error (MSE), suggests that a model has possibly over-fit, and is not able to represent data samples well. By comparing MSE between models, we can compare the extent to which one model has over-fit compared to another.
Table I shoes how our inversion approach may be used to quantitatively compare models ( in the case of the shoes dataset) across three datasets, CelebA, shoes and Omniglot. The Table shows mean squared reconstruction error on a large 555CelebA:100 samples, Shoes and Omniglot:500 samples batch of test samples.
From Table I we may observe the following:
The (latent) representation learned by the WGAN generalises to test samples, better than either the GAN or the GAN trained with noise. Results also suggests that training a GAN with noise helps to prevent over-fitting. These conclusions are consistent with both empirical and theoretical results found in previous work [2, 1], suggesting that this approach for quantitatively comparing models is valid.
Using inversion to quantify the quality of a representation allows us to make fine grain comparisons between models. We see that training a model using higher resolution images reduces reconstruction error by almost a factor of two, in the case of the GANnoise, compared to a similar model trained at a lower resolution. We know from earlier observations (Figure VI-B), that this is because the model trained at higher resolution, captures finer grain details, that the model trained on lower resolution images.
Comparing models using our proposed inversion approach, in addition to classifier based measures, helps to detect fine grain differences between models, that may not be detected using classification based measures alone. A “good” discriminative model learns many features that help to make decisions about which class an object belongs to. However, any information in an image that does not aid classification is likely to be ignored (e.g. when classifying cars and trucks, the colour of a car is not important 666Bottom left of Figure 10 , shows that the 1st layer of a discriminatively trained CNN ignores the colours of the input image.). Yet, we may want to compare representations that encode information that a classifier ignores (e.g. colour of the car). For this reason, using only a classification based measure  to compare representations learned by different models may not be enough, or may require very fine grain classifiers to detect differences.
From Figure 4, it was clear that both models trained on the Omniglot dataset had over-fit, but not to the same extent. Here, we are able to quantify the degree to which each model has over-fit. We see that the WGAN has over-fit to a lesser extent compared to the GAN trained with noise, since the WGAN has a smaller MSE. Quantifying over-fitting can be useful when developing new architectures, and training scheme, to objective compare models.
In this section we have demonstrated how our inversion approach may be used to quantitatively compare representations learned by GANs. We intend this approach to provide a useful, quantitative approach for evaluating and developing new GAN models and architectures for representation learning.
Finally, we emphasise that while there are other techniques that provide inversion, ours is the only one that is both (a) immune to over-fitting, in other words we do not train an encoder network that may itself over-fit, and (b) can be applied to any pre-trained GAN model provided that the computational graph is available.
The generator of a GAN learns the mapping . It has been shown that values that are close in -space produce images that are visually similar in image space, . We propose an approach to map data, samples back to their latent representation, (Section II).
For a generative model, in this case a GAN, that is trained well and given target image, , we should be able to find a representation, , that when passed through the generator, produces an image, , that is similar to the target image. However, it is often the case that GANs are difficult to train, and there only exists a latent representation, , that captures some of the features in the target image. When only captures some of the features, this results in, , being a partial reconstruction, with certain features of the image missing. Thus, our inversion technique provides a tool, to provide qualitative information about what features are captured by in the (latent) representation of a GAN. We showed several visual examples of this in Section VI.
Often, we want to compare models quantitatively. In addition to providing a qualitative way to compare models, we show how we may use mean squared reconstruction error between a target image, and , to quantitatively compare models. In our experiments, in Section VII, we use our inversion approach to quantitatively compare models trained on datasets. Our quantitative results support claims from previous work that suggests, that certain modified GANs are less likely to over-fit.
We expect that our proposed inversion approach may be used as a tool to asses and compare various proposed modifications to generative models, and aid the development of new generative approaches to representation learning.
We like to acknowledge the Engineering and Physical Sciences Research Council for funding through a Doctoral Training studentship.
Alice: Towards understanding adversarial learning for joint distribution matching.In Advances in Neural Information Processing Systems, pages 5501–5509, 2017.
Learning inverse mapping by autoencoder based generative adversarial nets.In International Conference on Neural Information Processing, pages 207–216. Springer, 2017.