{'fraction_non_alphanumeric': 0.03281171125694094, 'fraction_numerical': 0.006646474844354703, 'mean_word_length': 4.452522935779816, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 1, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Generating Embroidery Patterns Using Image-to-Image Translation

In many scenarios in computer vision, machine learning, and computer graphics, there is a requirement to learn the mapping from an image of one domain to an image of another domain, called Image-to-image translation. For example, style transfer, object transfiguration, visually altering the appearance of weather conditions in an image, changing the appearance of a day image into a night image or vice versa, photo enhancement, to name a few. In this paper, we propose two machine learning techniques to solve the embroidery image-to-image translation. Our goal is to generate a preview image which looks similar to an embroidered image, from a user-uploaded image. Our techniques are modifications of two existing techniques, neural style transfer, and cycle-consistent generative-adversarial network. Neural style transfer renders the semantic content of an image from one domain in the style of a different image in another domain, whereas a cycle-consistent generative adversarial network learns the mapping from an input image to output image without any paired training data, and also learn a loss function to train this mapping. Furthermore, the techniques we propose are independent of any embroidery attributes, such as elevation of the image, light-source, start, and endpoints of a stitch, type of stitch used, fabric type, etc. Given the user image, our techniques can generate a preview image which looks similar to an embroidered image. We train and test our propose techniques on an embroidery dataset which consist of simple 2D images. To do so, we prepare an unpaired embroidery dataset with more than 8000 user-uploaded images along with embroidered images. Empirical results show that these techniques successfully generate an approximate preview of an embroidered version of a user image, which can help users in decision making.


INTRODUCTION
Customizable fashion is on the rise in fashion industry. Embroidery customization is considered to be the most popular type of customization in comparison to the counterparts, screenprint and digital print. For this work we partnered with a firm that personalize apparels with custom embroidery. A customer uploads an image to be embroidered. Having a real-time approximate preview of their design is a significant factor in the decision making of the client. Based on our industry research and knowledge, we did not find any work which simulates an approximate embroidered version of a user-uploaded image automatically to facilitate the customization experience. The image-to-image translation is one of the techniques that can help us in providing an approximate embroidered version of an image.
The image-to-image translation is a process of translating one possible representation of an image to another, while keeping the original structure and semantics of the image (such as a change in style, design alteration, colorization, and others) intact. Alterntively stated, the objective is to learn the mapping from an image of one domain to an image of another domain Before the introduction of this technique, to achieve a similar task a combination of different image processing techniques were required [12] like image quilting [3], image analogies [8], image denoising [1], depth prediction [4], semantic labeling [4], surface normal estimation [4], image colorization [17] and many others [2,9,13,16].  In this work, we propose to solve embroidery image-to-image translation, where a 2D image is translated to an embroidered version of the image without altering the high level semantic content. Figure (1), shows an example of an embroidery image-to-image translation. We propose to solve this problem by modification of two existing techniques, neural style transfer [5] and cycle-consistent generative adversarial networks (CycleGAN) [19]. Both of these techniques have shown remarkable results in solving different image-to-image translation problems. Gatys et al. [5] introduced neural style transfer to solve an image-to-image translation problem for art. They used a convolutional neural network (CNN) [14] to transfer the style of a painting on a photograph. Since then, many different researchers have proposed different modifications and advancements to the original algorithm, and all of them have achieved groundbreaking results. Our work is directly related to the line of work initiated by Gatys. Another technique that we propose to solve the embroidery image-to-image translation problem is using cycle-consistent generative adversarial networks. Goodfellow et al. [6] introduced generative adversarial networks (GAN). They proposed a framework which uses an adversarial process for estimating the generative model. Different architectures and modification of generative adversarial networks are used to solve image-to-image translation problems. Isola et al. [12] as a general-purpose solution to image-to-image translation problem proposed conditional adversarial networks. They have tested this framework on a paired set of images, and the results were quite remarkable. Zhu et al. [19] used an unpaired set of images and cycle-consistent adversarial network to solve the image-to-image translation problem. The use of an unpaired set of images has given this framework an advantage to be widely used in different applications. Though there are many different approaches to solving an image-to-image translation problem, our work is closely related to the work of Zhu et al. [19] because of the freedom of using unpaired images.

BASELINE TECHNIQUES
Image-to-image translation is one of the prominent problem in computer vision and graphics. Many different approaches have been propose by different researchers to solve different applications of image-to-image translation. For the Manuscript submitted to ACM embroidery image-to-image translation, we use to existing machine learning techniques specially design to solve image-to-image translation. We modify these techniques for our specific application of embroidery.

Neural Style Transfer
Neural style transfer is a technique proposed by Gatys et al. [5], where the objective is to transfer the style of one image onto another image without changing its high-level semantic content. The main approach of neural style transfer is to jointly minimize the distance of the style representation and the content representation, which is learned in different layers of the convolution neural network (CNN). The authors of [5], advocate that we can distinguish layers that are responsible for representing the style and the ones responsible for representing the content of an image and we can separate these layers to work independently on the content and style and therefore can achieve the image-to-image translation. Neural style transfer defines a loss function that tries to minimise the differences between a content image, a style image and a generated image.
Definition 1 (Feature Map). Let d l is the number of filters in the l th layer of the CNN.û l andv l be the height and weight of the feature map at layer l, p l represents the size of the feature map at layer l. p l is the product ofû l ×v l . Let z be any given image, we define the feature map as: where H l i j is the activation of the i t h filter at position j in layer l.
Feature maps are a good representation of the features of input image. They capture spatial information of an image without containing the style information. The feature maps helps in defining the loss function of neural style transfer.
Definition 2 (Gram Matrix). Given an image z, we define gram matrix as the dot product of the feature map defined above in (1).
Flattened feature vectors from a convolutional feature map representing features of the input space, and their dot product give us the information about the style or texture of an image and this dot product is referred as gram matrix.

Cycle-Consistent Generative Adversarial Network
The second method we propose is inspired by cycle-consistent adversarial network (CycleGAN) introduced by Zhu et al. [19], which is an extension of generative adversarial network (GAN) [6]. CycleGAN has achieved substantial success in the image-to-image translation problem domain because it uses an unpaired image dataset. However, Isola et al. [12] have already introduced a conditional adversarial network which provides promising results, but it requires a paired image dataset and creating paired image dataset for a new task like ours is a tedious process.

Split Style Transfer
Split style transfer is a modification to neural style transfer [5] technique, discussed in previous section (2). During our experiments, neural style transfer was manage to generate some decent results for a single color image but for multi-color images, the results are a bit different from the approximate preview. When an image is actually embroidered on an apparel, every distinct color in an image is embroidered using a different type of stitch pattern or a different orientation of a similar stitch pattern to distinguish surfaces. Generating approximate embroidery version using neural style transfer has the restriction of using only single style image. To achieve more realistic approximate preview, we propose to split a multi-color 2D image into sub-images of distinct colors. Then apply different style transfer on each of the sub-image using a different type of embroidery patterns and combine the subimages at the end. The benefit of split style transfer is that it ensures all distinct colors in an image have different embroidery pattern and the final combined generated image is a more realistic approximate preview. We also made some modifications in the loss function of neural style transfer. Figure (2) shows the split style transfer method we propose to translate embroidery on a given image.

Problem Formulation.
Let u ∈ Z + be the height, v ∈ Z + be the width and c ∈ Z + be the color channel of each image. For r = {1, · · · , e}, let x be the content image, such that x ∈ N u×v×c and Y = {y 1 , · · · , y e } represents the set of style image (embroidery images in our case), such that y i ∈ N u×v×c similar to neural style transfer. In split style transfer, we first split our content image x, into a set of images of distinct color where e is the total number of distinct colors in an image. Then, every sub image x ′ r is styled using a different style image, y r ∈ Y . The final styled images,Ŷ ′ = {ŷ 1 , · · ·ŷ e } are then combined to one imageŷ at the end after style transfer is performed separately. We define the content L l c , style L l s , and total loss L l t of split style transfer for a layer l as : Content Loss At any layer l, content loss is defined as the mean squared difference of the feature map [1] of the generated image and the feature map of the original content image.
Style Loss At any layer l, style loss is defined as the mean squared difference of the feature map [2] of the generated image and the feature map of the original content image.
Total Loss At any layer l, the total loss of split style transfer is the combination of the content loss and the style loss.
The hyperparameters α and β are the weights of content and style loss, respectively. By controlling α and β we can control the amount of content and style present in the styled image.

Embroidery Cycle Consistent GAN (EmbGAN)
The second technique we propose is inspired by the architecture of CycleGAN [19]. We propose some modifications to the architecture of CycleGAN for embroidery translation problem. Instead of using just instance normalization [15] like the original CycleGAN, we have added spectral normalization [11] similar to [18] to improve the quality of the generated image. We also took inspiration from [10], which uses a regularized embedded channel for both input and output of the generators. The embedded channel helps the generator memorize the important structures necessary for the reconstruction of the image. As the embedded channel is regularized, it eliminates the possibility of the generator learning the entire image and hence not depreciating the cycle-consistency of CycleGAN. Combining these two modifications to the original architecture of CycleGAN definitely help a lot in improving the quality of the generated images in comparison to original CycleGAN, which we have shown in the following section. Let us mathematically formulate the embroidery translation problem. Each image from the input domain has multiple visual attributes like color, shape, size, luminance, texture, and many others. In the CycleGAN [19] paper, the author has mentioned that there is a primal interconnection between the input image and the generated image. In the embroidery translation problem, the input image and the generated image have many similarities. Ideally, almost everything except the texture of the images is similar. We will introduce the modified loss function in the following section.

Problem Formulation.
Let the dataset consists of user images and embroidered images, both of the same dimension.
Let u ∈ Z + be the height, v ∈ Z + be the width and c ∈ Z + be the color channel of each image. Suppose n be the total number of images in the dataset and N = {1, 2, · · · , n}. Let X = {x 1 , x 2 , · · · , x n } be the set of user images, where x i ∈ N u×v×c for i ∈ N . Let Y = {y 1 , y 2 , · · · , y n } be the set of embroidered images, where y i ∈ N u×v×c for i ∈ N . For i ∈ {1, 2}, let G i be a generator and D i be a discriminator, they consist of neural networks with parameters θ G i and θ D i , respectively, θ G i and θ D i are the set of weights and biases of neural networks. Let Q × Q be the dimension of the noise vector z i ∈ Z . For i ∈ {1, 2}, we define differentiable function function f G i : N u×v×c × R Q ×Q → N u×v×c . We denote the set of images generated by the generator asŶ = {ŷ 1 ,ŷ 2 , · · · ,ŷ n }, whereŷ k ∈ N u×v×c for k ∈ N . Similarly, . We use cross entropy to compute the similarity measure between the probability of occurrence of labels based on the true data points from the training dataset and the probability of occurrence of labels from the generated data points. Let P = {P 1 , P 2 , . . . , P n } be the visual attributes of an image ∈ {x, y} and, Q = {Q 1 , Q 2 , . . . , Q n } be the values of these attributes. Our goal is to find two optimized mappings f G * 1 : X → Y and f G * 2 : Y → X . For ∀x ∈ X , the generated imageŷ = f G 1 (θ G 1 , x), the values Q x and Q y of all attributes P should be kept same except the embroidery attribute. That means the optimal mapping f G * 1 transferred only the embroidery attribute without destroying any other attribute [18].

Loss Function
The loss function of our model is different from our baseline CycleGAN [19]. To achieve better quality results, we have taken inspirations from different researchers and modified the architecture of CycleGAN to achieve the best results for embroidery translation problem. The loss function for our model is as follows: The adversarial loss and the cyclic loss is similar to CycleGAN. We have also introduced identity loss, and embedded loss.
We have used a L1 norm loss function for the additional channel to the input image which is defined as the absolute difference between the target value and the estimated value. For i = {1, 2, · · · , n}, let S be the difference, t i be the target value and p i be the estimated value, we define L1-norm as: Adversarial Loss: Each generator in the model tries to minimize its loss, whereas each discriminator in the model tries to maximize its loss. The adversarial loss focuses on the fact that the data distribution between the output domain and generated domain measures up with each other. Let the set of input images be X = {x 1 , x 2 , . . . , x n } and the set of output images be Y = {y 1 , y 2 , . . . , y n }. Suppose L j (θ G j , θ D j ) be the adversarial loss for the generative adversarial network j, for j ∈ {1, 2} and x i ∈ X , y i ∈ Y , z i ∈ Z for all i ∈ {1, 2, . . . , N }. We define adversarial loss as follows: for all θ D 1 and θ G 1 , and for all θ D 2 and θ G 2 . The goal here is to find the optimal value of θ * G j and θ * D j , ∀j. We can state the objective as follows: Cycle Consistency Loss: CycleGAN introduces a cyclic approach to converting the generated image back to its subsequent image from the original domain. The loss incurred during the process is addressed as Cycle Consistency Loss. Since we have two generators and discriminator pairs, we also have two cyclic consistency loss, namely: (1) Forward Consistency Loss: An image x i ∈ X domain is fed to the generator f G 1 (θ G 1 x i , y i ) which generates y ′ i and then this is again fed to generator Manuscript submitted to ACM consistency loss is defined as: (2) Backward Consistency Loss : Similarly to forward consistency loss, we define backward consistency loss as follows: Identity Loss: Identity loss is an optional loss in the original CycleGAN paper, but during our training process, we learned that adding an identity loss helps in training the network efficiently similar to [18]. Identity loss helps the generator identify whether the image is from the input domain or the output domain and hence reducing the chances of making it just a mapping algorithm. We define identity loss as: Embedding Loss: We have added an embedded channel to the input and output of both the generators to assist in the process of learning the structure of the input image as shown in the figure (3), is able to learn a separate channel, which can be thought of as an embedding, that helps in the reconstruction of the input. The idea is to encourage the generator to generate a properly translated image and an embedding of the changes that were made are required to reconstruct the input. An L1 distance is used with the embedding loss, similar to [19]. The reason of using an L1 distance instead of L2 is that L1 produces less blurring images [12]. Also, since a L2-norm squares the error (increasing by a lot if error > 1), the model will see a much larger error ( e vs e 2 ) than the L1-norm, so the model is much more sensitive to this example, and adjusts the model to minimize this error. If this example is an outlier, the model will be adjusted to minimize this Our goal is to obtain optimal parameters for the sum of all losses. Our final loss is defined as: Manuscript submitted to ACM

Dataset
We have prepared a dataset of 2D images for embroidery image-to-image translation. The image used in the dataset are simple two dimensional images which can be embroidered have fairly less complex semantic content. A photographic image of a skyline or an image with complex semantic content would be feasible for embroidery and hence is not used in preparing the dataset. Most of the images are simple flags, texts, logs or other simple structural images as shown in 4. We can broadly divide the dataset into two categories, one of which is the textual image which consists of word(s) that a user has uploaded to be customized on their apparel and the other category are any non-textual images. The other division of the dataset is based on the color of the images, we have multi-color images, single-color images and gray-scale images. The total number of images in the dataset is 8668, out of which 4643 are user uploaded images, and 4025 are manually embroidered version of these images.

Perceptual Studies
The absolute best metric for evaluating the results of any image-to-image translation problem or any other image synthesization task in the field of computer graphics and computer vision is to check how compelling the results are to a human observer. Similar to the baseline architecture, we have run perceptual studies of "real vs. fake" on Amazon Mechanical Turk, the protocol used is identical to [[12], [19], [17]]. We have also run a comparison perceptual studies comparing the results of baseline architecture and our modified architecture. For the "real vs. fake" perceptual studies, the turkers were presented with a series of pair of images, where one of the images is ground truth which is considered "real, " and the other image is generated by our algorithm which is considered as "fake" in our study. The pair of images appeared for 1 second, and after the images disappeared, the turkers were given an unlimited amount of time to decide which one was real or fake. The Table 2, displays the result of the first perceptual study. We can see that the images generated from EmbGAN were manage to fool the turkers 4 times more than the images generated from CycleGAN.
For the comparative perceptual study, we have increased the time to 2 seconds, so that users will have more time to make the comparison. The  [17], 10% of the trials pitted the ground truth with the images generated from initial epochs and the turkers were able to identify the generated image as fake 94% of the time, indicating that the turkers understand the task in hand. To ensure that there is no bias and all the algorithms were tested in the similar environment, we ensure that all the experiments were carried out at the same time of the day and all the sessions were independent and identically distributed to the turkers simultaneously.     Figure 5 shows the images generated by EmbGAN. We have compare them with the images generated by the original CycleGAN. Also, we will compare both these generated images with the images digitized by proprietary software which is considered to be the ground truth and see how our well both methods perform in generating an approximate embroidered version of an image. Each subfigure has four versions. One of them is the user-uploaded input image.

EmbGAN Results
Second, is the manually digitized embroidered version of the image used as ground truth. Third, is the result generated by original CycleGAN. Lastly, the result generated by EmbGAN.
Manuscript submitted to ACM

CONCLUSION
In this work, we propose two techniques to solve our embroidery image-to-image translation problem. The techniques we propose are a modification of two existing machine learning techniques which are popular in producing good results for any given image-to-image translation task, neural style transfer and cycle-consistent generative adversarial network.
We have done an embroidery image-to-image translation to generate an approximate real-time preview for a customer who wants to have customized embroidery on their apparel. The approximate preview of the final embroidered version of their uploaded two-dimensional image will help the customer in the decision-making process and hence will help in reducing the amount of product returned because of customer's dissatisfaction. The results from both the techniques were very satisfactory. We used perceptual studies to compare the results with the baseline architectures and the results from the split style transfer were qualitatively better than neural style transfer, and the generated images from EmbGAN were qualitatively better than CycleGAN. We also compared the generated images from both the techniques which conclude that the EmbGAN generates the best quality embroidered image for any given image. We have used the digitized embroidered version of images using industry software as the baseline for comparison. After our final comparison between the generated images, we find out that, the style transfer technique is not the best approach for solving embroidery image-to-image translation. The images generated by style transfer feel less realistic visually, and hence, would not be the best approximate preview of the embroidered version of a user-uploaded image.