Overview

4 Image Generation with GANs

This chapter moves from simple sequence generation to practical image synthesis with GANs, showing how to build, train, and evaluate models in PyTorch for both grayscale and high-resolution color images. A key design principle is to construct the generator by mirroring the discriminator’s structure, ensuring shapes and channels align as data flow is inverted from noise to image. To scale image fidelity and efficiency, the chapter introduces convolutional building blocks, explains how standard convolutions downsample and extract spatial features, and how transposed convolutions upsample to form higher-resolution feature maps. Throughout, model quality is judged primarily by visual inspection, reflecting common practice in GAN training.

The first project targets 28×28 grayscale clothing images using multilayer perceptrons. The discriminator is a binary classifier over flattened inputs, and the generator reverses its layer dimensions, mapping a 100-dimensional latent vector through fully connected layers to a Tanh output in the range [-1, 1]. Training alternates updates to the discriminator on real and fake batches and updates to the generator to fool the discriminator, using binary cross-entropy loss and Adam optimization. Periodic visualization monitors progress as outputs sharpen from noise to recognizable items; after training, the discriminator is discarded and the generator is saved for sampling.

To generate high-resolution color images, the chapter transitions to DCGANs with convolutional layers for downsampling, transposed convolutions for upsampling, and batch normalization to stabilize training. It details how kernel size, stride, and padding control spatial dimensions, and motivates weight sharing and local receptive fields for parameter efficiency. The DCGAN discriminator stacks Conv2d, BatchNorm2d, and LeakyReLU layers to compress 3-channel inputs to a real/fake probability, while the generator mirrors this hierarchy with ConvTranspose2d, BatchNorm2d, and ReLU layers that map 100-dimensional noise to 64×64 RGB outputs with Tanh. Using Adam with tuned betas, the model is trained on channels-first tensors and evaluated visually after each epoch; the trained generator is then saved and used to produce diverse Anime faces, setting the stage for controlled generation in the next chapter.

Designing a generator network to create clothing items by mirroring the layers in the discriminator network. The right side of the diagram shows the discriminator network, which contains four dense layers. To design a generator that can conjure up clothing items from thin air, we mirror the layers in the discriminator network. Specifically, as shown on the left half of the figure, the generator has four similar dense layers in it, but in reverse order: the first layer in the generator mirrors the last layer in the discriminator, the second layer in the generator mirrors the second to last layer in the discriminator, and so on. Further, in each of the top three layers, the numbers of inputs and outputs in the discriminator are reversed and used as the numbers of outputs and inputs in the generator.
Output from the GAN model to generate clothing items before training. Since the model is not trained, the generated images are nothing like the images in the training set.
Clothing items generated by an image GAN model after ten epochs of training
Clothing items generated by a trained image GAN model (after 50 epochs)
A numerical example of how convolutional operations work, with stride equal to 1 and no padding
A numerical example of how transposed convolutional operations work
Examples from the Anime faces training dataset.
Designing a generator network in DCGAN to create Anime faces by mirroring the layers in the discriminator network. The right side of the diagram shows the discriminator network, which contains five 2D convolutional layers. To design a generator that can conjure up Anime faces out of thin air, we mirror the layers in the discriminator network. Specifically, as shown on the left half of the figure, the generator has five 2D transposed convolutional layers in it, symmetric to the 2D convolutional layers in the discriminator. Further, in each of the top four layers, the numbers of input and output channels in the discriminator are reversed and used as the numbers of output and input channels in the generator.
Generated images in DCGAN after one epoch of training.
Generated Anime face images by the trained generator in DCGAN.

Summary

  • To conjure up realistic-looking images out of thin air, the generator mirrors layers used in the discriminator network.
  • While it’s feasible to generate grayscale images by using just fully connected layers, to generate high-resolution color images, we need to use convolutional neural networks (CNNs).
  • 2D convolutional layers are used for feature extraction. They apply a set of learnable filters (also known as kernels) to the input data to detect patterns and features at different spatial scales. These layers are essential for capturing hierarchical representations of the input data.
  • 2D Transposed Convolutional Layers (also known as Deconvolution or Upsampling Layers) are used for upsampling or generating high-resolution feature maps. They apply a filter to the input data. However, unlike standard convolution, they increase the spatial dimensions by inserting gaps between the output values, which effectively "upscales" the feature maps. This process generates feature maps of a higher resolution.
  • 2D Batch Normalization is a technique commonly used in deep learning and neural networks to improve the training and performance of convolutional neural networks (CNNs) and other models that work with 2D data, such as images. It normalizes the values for each feature channel, so they have a mean of 0 and a standard deviation of 1, which helps stabilize and speed up training.

FAQ

What does it mean to “mirror” the discriminator when designing a generator?The generator is built by reversing the sequence and sizes of layers used in the discriminator. For dense GANs (grayscale clothing), the generator’s first layer mirrors the discriminator’s last layer, the second mirrors the second-to-last, and so on, reversing input/output sizes. In DCGANs (Anime faces), ConvTranspose2d layers in the generator mirror Conv2d layers in the discriminator by swapping the numbers of input/output channels and using symmetric kernel/stride/padding settings.
Why switch from fully connected layers to CNNs for high‑resolution color images?Dense layers explode in parameter count when images get large, making training slow and ineffective. CNNs use local connectivity and weight sharing, dramatically reducing parameters and computation, while better capturing spatial hierarchies in images. This makes them the natural choice for high‑resolution color image generation.
How do kernel size, stride, and padding affect a convolutional layer?- Kernel size determines the receptive field used to compute each output value. Larger kernels aggregate information over larger regions.
- Stride controls the step size of the filter; larger strides downsample more aggressively (smaller output spatial size).
- Padding adds zeros around the input to control output size; with appropriate padding, you can preserve spatial dimensions after convolution.
What is a transposed convolution and how does it upsample feature maps?Transposed convolution (a.k.a. deconvolution or upsampling) increases spatial resolution by effectively inserting gaps and spreading input activations across a larger output grid using learnable kernels. The stride controls the upsampling factor (larger stride ⇒ larger spatial expansion). It’s used in generators to build higher‑resolution feature maps from low‑dimensional inputs.
Why use Tanh in the generator output and Sigmoid in the discriminator?Training images are normalized to the range [-1, 1], so the generator ends with Tanh to match this scale. The discriminator outputs a probability that an input is real; using Sigmoid squashes its scalar output to [0, 1], which aligns with binary cross‑entropy training.
How is GAN training organized in this chapter?For each batch: (1) train the discriminator on real samples, (2) train the discriminator on generator-produced fake samples, then (3) train the generator to fool the discriminator. Binary cross‑entropy loss and Adam optimizers are used. Typical settings shown are lr=0.0001 for the dense GAN, and for DCGAN lr=0.0002 with Adam betas (0.5, 0.999).
How are GANs evaluated here, and why not rely on discriminator loss?Quality is assessed primarily by visual inspection of generated samples during training. Discriminator loss is not a reliable quality proxy because as the generator improves, the discriminator’s task changes and its loss does not correlate well with sample realism. Metrics like Inception Score exist but have known weaknesses; hence visual checks are emphasized.
What does Batch Normalization do in DCGANs, and why is it helpful?BatchNorm normalizes each feature channel to have approximately zero mean and unit variance within a batch, stabilizing activations and gradients. This reduces internal covariate shift, mitigates vanishing/exploding gradients, and speeds up training—particularly important in deep convolutional generators and discriminators.
Why use LeakyReLU in the discriminator (and ReLU in the generator)?LeakyReLU allows a small negative slope for negative inputs, preventing “dead” neurons and improving gradient flow—useful for stabilizing discriminator training in DCGANs. Generators commonly use ReLU in intermediate layers (and Tanh at the output) to produce diverse, non-saturated activations.
How are the Anime face images prepared, and what about channels-first tensors?Images are resized (e.g., to 64×64), converted to tensors, and normalized to [-1, 1]. PyTorch stores images in channels-first format [C, H, W] (e.g., [3, 64, 64] for RGB). When plotting with libraries expecting channels-last, permute to [H, W, C] and denormalize (map from [-1, 1] back to [0, 1]). Noise for the dense GAN is shaped [batch, 100]; for DCGAN it’s [batch, 100, 1, 1].

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