{'fraction_non_alphanumeric': 0.02869496855345912, 'fraction_numerical': 0.00909001572327044, 'mean_word_length': 4.677266387726639, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
CanvasGAN: A simple baseline for text to image generation by incrementally patching a canvas

We propose a new recurrent generative model for generating images from text captions while attending on specific parts of text captions. Our model creates images by incrementally adding patches on a"canvas"while attending on words from text caption at each timestep. Finally, the canvas is passed through an upscaling network to generate images. We also introduce a new method for generating visual-semantic sentence embeddings based on self-attention over text. We compare our model's generated images with those generated Reed et. al.'s model and show that our model is a stronger baseline for text to image generation tasks.


Introduction
With introduction of Generative Adversarial Networks (GAN) Goodfellow et al. [8] and recent improvements in their architecture and performance [1] [10], the focus of research community has shifted towards generative models. Image generation is one of the central topic among generative models. As a task, image generation is important as it exemplifies model's understanding of visual world semantics. We as humans take context from books, audio recordings or other sources and are able to imagine corresponding visual representation. Our models should also have same semantic understanding of context and should be able to generate meaningful visual representations of it. Recent advances, quality improvements and successes of the discriminative networks has enabled the industrial applications of image generation [27] [12] [38]. In this paper, we propose a sequential generative model called CanvasGAN for generating images based on a textual description of a scenario. Our model patches a canvas incrementally with layers of colors while attending over different text segments at each patch.
Variety of sequential generative models have been introduced recently, which were shown to work much better in terms of visual quality as model gets multiple chances to improve over previous drawings. Similar to CanvasGAN's motivation from human execution of painting, attention is motivated by the fact that we humans improve our performance by focus on a particular aspect of task at a moment rather than whole of it [2] [30]. In recent works, attention alone has been shown to be really effective without anything else [29]. For our model, at each timestep, model focuses on a particular part of text for creating a new patch rather than whole sentence.
A lot of models have been proposed in the recent year for the text to image generation task [34][25] [31]. Many of these models incorporate GANs and a visual semantic embedding [6] for capturing text features which are semantically important to images. Generator networks create an image from sampled noise conditioned on the text and discriminator predicts whether the images is real or generated. However, the images generated by these networks are mostly not coherent and seem distant from text's semantics. To overcome this incoherence, we propose new method for generating image-coherent sentence embeddings based on self-attention over the text.
In this work, we make two major contributions: 1. We propose a new model for text to image generation, called CanvasGAN, which analogous to human painters generates an image from text incrementally by sequentially patching an empty canvas with colors. Furthermore, CanvasGAN uses attention to focus over text to use for generating new patch for canvas at each time-step.
2. We introduce a new visual semantic embedding generation mechanism which uses self-attention to focus on important hidden states of RNN to generate a sentence embedding instead of taking hidden state at last timestep as usual. This sentence embedding generator is separately trained to be coherent with image semantics using pairwise ranking loss function between sentence embedding and image.

Related work
Generative Adversarial Networks (GAN) [8] and Variational Auto-Encoders (VAE) [16] can be considered as two major categories in deep generative models. In conditional image generation, GANs have been studied in-depth where the initial work used simple conditional variables like object attributes or class labels (MNIST) to generate images [28][22] [32]. Multiple models were introduced in image to image translation which encompasses mapping from one domain to other [38][15] [39], style transfer [7] and photo editing [3] [37]. In context of sequential generative models, Denton et al. [5] uses a laplacian pyramid generator and discriminator called LAPGAN to synthesize images from low to high resolutions levels sequentially. Similar to our work, DRAW network 9 is a sequential version of an auto-encoder where images are generated by incrementally adding patches on a canvas. Closest to our work is Mansimov et al. [19], which uses a variational auto-encoder to patch a canvas and then use an inference network to map back to latent space. In CanvasGAN, we use a discriminator based loss function with GAN based architecture and our new visual-semantic embedding to generate images from text.
Image caption generation, which is reverse of text to image generation has seen a lot of significant models which have good performance. Karpathy and Fei-Fei [14] uses alignments learned between CNN over image regions and BiRNN over sentences through multi-modal embedding to infer descriptions of image regions. Xu et al. [30] uses attention over weights of a convolutional net's last layer to focus on a particular area of image to generate captions as a time-series data via a gated recurrent unit [11] [4].
In Reed et al. [25], a simple generative model was introduced for converting text captions into images. This model started a series of work on text-to-image generation task. It contained a simple upscaling generator conditioned on text followed by a downsampling discriminator. The model also used visual semantic embedding [24] for efficiently representing text in higher continuous space which is further upsampled into an image. This was further improved by Zhang et al. [34], by generating high quality 256x256 images from a given text caption. Their model (StackGAN) employs a two step process. They first use a GAN to generate a 64x64 image. Then, this image along with text embedding is passed on to another GAN which finally generates a 256x256 image. Discriminators and generators at both stages are trained separately.
Most notable recent works in the text to image task are AttnGAN [31] and HDGAN [36]. In AttnGAN, the authors improved StackGAN by using attention to focus on relevant words in the natural language description and proposed a deep attentional multimodal similarity model to compute a fine-grained imagetext matching loss for training the visual semantic embedding generator. In HDGAN, authors generate images at different resolutions with a single-streamed generator. At each resolution, there is a separate discriminator which tells (i) whether image is fake or real and (ii) if it matches the text or not. Another important contribution is made by Zhang et al. [35] in which the authors generate details using cues from all feature locations in the feature maps. In their model (SAGAN), the discriminator can check that highly detailed features in distant portions of the image are consistent with each other which leads to a boost in the inception score.

Model
We propose CanvasGAN, a new sequential generative network for generating images given a textual description. Model structure is shown in Figure 1. We take motivation from human painters in how they create a painting iteratively instead of painting it in single step. Keeping that in mind, starting with an empty canvas, we paint it with patches iteratively. At each step, a patch is generated based on attended features from text.
First, we retrieve GloVe [23] embeddings g for the words in caption, w and encode it using our visual semantic network f vs which we explain in Section 3.1. This provides us with sequentially encoded word embeddings, e and a sentence embedding, s for whole sentence. We sample our noise z ∈ R D from standard normal distribution N (0, 1). We use conditional augmentation, f ca over sentence embedding, s to overcome the problem of discontinuity in latent mapping in higher dimensions due to low amount of data [34]. Conditional augmented sentence vector c along with noise z is passed through neural network f to generate initial hidden state, h 0 of the recurrent unit. Initially, canvas is empty and a zero tensor canvas 0 = 0. Now at each timestep, i ∈ {1, . . . , t}, we execute a series of steps to patch canvas. Attention weights α i are calculated by neural network f att i with inputs c, z and h i−1 which are scaled between (0, 1) as β i by taking a softmax. Attended sentence embedding,ē i for current time-step is calculated as σ j β ij e j . Next hidden state, h i is calculated by recurrent unit f i which take previous hidden state h i−1 and attended sentence embeddingē i as inputs. The r, g and b channels for next patch are produced by neural networks f r i , f g i and f b i which are concatenated to produce a flattened image. This image is passed through an upscaling network, f up i , to generate a patch of size same as canvas. For adding this patch to canvas, we calculate a parameter γ from neural network, f γ i which determines how much of delta will be added to the canvas. Finally, γ * δ is added canvas i−1 to generate canvas i . Our final image representation is denoted by canvas t at last time-step. Mathematically, our model can be written as, for i = 1, . . . , t: For our experiments, we implement f ca , f r i , f g i , f b i and f γ i as simple affine network followed by rectified linear unit non-linearity. We choose f 0 and f i as Gated Recurrent Unit [4]. For f att i , we first concatenate c, z and h i−1 and then, pass them through a affine layer followed by a softmax. Finally, upscaling network using deconvolutional layers with residual blocks to scale the image to higher resolutions [18] [ 33]. − → h f i ) for each word. We pass these hidden states through a feed-forward network to calculate attention weights (α i ) for each hidden state. Finally, we linearly combine hidden state multiplied by their respective attention weights to get final hidden state. This is compared via pairwise ranking loss with image representation of original image downsampled through a CNN to same number of dimensions. Whole network is trained end-to-end.

Visual Semantic Embedding
Standard models for representations of words in continuous vector space such as GLoV e [23], W ord2V ec [20] or f astT ext [13] are trained on a very large corpus (e.g. Wikipedia) which are not visually focused. To overcome this issue, visual semantic models have been proposed in recent works [24] [17]. These representations perform well, but don't have power to focus on important words in text. In these embeddings, focus is on last encoded state of recurrent unit as in machine translation [2]. We propose a new method for calculating visually semantic embedding using self-attention. By using self-attention over encoded hidden states of a recurrent units, our method generates a sentence embedding in latent space. To introduce component of visual semantic, we compare image with sentence embedding using pairwise ranking loss function similar to [17]. For proper comparison, image is encoded into same latent space as text by passing features extracted from last average-pooling layer of pretrained Inception-v3 through an affine layer. This method allows embedding to focus on most important part of sentence to generate sentence embedding. To introduce visual semantic component, a lot of models were proposed. Some of these models used a smaller datasets with visual descriptions, like Wikipedia article's text along with its images. These embeddings didn't perform well in practice. Also, these models were not capable of being generalized for any zero-shot learning task and had to be fine-tuned separately according to the task, To overcome this, a lot of visual semantic models [24] [17] were proposed which take in account both the image and its representative text while training. Fig. 3: Architecture of our model's discriminator. Image is downsampled and passed through residual blocks to extract feature. Then, it is concatenated with spatially replicated text representation and the combination is passed through convolutional layers to predict the probability of whether the text is semantically relevant to image or not.
where − → w 1 , − → w 2 , . . ., − → w n are the vector representation of y, original words in one-hot (1-of-K) encoded format, y = (y 1 , y 2 , . . . , y n ) where K represents the size of vocabulary and n is length of sequence. GRU is our gated recurrent unit, while f score is scoring function for attention weights (α i ).
− → h i and ← − h i are bidirectional hidden states for − → w i .
− → h f i is sum of both bidirectional hidden states which is combined with α i to get final hidden state h f

Discriminator
Our model's discriminator is a general downsampling CNN which takes an image and a text representation to predict whether image is semantically relevant to text or not. Image is downsampled and passed through residual branch for feature extraction. Text representation (h f ) is spatially replicated so that it and the image are of same dimensionality. These are concatenated and further passed through convolutional layers to generate a real number between 0 and 1 predicting the probability of semantic relevance.

Experiments and Results
We train our model on oxford flowers-102 dataset [21] which contains 103 classes for different flowers. Each flower has an associated caption each describing different aspects, namely the local shape/texture, the shape of the boundary, the overall spatial distribution of petals, and the colour. Fig. 4: Calculation of Generator and Discriminator loss using matching, mismatching and relevant text. In the left side of the figure, we optimize our discriminator using matching text and mismatching text with the original training image. We also use relevant text with generator generated images to further optimize the discriminator. On the right side of the figure, we use discriminator's loss for generator generated image for matching text and try to maximize that loss.
We create extra augmented data via rolling matching text across the text dimension which matches each text with wrong image and thus creates a mismatching batch corresponding to a matching batch. We also create a batch of relevant text in which we roll half of the text with rest of the batch, in this  Figure  7a shows loss curves for RCAGAN which includes Generator loss, Discriminator loss with matching, mismatching and relevant text. Figure 7b shows similar loss curves for Reed et al. [25]. Figure 5c shows comparison of Generator loss for RCAGAN and Reed et al. [25] .
case the text is somewhat relevant to image but still doesn't match semantically. We directly pass mismatching and matching text and images to discriminator while minimizing binary cross entropy (BCE) loss with one (relevant) and zero (non-relevant) respectively. For relevant text, we pass the text to generator and further pass generated image to discriminator while minimizing BCE loss with zero (fake). We also calculate generator loss in case of matching text by passing generated image to discriminator with task and minimizing BCE loss for discriminator prediction with respect to zero. See Figure 4 for overview of loss calculation.

Quantitative Analysis
For analyzing CanvasGAN quantitatively, we will review the loss curves for generator and different losses of discriminator. In Figure 7, we can see various loss curves for generator and discriminator losses for both ours and Reed et al. [25] model. In both models, discriminator loss for matching and relevant text drops progressively with time and shows that discriminator gets better. For relevant text though, discriminator loss drops close to zero in the beginning as the generator is untrained and is not able to generate plausible images. However, it recovers from that after a few epochs. For CanvasGAN's loss curves, we can see the evident effect of applying RNN based incremental generator with attention: generator loss drops quite drastically in the start as discriminator fails to catch up with the generator. This shows that with attention, the generator is able to produce plausible images from the get-go. After a few epochs, the discriminator eventually copes up with the generator and the generator loss starts increasing, which should result in a better discriminator and generator overall and it is supported by the fact that our loss always remains below than that of Reed et al. [25]. Fig. 6: Attention weights for 4 timesteps for image caption "this orange flower has five fan shaped petals and orange stamen barely visible". We can see how the attention weights change with timesteps. Significant weights are concentrated around "orange". The image on left is the generated image.

Caption
Reed et al CanvasGAN Original this flower has white petals with pointed tips and a grouping of thick yellow stamen at its center. this flower has large pink petals with a deep pink pistil with a cluster of yellow stamen and pink pollen tubes this flower has petals that are red and has yellow stamen the violet flower has petals that are soft, smooth and arranged separately in many layers around clustered stamens there is a star configured array of long thin slightly twisted yellow petals around an extremely large orange and grey stamen covered ovule Table 1: Comparison of images generated by our CanvasGAN model and those generated by Reed et al. [25]. We also provide original image related to caption for reference. Figure 6 shows the attention weights calculated per timestep for an image caption. Our results show that RCA-GAN almost always focuses on color and shape mentioned in image caption which is very good for visual semantics. For more attention weights maps, see Appendix 1.
To evaluate CanvasGAN's generative capabilities, we calculated the inception score [26] using pre-trained ImageNet. We achieved an inception score [26] of 2.94 ± 0.18 using only 4 timesteps which is close to the score of 3.25, achieved by the state-of-the-art model [34]. This shows that RCA-GAN has a huge potential-with further optimizations and increased timesteps, it can perform much better.

Qualitative Analysis
Our results show that images generated by CanvasGAN are always relevant to text and never non-sensical as is the case we observed with Reed et al. [25]. Table  1 shows the images generated by both models for a certain text description.
We can see that CanvasGAN generates semantically relevant images almost all of the time, while Reed et al. [25] generates distorted relevant images most of the times, but fails badly on images with long captions. CanvasGAN's outputs that have been generated incrementally and then sharpened using CNN are usually better and expressive. Further improvements for quality can be made by generating more patches by increasing number of time-steps.

Conclusions
Text to image generation has become an important step towards models which better understand language and its corresponding visual semantics. Through this task we aim to create a model which can distinctly understand colors and objects in a visual sense and is able to produce coherent images to show it. There has been a lot of progress in this task and many innovative models have been proposed but the task is far from being solved yet. With each step we move towards human understanding of text as visual semantics. In this paper, we propose a novel architecture for generating images from text incrementally like humans by focusing at a part of time at a particular incremental step. We use GAN based architecture using a RNN-CNN generator which incorporates attention and we name it CanvasGAN. We show how the model focuses on important words in text at each timestep and uses them to determine what patches to add to canvas. Finally, we compare our model with previous prominent work and show our generator's comparatively better results.