Image2GIF: Generating Cinemagraphs using Recurrent Deep Q-Networks
Yipin Zhou Yale Song Tamara L. Berg
cs.CV
























1]Yipin Zhou This work was done while the author was an intern at Yahoo Research.
2]Yale Song
1]Tamara L. Berg
[1]University of North Carolina at Chapel Hill 
[2]Yahoo Research
[ ]
<http://bvision11.cs.unc.edu/bigpen/yipin/WACV2018>

Image2GIF: Generating Cinemagraphs using Recurrent Deep Q-Networks
    [
    December 30, 2023
==================================================================

empty



Given a still photograph, one can imagine how dynamic objects might move against a static background. This idea has been actualized in the form of cinemagraphs, where the motion of particular objects within a still image is repeated, giving the viewer a sense of animation. In this paper, we learn computational models that can generate cinemagraph sequences automatically given a single image. To generate cinemagraphs, we explore combining generative models with a recurrent neural network and deep Q-networks to enhance the power of sequence generation. To enable and evaluate these models we make use of two datasets, one synthetically generated and the other containing real video generated cinemagraphs. Both qualitative and quantitative evaluations demonstrate the effectiveness of our models on the synthetic and real datasets.





§ INTRODUCTION


Based on our life-long observations of the natural world, humans have the ability to reason about visual appearances of static and dynamic objects. In particular, given an image, one can easily picture which objects will move and how they might move in the future. For instance, given an image showing a can pouring liquid into a cup, as illustrated in Fig. <ref> (left), one can imagine how the liquid must be flowing due to gravity and what that might look like. Or, given an image showing a woman with billowing hair (right), we can imagine how her hair might wave in the breeze.

Cinemagraphs are still photographs in which a repeated movement of one or more objects occur, often served as an animated GIF to produce a repeating effect. Often these objects are natural entities, e.g. water flowing in a fountain, plants blowing in the breeze against a still background, and the steps of an escalator ascending. In this way, cinemagraphs can be seen as highlighting and illustrating the motion of specific dynamic objects in static scenes. Because of this, we posit that understanding and generating cinemagraphs is useful for providing insight into the world – for understanding what objects can move and how they move – and for evaluating how well computational models learn to represent the dynamic nature of objects. 



In this work, our goal is to learn a computational model to reason about visual appearances of static and dynamic objects. Specifically, we train a generative model to animate part of the input photograph and output a cinemagraph sequence. Recent generation works have explored the tasks of generating novel images <cit.> and future video frames <cit.>, but this work is the first to explore the generation of cinemagraphs from a single image, which include the features of both images (static) and videos (dynamic). Cinemagraph generation from a single image is very challenging because the model needs to figure out both where to animate (localization) and how to animate (generation). 

With both dynamic and static characteristics, cinemagraphs highlight particular
objects within an image, bestowing a sense of feeling or emphasis to a static image. As a result, they have become quite popular on the Internet (querying cinemagraph on Google yields 1,960,000 results). However, one drawback is that creating a cinemagraph requires tedious manual work: people use videos to create a cinemagraph using commercial softwares, fixing part of frames and animating others. Thus, one useful practical benefit of this work is to automate the process of generating a cinemagraph from a single image. Other benefits include learning about what objects can move in a scene and how they move. This can be accomplished by developing models to generate cinemagraphs and then evaluating how well their predictions match the reality.

We explore different model architectures stacked in a recurrent structure to recursively generate cinemagraph sequences. Additionally, we incorporate Deep Q-networks, a deep reinforcement learning algorithm (Sec. <ref>), to improve performance by brining in stochasticity to the model. To enable evaluation of these models, we use two new datasets: one is synthetically generated, the other is cinemagraph data we collected from the Internet (Sec. <ref>).Finally, we provide both quantitative and qualitative evaluation of the models on our synthetic and real datasets (Sec. <ref>).


Our contributions include: 1) We introduce a new task of generating cinemagraphs from a single image, 2) We release two new datasets for our task, 3) We explore and evaluation various recurrent generative model structures combined with deep Q-net for cinemagraph generation.




§ RELATED WORK

Image generation: Applying generative models on natural images has attracted a great deal of attention in recent years. Gregor  <cit.> recurrently generate different areas of a single image using an attention mechanism with variational autoencoders. Goodfellow  <cit.> proposed generative adversarial networks (GANs) that greatly improve image generation quality. GANs consist of a generative model and a discriminative model, trained jointly for enhancing the realism of generated images. Radford  <cit.> explored combining deep convolutional neural networks in GANs to further improve the image quality. Pathak  <cit.> proposed a network to generate contents of an arbitrary image region conditioned on its surroundings. Finally, Zhao  <cit.> proposed energy-based GANs that consider the discriminative model as an energy function. Following these great successes, we also explore the use of GANs for cinamagraph sequence generation.

Video frame generation: As a step beyond creating realistic images, video generation requires the model to incorporate temporal information into the generation process. Therefore, video generation models usually generate frames conditioned on the previous frame (or several previous frames), rather than generating from noise, as is usually done in image generation tasks. Some related approaches <cit.> train generative models either to reconstruct input video frames or to generate the next few consecutive frames, learning representations in an unsupervised framework. Mathieu  <cit.> combines a mean square error (MSE) loss with an adversarial loss to produce high-quality generation results. Vondrick  <cit.> proposed a GAN with a spatio-temporal convolutional architecture to learn a scene's foreground and background simultaneously. Zhou  <cit.> incorporated a recurrent neural network with a generative model to generate frames showing future object states in timelapse videos. Inspired by their work, we apply an LSTM <cit.> to encode temporal information during cinemagraph generation.

Deep reinforcement learning: Reinforcement Learning (RL) has been applied to many applications <cit.>. Recently, works combining RL (especially model-free RL) with deep neural networks have attracted extensive attention <cit.>. Model-free RL algorithms can be divided into two types: Q-learning and policy gradient learning. Deep Q-learning (DQL) predicts which action to take at each time step to maximize future rewards, while policy gradient methods directly optimize a policy of expected reward using gradient descent. For example, Mnih  <cit.> use DQL to control an agent to play ATARI games (manipulating the joystick), while Gu  <cit.> proposed a DQL method with a continuous action space. Silver  <cit.>, Lillicrap  <cit.>, Mnih  <cit.>, and Schulman  <cit.> make use of an actor-critic or an asynchronous variant of the actor-critic algorithm based on deterministic policy gradient that can solve the tasks with continuous action space, e.g., cartpole swing-up, 3D locomotion, or other robotics tasks. 

On the Atari task, Mnih  <cit.> utilized a Deep Q-network (DQN) to select an action a, which they apply to the state s in order to achieve maximal award for each time step. Here, a (e.g. joystick operation), s (game screen) and award (game score) can be easily defined. Moreover the action space is discrete (e.g. agents can only move up and down). Later work <cit.> applies a DQN to a continuous action space for controlling robotic arms. Those works have clear definitions for action space (e.g., angles, positions). Previous success of Deep Q-learning witnessed the potential of DQN for sequential decision-making. In this work, we apply a DQN in a sequence generation task, which differs with previous works in that our action space consists of abstract concepts that help with the sequence decision making. In other words, we select actions to apply to the current image to further lower the generative loss (maximize award).








§ METHODS


Our goal is to generate a cinemagraph with n frames Y = {y_1, ..., y_n} given a static image x as input. This requires building models that can generate depictions of future states of moving objects over time. Given an image, we expect the model to learn both what part of the image should be animated (what objects can move and where they are in the image) and how to create an animation for their motion. In this work, we explore two frameworks for the generation task. In the first framework, we stack an autoencoder-like structure with a recurrent neural network layer to recursively generate future frames. The second framework adds a Deep Q-network to help the model make better decisions sequentially while generating future frames.



 §.§ Recurrent Model





In this scenario, we recursively generate frames of a cinemagraph, one by one, from a single static image. We do this by applying an autoencoder-style convolutional neural network stacked with an LSTM layer <cit.>, recursively generating future frames based on a hidden variable z (illustrated in Fig <ref>). An LSTM is ideal for our purpose due to its recurrent architecture and the ability to cope with temporal information of relatively long sequences.

Inspired by work on recursively generating future frames for timelapse videos of objects <cit.>, we design our network with a similar structure and define a loss function with two tasks. The first is a mean square error (MSE) loss, that is, pixel-wise mean square error between generated outputs and the ground truth frames:

    loss_mse  = | Y - G(x) |^2

where x is the input image (the first frame in a cinemagraph sequence), G(.) is the output of the generative model, and Y is the ground truth cinemagraph sequence.

The second part of the loss function is an adversarial loss. We define an autoencoder as the generator and introduce an additional binary CNN classifier as the discriminator. The discriminator classifies a given input image as real or fake (i.e. generated). These two components, generator and discriminator, can be seen as adversaries as they operate in competition with one another, and forms a generative adversarial network (GAN). Several previous generation works <cit.> have used GANs for generating high quality images. 

The adversarial loss is represented as:

    loss_adv  = -log(D[G(x)])

where D[.] is the output from the discriminator. This loss encourages the generated frames look like real images. Our dual-task loss function is then represented as 

    loss = loss_mse + λ_adv * loss_adv
 
where λ_adv is a hyper-parameter that controls the impact of adversarial loss.

The recurrent model partially solve our sequential generation task. However, for cinemagraph generation, which is sightly different from video frame generation, we need a more expressive model with high stochasticity because the model needs to reason about the notion of foreground (moving) and background (fixed). Next, we discuss one possible way to achieve this by using Deep Q-network <cit.>.



 §.§ Recurrent Deep Q-Network





Recent work <cit.> has applied deep Q-learning to train a model to play Atari games, where the model makes decisions for each time step regarding how to adjust a joystick. Similarly, our cinemagraph generation task is a sequential decision making process. During generation, the model must decide which action to apply to generate the next frame. 

Inspired by the success of deep Q-learning, we incorporate a Deep Q-network (DQN), which is a convolutional neural network trained with Q-learning, into our framework. For DQN, the input is a state s (e.g., game screenshots in Atari games) and the output is a Q-vector. The dimension of the Q-vector is equal to the number of possible actions (e.g. up and down). The value q(s,a) for each dimension is defined as the discounted cumulative maximum future expected reward of taking action a from state s. 

    q(s,a)  =  πmax𝐄 [r_t + σ r_t+1 + σ ^2 r_t+2 ... |s_t,a_t=s,a,π]
 
where r_t is a reward at time step t, σ is a discount factor, and π is a policy mapping input states to actions. For each time step, the policy (what action to take when in state s) would then pick the action that maximizes the expected future reward in a greedy manner.

In this work, we apply DQN in a supervised generation task to make decisions about what “action” to take to generate the next frame. We first quantize a continuous action space into a discrete space (a Q-vector with limited dimensions). We then make use of the decoding structure of our generative model to decode how a “quantized action” will affect the state to generate the next frame. We evaluate how sensitive our model is to the size of the discretized space in Section <ref>





Specifically, we propose a generation model cooperating with a DQN. The generator structure is the same as the recurrent model described in Sec. <ref>. We introduce a DQN whose input is the previous generated frame from the generator (or the input image for the first time step). For the output, we quantize the continuous action space into a discrete space by defining the output Q-vector as an N dimension vector to decide what “action” to take for a given state. We then encode it to a one-hot vector by assigning the maximal value of the Q-vector to 1 and others to 0. For each time step, we compute a different Q-vector based on the previous generation and concatenate it with the latent variable from the LSTM layer (as illustrated in Fig. <ref>). We let the concatenated vector pass through the decoding part to generate the frame showing the next state.

As we show in Sec. <ref>, adding the DQN to our recurrent generative model helps us achieve superior results – in terms of both the sequence generation aspect (Sec. <ref>) and the “action” prediction aspect, i.e., how the foreground object will move in the next step (Sec.<ref>).










§ DATASETS


We collected two datasets: synthetic and real. The synthetic data allows us to evaluate our models with a large amount of data in a controlled setting, and to objectively measure performances with clearly defined ground truth labels. The real-data, on the other hand, allows us to test whether our models can indeed tackle the real-world problem of generating cinemagraphs from a single image.




 §.§ Synthetic Dataset

To emulate a cinemagraph-like data, we collect 10 cluttered texture images and randomly pick multiple random offsets between [0.90, 1.1] for 3 channels as the fixed background. We then draw a randomly sized rectangle filled with a random color to one of the texture images as the “foreground object.” The foreground object will move in one of 6 moving patterns: “I” pattern, “O” pattern, “L” pattern, “8” pattern, rotate (counter clockwise), and scale (horizontally and vertically). Fig. <ref> shows example frames from the synthetic sequences.














 §.§ Cinemagraph Dataset

To show the ability of our models generating cinemagraphs from natural images, we collect a dataset of real cinemagraphs from the Giphy website. We crawl GIFs with the tag “cinemagraph” and manually annotate the data with our predefined category names, e.g., water flowing, water pouring, fire, and candle light. We then select those categories with more than 200 samples. The resulting dataset contains 2 categories of cinemagraphs, depicting water flowing and fire. The number of cinemagraphs in the water flowing and fire categories are 926 and 350, respectively, with a total of 1,276 cinemagraphs. 


A cinemagraph with long duration usually contains a great deal of movement replication. To alleviate this, we cut the cinemagraphs to be no more than 1 second. For long cinemagraphs that are more than 3 seconds long, we sample two to three 1-second clips with a maximum margin. This helps us augment the data for training, since the movements at different time steps may look different. We make sure to assign clips from the same cinemagraph to the same training/testing split. 

Some example frames are shown in Fig.<ref>. We observe that frames contain both static and dynamic parts depicting water flowing and fire moving. Since the two moving types are difficult to see in still image frames, we crop and enlarge some key patches to show details of moving objects.[We include examples of animated cinemagraphs in the demo video in the project webpage.]









§ EXPERIMENTS






 §.§ Implementation Details


For the generator of both the RNN (Sec <ref>) and the RNN+DQN (Sec <ref>) models, inputs and outputs at each time step are of dimension 64x64x3, and the encoding and the decoding parts include 4 convolution/deconvolution layers with a kernel of size 5x5 and a stride of 2. The decoding part of each time step shares the same weights, and the number of feature maps for the layers are 64, 28, 256, 512/512, 256, 128, 64 respectively. The size of the hidden variable z is 64 for synthetic data and 512 for real cinemagraph data generation. After each layer, we use ReLU as the activation function; for the last layer we apply a tanh activation function. 

For the RNN+DQN model, the architecture of DQN is the same as the encoding part of the generator, except we do not apply the tanh activation on the output layer. The output Q-vector is a 64 dimensional vector for synthetic data task, and 512 dimension for the real cinemagraph generation task.

We implement all models using Tensorflow. For all experiments, we use an ADAM optimizer <cit.> with a learning rate of 0.0002, and a batch size of 64. We set the weight for the adversarial loss λ_adv to 0.005 for synthetic data and to 0.05 for real data experiments (equation <ref>). We raise the λ_adv in real data experiments in order to enhance the realistic appearance of natural images.






 §.§ Synthetic Data Results


We evaluate the generation performance of the RNN model (Sec <ref>), RNN+DQN model (Sec <ref>), and a baseline generation method from Mathieu  <cit.> on 6 categories of synthetic data. For each category, we generate 100K sequences (95K for training and 5K for testing). Our proposed RNN and RNN+DQN models receive one input frame and recurrently generate the future frames. For each synthetic category, we train the model for 6K iterations. We note that Mathieu  <cit.> is stateless, in that it takes the previous output as the input for the next time step to generate the sequence. For their model we experiment with several parameters settings, and find that applying the default setting described in their paper works the best. We also train their model for 6K iterations. 

For the synthetic data, we sample frames of sequences of each category. If the length of sampled sequences in one category is different, we pad the short sequences with a black image of the same size. The maximum length for 6 categories are: I Pattern : 27; O pattern : 21; L Pattern : 26; Eight Pattern : 21; Rotate pattern : 21;  Scale pattern : 17.

Fig.<ref> qualitative results. We observe that the baseline method <cit.> has a difficult time modeling the temporal movement of the foreground objects. We believe this is because it generates the future frame conditioned on only the previous (several) inputs while the sequence is relatively long. The superior quality of results of our RNN and RNN+DQN models suggest that they successfully learned the moving patterns of different categories. We can also see that the RNN+DQN model produces slightly more visually appealing results (the foreground objects have sharper boundary and more accurate shapes) than RNN method.



We also quantitatively evaluate the methods. For the evaluation metrics, we compute the Peak Signal to Noise Ratio (PSNR) <cit.> and Structural Similarity Index (SSIM) <cit.> between a generated sequence and the ground truth sequence. Table <ref> shows that the RNN+DQN model works better than the RNN model. This is because, although both can generate a sharp background, RNN+DQN generates more accurate foreground objects. These two methods both significantly outperform the baseline method <cit.>, which does not apply an RNN structure to incorporate temporal information. 




 §.§ Real Data Results


We evaluate our RNN and RNN+DQN models, as well as a baseline <cit.> method, on “water flowing” and “fire” categories of our real cinemagraph dataset. For each cinemagraph, we sample 6 frames (1 input and 5 generation outputs). We split the training and testing sets with a ratio of 0.85 : 0.15. We augment the training data by cropping different regions from frames (random location fixed across time) and flipping frames left-right across time. For all three methods and categories, we train the models for 13,500 iterations. 

We first evaluate the method qualitatively. Fig.<ref> shows input frames and generated frames under each method. We refer to our project website for detailed examples of generated cinemagraphs. The baseline method <cit.> achieves decent results because the length of the generation sequences is shorter than the synthetic data. However, similar to the case with synthetic data, background and foreground is still not well distinguished.

Overall, our models learn the correct motions for each object. The results suggest that our RNN and RNN+DQN models better differentiate and animate the foreground motions of water flowing and fire flickering, while keeping background fixed.[We note that, in the resulting frames shown in the paper, the foreground motion is hard to observe due to small image size and the nature of these small motions. We show videos of the generated cinemagraphs in our project webpage to better illustrate the results.] For the “fire” category, the resulting animated fires sometimes wave less strongly than the ground truth due to the limited data and high variance. 

For a quantitative evaluation, we compute PSNR and SSIM values between generated results and the ground truth sequences. Table <ref> shows the results. RNN+DQN, which generates more visually appealing results, also achieves the best quantitative performance compared to the other two methods.

















 §.§ Constant Baseline

To further show that the proposed method is able to capture the (foreground) moving objects while making the background static, we provide a simple “constant” baseline, i.e., new frames are simply copied from the first frame. To capture the temporal dynamics, we compute PSNR/SSIM on frame-wise difference images (two consecutive frames) rather than on the original frames. For the constant baseline, frame-wise difference results in all-zero images because there is no movement over time. 
Table <ref> shows that RNN+DQN method achieves better performance than the baseline, which suggests the capability of our method discriminating foreground objects from backgrounds.





 §.§ Deep Q-Network


Deep Q-network (DQN) helps improve the results by bringing in more stochasticity into our model, improving sequential decision making and thus the overall visual quality of generated cinemagraphs. Here, we show the ability of DQN to help decide what objects in a scene should move and how they should move. 

We conduct an experiment on synthetic dataset to evaluate the model's ability to predict the location of moving objects (find which object to move and how to move). Specifically, we pre-compute the segmentation maps (segmenting the moving object and the background) of the synthetic data. During testing, we segment out the object from the ground truth data and run a sliding window on the generated frames to get the resulting segmentation maps. And we compute the average Euclidean distance between the object centers of ground truth and generated frames. 

Table <ref> shows the results. The results suggest that adding DQN indeed improves the prediction quality on how objects move in future frames. We do not evaluate Mathieu  <cit.> because it often fails to generate moving objects, and thus the sliding window technique cannot be applied. 



 §.§ Sensitivity Analysis


We conduct an ablation study to show how sensitive the results are to the size of z (bottleneck of the auto-encoder) and the Q-vector (output of DQN). Specifically, we evaluate the RNN+DQN model with small (4-dim) and large (2048-dim) sizes of z and Q-vector, and compare them with the original results we presented earlier (64 for synthetic data and 512 for real data)

Table <ref> reports average PSNR/SSIM scores of small and large z/Q-vector on both synthetic and real datasets. We notice that utilizing the small bottleneck and/or Q-vector significantly harm the results due to the severe information loss and over quantization (except for applying small Q-vector on synthetic dataset because the action space of partial synthetic categories is relatively small). 





 §.§ Human Evaluation


There is a universal problem in evaluating video prediction methods: There might be multiple correct predicted results. There might be multiple cinemagraphs that share the first frame but look different from each other, all of which could be considered reasonable to a human observer. Therefore, we design two human experiments to judge the quality of real cinemagraph generated by different methods. 

In the first experiment, we show human subjects three cinemagraphs generated from different methods (RNN, RNN+DQN, Mathieu  <cit.>) and ask them to choose one that looks the most superior. We used Amazon Mechanical Turk (Mturk) for this study. Results are shown in Table <ref> (Col 2-3), where numbers indicate how often humans selected results from each method as the best cinemagraph. We note that RNN+DQN method achieves the best results under both categories.

In the second experiment, we show human subjects a single cinemagraph (either real or generated) at a time and ask them whether it looks realistic. We measure how often humans choose the generated ones as real and report the results in Table <ref> (Col 4-5), which shows that our RNN+DQN is the most successful at this task.





§ CONCLUSION

In this paper, we explore a challenging task of generating a cinemagraph from a single image. We propose a method that combines recurrent generative models with a deep Q-network to learn what regions should move and how they should move over time. On both synthetic and real datasets, we evaluate qualitative and quantitative results of the proposed methods and show improved performance over a baseline method of Mathieu  <cit.>. 

Future work includes expanding the natural cinemagraph dataset to additional categories with increased numbers of examples. We also plan to analyze the learned models to better understand what the models are learning about image content semantics and motions of objects in the world.





ieee



