Assignment 3: Cats Generator Playground

Grace Su (Andrew ID: gdsu)

0. Project Description

In this assignment, I first implemented and trained a Deep Convolutional GAN (DCGAN) to generate Grumpy cat images from samples of random noise. This involved implementing the discriminator and generator networks in PyTorch, creating the training loop, and implementing differentiable augmentation. Then, I implemented and trained a CycleGAN to perform unpaired image-to-image translation between Russian-Blue cats and Grumpy cats. I also trained a CycleGAN to perform unpaired image-to-image translation between apples and oranges.

1. DCGAN

Discriminator

The formula to calculate the output size of a convolutional layer is given by: \[ \text{output size} = \frac{\text{input size} - \text{kernel size} + 2 \times \text{padding}}{\text{stride}} + 1 \] Given that we use kernel size 4 and stride 2, we find that the padding size should be 1 when we want to downsample by a factor of 2: \[ H = \frac{2H - 4 + 2 \times \text{padding}}{2} + 1 \] \[ H = H - 2 + \text{padding} + 1 \] \[ \text{padding} = 1 \] For conv5, where we downsample by a factor of 4, the padding size is 0: \[ 1 = \frac{4 - 4 + 2 \times \text{padding}}{2} + 1 \] \[ 1 = 2 - 2 + \text{padding} + 1 \] \[ \text{padding} = 0 \]

The figure below illustrates the DCGAN discriminator architecture (also defined in the assignment instructions):

Diagram of the DCGAN discriminator

Generator

The figure below illustrates the DCGAN generator architecture (also defined in the assignment instructions):

For up_conv1, I used a ConvTranspose2D layer with kernel size 4, stride 1, and padding 0.

DCGAN Training Results

Basic Preprocessing

Training losses of the DCGAN with basic data preprocessing (and no differentiable augmentation)

Deluxe Preprocessing

Training losses of the DCGAN with deluxe data preprocessing (and no differentiable augmentation)

Basic Preprocessing and Differentiable Augmentation

Training losses of the DCGAN with basic data preprocessing AND differentiable augmentation
Samples at step 200 (deluxe data preprocessing, NO differentiable augmentation)
Samples at step 6400 (deluxe data preprocessing, NO differentiable augmentation)

Deluxe Preprocessing and Differentiable Augmentation

Training losses of the DCGAN with deluxe data preprocessing AND differentiable augmentation
Samples at step 200 (deluxe data preprocessing AND differentiable augmentation)
Samples at step 6400 (deluxe data preprocessing AND differentiable augmentation)

Effect of differentiable augmentation: The deluxe data preprocessing applies image augmentations to only the real images during training. Differentiable augmentation applies image augmentations to both the real AND fake images during training, therefore reducing overfitting in both the generator and the discriminator. In general, the generator loss becomes lower than it was without differentiable augmentation. Therefore the discriminator has a harder time differentiating between real and fake images, and the discriminator loss increases.

What the loss curves should look like: If the GAN manages to train successfully, the generator loss should trend downwards and the discriminator loss should trend upwards as the number of training steps increases. It makes sense that the loss curves are not smooth because optimizing the generator vs. discriminator are adversarial objectives - as the generator gets better, the discriminator gets fooled more.

Sample quality: Training with differentiable augmentation seems to have improved the quality of the generated images at 6400 steps. The images have less unrealistic artifacts and have more defined features. In general, as training progresses, sample quality improves, from very blurry blobs of grumpy cat colors to recognizable images of grumpy cats.

2. CycleGAN

CycleGAN PatchDiscriminator

The diagram below illustrates the elements I removed from the DCGAN discriminator to create the PatchDiscriminator.
Diagram of the PatchDiscriminator

CycleGAN Generator

The diagram below illustrates the CycleGAN generator architecture (also defined in the assignment instructions):

CycleGAN Results

2.1 Grumpy Cat --- Russian Blue Cat

2.1.1 PatchDiscriminator and NO Cycle-Consistency Loss

Samples after training for 1,000 iterations.
Samples after training for 10,000 iterations.

2.1.2 PatchDiscriminator AND Cycle-Consistency Loss

Samples after training for 1,000 iterations.
Samples after training for 10,000 iterations.

2.1.3 DCDiscriminator AND Cycle-Consistency Loss

Samples after training for 1,000 iterations.
Samples after training for 10,000 iterations.

2.2 Apple --- Orange

2.2.1 PatchDiscriminator and NO Cycle-Consistency Loss


Samples after training for 10,000 iterations.

2.2.2 PatchDiscriminator AND Cycle-Consistency Loss


Samples after training for 10,000 iterations.

2.2.3 DCDiscriminator AND Cycle-Consistency Loss


Samples after training for 10,000 iterations.

Effect of Cycle-Consistency Loss: In general, Cycle-Consistency Loss improves the quality of the generated images. The samples look more realistic and have less artifacts. The CycleGAN also converges faster - at 1,000 iterations, the samples already have fewer white artifacts compared to the samples without Cycle-Consistency Loss. The image translation result is also better: the generated cat images have more accurate spatial placement of facial features. Cycle-Consistency Loss's improvements make sense because it trains a GAN for both directions of image translation, so when cycle-consistency loss is applied, the generated samples are encouraged to stay within one of the 2 image domains and therefore look more realistic.

Effect of PatchDiscriminator vs. DCDiscriminator: The PatchDiscriminator seems to generate better image translation results than the DCDiscriminator. This is mostly evident in the Grumpy cat to Russian Blue cat translation, where the PatchDiscriminator preserves the structure of the input image better while the DCDiscriminator seems to only generate a front-facing Russian Blue cat. The PatchDiscriminator's improvements make sense because it discriminates image patches rather than the entire image, making it more effective at capturing local image structures.

3. Bells and Whistles

3.1 Diffusion Model Training

I used the provided skeleton code and referenced https://huggingface.co/blog/annotated-diffusion to implement and train a simple diffusion model on our Grumpy Cat dataset. The generated results look a lot more like real photos of grumpy cats than the DCGAN and CycleGAN samples.

3.2 Trying a Pre-Trained Diffusion Model

I also tried using the diffusers implementation of Stable Diffusion-XL to do text-to-image generation. Note that the original generated image size is 1024x1024 and the samples below are resized for visibility.

Prompt: "A grumpy cat"
Prompt: "A russian blue cat"
Prompt: "A russian blue grumpy cat"