Overview

5 Selecting Characteristics in Generated Images

This chapter shows how to steer generative image models so outputs exhibit desired attributes, moving beyond unconstrained results from earlier GANs. It introduces two complementary control mechanisms: conditioning on labels (conditional GANs) and selecting or interpolating latent vectors that correlate with visual traits. To make training stable and improve fidelity—especially at higher resolutions—the chapter integrates Wasserstein distance with a gradient penalty (WGAN-GP), yielding smoother optimization, mitigating mode collapse, and producing sharper, more realistic faces.

The workflow begins with a curated eyeglasses dataset, where images are organized and labels verified to support supervised conditioning. A conditional GAN is built in PyTorch with a generator that consumes a 100-dimensional noise vector concatenated with a one-hot label, and a critic (WGAN terminology for discriminator) that scores images while also receiving the label as extra channels alongside RGB. Training uses careful weight initialization, Adam optimization, and the WGAN-GP objective, which enforces a 1‑Lipschitz critic via a gradient penalty computed on interpolations between real and generated samples. Images are normalized and resized to 256×256 to target higher quality than earlier chapters, and progress is monitored by periodically sampling the generator under different label settings.

After training, attribute selection becomes practical and flexible. Label control directly toggles features such as the presence or absence of eyeglasses, while latent vector selection highlights instance-specific traits (for example, vectors that tend to yield more masculine or feminine facial characteristics). Interpolating labels produces smooth transitions in a single attribute (label arithmetic), and interpolating latent vectors morphs identity or style (vector arithmetic). Combining both lets the model specify two independent attributes at once—such as facial characteristics and eyeglasses—delivering four targeted groups in one framework. The chapter closes by weighing trade-offs: conditional GANs require labeled data but produce diverse samples per attribute, whereas handpicked latent vectors need no labels but tend to control only individual instances unless many vectors are curated.

Sample images with eyeglasses in the training dataset.
Sample images without eyeglasses in the training dataset.
A diagram of Wasserstein GAN with gradient penalty. The discriminator network in Wasserstein GAN (which we call the critic) rates input images: it tries to assign a score of -∞ to a fake image (bottom left) and a score of ∞ to the real image (top middle). Further, an interpolated image of the real and fake images (top left) is presented to the critic, and the gradient penalty with respect to the critic’s rating on the interpolated image is added to the total loss in the training process.
A diagram of the training process for conditional GANs.
Images of human faces with eyeglasses that are generated by the trained conditional GAN model.
Images of human faces without eyeglasses that are generated by the trained conditional GAN model.
Label arithmetic in conditional GAN. We first create two labels: the no eyeglasses label labels_ng and the eyeglasses label labels_g. These two labels instruct the trained generator to produce images with and without eyeglasses in them, respectively. We then create five interpolated labels, each as a weighted average of the original two labels: w*labels_ng +(1-w)*labels_g, where the weight w takes five different values, 0, 0.25, 0.5, 0.75, and 1. The five generated images based on the five interpolated labels are shown above. The image on the far left has eyeglasses. As we move from the left to the right, the eyeglasses gradually fade away, until the image on the far right has no eyeglasses in it.
Vector arithmetic in GAN. We first save two random noise vectors z_female_ng and z_male_ng. The two vectors lead to images of female and male faces, respectively. We then create five interpolated vectors, each as a weighted average of the original two vectors: w*z_female_ng+(1-w)*z_male_ng, where the weight w takes five different values, 0, 0.25, 0.5, 0.75, and 1. The five generated images based on the five interpolated vectors are shown above. The image on the far left has male features. As we move from the left to the right, the male features gradually fade away and the female features gradually appear, until the image on the far right shows a female face in it.
Select two characteristics simultaneously in the generated image. We select a noise vector from the following two choices z_female_ng and z_male_ng. We also select a label from the following two choices: labels_ng and labels_g. We then feed the noise vector and the label to the trained generator to create an image. Based on the values of the noise vector and the label, the trained model can create four types of images. By doing this, we effectively select two independent characteristics in the generated image: a male or a female face, and whether the image has eyeglasses in it or not.
Conduct vector arithmetic and label arithmetic simultaneously. The value of i changes from 0 to 35; p and q are the integer quotient and remainder, respectively, when i is divided by 6. Therefore, p and q each can take six different values, 0, 1, 2, 3, 4, and 5. The interpolated noise vector, z_female_ng*p/5+z_male_ng*(1-p/5), and the interpolated label, labels_ng[0]*q/5+labels_g[0]*(1-q/5), can each take six different values. In each row, when you go from left to right, the eyeglasses gradually fade away. In each column, when you go from top to bottom, the image changes gradually from a male face to a female face.

Summary

  • By selecting a certain noise vector in the latent space and feeding it to the trained GAN model, we can select a certain characteristic in the generated image, such as whether the generated image has a male or female face in it.
  • A conditional GAN is different from a traditional GAN. We train the model on labeled data and ask the trained model to generate data with a specific attribute. For example, one label tells the model to generate images of human faces with eyeglasses while another tells the model to create human faces without eyeglasses.
  • After a conditional GAN is trained, we can use a series of weighted averages of the labels to generate images that transition from an image represented by one label to an image represented by another label. For example, a series of images in which the eyeglasses gradually fade away on the same person’s face. We call this label arithmetic.
  • We can also use a series of weighted averages of two different noise vectors to create images that transition from one attribute to another. For example, a series of images in which the male features gradually fade away and female features gradually appear. We call this vector arithmetic.
  • Wasserstein GAN (WGAN) is a technique used to improve the training stability and performance of GAN models by using Wasserstein distance instead of the binary cross-entropy as the loss function. Further, for the Wasserstein distance to work correctly, the critic in WGANs must be 1-Lipschitz continuous, meaning the gradient norms of the critic's function must be at most 1 everywhere. The gradient penalty in WGANs adds a regularization term to the loss function to enforce the Lipschitz constraint more effectively.

FAQ

What are the two main ways to control characteristics in generated images covered in this chapter?Two methods: (1) Conditional GANs (cGANs), where you attach a label to guide generation (e.g., with or without eyeglasses), and (2) selecting specific latent vectors in the noise space that correlate with desired features (e.g., male vs. female faces). You can also combine both to control multiple attributes simultaneously.
How are labels incorporated into the generator and critic in this implementation?The generator receives a 100‑dimensional noise vector concatenated with a 2‑dimensional one‑hot label ([1,0] for glasses, [0,1] for no glasses), forming a 102‑dim input. The critic receives the RGB image plus two additional label channels, making a 5×256×256 tensor where the 4th and 5th channels are filled according to the one‑hot label.
Why use Wasserstein GAN with Gradient Penalty (WGAN‑GP) instead of standard GAN loss?WGAN‑GP provides smoother gradients, improves training stability, and mitigates mode collapse by optimizing the Wasserstein‑1 distance. The gradient penalty enforces the 1‑Lipschitz constraint on the critic more effectively than weight clipping, leading to better convergence and image quality.
What is the difference between a critic and a discriminator in this context?The critic (WGAN) outputs real‑valued scores (−∞ to ∞) without a sigmoid, aiming to assign higher scores to real data than fake. A standard discriminator outputs probabilities via sigmoid and uses binary cross‑entropy. The critic must satisfy a 1‑Lipschitz constraint, enforced via gradient penalty.
How is the gradient penalty computed in WGAN‑GP here?Interpolate between real and generated images (and their labels), pass the interpolates to the critic, compute the gradient of the critic score w.r.t. the interpolated inputs, and add a penalty proportional to (||gradient||₂ − 1)² to the critic loss.
How can this setup handle more than two classes or attributes?Use a k‑dimensional one‑hot label for k classes (e.g., hair colors) and attach k label channels to the critic’s image input. Concatenate the k‑dim one‑hot to the noise vector for the generator. The approach generalizes directly beyond binary labels.
What image size and batch size are used, and how do you handle limited GPU memory?Images are resized to 256×256 to improve resolution; a batch size of 16 is used to fit typical GPU memory. If you encounter out‑of‑memory issues, reduce the batch size (e.g., to 8–10) or switch to CPU training.
What are label arithmetic and vector arithmetic, and what effects do they produce?Label arithmetic: interpolate between labels (e.g., w·[0,1] + (1−w)·[1,0]) to smoothly transition an attribute in the output (eyeglasses gradually appearing/disappearing). Vector arithmetic: interpolate between latent vectors to morph semantic traits (e.g., male to female), even with a fixed label. Both can be combined to transition multiple attributes jointly.
What are the trade‑offs between using a cGAN and hand‑selecting latent vectors?cGAN: needs labeled data but can generate many diverse images for a target attribute by sampling noise. Latent vector selection: no labels needed, but a chosen vector typically maps to one instance; generating diverse images with the same attribute requires many suitable vectors.
How is the eyeglasses dataset prepared, and why is manual correction necessary?Images are split into two folders (with and without glasses) based on Kaggle CSV labels, then converted to tensors. Since labels are imperfect (about 10% mislabeled), manual moving of images between folders is recommended to clean the data, which improves training quality and final results.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn Generative AI with PyTorch ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn Generative AI with PyTorch ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn Generative AI with PyTorch ebook for free