{'fraction_non_alphanumeric': 0.03285721760058599, 'fraction_numerical': 0.013760267880500183, 'mean_word_length': 4.913985148514851, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Self-Supervised Image-to-Text and Text-to-Image Synthesis

A comprehensive understanding of vision and language and their interrelation are crucial to realize the underlying similarities and differences between these modalities and to learn more generalized, meaningful representations. In recent years, most of the works related to Text-to-Image synthesis and Image-to-Text generation, focused on supervised generative deep architectures to solve the problems, where very little interest was placed on learning the similarities between the embedding spaces across modalities. In this paper, we propose a novel self-supervised deep learning based approach towards learning the cross-modal embedding spaces; for both image to text and text to image generations. In our approach, we first obtain dense vector representations of images using StackGAN-based autoencoder model and also dense vector representations on sentence-level utilizing LSTM based text-autoencoder; then we study the mapping from embedding space of one modality to embedding space of the other modality utilizing GAN and maximum mean discrepancy based generative networks. We, also demonstrate that our model learns to generate textual description from image data as well as images from textual data both qualitatively and quantitatively.


Introduction
The web contains a multitude of images; most of the content images are unannotated. Describing the content of an image automatically using proper natural languages is a vital task, relevant to the area of both Natural Language Processing and Computer Vision, the impact of which could be significant, for example, it will help visually impaired people to have a better understanding of the content of images on the web using existing text-to-speech systems. It has many other important applications such as semantic visual search [6], or visual intelligence in chatbots [4]. The reverse problem is the generation of realistic images from human-written descriptions. Although notable progress has been made in generating visually realistic images, those are still far from this goal. The Generative Adversarial Network (GAN) [7] based models showed promising results by generating many probable visual representations of a given textual description [22], [23], [31].
However, one major problem in both text to image synthesis and image to text generation is that recent state-of-the-art deep models are supervised learning-based that require annotated data. Most of the available web data is unlabeled and requires expensive human annotations. Similar problems have been addressed in machine translation where unsupervised machine translation [13] attains astounding results even in cases of low-resource languages and also performs reasonably well in the case of distant languages [14]. Motivated by the recent success in machine translation, we aim to investigate whether it is possible to learn the cross-modal embedding spaces between text and visual data in a self-supervised fashion.
The major contributions of this paper can be summarized as follows: i. To the best of our knowledge, the current work is the first attempt in developing a self-supervised or unsupervised way of generating images from texts and texts from images. ii. Firstly a generative deep image autoencoder setup is developed for generating compelling image vector semantic space as per the reconstructions are concerned. Secondly, a recurrent neural network based autoencoder is developed which embeds sentences in textual semantic space. Finally, the mapping between cross-modal semantic spaces is established using both GANs and Maximum Mean Discrepancy (MMD) [32] based generative networks [16] which utilize the adversarial kernel learning technique. iii. Results on Caltech-UCSD Birds-200-2011 and Oxford-102 datasets illustrate that self-supervised Image-to-Text and Text-to-Image generation techniques can generate one modality given the other modality.

Related Works
Caption generation using neural networks was first proposed in [12] which used a multimodal log-bi-linear model. In the paper [27], the authors used deep convolution neural networks (CNN) as an image encoder while RNN as a decoder that generates captions. The authors of [29], introduced an attention-based approach for image caption generation which uses convolutional networks for image feature extraction and attention-based RNN as decoder for caption generation. A deep visual semantic captioning model is proposed in [26] that takes the advantage of external sources and exploits the semantic information to generate captions and can describe the objects not present in image-caption datasets. A caption generation model that is based on the dependencies between caption words, image regions, and RNN language model has been proposed in [20]. The authors [15] showed in their work, that the meaningful style representation which is not well-described in text, but present in the images can be learned in an unsupervised manner. There have been several works on the generation of text for a given image, but there are also the ones that generate an image for a given text. Generative Adversarial Networks(GANs) [7] are proven to be useful in generating photo-realistic images [22] [30]. For text to image task, StackGAN-v2 [31] employs a tree-like structure comprising of multiple generators and discriminators. It generates images at multiple scales at different branches of the tree.
Our work is as closely related to machine translation as it is related to imagecaption generation. Recent work in unsupervised machine translation [13] uses a shared encoder-decoder architecture and maps the sentences from two different languages into common latent space. A work, related to unsupervised learning [16] that employs maximum mean discrepancy (MMD) [32] based on two-sample tests, minimizes the MMD distance between the two distributions to obtain the mapping. Building on the ideas and advances in these previous related works, we propose a novel architecture for cross-modal generations that utilizes GAN and MMD GANs [16] for unsupervised learning.

Methods
Our proposed self-supervised framework comprises of three sub-modules: 1) A deep StackGAN [31] based image autoencoder 2) LSTM-based Sequence-to-Sequence text autoencoder model 3) A cross-modal embedding space mapper that maps embedding from one semantic space to the other. We make our code publicly available 1 .

StackGAN based Image Autoencoder
Our proposed model for image autoencoder is StackGAN based. StackGAN-v2 [31] takes a text embedding vector as input and produces images of increasing resolution at different branches of the network. We modified the architecture to make it an encoder-decoder based network as shown in Figure 1, where an encoder takes an image, extracts different features at different layers of the deep network to obtain an image embedding; this image embedding subsequently is fed as input to the original conditional StackGAN decoder model which reconstructs the image at the output of the conditional StackGAN thus working as an image autoencoder. For the encoder part of the autoencoder, we have used the pretrained ResNet-50 [9]. We have redefined the last layer of ResNet-50 to obtain 1024 dimensional image embedding.
On the decoder side, the generated image is conditioned on the imageembedding vector. As described in Figure 1, firstly the ResNet encoder encodes image i into image embedding ψ i . Then Conditional Augmentation technique [31] is applied on image embedding ψ i to produce continuous latent space, yielding condition variableĉ ∼ N (µ(ψ i ), σ(ψ i )). The following Kullback-Leibler (KL) divergence loss term is optimized during generator training which acts as regularization that ensures the smoothness of the conditioning variable distribution: For each generator, G i , at different branches of the tree, the hidden feature, The unconditional loss dictates whether the image at discriminator input is fake or real; the conditional loss decides whether the generated image corresponds to the respective conditioning variable (Figure 1). During the training of the generators, the following loss function is being optimized: The final loss function for training the generators is given by

LSTM-based Sequence-to-Sequence Text Autoencoder Model
We have used a single-layer LSTM encoder and decoder in our text autoencoder as a sequence-to-sequence model. The encoder is bidirectional LSTM with hidden dimension 50, while the decoder is unidirectional with hidden size 100. The text encoder takes the word embedding vectors at each time step as inputs and generates corresponding hidden vectors. In this paper, we have considered all the hidden vectors to obtain the sentence embedding by max-pooling over all the hidden vectors. This latent vector is used to initialize the decoder LSTM network which regenerates the text description at the output.

Cross-modal Embedding Space Mapping Networks
Cross-modal embedding space mapping networks map from one modality embedding space to the other modality embedding space. The images and texts need not be paired, as both the networks minimize the distance between the two semantic distributions. We have employed two different architectures: One is GAN-based, and the other utilizes MMD-based generative networks.

GAN-based Cross-modal Embedding Space Mapping Networks
In this architecture, simple GAN models are used, for both image-to-text and text-to-image conversions. The generator translates one modality embedding into the other modality embedding, and the discriminator determines whether two embedding distributions match or not.

MMD GAN-based Cross-modal Embedding Space Mapping Networks
Maximum Mean Discrepancy (MMD) is a distance measure on the embedding probability space in Reproducing Kernel Hilbert Space (RKHS) [2]. Given two probability distributions P and Q and a continuous positive definite realvalued kernel k (H to be corresponding RKHS); the corresponding kernel means be defined as µ p = ∫ k(., x)dP (x) and µ q = ∫ k(., y)dQ(y), the distance M M D(P, Q) = µ p − µ q measures the similarity between the two distributions, is known as MMD [8]. In this paper, we propose a mapping network based on MMD-GAN [16] that trains the generator g θ to minimize MMD distance between two distributions, i.e. min θ M k (P X , P θ ), hence passes the hypothesis test.
Here real data x ∼ P X , generator distribution g θ (z) ∼ P θ and P Z is the base distribution such that z ∼ P Z and M k is the square of MMD distances :

Experiment
We have compared our MMD-based Cross-modal mapping network with a GANbased mapping network, in which the generator generates a mapping from Fig. 2: End-to-end Networks: Self-supervised Image-to-Text Generator and Selfsupervised Text-to-Image Generator one embedding space to another, and the discriminator is a simple network to distinguish real and fake embeddings.
We first trained our image autoencoder (Section 3.1) for 600 epochs. We used pre-trained ResNet-50 encoder [9], the last layer of ResNet is re-defined as a fully connected layer with output dimension 1024, which is also our image vector dimension. During the training of the entire autoencoder, the weight of the ResNet, except the last layer, is kept fixed, while the parameters of the remaining network, are set as trainable. As the ResNet model is pre-trained on Imagenet dataset [5], we resized images to 224 × 224 and normalized images of our dataset using the mean and standard deviation of the Imagenet dataset.
Next, we pre-train our LSTM-based text autoencoder (Section 3.2) on 1 million sentences extracted from One Billion Word Benchmark dataset [3]. For initialization of our text autoencoder model, we have used top 50k 100-dimensional Glove embeddings [21] along with Glove embeddings of other unique words in caption datasets. The model was pre-trained for 50 epochs; this pre-trained model worked as an initializer; the model was then trained on all the captions of train sets. We have used Cross-Entropy between the generated and target sentences as the loss function.
After we trained both our autoencoders, the GAN-based Mapping Networks (Section 3.3) and the MMD-GAN Mapping Networks (Section 3.3) are trained separately using the unpaired image and text data, for both the image-to-text and the text-to-image conversions (Figure 2). The weights of the trained autoencoders are kept fixed while we train the mapping networks. In image-to-text setup, first, the image encoder encodes an input image into image embedding, which is then used as input to image-to-text embedding mapping networks, that generate corresponding text embedding, the batch of generated text embeddings are used as fake text embeddings while the text embeddings obtained directly from text encoder act as true text embeddings; fake embeddings and true embeddings are then used to train the generative networks. Likewise, the text-to-image setup is trained.

Baselines:
We have compared our StackGAN image autoencoder with the following baselines: ResNet autoencoder : In this architecture we fine tuned ResNet-50 as encoder and the decoder comprises of several layers of upsampling followed by deconvolution layer as used in [30]. The latent dimension is set to 1024.
Com-Rec autoencoder : This model is based on [11] which is completely CNN based. We added linear layers in the bottleneck to obtain dense vector (dimension 1024) latent space.
Compressive autoencoder : This architecture is based on model as discussed in the paper [25] which comprises of an encoder, a decoder and a probabilistic model. We have added a linear layer in the bottleneck to obtain dense 1024-dimensional vector embeddings.
For Mapping Networks, as this is a novel self-supervised approach with no existing baselines available for comparisons, we define the following baselines to evaluate the performance: GAN-based Cross-modal Mapping Network : In this architecture simple GAN models are used for both the image-to-text and text-to-image conversions. The generator translates one modality embedding into the other modality embedding and the discriminator determines whether two embedding distributions match or not.
MMD GAN-based Cross-modal Mapping Network : This is statistics based network that uses MMD [8] as objective function (Section 3.3).
Dataset: For the task, we have used publicly available standard Caltech-UCSD Birds-200-2011 Dataset [28] and Oxford-102 Flower Dataset. The CUB-200-2011 contains a total of 11,788 images of 200 bird species, with multiple annotations per image. All attributes are visual; mostly related to the shape, color, or pattern of the particular part. We split the dataset into train set (8,855 images, 88540 captions, 150 classes) and test set (2,933 images, 29330 captions, 50 classes) such that the respective classes do not overlap. The object-image size ratio in the dataset is less than 0.5 for 80% of the images, so we pre-processed the images to maintain the size ratio at values greater than 0.75.
The Oxford-102 dataset contains a total of 8,189 images of 102 categories of flowers. The dataset is split into train set (7,034 images, 70340 captions, 82 classes) and test set (1,155 images, 11,550 captions, 20 classes).
We have used four standard metrics for the performance evaluation of our LSTM-based text autoencoder. These metrics are BLEU [19], METEOR [1], ROUGEL [17], CIDEr [18] to quantify the similarity between the ground reference sentence and autoencoder generated sentence. The official MSCOCO caption evaluation scripts 2 are used for evaluation.
The following evaluation metrics are used to assess the performance of our mapping networks: 1. Human Score: We conducted user studies for evaluation of our end-toend text-to-image and image-to-text systems. We sampled 2000 samples for each of the text-to-image and image-to-text cases, assigned 5 different users (excluding any of the authors) to score the results. test classes and the Oxford-102 dataset has 82 and 20 train and test classes, respectively. Classes define different species for birds. Birds in the same class are from the same species, have identical features and descriptions. It is logical to expect that when one modality embedding maps into the other modality embedding, they are mapped to the same class. We first obtain the embeddings of one modality (say modality A) call it true embeddings in modality A. Then we obtain the embeddings of other modality (say modality B) and use them to feed into the "Modality B-To-Modality A" mapping network to obtain the fake embeddings in modality A. Then the cosine similarity between the true embeddings and fake embeddings are computed and we do an argmax to determine the class of fake embedding based on the highest cosine similarity scores. Then we calculate the class accuracy based on class labels of the true embeddings and predicted class labels of the fake embeddings.

Result and Analysis
The Inception score and FID of the StackGAN autoencoder model have been reported and compared with the baselines in Table 1 The sample outputs of the End-To-End Image-To-Text synthesis are shown in Figure 4; Figure 3 depicts the sample outputs for Text-to-Image synthesis network. Evaluation metric scores are reported in Table 2 for GAN-based end-toend networks and MMD-based networks.
The human score indicates that there exist some correlations between the two modalities of Image-to-Text and Text-to-Image synthesis systems. However, Class Accuracy score is indicative of a low semantic correlation between the two modalities for both GAN based and MMD based Image-to-Text and Text-to-Image systems. The scores at Table 2 imply similar performances by both the GAN-based system and the MMD-based system.

Error Analysis
A thorough analysis revealed scenarios for possible reasons for errors. The strength of the end-to-end model is dependant on strength of the individual components, as models are trained separately; so error in each component has a cumulative impact on the end-to-end network. The autoencoders are trained separately without sharing weights or information, as a result, the latent spaces are disjoint, which further makes it harder for the mapping networks to align learned embedding spaces on the semantic level.

Conclusion
In this paper, we proposed a novel self-supervised approach that performs the cross-modal translation from image to text and text to image. We showed that our Text-to-Image and Image-to-Text synthesis networks learn to map the semantic space of one modality to the semantic space of the other modality in an unsupervised fashion. However, we figure out while learning the mapping, the semantic correlation across the modalities is low. Though the current end-to-end network depicts low cross-modal semantic alignments, as part of the future work, these learned network weights can be used as initialization for the synthesis networks and the entire network can be fine-tuned till the embeddings of the two modalities semantically align in the latent space.