{'fraction_non_alphanumeric': 0.03569085421860648, 'fraction_numerical': 0.012115645365874995, 'mean_word_length': 4.71457135716071, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Semantic Facial Expression Editing using Autoencoded Flow

High-level manipulation of facial expressions in images --- such as changing a smile to a neutral expression --- is challenging because facial expression changes are highly non-linear, and vary depending on the appearance of the face. We present a fully automatic approach to editing faces that combines the advantages of flow-based face manipulation with the more recent generative capabilities of Variational Autoencoders (VAEs). During training, our model learns to encode the flow from one expression to another over a low-dimensional latent space. At test time, expression editing can be done simply using latent vector arithmetic. We evaluate our methods on two applications: 1) single-image facial expression editing, and 2) facial expression interpolation between two images. We demonstrate that our method generates images of higher perceptual quality than previous VAE and flow-based methods.


Introduction
The pixel-level manipulation of photographs is a common task that has received considerable attention, both in the form of commercial products and research papers. High-level, semantic editing of images, such as turning a frown into a smile, is much less well-understood, because it requires holistic understanding of how images correspond to linguistic concepts. Recent and dramatic advances in image understanding, however, suggest that we can start performing these types of semantic image edits.
In this paper we address the problem of high-level facial expression editing, such as changing emotions. For example, we take a neutral face photo and make it smile, or squint, or exhibit disgust. We can also exaggerate an existing expression, or perform the opposite task of making an expression subtler. Finally, we also show how to interpolate between two facial expressions, while maintaining a more realistic image than a simple cross-fade or morph would afford.
Our approach to realistic facial expression manipulation is inspired by recent facial synthesis work using convolutional neural networks (CNNs) [14,27,7]. These techniques model the space of realistic faces by auto-encoding a dataset of faces down to a low-dimensional latent vector; they then generate a face image from this latent vector. The advantage of this latent space is that it is more linear than the space of general images [22,14]. When this space is well-formed, sampling novel latent vectors yields novel but realistic face images. Also, interpolating latent vectors yield realistic interpolated faces, and certain directions in the latent space are aligned with semantic properties, such as smiles, or beards.
These methods show impressive and exciting generalization capabilities, and can generate recognizable faces over a wide range of identities and expressions. However, close inspections of these generated images often reveal low res-olution, blurriness, and broken facial features. Not surprisingly, synthesizing faces by hallucinating RGB values from scratch is very difficult. An alternative and less ambitious approach, long employed by the graphics community [19,2], is to generate or manipulate images by re-using parts of existing ones. While these results are often highresolution and very realistic, the lack of a latent space prevents more high-level, semantic operations.
In this paper we explore whether the strengths of these two methods can be combined. Inspired by recent techniques that place differentiable optical flow layers within neural networks [10,32], we train an autoencoder that learns to synthesize face images by flowing the pixels of existing face images. Instead of a latent space that directly encodes RGB values of face images, our latent space encodes how to flow one face image to another of the same person. We show that this approach allows us to maintain the high-resolution, sharp textures of existing images while manipulating images in a meaningful latent space.

Related Work
Deep neural network methods for image synthesis have recently produced impressive results [14,27,7,5,21,20]. Many of these techniques employ an additional adversarial network to further optimize the appearance of the generated image [5,21]. The most similar paper to our work is DeepWarp [6], which also uses neural networks to synthesize flow fields that manipulate facial expression. However, this technique is specifically limited to changing the gaze direction of eyes in an image, most typically for videoconferencing applications.
Traditional graphics techniques for editing and synthesizing facial expressions operate by manipulating patches of existing images. The Visio-lization method [19] hallucinates novel faces by compositing facial patches from a large dataset. Optical flow computed over 3D face models allow facial expression manipulation and compositing of expressions [28,29,30]. Beyond faces, feature correspondence has long been used to warp and morph images [3]. We compare the results of our CNN-based method against both morphing and optical flow in Section 6.

Approach
Deep generative models, such as Variational Autoencoders (VAEs) [13], have demonstrated that image generation and editing can be done through vector arithmetic in the latent space [27,21,14]. Inspired by those models, we proposed the Flow Variational Autoencoder (FVAE) model, which is a hybrid of VAE and the classical flow-based approach [30,28]. In particular, the decoder of our FVAE learns a mapping from the latent space to the flow space, rather than pixel space.
The FVAE takes a pair of source and target images as input and outputs a per-pixel flow field and a per-pixel confidence mask. The flow field warps the source image to be similar to the target image, and the confidence mask indicates how confident the model is with its flow predictions.
In the following sections, we describe our objective functions to train a FVAE, and how to utilize a trained FVAE for two applications: 1) single image facial expression editing and 2) facial expression interpolation between two face images.

Learning Flow Variational Autoencoder
The objective function for learning a FVAE consists of three parts: 1) L reconstruct , which controls the pixel-wise image difference, 2) L prior , which controls the smoothness in the latent space, and 3) L f low , which controls the coherence in the flow space. The overall objective is to minimize the following loss function, where λ 1 and λ 2 are hyperparameters to be tuned.

FVAE Variational Autoencoder
We first denote the following notations that will be used throughout the formulation: T = target image, S = source image, f = encoder, g = decoder, D = set of (S, T ) tuples in the dataset, and Z = latent vector. Encoder Network f (T ): Following the VAE framework [13], we assume the prior distribution follows a multivariate Gaussian distribution. Using the reparameterization trick, our model's encoder network, f (T ) = (µ(T ), σ(T )), outputs the mean µ, and standard deviation σ of the approximate posterior. To compute the latent vector Z for a given T , we sample a random from the multivariate Gaussian distribution, N .
As proposed and derived in [13], L prior has the following form, subject to g j (S, Z(T )) ∈ {S} ∀j, where j denotes the pixel location.
In order to satisfy the constraint the network computes a dense flow field, F , as an intermediate state. Denote F (j) (F (j) x , F (j) y ), which specifies the pixel location to sample from S to reconstruct the j th pixel of the output, g j (S, Z(T )). To train the network from end-to-end, we relax the constraint in Eq.4, such that g j (S, T ) is in the set of bilinear interpolated values from {S} [32]. The decoder network, g, has the form where (F (j) x , F (j) y ) denotes the absolute coordinates to sample from the source image, N (F (j)) denotes the set of the 4 neighbors of F (j), S k denotes the k th pixel of the source image, and (x k , y k ) denotes the pixel absolute coordinates of the k th pixel.
This bilinear sampling mechanism was introduced by Jaderberg et al. as differentiable image sampling with a bilinear kernel; Error backpropagation can be done efficiently through this mechanism [10].

Flow Coherence Regularization
Our choice of the flow coherence regularizer, L f low , is based on the intuition that flow directions should be spatially coherent except at boundaries. We design a pair-wise regularization term as follows: where, i, j are 2D-coordinates, F i is the flow vector at i, T i is a RGB color vector of the target image at i, α is a weighting hyper-parameter, and N (i) is the set of neighboring pixels of i. In our experiment we use a 7 × 7 neighborhood centered at i. Eq.6 implies that the flow is encouraged to be similar for nearby pixel coordinates, especially when RGB values are similar in the target image. Similar smoothness formulations have been used in the context of semantic image segmentation [31,24,4,16].

Learning the Confidence Mask
From a single source image, it may not be possible to form a good reconstruction of the target image. For example, it will be very difficult to reconstruct a smile expression given a neutral expression, as the teeth region simply does not exist in the source image. Ideally, we would like to have a mechanism to identify the regions where the network will perform poorly given a particular pair of source and target image. With such a mechanism, we would be able combine multiple source images to generate one output image.
In order to identify such regions, we trained another network to predict a per-pixel soft confidence mask M where M j predicts the confidence of the generated output g j (S, Z(T )). This confidence-learning problem can be formulated as a binary classification task, where the label is 1 if the target pixel is in the source image, and 0 otherwise. However, getting such labels would require searching each pixel pair in S and T for all (S, T ) pairs, and is not scalable. Instead we choose to approximate the true label, y, using the 2 -norm, as follows: Given a trained encoder f and decoder g, , where β is a normalization constant, such that y ∈ [0, 1].
The confidence mask network takes Z(T ) and S as inputs (i.e., M (S, Z(T ))), as at test-time the target image is not available. With the approximate true label y, the objective function for learning the confidence mask is the sum of cross entropy losses over all pixel locations. To train the network we minimize the following loss function:

Applications
With a trained FVAE model, we proposed two applications 1) expression editing on a single source image, and 2) expression interpolation from a pair of source images.

Expression Editing Using Latent Vector
For this task, the goal is to transform the expression of a given face image to a desired target expression; see Figure  2 for illustration. The task is an analogy making problem, A:B::C:D means A is to B as C is to D [22] (i.e., generate a D such that the relationship between C and D is similar to that of A and B).
Latent representations from deep models have been shown to capture semantic and visual relationships, and analogy can be made through vector arithmetic; if A:B::C:D then (A−B) ≈ (C −D). For example, in language models, the vector representations of "King" -"Man" + "Woman" is a vector similar to "Queen." [17,18]. Similarly, visual analogy making has been explored in [22,21].
Algorithm 1 details the method for performing expression editing using the proposed FVAE. The inputs are: 1) the trained FVAE network, (f, g), 2) training dataset face images, I, and expression labels, y, 3) a source image for editing, S, 4) a target expression label, y target , 5) the source expression label, y source , and 6) the number of steps, K. Outputs: g(S, Z edit ) 7: end for Algorithm 1 computes a latent expression direction by taking the difference of the averaged latent vectors for training images with y target expression label, and training images with y source expression label. Next, the source image's latent vector, Z source , is interpolated linearly along the expression direction.

Expression Interpolation Using Latent Vector
The generative power of VAE models is often demonstrated by performing linear interpolation between two given locations in the latent space. For this task, the goal is to generate all the intermediate "levels" of expression between two given source images; see Figure 6 for illustration. Algorithm 2 details the method for expression interpolation. The inputs are: 1) the two source images S 1 and S 2 , 2) a trained FVAE (f, g), 3) a trained mask model, M , and 4) the number of steps, K.
In order to utilize both source images in the linear interpolation, we use the normalized confidence mask to blend the interpolated outputs from each source image together. This is useful when certain facial parts are only in one of the source image, (e.g., teeth when interpolating from neutral to smile).

Flow Upsampling for High Resolution Output
Observe that one limitation of using a deep model is that the output dimensions are fixed. A model trained to generate 128 × 128 images cannot output 512 × 512 images. To get higher resolution output, typically, one will have to upsample in the pixel domain; this often introduces blurriness. Here we proposed to perform upsampling in the flow domain instead. We assume that high-resolution source images are available. Applying the upsampled flow to the high-resolution source images tends to produce sharper images, see Figure 7 for comparison.

Implementation Details
In this section, we describe the data processing and training implementation details.

Dataset
To evaluate our proposed method, we conducted experiments on the CMU MULTI-PIE Dataset [8], which contains the identity labels to train a FVAE, as well as the expression labels necessary at test time.

Data Preparation
We use all the examples with camera view 05 1, and illumination 06 from the MULTI-PIE dataset. The training and testing set is divided based on identity, with 80% and 20% random split. We aligned the faces using landmarks detected from [11], then cropped and resize the images to dimension of 128 × 128. To prepare the training example pairs of (S, T ), we created all pairs of expression per identity. For each source and target image pair we apply a color transform [25] to adjust the color of the source image to better match the target images; this makes the learning easier, as 2 -norm loss is sensitive to lighting and color changes. We also applied data augmentation by randomly flipping images horizontally. Lastly, we normalized the pixel values into range of [-1,1] for the ease of training [21].

Model Architecture
Our model architecture follows a similar design spirit as [21]. For the encoder, convolution layers channel sizes double as the spatial size halves. For the decoder, convolution layers channel sizes halves and spatial size doubles. We used nearest neighbor interpolation for upsampling in the decoder. Figure 3, shows the exact architecture used. The convolution layers' filter sizes are 3 × 3, with the exception of the first and final layers, which are 5 × 5 filters. Each convolution layer uses relu except for the final layer outputting the flow which uses tanh, as our network outputs normalized coordinates. Additionally, all convolution layer uses batch-normalization [9], except at the final two layers before the output. All the weights are initialized from a truncated Gaussian distribution with standard deviation of 0.01.

Training
We train the network using the Adam optimizer [12], with learning rate of 0.0003, β 1 = 0.9, β 2 = 0.999 and mini-batch size of 64. We use gradient clipping by value with threshold of ±5. Empirically, we chose λ 1 = 0.003 and λ 2 = 0.001. The training and validation loss are moni-tored to use early stopping to prevent overfitting. The models are implemented using TensorFlow [1].

Evaluations
In this section, we provide comparison experiments with qualitative and quantitative results for the single-image editing task and image interpolation task.

Facial Expression Editing
For the facial expression editing task, we compare our FVAE model with both the VAE [13] using [21]'s architecture, and an optical flow method [23]. Figure 4 compares the expression editing results on examples from the test set. Note that for all results in our paper the person shown in the result is part of hold-out data; no images of that person were in the training set. For VAE, we train a model with the same architecture as our FVAE, except the last layer is replaced to predict the RGB pixel values directly. Then, we use Algorithm 1 to perform the editing. For optical flow, we use the average flow extracted from all identities in the training set. It can be observed that our method generates superior image quality compare to the other methods. VAE results in blurry images, and the optical flow method cannot get a flow conditioned on the source image, thus the flow is not tailored to the source image and creates artifacts.
For quantitative evaluation, the PSNR for each method is shown in Table 1. In order to measure PSNR, we use the ground truth target image's latent vector for FVAE and VAE. For optical flow, using a state-of-the-art method [23], we compute the flow field using the ground truth target image, and warp the source image accordingly.
It is not surprising that a VAE obtains the highest PSNR, as it is directly generating pixels to minimize the 2 -norm loss. However, judging by the perceptual quality, our FVAE significantly outperforms the VAE; VAE generates overly smooth images with poor low-level image quality (e.g., hair and skin textures). This just demonstrates that PSNR is not an ideal quantitative measurement for this task.
Next, we demonstrate that the proposed FVAE method can be transferred to out-of-dataset samples and expressions. Figure 5 demonstrates that our method can be used to edit web images to suppress or magnify the facial expressions. The third row in Figure 5, is the expression of fear, which is not in the dataset; however, the editing can be still be done by approximating Z source = Z(S), in Algorithm 1. Note that for web-images to generate reasonable quality images, the background needs to be clean and the face region should not contain significant lighting variations, such as shadows. 2 Images from [28] and http://www.soalinejackphotography.com

Facial Expression Interpolation
For the facial expression interpolation task, we compared our method against five different methods: VAE, crossfading, mesh morphing [26], optical flow [15], and face morphing [29]. Figure 6 shows the qualitative comparison between the methods on examples from the test set. It can be observed that our method produces the sharpest and most realistic image out of all methods. The confidence mask successfully handles the teeth regions, whereas morphingbased methods [26,29] and optical flow [15] generate blurry results around the mouth regions. Classical mesh morphing produces the second-best results, but still has trouble with the teeth. Moreover, our method provides a more coherent and natural transition between the expression levels.

Perception User Study
We perform a perception user study to compare the photorealism of real images, images generated from our proposed method, VAE, and optical flow. We perform an experiment asking Amazon Mechanical Turk's workers to label images as real or fake. The experiment is conducted on 25 participants; each is asked to label 10 examples generated from each of the methods, and 10 real images. the images are provided in random order on a single web page.
To guarantee the quality of the results, users performing below 50% accuracy on the real images are dropped from the experiment. Additionally, some users skipped over certain images; we also do not count the skipped images in our result. The percentage of images labeled as real for each of the method is reported in Table 2. As can be seen, our method outperforms VAE and optical flow in perceptual quality.   Figure 7 shows the comparison between upsampling in the pixel domain versus in the flow domain. The source images is of dimension 512 × 512. In order to use our trained network of dimension 128 × 128, we resize the source image to 128 × 128. For comparison, we upsample the output images back to 512 × 512 in the pixel domain. For flow based upsampling, we upsample the flow back to 512 × 512 using bilinear interpolation. Next, we pass the upsampled flow through the bilinear sampling mechanism with the high-resolution source image to synthesize the output. As can be seen, upsampling in the pixel domain leads to blurrier results compared to the flow upsampling method. The flow upsampling method preserves more of the finer details, (e.g., edges, and facial hair texture), from the source image. We are able to upsample the flow by four times without noticeable distortions in the output image.

Conclusion
We have presented an approach to the realistic manipulation of facial expressions that combines the strengths of deep models and flow-based image editing approachs. We demonstrate two applications: 1) high-level facial expression editing from a single source image, and 2) highlevel facial expression interpolation from two source images. The proposed method successfully outperforms the baseline methods on the MULTI-PIE dataset. Our method generates higher resolution images, with realistic image quality and more natural expression changes. We further demonstrate that the learned transformation can be generalized to out-of-dataset samples with significantly different image statistics, while deep models hallucinating RGB values directly fail to generalize.
Despite outperforming the baseline methods, our method still faces some challenges: 1. Our method is limited to frontal pose faces with small rotations. Although [32] demonstrates 3D rotation on objects is plausible, the task becomes more difficult when the training data does not contain a fine scales of rotated images. For example, the MULTI-PIE dataset contains 45-degree jumps between the rotation angles, and this lead to difficulties during training. However, we believe that our method can properly handle rotation given a more suitable dataset.
2. Training data needs to be very controlled. Our method requires face images taken under similar lighting conditions and backgrounds. The main reason is that 2norm loss is sensitive to RGB color changes. Without controlled data, the model will focus on learning the lighting or background difference, and less on the expression. One possible solution is to utilize face and lighting information to design a lighting and background invariant loss function.
We leave these challenges for future work.