We recently worked with our partner Getty Images , a global stock photo agency, to explore image to image translation on their massive collection of photos and art. The ability to use Deep Learning to change the aesthetics of a stock image closer to what the customer is looking for could be game-changing for the industry. With recent advancements in Generative Adversarial Networks (GANs), specifically PIX2PIX image mapping and CycleGANs, such image translation is now possible.

The core idea behind a Generative Adversarial Network is to create two models and have them compete against each other in order to improve them both through competition. The first model can be considered a, which is trained to recognize a particular type of image. For example, a discriminator can be taught to recognize images of handwritten digits with training on the classic MNIST dataset . The discriminator’s goal is to accurately score any incoming images as either “Yes, this looks like a handwritten digit from the training dataset” or “No, this is not an image of a handwritten digit” for any other type of image.

For this project, we’ve focused on the application of GANs in image-related tasks, but it is worth mentioning that they are not limited to them (for example, this text application of GANs).

Learning, has described GANs as the most important idea in Machine Learning in the last 20 years

While Deep Convolutional Networks have greatly improved the ability for computers to see and understand images in recent years, the ability to generate or manipulate images into a different visual space was still prohibitively difficult. A significant breakthrough occurred, however, with the development of Generative Adversarial Networks (GANs). Yann LeCun, one of the fathers of Deep

The generator is successful if fake (generated) images are so good that discriminator can not distinguish those from real images. In other words, the discriminator’s output for fake images should be as close to 1 as possible. In TensorFlow terms, the generator would like to minimize:

The power of CycleGANs is in how they set up the loss function, and use the full cycle loss as an additional optimization target.

As recommended by the CycleGAN authors, instance normalization should be used in residual blocks and we noticed that indeed does improve quality. The discriminator part of the graph has consequentially similar architecture (though different convolutions, filters, and strides numbers). The output of the discriminator is a “decision” whether the given image is fake (produced by the generator) or from the real dataset.

All nonresidual convolutional layers are followed by batch normalization and ReLU nonlinearities with the exception of the output layer, which instead uses a scaled tanh to ensure that the output has pixels in the range [0, 255].

The training graph and main training routines are defined in cyclegan.py (for network architecture details refer to the original paper by Jun-Yan Zhu). The generator is defined in the “generator” function:

In this section, we will go more into implementation details depth assuming the reader has some familiarity with how to build CNNs in TensorFlow.

Run to_tfrecords.py to convert images in the directories above to a corresponding binary representation in tfrecord format. During training, images will be loaded in batches from tfrecords files using the Images class defined in image.py

The first step in training GANs is to load images. We use tfrecord format for a more effective image loading pipeline comparing to one-by-one image loading from disc. We assume that your images for CycleGANs training and testing are in the following folders:

For our training images, we decided to take advantage of the treasure trove of stock photos Getty Images maintains and created four different image groups over two different CycleGAN models. One CycleGAN model would learn to translate sunny beach photos to overcast beach photos while a second CycleGAN would learn to translate between daytime and sunset beach photos.

The original CycleGANs paper, “Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks” , was published by Jun-Yan Zhu, et al. The accompanying code was written in Torch and hosted on GitHub . However, for our Getty Images hackfest, we decided to implement a CycleGAN in TensorFlow which can be trained and hosted on Azure. We chose TensorFlow because there were existing CycleGAN implementations we could draw from. We also felt TensorBoard would help us visualize the complexity of the CycleGAN architecture. In addition, we had expertise in distributed training using Kubernetes and with the strict time-frame of a hackfest, we needed all the help we could get. Later we were able to focus on migrating the code to the Microsoft Cognitive Toolkit (see project repo ).

For example, if you have an English phrase and use an online English-to-Spanish translation system to translate the phrase into Spanish, you will get one result. When you then take that translated result and re-translate it through the complementary Spanish-to-English translator, you will now have a full cycle of translation. Comparing how similar the resulting English-Spanish-English translated phrase is to the original English input gives you an additional score that you can use to improve your entire translation system. This approach is the clever power that CycleGANs brings to image-to-image translations and how it enables better translations among non-paired image styles.

The way CycleGANs are able to learn such great translations without having explicit X/Y training images involves introducing the idea of a full translation cycle to determine how good the entire translation system is, thus improving both generators at the same time.

The key idea behind CycleGANs is that they can build upon the power of the PIX2PIX architecture, but allow you to point the model at two discrete, unpaired collections of images. For example, one collection of images, Group X, would be full of sunny beach photos while Group Y would be a collection of overcast beach photos. The CycleGAN model can learn to translate the images between these two aesthetics without the need to merge tightly correlated matches together into a single X/Y training image.

While PIX2PIX can produce truly magical results, the challenge is in training data. The two image spaces that you wanted to learn to translate between needed to be pre-formatted into a single X/Y image that held both tightly-correlated images. This could be time-consuming, infeasible, or even impossible based on what two image types you were trying to translate between (for instance, if you didn’t have one-to-one matches between the two image profiles). This is where the CycleGAN comes in.

This model was popularized with the famous PIX2PIX paper. A fantastic write up on PIX2PIX and how the generators and discriminators work at a lower level can be found in this article

Once this model architecture is properly trained, the GAN allows us to translate an image from one aesthetic profile to another. We can then take one type of picture and convert it to another, as long as our GAN has been properly trained on both types of images.

For example, if you have training data that presents two copies of a photo of a flower, one in black and white and the other copy in color, you can set up a discriminator for each side. One discriminator will learn to identify the black and white aesthetic, while the other one will learn to recognize color photos. The generators do not attempt to fool their target discriminators from scratch; instead, they begin by using an image from the opposite collection. In this example, Generator G attempts to fool the Color Discriminator Y by starting with a black and white photo and then manipulating it until it is colored successfully. Similarly, Generator F starts with a color image and learns how to desaturate it well enough to fool the black and white discriminator.

Once the power of GANs started to be understood and applied, machine learning experts began creating even more powerful solutions by fighting teams of GANs against each other. This method allows us to generate a certain type of image artificially, such as the handwritten digits in the example above. It also allows us to have a pair of discriminators that have learned two related types of images compete against a pair of generators working to fool them so that the GAN would learn how to manipulate images from one type into its counterpart type. We call this image-to-image translation .

The other party in this adversarial “wrestling” game is our generator — a model capable of counterfeiting images . The generator competes against this discriminator and generator’s sole task is to learn to create synthetic images that will eventually be good enough to fool the discriminator. The generator uses the discriminator’s score as its feedback mechanism for improving itself over time (and thus make images look more “realistic”).

Note: the “**” symbol above is the power operator in Python.

We need to capture cyclic loss: as we go from one generator back to the original space of images using another generator, the difference between the original image (where we started the cycle) and the cyclic image should be minimized.

Finally, the generator loss is the sum of these two terms:

Because cyclic loss is so important we want to multiply its effect. You can make a comparison to measuring the quality of a translator: if you translate a phrase from English to Spanish, then from Spanish to English and result is close to the original, then the translator is good. Similarly, with Cycle GANs, after doing a cycle of transformation the resulting image should be close to the original.

We used an L1_lambda constant for this multiplier (in the paper the value 10 was used).

Now the grand finale of the generator loss looks like:

Note: in the cyclegan.py you will see generator loss defined in one statement. We also experimented with soft labels for g_loss_G_disc and g_loss_F_disc logical parts; rather than requiring a strong “Yes” (meaning 1) from the discriminator we allowed “Quite sure” (0.95 for example).

Discriminator Loss

Training and Results

Building the CycleGAN model and importing data is a significant accomplishment. However, during the training phase, we needed to experiment with various hyperparameter configurations such as batch size, learning rate, and momentum. Since the purpose of this CycleGAN is to generate aesthetically appealing results, we also experimented with the training data itself. Would a higher number of lower quality images learn better than a smaller amount of more highly curated images?

To explore this question, we created a quick Python program that allowed us to load videos of beaches and capture screen grabs repeatedly to generate many training images very quickly. Here is the code:

import numpy as np import cv2 SPEED = 25 cap = cv2.VideoCapture('./source/beach/beachvideo.mp4') capture_run = np.random.randint(1000) totalFrames = 0 def capture_square(frame, square_size, capture_run, totalFrames): startX = np.random.randint( int((w-square_size)) ) startY = np.random.randint( int((h-square_size)) ) capture = frame[startY:startY+square_size, startX:startX+square_size] cv2.imwrite("./captures/cap{0}_f{1}.jpg".format(capture_run, totalFrames), capture, [int(cv2.IMWRITE_JPEG_QUALITY), 90]) totalFrames = totalFrames+1 while(cap.isOpened()): ret, frame = cap.read() h, w, c = frame.shape cv2.imshow('frame', frame) if (h < 600) or (w < 600): print("width or height of video must be at least 600px") break key = cv2.waitKey(SPEED) # CAPTURE FRAMES WHEN SPACE IS PRESSED if key == 32: square_size = 600 capture_square(frame, square_size, capture_run, totalFrames) totalFrames = totalFrames + 1 # QUIT ON 'Q' if key == ord('q'): break cap.release() cv2.destroyAllWindows() For our task, this approach didn’t deliver a better quality result than the superior images we had access to from Getty Images. However, it provides an interesting avenue for additional CycleGAN projects and we may return to it as a training data source in the future. Once we had a stable model and good set of training data image sets, we began running various training sessions overnight leveraging both local machines and Azure VM instances to compare various runs concurrently. During the run, we would periodically check with TensorBoard to see how well our model was doing. TensorBoard proved invaluable for monitoring our model. However, it seemed to struggle with graphing the model itself, so CycleGANs might be a bit much for it: We set up TensorBoard variables that allowed us to monitor not only our loss functions, learning rate decay, and other important parameters but also provided us with glimpses into the images being generated by our model. During training these varied from bizarre to strangely beautiful before settling down to results that looked closer to our goal. Here’s an example of an exotic sunset from one of our runs: Even if we could never generate photorealistic results, we could at least generate science fiction book covers! Here is a cherry-picked sample of our results at the end of the hackfest, showing that in many cases we approached our goal. While some of the results were very impressive, there are further optimizations we would like to explore in the future. Additional GANs can be supplemented to help clean up various artifacts created in the process, as well as to allow us to have better resolution while improving the output size. There are also traditional post-processing techniques that can be applied which don’t require deep learning and can improve the aesthetic quality and color balance of the images, which would be valuable if this approach eventually moves toward production quality.

Conclusion

Generative Adversarial Networks are at the forefront of Deep Learning research right now, and CycleGANs are one of the newest methods. We love that our team gets the chance to explore the possibilities of this new technology with partners committed to innovation. The impact of this technology on the creative sector is going to be immense, so we’re trying to ensure that we use approaches that augment creative abilities rather than replace them.

Generative Networks such as the one above are almost always shown operating in the visual realm. One reason is the Convolutional/Deconvolutional nature of the networks, but another large contributor is the striking visual impact of the resulting images. However, we see future generative networks operating in other domains (audio, text, gene sequences, etc.) to help us generate new data for training or, with guidance, creating new works. How can we design loss functions for judging the “aesthetics” of these results? Will other generator and discriminator network topologies produce better results in different domains? There are many open questions, and it’s an exciting time to be working in this field.

If you try out our code on GitHub, please let us know and share your results! If you liked this work, please share it — the more people exploring these emerging questions, the faster we’ll solve them which will benefit us all.

Further Reading

Finally, some useful links for further reading: