Invertible Conditional GANs for image editing
Abstract
Generative Adversarial Networks (GANs) have recently demonstrated to successfully approximate complex data distributions. A relevant extension of this model is conditional GANs (cGANs), where the introduction of external information allows to determine specific representations of the generated images. In this work, we evaluate encoders to inverse the mapping of a cGAN, i.e., mapping a real image into a latent space and a conditional representation. This allows, for example, to reconstruct and modify real images of faces conditioning on arbitrary attributes. Additionally, we evaluate the design of cGANs. The combination of an encoder with a cGAN, which we call Invertible cGAN (IcGAN), enables to regenerate real images with deterministic complex modifications.
Invertible Conditional GANs for image editing
Guim Perarnau, Joost van de Weijer, Bogdan Raducanu Computer Vision Center Barcelona, Spain guimperarnau@gmail.com {joost,bogdan}@cvc.uab.es Jose M. Álvarez Data61 @ CSIRO Canberra, Australia jose.alvarez@nicta.com.au
noticebox[b]Workshop on Adversarial Training, NIPS 2016, Barcelona, Spain.\end@float
1 Introduction
Image editing can be performed at different levels of complexity and abstraction. Common operations consist in simply applying a filter to an image to, for example, augment the contrast or convert to grayscale. These, however, are lowcomplex operations that do not necessarily require to comprehend the scene or object that the image is representing. On the other hand, if one would want to modify the attributes of a face (e.g. add a smile, change the hair color or even the gender), this is a more complex and challenging modification to perform. In this case, in order to obtain realistic results, a skilled human with an image edition software would often be required.
A solution to automatically perform these nontrivial operations relies on generative models. Natural image generation has been a strong research topic for many years, but it has not been until 2015 that promising results have been achieved with deep learning techniques combined with generative modeling (Gregor et al., 2015; Radford et al., 2016). Generative Adversarial Networks (GANs) Goodfellow et al. (2014) is one of the stateoftheart approaches for image generation. GANs are especially interesting as they are directly optimized towards generating the most plausible and realistic data, as opposed to other models (e.g. Variational Autoencoders Kingma and Welling (2014)), which focus on an image reconstruction loss. Additionally, GANs are able to explicitly control generated images features with a conditional extension, conditional GANs (cGANs). However, the GAN framework lacks an inference mechanism, i.e., finding the latent representation of an input image, which is a necessary step for being able to reconstruct and modify real images.
In order to overcome this limitation, in this paper we introduce Invertible Conditional GANs (IcGANs) for complex image editing as the union of an encoder used jointly with a cGAN. This model allows to map real images into a highfeature space (encoder) and perform meaningful modifications on them (cGAN). As a result, we can explicitly control the attributes of a real image (Figure 1), which could be potentially useful in several applications, be it creative processes, data augmentation or face profiling.
The summary of contributions of our work is the following:

Introducing an encoder in the conditional GAN framework to compress a real image into a latent representation and conditional vector . We consider several designs and training procedures to leverage the performance obtained from available conditional information.

Evaluating and refining cGANs through conditional position and conditional sampling to enhance the quality of generated images.
2 Related work
There are different approaches for generative models. Among them, there are two promising ones that are recently pushing the stateoftheart with highly plausible generated images.
The first one is Variational Autoencoders (VAE) (Gregor et al., 2015; Kingma and Welling, 2014; Rezende et al., 2014; Kingma et al., 2014), which impose a prior representation space (e.g. normal distribution) in order to regularize and constrain the model to sample from it. However, VAEs main limitation is the pixelwise reconstruction error used as a loss function, which causes the output images to look blurry. The second approach is Generative Adversarial Nets (GANs). Originally proposed by Goodfellow et al. (Goodfellow et al., 2014), GANs have been improved with a deeper architecture (DCGAN) by Radford et al. (Radford et al., 2016). The latest advances introduced several techniques that improve the overall performance for training GANs (Salimans et al., 2016) and an unsupervised approach to disentangle feature representations (Chen et al., 2016). Additionally, the most advanced and recent work on cGANs trains a model to generate realistic images from text descriptions and landmarks Reed et al. (2016a).
Our work is considered in the content of the GAN framework. The baseline will be the work of Radford’s et al. (DCGANs) (Radford et al., 2016), which we will add a conditional extension. The difference of our approach to prior work is that we also propose an encoder (Invertible cGAN) with which we can, given an input image , to obtain its representation as a latent variable and a conditional vector . Then, we can modify and to regenerate the original image with complex variations. Dumoulin et al. (Dumoulin et al., 2016) and Donahue et al. (Donahue et al., 2016) also proposed an encoder in GANs, but in a nonconditional and jointly trained setting. Additionally, Makhzani et al. (Makhzani et al., 2016) and Larsen et al. (Larsen et al., 2015) proposed a similar idea to this paper by combining a VAE and a GAN with promising results.
Reed et al. (Reed et al., 2016b) implemented an encoder in a similar fashion to our approach. This paper builds alongside their work in a complementary manner. In our case, we analyze more deeply the encoder by including conditional information encoding and testing different architectures and training approaches. Also, we evaluate unexplored design decisions for building a cGAN.
3 Background: Generative Adversarial Networks
A GAN is composed of two neural networks, a generator and a discriminator . Both networks are iteratively trained competing against each other in a minimax game. The generator aims to approximate the underlying unknown data distribution to fool the discriminator, whilst the discriminator is focused on being able to tell which samples are real or generated. On convergence, we want , where is the generator distribution.
More formally, considering the function , where and are the parameters of the generator and discriminator respectively, we can formulate GAN training as optimizing
(1) 
where is a vector noise sampled from a known simple distribution (e.g. normal).
GAN framework can be extended with conditional GANs (cGANs) (Mirza and Osindero, 2014). They are quite similar to vanilla (nonconditional) GANs, the only difference is that, in this case, we have extra information (e.g. class labels, attribute information) for a given real sample . Conditional information strictly depends on real samples, but we can model a density model in order to sample generated labels for generated data . Then, Equation 1 can be reformulated for the cGAN extension as
(2) 
Once a cGAN is trained, it allows us to generate samples using two level of variations: constrained and unconstrained. Constrained variations are modeled with as it directly correlates with features of the data that are explicitly correlated with and the data itself. Then, all the other variations of the data not modeled by (unconstrained variations) are encoded in .
4 Invertible Conditional GANs
We introduce Invertible Conditional GANs (IcGANs), which are composed of a cGAN and an encoder. Even though encoders have recently been introduced into the GAN framework Dumoulin et al. (2016); Donahue et al. (2016); Reed et al. (2016b), we are the first ones to include and leverage the conditional information into the design of the encoding process. In section 4.1 we explain how and why an encoder is included in the GAN framework for a conditional setting. In section 4.2, we introduce our approach to refine cGANs on two aspects: conditional position and conditional sampling. The model architecture is described in section 4.3.
4.1 Encoder
A generator from a GAN framework does not have the capability to map a real image to its latent representation . To overcome this problem, we can train an encoder/inference network that approximately inverses this mapping . This inversion would allow us to have a latent representation from a real image and, then, we would be able to explore the latent space by interpolating or adding variations on it, which would result in variations on the generated image . If combined with a cGAN, once the latent representation has been obtained, explicitly controlled variations can be added to an input image via conditional information (e.g. generate a certain digit in MNIST or specify face attributes on a face dataset). We call this combination Invertible cGAN, as now the mapping can be inverted: and , where is an input image and its reconstruction. See Figure 2 for an example on how a trained IcGAN is used.
Our approach consists of training an encoder once the cGAN has been trained, as similarly considered by Reed et al Reed et al. (2016b). In our case, however, the encoder is composed of two subencoders: , which encodes an image to , and , which encodes an image to . To train we use the generator to create a dataset of generated images and their latent vectors , and then minimize a squared reconstruction loss (Eq. 3). For , we initially used generated images and their conditional information for training. However, we found that generated images tend to be noisier than real ones and, in this specific case, we could improve by directly training with real images and labels from the dataset (Eq. 4).
(3)  
(4) 
Although and might seem completely independent, we can adopt different strategies to make them interact and leverage the conditional information (for an evaluation of them, see section 5.3):

SNG: One single encoder with shared layers and two outputs. That is, and are embedded in a single encoder.

IND: Two independent encoders. and are trained separately.

INDCOND: Two encoders, where is conditioned on the output of encoder .
Recently, Dumoulin et al. (Dumoulin et al., 2016) and Donahue et al. (Donahue et al., 2016) proposed different approaches on how to train an encoder in the GAN framework. One of the most interesting approaches consists in jointly training the encoder with both the discriminator and the generator. Although this approach is promising, our work has been completely independent of these articles and focuses on another direction, since we consider the encoder in a conditional setting. Consequently, we implemented our aforementioned approach which performs nearly equally (Donahue et al., 2016) to their strategy.
4.2 Conditional GAN
We consider two main design decisions concerning cGANs. The first one is to find the optimal conditional position on the generator and discriminator, which, to our knowledge, has not been previously addressed. Secondly, we discuss the best approach to sample conditional information for the generator.
Conditional position In the cGAN, the conditional information vector needs to be introduced in both the generator and the discriminator. In the generator, and (where are always concatenated in the filter dimension at the input level (Reed et al., 2016b; Mirza and Osindero, 2014; Gauthier, 2014). As for the discriminator, different authors insert in different parts of the model (Reed et al., 2016b; Mirza and Osindero, 2014; Gauthier, 2014). We expect that the earlier is positioned in the model the better since the model is allowed to have more learning interactions with . Experiments regarding the optimal position will be detailed in section 5.2.
Conditional sampling There are two types of conditional information, and . The first one is trivially sampled from and is used for training the discriminator with a real image and its associated label . The second one is sampled from and serves as input to the generator along with a latent vector to generate an image , and it can be sampled using different approaches:

Kernel density estimation: also known as Parzen window estimation, it consists in randomly sampling from a kernel (e.g. Gaussian kernel with a crossvalidated ).

Direct interpolation: interpolate between label vectors from the training set (Reed et al., 2016b). The reasoning behind this approach is that interpolations can belong to the label distribution .

Sampling from the training set , : Use directly the real labels from the training set . As Gauthier (Gauthier, 2014) pointed out, unlike the previous two approaches, this method could overfit the model by using the conditional information to reproduce the images of the training set. However, this is only likely to occur if the conditional information is, to some extent, unique for each image. In the case where the attributes of an image are binary, one attribute vector could describe a varied and large enough subset of images, preventing the model from overfitting given .
Kernel density estimation and direct interpolation are, at the end, two different ways to interpolate on . Nevertheless, interpolation is mostly suitable when the attribute information is composed of real vectors , not binary ones. It is not the case of the binary conditional information of the datasets used in this paper (see section 5.1 for dataset information). Directly interpolating binary vectors would not create plausible conditional information, as an interpolated vector would not belong to nor . Using a kernel density estimation would not make sense either, as all the binary labels would fall in the corners of a hypercube. Therefore, we will directly sample from .
4.3 Model architecture
Conditional GAN The work of this paper is based on the Torch implementation of the DCGAN^{1}^{1}1Torch code for DCGAN model available at https://github.com/soumith/dcgan.torch Radford et al. (2016). We use the recommended configuration for the DCGAN, which trains with the Adam optimizer (Kingma and Ba, 2014) () with a learning rate of and a minibatch size of (samples drawn independently at each update step) during epochs. The output image size used as a baseline is . Also, we train the cGAN with the matchingaware discriminator method from Reed et al. Reed et al. (2016b). In Figure 3 we show an overview architecture of both generator and discriminator for the cGAN. For a more detailed description of the model see Table 1.
Encoder For simplicity, we show the architecture of the IND encoders (Table 2), as they are the ones that give the best performance. Batch Normalization and nonlinear activation functions are removed from the last layer to guarantee that the output distribution is similar to . Additionally, after trying different configurations, we have replaced the last two convolutional layers with two fully connected layers at the end of the encoder, which yields a lower error. The training configuration (Adam optimizer, batch size, etc) is the same as the one used for the cGAN model.
5 Experiments
5.1 Datasets
We use two image datasets of different complexity and variation, MNIST (Lecun et al., 1998) and CelebFaces Attributes (CelebA) (Liu et al., 2015). MNIST is a digit dataset of grayscale images composed of 60,000 training images and 10,000 test images. Each sample is a centered image labeled with the class of the digit (0 to 9). CelebA is a dataset composed of 202,599 face colored images and 40 attribute binary vectors. We use the aligned and cropped version and scale the images down to . We also use the official train and test partitions, 182K for training and 20K for testing. Of the original 40 attributes, we filter those that do not have a clear visual impact on the generated images, which leaves a total of 18 attributes. We will evaluate the quality of generated samples of both datasets. However, a quantitative evaluation will be performed on CelebA only, as it is considerably more complex than MNIST.
5.2 Evaluating the conditional GAN
The goals of this experiment are two. First, we evaluate the general performance of the cGAN with an attribute predictor network (Anet) on CelebA dataset. Second, we test the impact of adding in different layers of the cGAN (section 4.2, conditional position).
We use an Anet^{2}^{2}2The architecture of the Anet is the same as from Table 2. as a way to make a quantitative evaluation in a similar manner as Salimans et al. Inception model Salimans et al. (2016), as the output given by this Anet (i.e., which attributes are detected on a generated sample) is a good indicator of the generator ability to model them. In other words, if the predicted Anet attributes are closer to the original attributes used to generate an image , we expect that the generator has successfully learned the capability to generate new images considering the semantic meaning of the attributes. Therefore, we use the generator to create images conditioned on attribute vectors (i.e. ), and make the Anet predict them. Using the Anet output, we build a confusion matrix for each attribute and compute the mean accuracy and F1Score to test the model and its inserted optimal position of in both generator and discriminator.
In Table 3 we can see how cGANs have successfully learned to generate the visual representations of the conditional attributes with an overall accuracy of %. The best accuracy is achieved by inserting in the first convolutional layer of the discriminator and at the input level for the generator. Thus, we are going to use this configuration for the IcGAN. Both accuracy and F1Score are similar as long as is not inserted in the last convolutional layers, in which case the performance considerably drops, especially in the generator. Then, these results reinforce our initial intuition of being added at an early stage of the model to allow learning interactions with it.
5.3 Evaluating the encoder
In this experiment, we prioritize the visual quality of reconstructed samples as an evaluation criterion. Among the different encoder configurations of section 4.1, IND and INDCOND yield a similar qualitative performance, being IND slightly superior. A comparison of these different configurations is shown in Figure 4a and in Figure 4b we focus on IND reconstructed samples. On another level, the fact that the generator is able, via an encoder, to reconstruct unseen images from the test set shows that the cGAN is generalizing and suggests that it does not suffer from overfitting, i.e., it is not just memorizing and reproducing training samples.
Additionally, we compare the different encoder configurations in a quantitative manner by using the minimal squared reconstruction loss as a criterion. Each encoder is trained minimizing with respect to latent representations () or conditional information (). Then, we quantitatively evaluate different model architectures using as a metric on a test set of 150K CelebA generated images. We find that the encoder that yields the lowest is also IND (0.429), followed closely by INDCND (0.432), and being SNG the worst case (0.500).
Furthermore, we can see an interesting property of minimizing a loss based on the latent space instead of a pixelwise image reconstruction: reconstructed images tend to accurately keep highlevel features of an input image (e.g. how a face generally looks) in detriment to more local details such as the exact position of the hair, eyes or face. Consequently, a latent space based encoder is invariant to these local details, making it an interesting approach for encoding purposes. For example, notice how the reconstructions in the last row of CelebA samples in Figure 4b fill the occluded part of the face by a hand. Another advantage with respect to elementwise encoders such as VAE is that GAN based reconstructions do not look blurry.
5.4 Evaluating the IcGAN
In order to test that the model is able to correctly encode and regenerate a real image by preserving its main attributes, we take real samples from MNIST and CelebA test sets and reconstruct them with modifications on the conditional information . The result of this procedure is shown in Figure 5, where we show a subset of 9 of the 18 for CelebA attributes for image clarity. We can see that, in MNIST, we are able to get the handwritten style of real unseen digits and replicate these style on all the other digits. On the other hand, in CelebA we can see how reconstructed faces generally match the specified attribute. Additionally, we noticed that faces with uncommon conditions (e.g., looking away from the camera, face not centered) were the most likely to be noisy. Furthermore, attributes such as mustache often fail to be generated especially on women samples, which might indicate that the generator is limited to some unusual attribute combinations.
Manipulating the latent space The latent feature representation and conditional information learned by the generator can be further explored beyond encoding real images or randomly sampling . In order to do so, we linearly interpolate both and with pairs of reconstructed images from the CelebA test set (Figure 6a). All the interpolated faces are plausible and the transition between faces is smooth, demonstrating that the IcGAN learned manifold is also consistent between interpolations. Then, this is also a good indicator that the model is generalizing the face representation properly, as it is not directly memorizing training samples.
In addition, we perform in Figure 6b an attribute transfer between pairs of faces. We infer the latent representation and attribute information of two real faces from the test set, swap between those faces and regenerate them. As we previously noticed, the results suggest that encodes pose, illumination and background information, while tends to represent unique features of the face.
6 Conclusions
We introduce an encoder in a conditional setting within the GAN framework, a model which we call Invertible Conditional GANs (IcGANs). It solves the problem of GANs lacking the ability to infer real samples to a latent representation , while also allowing to explicitly control complex attributes of generated samples with conditional information . We also refine the performance of cGANS by testing the optimal position in which the conditional information is inserted in the model. We have found that for the generator, should be added at the input level, whereas the discriminator works best when is at the first layer. Additionally, we evaluate several ways to training an encoder. Training two independent encoders – one for encoding and another for encoding – has proven to be the best option in our experiments. The results obtained with a complex face dataset, CelebA, are satisfactory and promising.
Acknowledgments This work is funded by the Projects TIN201341751P of the Spanish Ministry of Science and the CHIST ERA project PCIN2015226.
References
 Gregor et al. (2015) K. Gregor, I. Danihelka, A. Graves, D. Jimenez Rezende, and D. Wierstra, “DRAW: A Recurrent Neural Network For Image Generation,” International Conference on Machine Learning (ICML), pp. 1462–1471, 2015.
 Radford et al. (2016) A. Radford, L. Metz, and S. Chintala, “Unsupervised Representation Learning with Deep Convolutional Generative Adversarial Networks,” International Conference on Learning Representations (ICLR), 2016.
 Goodfellow et al. (2014) I. Goodfellow, J. PougetAbadie, M. Mirza, B. Xu, D. WardeFarley, S. Ozair, A. Courville, and Y. Bengio, “Generative adversarial nets,” in Advances in Neural Information Processing Systems 27, Z. Ghahramani, M. Welling, C. Cortes, N. D. Lawrence, and K. Q. Weinberger, Eds. Curran Associates, Inc., 2014, pp. 2672–2680. [Online]. Available: http://papers.nips.cc/paper/5423generativeadversarialnets.pdf
 Kingma and Welling (2014) D. P. Kingma and M. Welling, “Autoencoding variational bayes,” International Conference on Learning Representations (ICLR), 2014. [Online]. Available: http://arxiv.org/abs/1312.6114
 Lecun et al. (1998) Y. Lecun, L. Bottou, Y. Bengio, and P. Haffner, “Gradientbased learning applied to document recognition,” in Proceedings of the IEEE, 1998, pp. 2278–2324.
 Liu et al. (2015) Z. Liu, P. Luo, X. Wang, and X. Tang, “Deep learning face attributes in the wild,” in Proceedings of International Conference on Computer Vision (ICCV), December 2015.
 Rezende et al. (2014) D. J. Rezende, S. Mohamed, and D. Wierstra, “Stochastic backpropagation and approximate inference in deep generative models,” Proceedings of the 31st International Conference on Machine Learning (ICML), vol. 32, pp. 1278–1286, 2014.
 Kingma et al. (2014) D. P. Kingma, D. J. Rezende, S. Mohamed, and M. Welling, “Semisupervised learning with deep generative models,” Proceedings of Neural Information Processing Systems (NIPS), 2014. [Online]. Available: http://arxiv.org/abs/1406.5298
 Salimans et al. (2016) T. Salimans, I. J. Goodfellow, W. Zaremba, V. Cheung, A. Radford, and X. Chen, “Improved techniques for training GANs,” Neural Information Processing Systems (NIPS), 2016. [Online]. Available: http://arxiv.org/abs/1606.03498
 Chen et al. (2016) X. Chen, Y. Duan, R. Houthooft, J. Schulman, I. Sutskever, and P. Abbeel, “InfoGAN: Interpretable Representation Learning by Information Maximizing Generative Adversarial Nets,” Neural Information Processing Systems (NIPS), 2016. [Online]. Available: http://arxiv.org/abs/1606.03657
 Reed et al. (2016a) S. Reed, Z. Akata, S. Mohan, S. Tenka, B. Schiele, and H. Lee, “Learning what and where to draw,” in Advances in Neural Information Processing Systems (NIPS), 2016.
 Dumoulin et al. (2016) V. Dumoulin, I. Belghazi, B. Poole, A. Lamb, M. Arjovsky, O. Mastropietro, and A. Courville, “Adversarially Learned Inference,” Arxiv, 2016. [Online]. Available: http://arxiv.org/abs/1606.00704
 Donahue et al. (2016) J. Donahue, P. Krähenbühl, and T. Darrell, “Adversarial feature learning,” CoRR, vol. abs/1605.09782, 2016. [Online]. Available: http://arxiv.org/abs/1605.09782
 Makhzani et al. (2016) A. Makhzani, J. Shlens, N. Jaitly, and I. Goodfellow, “Adversarial autoencoders,” in International Conference on Learning Representations (ICLR), 2016. [Online]. Available: http://arxiv.org/abs/1511.05644
 Larsen et al. (2015) A. B. L. Larsen, S. K. Sønderby, and O. Winther, “Autoencoding beyond pixels using a learned similarity metric,” Proceedings of the 33rd International Conference on Machine Learning (ICML), pp. 1558–1566, 2015. [Online]. Available: http://arxiv.org/abs/1512.09300
 Reed et al. (2016b) S. E. Reed, Z. Akata, X. Yan, L. Logeswaran, B. Schiele, and H. Lee, “Generative adversarial text to image synthesis,” International Conference on Machine Learning (ICML), 2016. [Online]. Available: http://arxiv.org/abs/1605.05396
 Mirza and Osindero (2014) M. Mirza and S. Osindero, “Conditional Generative Adversarial Nets,” CoRR, vol. abs/1411.1784, 2014. [Online]. Available: http://arxiv.org/abs/1411.1784
 Gauthier (2014) J. Gauthier, “Conditional generative adversarial nets for convolutional face generation,” Class project for Stanford CS231N: Convolutional Neural Networks for Visual Recognition, Winter semester 2014, 2014. [Online]. Available: http://cs231n.stanford.edu/reports/jgauthie_final_report.pdf
 Kingma and Ba (2014) D. Kingma and J. Ba, “Adam: A Method for Stochastic Optimization,” International Conference for Learning Representations (ICLR), 2014. [Online]. Available: http://www.arxiv.org/pdf/1412.6980.pdf