liveBook · Manning

Cycle consistency loss

As explained in Learn Generative AI with PyTorch

A loss term that penalizes failure to reconstruct the original input after translating it to another domain and back, used to preserve content in CycleGAN.

Cycle consistency loss penalizes changes that prevent an image from being reconstructed after translation to another domain and back. In CycleGAN, it preserves important content while generators change attributes such as hair color, without requiring paired examples. section 6.0 section 6.1

Adversarial loss makes a translated image resemble the target domain; cycle consistency loss checks whether the round trip returns to the original domain. CycleGAN minimizes both objectives. section 6.1.2

A CycleGAN applies the loss in two directions:

  • Translate a real black-hair image into a fake blond-hair image, then translate it back into a fake black-hair image. section 6.1.2
  • Measure the pixel-level mean absolute error between the reconstructed image and the original; call this Loss_Cycle_Black. section 6.1.2
  • Repeat the process from blond hair to black hair and back, producing Loss_Cycle_Blond. section 6.1.2
  • Add the two directional losses to obtain the total cycle consistency loss. section 6.1.2

Pixel-level implementation

For images shaped (3, 256, 256), L1 loss averages absolute differences across 3 × 256 × 256 = 196,608 corresponding pixel positions. A PyTorch implementation uses nn.L1Loss() for cycle consistency, while nn.MSELoss() measures adversarial loss. section 6.3.2

Figure 6.2 How CycleGAN minimizes cycle consistency losses between original black hair images and fake ones after round trips and cycle consistency losses between original blond hair images and fake ones after round trips

Use the round-trip diagram to follow how original black- and blond-hair images are compared with their reconstructed versions. section 6.1.2

How Learn Generative AI with PyTorch covers it

How deep it goes
Explains two directional pixel losses and their sum, then gives L1 implementation details, but omits a full training loop.
Examples
Demonstrates black-hair and blond-hair round trips using reconstructed images and mean absolute pixel error.
What it assumes
Assumes familiarity with GANs, generators, discriminators, image domains, and pixel-level losses.
What you can do after
Build it from scratch by adding two directional L1 reconstruction losses to a CycleGAN generator objective.
How it connects
Connects CycleGAN with adversarial loss, generators, discriminators, image-to-image translation, L1 loss, and L2 loss.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.