Image 2 Style Gan
Image 2 Style Gan
Abstract
arXiv:1904.03189v2 [cs.CV] 3 Sep 2019
is not only able to embed human face images, but also suc-
cessfully embeds non-face images from different classes.
We propose an efficient algorithm to embed a given im- Therefore, we continue our investigation by analyzing the
age into the latent space of StyleGAN. This embedding en- quality of the embedding to see if the embedding is semanti-
ables semantic image editing operations that can be applied cally meaningful. To this end, we propose to use three basic
to existing photographs. Taking the StyleGAN trained on operations on vectors in the latent space: linear interpola-
the FFHQ dataset as an example, we show results for image tion, crossover, and adding a vector and a scaled difference
morphing, style transfer, and expression transfer. Studying vector. These operations correspond to three semantic im-
the results of the embedding algorithm provides valuable age processing applications: morphing, style transfer, and
insights into the structure of the StyleGAN latent space. We expression transfer. As a result, we gain more insight into
propose a set of experiments to test what class of images can the structure of the latent space and can solve the mystery
be embedded, how they are embedded, what latent space is why even instances of non-face images such as cars can be
suitable for embedding, and if the embedding is semanti- embedded.
cally meaningful. Our contributions include:
1
Figure 1: Top row: input images. Bottom row: results of embedding the images into the StyleGAN latent space.
to improve the performance of GANs from different as- ii) select a random initial latent code and optimize it using
pects, e.g. the loss function [23, 2], the regularization or gradient descent [39, 4]. Between them, the first approach
normalization [9, 25], and the architecture [9]. However, provides a fast solution of image embedding by performing
due to the limitation of computational power and the short- a forward pass through the encoder neural network. How-
age of high-quality training data, these works are only tested ever, it usually has problems generalizing beyond the train-
with low resolution and poor quality datasets collected for ing dataset. In this paper, we decided to build on the second
classification / recognition tasks. Addressing this issue, approach as the more general and stable solution. As a con-
Karras et al. collected the first high-quality human face currently developed work, the Github repository stylegan-
dataset CelebA-HQ and proposed a progressive strategy to encoder [26] also demonstrated that the optimization-based
train GANs for high resolution image generation tasks [14]. approach leads to embeddings of very high visual quality.
Their ProGAN is the first GAN that can generate realistic
human faces at a high resolution of 1024 × 1024. How-
Perceptual Loss and Style Transfer Traditionally, the
ever, the generation of high-quality images from complex
low-level similarity between two images is measured in the
datasets (e.g. ImageNet) remains a challenge. To this end,
pixel space with L1/L2 loss functions. While in the past
Brock et al. proposed BigGAN and argued that the training
years, inspired by the success of complex image classifica-
of GANs benefit dramatically from large batch sizes [3].
tion [18, 22], Gatys et al. [7, 6] observed that the learned
Their BigGAN can generate realistic samples and smooth
filters of the VGG image classification model [22] are ex-
interpolations spanning different classes. Recently, Karras
cellent general-purpose feature extractors and proposed to
et al. collected a more diverse and higher quality human
use the covariance statistics of the extracted features to mea-
face dataset FFHQ and proposed a new generator archi-
sure the high-level similarity between images perceptually,
tecture inspired by the idea of neural style transfer [10],
which is then formalized as the perceptual loss [12, 5].
which further improves the performance of GANs on hu-
To demonstrate the power of their method, they showed
man face generation tasks [15]. However, the lack of con-
promising results on style transfer [6].
trol over image modification ascribed to the interpretability
Specifically, they argued that different layers of the VGG
of neural networks, is still an open problem. In this paper,
neural network extract the image features at different scales
we tackle the interpretability problem by embedding user-
and can be separated into content and style.
specified images back to the GAN latent space, which leads
to a variety of potential applications. To accelerate the initial algorithm, Johnson et al. [12]
proposed to train a neural network to solve the optimiza-
tion problem of [6], which can transfer the style of a given
Latent Space Embedding In general, there are two exist- image to any other image in real-time. The only limitation
ing approaches to embed instances from the image space to of their method is that they need to train separate neural
the latent space: i) learn an encoder that maps a given image networks for different style images. Finally, this issue is re-
to the latent space (e.g. the Variational Auto-Encoder [16]); solved by Huang and Belongie [10] with adaptive instance
normalization. As a result, they can transfer arbitrary style Transformation L(×105 ) kw∗ − w̄k
in real-time. Translation (Right 140 pixels) 0.782 48.56
Translation (Left 160 pixels) 0.406 44.12
3. What images can be embedded into the Zoom out (2X) 0.225 38.04
StyleGAN latent space? Zoom in (2X) 0.718 40.55
90◦ Rotation 0.622 47.21
We set out to study the question if it is even possible 180◦ Rotation 0.599 42.93
to embed images into the StyleGAN latent space. This
question is not trivial, because our initial embedding ex- Table 1: Embedding results of the transformed images. L
periments with faces and with other GANs resulted in faces is the loss (Eq.1) after optimization. kw∗ − w̄k is the dis-
that were no longer recognizable as the same person. Due to tance between the latent codes w∗ and w̄ (Section 5.1) of
the improved variability of the FFHQ dataset and the supe- the average face [15].
rior quality of the StyleGAN architecture, there is a renewed
hope that embedding existing images in the latent space is
possible. to affine transformations (translation, resizing and rotation).
Among them, the translation seems to have the worst perfor-
3.1. Embedding Results for Various Image Classes mance as it can fail to produce a valid face embedding. For
To test our method, we collect a small-scale dataset of 25 resizing and rotation, the results are valid faces. However,
diverse images spanning 5 categories (i.e. faces, cats, dogs, they are blurry and lose many details, which are still worse
cars, and paintings). Details of the dataset are shown in than the normal embedding. From these observations, we
the supplementary material. We use the code provided by argue that the generalization ability of GANs is sensitive to
StyleGAN [15] to preprocess the face images. This prepro- affine transformation, which implies that the learned rep-
cess includes registration to a canonical face position. resentations are still scale and position dependent to some
To better understand the structure and attributes of the extent.
latent space, it is beneficial to study the embedding of a
larger variety of image classes. We choose faces of cats, Embedding Defective Images As Figure 3 shows, the
dogs, and paintings as they share the overall structure with performance of StyleGAN embedding is quite robust to de-
human faces, but are depicted in a very different style. Cars fects in images. It can be observed that the embeddings of
are selected as they have no structural similarity to faces. different facial features are independent of each other. For
Figure 1 shows the embedding results consist of one ex- example, removing the nose does not have an obvious influ-
ample for each image class in the collected test dataset. It ence on the embedding of the eyes and the mouth. On the
can be observed that the embedded Obama face is of very one hand, this phenomenon is good for general image edit-
high perceptual quality and faithfully reproduces the in- ing applications. On the other hand, it shows that the latent
put. However, it is noted that the embedded face is slightly space does not force the embedded image to be a complete
smoothed and minor details are absent. face, i.e. it does not inpaint the missing information.
Going beyond faces, interestingly, we find that although
the StyleGAN generator is trained on a human face dataset, 3.3. Which Latent Space to Choose?
the embedding algorithm is capable to go far beyond hu- There are multiple latent spaces in StyleGAN [15] that
man faces. As Figure 1 shows, although slightly worse could be used for an embedding. Two obvious candidates
than those of human faces, we can obtain reasonable and are the initial latent space Z and the intermediate latent
relatively high-quality embeddings of cats, dogs and even space W . The 512-dimensional vectors w ∈ W are ob-
paintings and cars. This reveals the effective embedding ca- tained from the 512-dimensional vectors z ∈ Z by passing
pability of the algorithm and the generality of the learned them through a fully connected neural network. An impor-
filters of the generator. tant insight of our work is that it is not easily possible to
Another interesting question is how the quality of the embed into W or Z directly. Therefore, we propose to em-
pre-trained latent space affects the embedding. To conduct bed into an extended latent space W + . W + is a concate-
these tests we also used StyleGANs trained on cars, cats, ... nation of 18 different 512-dimensional w vectors, one for
The quality of these results is significantly lower, as shown each layer of the StyleGAN architecture that can receive
in supplementary materials. input via AdaIn. As shown in Figure 5 (c)(d), embedding
into W directly does not give reasonable results. Another
3.2. How Robust is the Embedding of Face Images?
interesting question is how important the learned network
Affine Transformation As Figure 2 and Table 1 show, weights are for the result. We answer this question in Fig-
the performance of StyleGAN embedding is very sensitive ure 5 (b)(e) by showing an embedding into a network that is
(a) (b) (c) (d) (e) (f) (g)
Figure 2: Top row: the input images. Bottom row: the embedded results. (a) Standard embedding results. (b) Translation
140 pixels to the right. (c) Translation 160 pixels to the left. (d) Zoom out by 2X. (e) Zoom in by 2X. (f) 90◦ rotation. (g)
180◦ rotation.
Figure 5: (a) Original images. Embedding results into the original space W : (b) using random weights in the network layers;
(c) with w̄ initialization; (d) with random initialization. Embedding results into the W + space: (e) using random weights in
the network layers; (f) with w̄ initialization; (g) with random initialization.
to the current state of the art. We leave this investigation to of the embedded content image for the first 9 layers (cor-
future work. responding to spatial resolution 42 − 642 ) and override the
latent codes with the ones of the style image for the last
9 layers (corresponding to spatial resolution 642 − 10242 ).
Our method is able to transfer the low level features (e.g.
colors and textures) but fails to faithfully maintain the con-
tent structure of non-face images (second column Figure 8),
especially the painting. This phenomenon reveals that the
generalization and expressing power of StyleGAN is more
likely to reside in the style layers corresponding to higher
spatial resolutions.
Figure 6: First column: style image; Second column: em- Given three input vectors w1 , w2 , w3 , expression trans-
bedded stylized image using style loss from conv4 2 layer fer is computed as w = w1 + λ(w3 − w2 ), where w1 is the
of VGG-16; Third to Sixth column: style transfer by re- latent code of the target image, w2 corresponds to a neu-
placing latent code of last 9 layers of base image with the tral expression of the source image, and w3 corresponds to
embedded style image. a more distinct expression. For example, w3 could corre-
spond to a smiling face and w2 to an expressionless face of
the same person. To eliminate the noise (e.g. background
4.2. Style Transfer noise), we heuristically set a lower bound threshold on the
L2 − norm of the channels of difference latent code, be-
Given two latent codes w1 and w2 , style transfer is com- low which, the channel is replaced by a zero vector. For
puted by a crossover operation [15]. We show the style the above experiment, the selected value of the threshold is
transfer results between an embedded stylized image and 1. We normalize the resultant vectors to control the inten-
other face images (Figure 6) and between embedded images sity of an expression in a particular direction. Such code is
from different classes (Figure 8). relatively independent of the source faces and can be used
More specifically in Figure 8, we retain the latent codes to transfer expressions (Figure 7). We believe that these
Figure 7: Results on expression transfer. The first row shows the reference images from IMPA-FACES3D [24] dataset. In the
following rows, the middle image in each of the examples is the embedded image, whose expression is gradually transferred
to the reference expression (on the right) and the opposite direction (on the left) respectively. More results are included in the
supplementary material.
5. Embedding Algorithm
Our method follows a straightforward optimization
framework [4] to embed a given image onto the manifold
of the pre-trained generator. Starting from a suitable ini-
tialization w, we search for an optimized vector w∗ that
minimizes the loss function that measures the similarity be-
tween the given image and the image generated from w∗ .
Algorithm 1 shows the pseudo-code of our method. An in-
teresting aspect of this work is that not all design choices
lead to good results and that experimenting with the design
choices provides further insights into the embedding.
Figure 12: First column: original image (1024 × 1024). Limitation of the ImageNet-based Perceptual loss All
Second column: embedded image with the perceptual loss existing perceptual losses utilize the classifiers trained on
applied to resized images of 256 × 256 resolution. Third the ImageNet dataset (e.g. VGG-16, VGG-19), which are
column: embedded image with the perceptual loss applied restricted to the resolution of 224×224. While in our paper,
to the images at the original 1024 × 1024 resolution. we aim to embed images of high resolution (1024 × 1024)
that are much larger than that of ImageNet images. Such
inconsistency in the resolution may disable the learned im-
7. Additional Materials on Embedding age filters as they are scale-dependent. To this end, we fol-
low the common practice [13, 19] and use a simple resizing
Dataset In order to test our embedding algorithm, we col-
trick to compute the perceptual loss on resized images of
lect a small dataset of 25 images in five different categories:
256 × 256 resolution. As Figure 12 shows, the embedding
human faces, cats, dogs, cars and paintings (Figure 17).
results with the resizing trick outperform the ones at the
original resolution. However, small details are lost during
Additional Embedding Results To further support our the resizing, which can slightly smoothen the embedding re-
findings about the initial latent code in the main paper, we sults. We expect to get better results with future perceptual
show more results in Figure 13. It can be observed that: losses that work on higher resolutions.
for face images, initializing the optimization with the mean
face latent code works better; while for non-face images, us- StyleGANs trained on Other Datasets To support our
ing the latent codes randomly sampled from a multivariate insights on the learned distribution, we further tested our
uniform distribution is a better option. embedding algorithm on the StyleGANs trained on three
more datasets: the LSUN-Car (512 × 384), LSUN-Cat
Quantitative Results on Defective Image Embedding (256 × 256) and LSUN-Bedroom (256 × 256) datasets. The
Table 3 shows the corresponding quantitative results on de- embedding results are shown in Figure 18. It can be ob-
fective image embedding (Figure 3 in the main paper). The served that the quality of the embedding is poor compared
results show that compared to non-defective faces, the em- to that of the StyleGAN trained on the FFHQ dataset. The
bedded images of defective faces are farther from the mean linear interpolation (image morphing) results of LSUN-Cat,
face. This reaffirms that the valid faces form a cluster LSUN-Car, and LSUN-Bedroom StyleGANs are shown in
around the mean face. Figure 19 (a), (b) and (c) respectively. Interestingly, we
observed that linear interpolation fails on the LSUN-Cat
and LSUN-Car StyleGANs. Recall that the FFHQ human
Inherent Circular Artifacts of StyleGAN Interestingly, face dataset is of very high quality in terms of scale, align-
we observed that the StyleGAN model trained on the FFHQ ment, color, poses etc., we believe that the low quality of
Figure 13: Additional Embedding Results into W + space. Left column: the original images. Middle column: the embedded
images with random latent code initialization. Right column: the embedded images with w̄ latent code initialization.
(a) (b) (c) (d) (e) (f) (g)
Figure 14: Additional results on the justification of latent space choice.(a) Original images. Embedding results into the
original space W : (b) using random weights in the network layers; (c) with w̄ initialization; (d) with random initialization.
Embedding results into the W + space: (e) using random weights in the network layers; (f) with w̄ initialization; (g) with
random initialization.
the LSUN datasets is the source of such failure. In other Justification of Loss Function Choice Figure 22 vali-
words, the quality of the data distribution is one of the key dates the algorithmic choice of the loss function used in the
components to learn a meaningful model distribution. main paper. It can be observed that (i) matching the image
features at multiple layers of the VGG-16 network works
Additional Results on the Justification of Latent Space better than at a single layer; (ii) the combination of pixel-
Choice Figure 14 shows additional results (cat, dog, car) wise MSE loss and perceptual loss works the best.
on the justification of our choice of latent space W + . Sim-
ilar to the main paper, we can observe that: (i) embedding Influence of Noise Channels Figure 16 shows that
into W directly does not give reasonable results; (ii) the restarting the embedding with a different noise leads to sim-
learned network weights is important to good embeddings. ilar results. In addition, we observed significantly worse
quality when resampling the noise during the embedding
(at each update step). To this end, we kept the noise chan-
Clustering or Scattering? To support our insight that
nel constant during the embedding for all our experiments.
only face images form a cluster in the latent space, we com-
pute the L2 distances between the embeddings of all pairs 8. Additional Results on Applications
of test images (Figure 20). It can be observed that the dis-
tances between the faces are relatively smaller than those of Figure 15 shows additional results of the image morph-
other classes, which justifies that they are close to each other ing. Figure 23 shows the complete table of the style transfer
in the W + space and form a cluster. For images in other results between different classes. The results support our in-
classes, especially the paintings, the pairwise distances are sight that the multi-class embedding works by using an un-
much higher. This implies that they are scattered in the la- derlying human face structure (encoded in the first couple of
tent space. layers) and painting powerful styles onto it (encoded in the
Figure 15: Additional morphing results between two embedded images (the left-most and right-most ones).
Figure 16: Image embedding using different constant
noises.
(b)
(b)
(c)
Figure 25: Additional results on expression transfer. In each subfigure, the first row shows the reference images from
IMPA-FACES3D [24] dataset; in the following rows, the middle image in each of the examples is the embedded image,
whose expression is gradually transferred to the reference expression (on the right) and the opposite direction (on the left)
respectively.