6 CycleGAN: Converting Blond Hair to Black Hair
This chapter introduces CycleGAN as a practical solution for unpaired image-to-image translation, using the task of converting blond hair to black hair and vice versa as a running example. Unlike earlier GAN variants that simply learn to generate realistic images, CycleGAN learns mappings between two visual domains without requiring paired examples. The key idea is cycle consistency: an image translated to the other domain should be able to return to its original form with minimal change. This capability enables a wide range of applications, from hair color editing and adding/removing eyeglasses to style transfer and cross-modality medical imaging.
The model architecture consists of two generators and two discriminators that translate images in both directions between domains. Training uses unpaired datasets and optimizes a composite objective: adversarial losses encourage outputs that look real to their target-domain discriminators, while cycle consistency loss (pixel-wise L1) penalizes discrepancies between original images and their round-trip reconstructions. The chapter builds a general PyTorch pipeline for two-domain datasets, demonstrating data preparation with resizing, normalization to the [-1, 1] range, and simple yet effective augmentations like horizontal flips. For the hair-color task, large subsets of celebrity faces with black and blond hair are assembled to support robust, data-rich training.
Implementation details include PatchGAN-style discriminators with reflection padding and sigmoid outputs, and generators that downsample, pass through nine residual blocks to stabilize deep training, then upsample and emit images via a tanh output. The adversarial term is measured with mean squared error, the cycle term with mean absolute error (weighted higher, e.g., by 10), and optimization uses Adam with standard GAN betas alongside automatic mixed precision for speed. The training loop jointly updates discriminators and generators, periodically saving sample translations for visual monitoring, and ultimately retains only the generators for inference. Results show high-fidelity, bidirectional hair-color translation and strong round-trip consistency, and the same framework readily generalizes to other two-domain tasks such as adding or removing eyeglasses.
The architecture of A CycleGAN to convert images with black hair to ones with blond hair and to convert images with blond hair to ones with black hair. The diagram also outlines the training steps to minimize adversarial losses. How the model minimizes cycle consistency losses is explained in figure 6.2.
How CycleGAN minimizes cycle consistency losses between original black hair images and fake ones after roundtrips and cycle consistency losses between original blond hair images and fake ones after roundtrips.
Sample images of celebrity faces with black or blond hair.
The architecture of a residual block. The input x is passed through a series of transformations (two sets of Conv2d layer & InstanceNorm2d layer, and a ReLU activation). The input x is then added back to the output of these transformations, f(x). The output of the residual block is therefore x+f(x).
A roundtrip conversion of images with black hair. Images in the top row are the original images with black hair from the training set. Images in the middle row are the corresponding fake images with blond hair, produced by the trained blond hair generator. Images in the bottom row are fake images with black hair after a roundtrip: we feed the images in the middle row to the trained black hair generator to create fake images with black hair.
A roundtrip conversion of images with blond hair. Images in the top row are the original images with blond hair from the training set. Images in the middle row are the corresponding fake images with black hair, produced by the trained black hair generator. Images in the bottom row are fake images with blond hair after a roundtrip conversion: we feed the images in the middle row to the trained blond hair generator to create fake images with blond hair.
Summary
- CycleGAN can translate images between two domains without paired examples. It consists of two discriminators and two generators. One generator converts images in domain A to domain B while the other generator converts images in domain B to domain A. The two discriminators classify if a given image is from a specific domain.
- CycleGAN uses a cycle consistency loss function to ensure the original image can be reconstructed from the transformed image, encouraging the preservation of key features.
- A properly constructed CycleGAN model can be applied to any dataset with images from two domains. The same model can be trained with different datasets and be used to translate images in different domains.
- When we have abundant high-quality training data, the trained CycleGAN can convert images in one domain to another and convert them back to the original domain. The images after a roundtrip conversion can potentially look almost identical to the original images.
Learn Generative AI with PyTorch ebook for free