Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch Generative Adversarial Network (GAN)

PyTorch Generative Adversarial Network (GAN)

Generative Adversarial Network (GAN) is one of the most creative model architectures in deep learning. By having two neural networks compete against and learn from each other, it can ultimately generate highly realistic data. GANs are widely used in scenarios such as image generation, style transfer, and data augmentation


1. Core Principles of GAN

The core idea of GAN comes from the "zero-sum game" in game theory. It consists of two networks that compete with each other

The two compete and improve each other during training, eventually reaching a Nash equilibrium

1.1 The Objective Function of GAN

The training objective of GAN can be expressed as the following minimax game

[ \min_G \max_D \mathbb{E}{x \sim p{data}(x)}[\log D(x)] + \mathbb{E}_{z \sim p_z(z)}[\log(1 - D(G(z)))] ]

Where:

1.2 Interpreting the Training Process

GAN training is divided into two stages

Stage 1: Train the discriminator

Fix the generator and improve the discriminator's ability to distinguish

[ \max_D \mathbb{E}{x \sim p{data}}[\log D(x)] + \mathbb{E}_{z \sim p_z}[\log(1 - D(G(z)))] ]

Stage 2: Train the generator

Fix the discriminator and improve the generator's ability to fool it

[ \min_G \mathbb{E}_{z \sim p_z}[\log(1 - D(G(z)))] ]

In practice, the discriminator is usually trained for k steps first, then the generator for 1 step, to maintain balance


2. Basic GAN Implementation

Below we implement the simplest GAN -- one used to generate two-dimensional data points

2.1 Defining the Generator and Discriminator

Example

import

torch

import

torch.

nn

as

nn

import

torch.

optim

as

optim

import

matplotlib.

pyplot

as

plt

Set the random seed

torch.

manual_seed

(

42

)

---- Generator Network ----

class

Generator

(

nn.

Module

)

:

   

"""
    Generator: generate data from random noise
    Input: noise vector (batch_size, noise_dim)
    Output: generated data (batch_size, data_dim)
    """

   

def

init

(

self

,

noise_dim

,

data_dim

,

hidden_dim

=

64

)

:

       

super

(

)

.

init

(

)

       

self

.

net

=

nn.

Sequential

(

            nn.

Linear

(

noise_dim

,

hidden_dim

)

,

            nn.

ReLU

(

)

,

            nn.

Linear

(

hidden_dim

,

hidden_dim

)

,

            nn.

ReLU

(

)

,

            nn.

Linear

(

hidden_dim

,

data_dim

)

,

           

No activation on the output; GAN will learn the appropriate distribution

       

)

   

def

forward

(

self

,

x

)

:

       

return

self

.

net

(

x

)

---- Discriminator Network ----

class

Discriminator

(

nn.

Module

)

:

   

"""
    Discriminator: distinguish real data from generated data
    Input: data points (batch_size, data_dim)
    Output: probability of real data (batch_size, 1)
    """

   

def

init

(

self

,

data_dim

,

hidden_dim

=

64

)

:

       

super

(

)

.

init

(

)

       

self

.

net

=

nn.

Sequential

(

            nn.

Linear

(

data_dim

,

hidden_dim

)

,

            nn.

LeakyReLU

(

0.2

)

,

 

LeakyReLU prevents vanishing gradients

            nn.

Linear

(

hidden_dim

,

hidden_dim

)

,

            nn.

LeakyReLU

(

0.2

)

,

            nn.

Linear

(

hidden_dim

,

1

)

,

            nn.

Sigmoid

(

)

 

Output probability

       

)

   

def

forward

(

self

,

x

)

:

       

return

self

.

net

(

x

)

Hyperparameters

NOISE_DIM

=

16

DATA_DIM

=

2

HIDDEN_DIM

=

64

BATCH_SIZE

=

128

Create the network

generator

=

Generator

(

NOISE_DIM

,

DATA_DIM

,

HIDDEN_DIM

)

discriminator

=

Discriminator

(

DATA_DIM

,

HIDDEN_DIM

)

print

(

f

"Number of generator parameters: {sum(p.numel() for p in generator.parameters()):,}"

)

print

(

f

"Number of discriminator parameters: {sum(p.numel() for p in discriminator.parameters()):,}"

)

2.2 Training Loop

Example

---- Optimizer ----

lr

=

0.001

g_optimizer

=

optim.

Adam

(

generator.

parameters

(

)

,

lr

=

lr

)

d_optimizer

=

optim.

Adam

(

discriminator.

parameters

(

)

,

lr

=

lr

)

Loss function: binary cross-entropy

criterion

=

nn.

BCELoss

(

)

---- Training Data: Ring Distribution ----

def

generate_real_data

(

batch_size

)

:

   

"""Generate real data with a ring distribution"""

    angles

=

torch.

rand

(

batch_size

)

2

pi

    radius

=

1.0

randn

(

batch_size

)

0.1

 

Radius approximately 1

    x

=

radius * torch.

cos

(

angles

)

    y

=

radius * torch.

sin

(

angles

)

   

return

torch.

stack

(

[

x

,

y

]

,

dim

=

1

)

-- Training loop ------------------------------------

NUM_EPOCHS

=

1000

d_losses

=

[

]

g_losses

=

[

]

for

epoch

in

range

(

NUM_EPOCHS

)

:

   

1. Train the discriminator

   

Generate fake data

    noise

=

torch.

randn

(

BATCH_SIZE

,

NOISE_DIM

)

    fake_data

=

generator

(

noise

)

.

detach

(

)

 

detach to avoid computing generator gradients

   

Generate real data

    real_data

=

generate_real_data

(

BATCH_SIZE

)

   

Discriminator loss

    real_pred

=

discriminator

(

real_data

)

    fake_pred

=

discriminator

(

fake_data

)

    d_loss

=

criterion

(

real_pred

,

torch.

ones_like

(

real_pred

)

)

             criterion

(

fake_pred

,

torch.

zeros_like

(

fake_pred

)

)

   

Update the discriminator

    d_optimizer.

zero_grad

(

)

    d_loss.

backward

(

)

    d_optimizer.

step

(

)

   

2. Train the generator

   

Generate a new batch of fake data

    noise

=

torch.

randn

(

BATCH_SIZE

,

NOISE_DIM

)

    fake_data

=

generator

(

noise

)

   

Generator loss: make the discriminator believe the generated data is real

    fake_pred

=

discriminator

(

fake_data

)

    g_loss

=

criterion

(

fake_pred

,

torch.

ones_like

(

fake_pred

)

)

   

Update the generator

    g_optimizer.

zero_grad

(

)

    g_loss.

backward

(

)

    g_optimizer.

step

(

)

   

Record the loss

    d_losses.

append

(

d_loss.

item

(

)

)

    g_losses.

append

(

g_loss.

item

(

)

)

   

if

(

epoch +

1

)

%

100

==

0

:

       

print

(

f

"Epoch {epoch+1:4d} | D_loss: {d_loss:.4f} | G_loss: {g_loss:.4f}"

)

print

(

"Training complete!"

)

2.3 Visualizing the Generated Results

Example

Generate data and visualize

def

visualize_results

(

generator

,

num_samples

=

1000

)

:

    noise

=

torch.

randn

(

num_samples

,

NOISE_DIM

)

    generated_data

=

generator

(

noise

)

.

detach

(

)

.

numpy

(

)

    plt.

figure

(

figsize

=

(

6

,

6

)

)

    plt.

scatter

(

generated_data

[

:

,

0

]

,

generated_data

[

:

,

1

]

,

                alpha

=

0.5

,

s

=

10

,

c

=

'blue'

,

label

=

'Generated'

)

    plt.

xlim

(

2

,

2

)

    plt.

ylim

(

2

,

2

)

    plt.

xlabel

(

'x'

)

    plt.

ylabel

(

'y'

)

    plt.

title

(

'GAN Generated Data'

)

    plt.

legend

(

)

    plt.

grid

(

True

,

alpha

=

0.3

)

    plt.

show

(

)

View the generation results

visualize_results

(

generator

)


3. DCGAN - Deep Convolutional GAN

DCGAN is a classic architecture that introduces convolutional neural networks into GANs, greatly improving image generation quality

3.1 Key Points of the DCGAN Architecture

3.2 DCGAN Implementation

Example

import

torch

import

torch.

nn

as

nn

---- DCGAN Generator ----

class

DCGenerator

(

nn.

Module

)

:

   

"""
    DCGAN generator: upsample using transposed convolution
    """

   

def

init

(

self

,

noise_dim

=

100

,

channels

=

3

,

features_g

=

64

)

:

       

super

(

)

.

init

(

)

       

self

.

noise_dim

=

noise_dim

       

Input: noise_dim x 1 x 1

       

self

.

net

=

nn.

Sequential

(

           

Transposed convolution: (batch, features_g*8, 4, 4)

            nn.

ConvTranspose2d

(

noise_dim

,

features_g *

8

,

4

,

1

,

0

,

bias

=

False

)

,

            nn.

BatchNorm2d

(

features_g *

8

)

,

            nn.

ReLU

(

True

)

,

           

(batch, features_g*4, 8, 8)

            nn.

ConvTranspose2d

(

features_g *

8

,

features_g *

4

,

4

,

2

,

1

,

bias

=

False

)

,

            nn.

BatchNorm2d

(

features_g *

4

)

,

            nn.

ReLU

(

True

)

,

           

(batch, features_g*2, 16, 16)

            nn.

ConvTranspose2d

(

features_g *

4

,

features_g *

2

,

4

,

2

,

1

,

bias

=

False

)

,

            nn.

BatchNorm2d

(

features_g *

2

)

,

            nn.

ReLU

(

True

)

,

           

(batch, features_g, 32, 32)

            nn.

ConvTranspose2d

(

features_g *

2

,

features_g

,

4

,

2

,

1

,

bias

=

False

)

,

            nn.

BatchNorm2d

(

features_g

)

,

            nn.

ReLU

(

True

)

,

           

Output: (batch, channels, 64, 64)

            nn.

ConvTranspose2d

(

features_g

,

channels

,

4

,

2

,

1

,

bias

=

False

)

,

            nn.

Tanh

(

)

 

Output range [-1, 1]

       

)

   

def

forward

(

self

,

x

)

:

       

x: (batch, noise_dim) -> (batch, noise_dim, 1, 1)

        x

=

x.

view

(

x.

size

(

0

)

,

x.

size

(

1

)

,

1

,

1

)

       

return

self

.

net

(

x

)

---- DCGAN Discriminator ----

class

DCDiscriminator

(

nn.

Module

)

:

   

"""
    DCGAN discriminator: downsample using convolution
    """

   

def

init

(

self

,

channels

=

3

,

features_d

=

64

)

:

       

super

(

)

.

init

(

)

       

Input: (batch, channels, 64, 64)

       

self

.

net

=

nn.

Sequential

(

           

(batch, features_d, 32, 32)

            nn.

Conv2d

(

channels

,

features_d

,

4

,

2

,

1

,

bias

=

False

)

,

            nn.

LeakyReLU

(

0.2

,

inplace

=

True

)

,

           

(batch, features_d*2, 16, 16)

            nn.

Conv2d

(

features_d

,

features_d *

2

,

4

,

2

,

1

,

bias

=

False

)

,

            nn.

BatchNorm2d

(

features_d *

2

)

,

            nn.

LeakyReLU

(

0.2

,

inplace

=

True

)

,

           

(batch, features_d*4, 8, 8)

            nn.

Conv2d

(

features_d *

2

,

features_d *

4

,

4

,

2

,

1

,

bias

=

False

)

,

            nn.

BatchNorm2d

(

features_d *

4

)

,

            nn.

LeakyReLU

(

0.2

,

inplace

=

True

)

,

           

(batch, features_d*8, 4, 4)

            nn.

Conv2d

(

features_d *

4

,

features_d *

8

,

4

,

2

,

1

,

bias

=

False

)

,

            nn.

BatchNorm2d

(

features_d *

8

)

,

            nn.

LeakyReLU

(

0.2

,

inplace

=

True

)

,

           

Output: (batch, 1, 1, 1)

            nn.

Conv2d

(

features_d *

8

,

1

,

4

,

1

,

0

,

bias

=

False

)

,

            nn.

Sigmoid

(

)

       

)

   

def

forward

(

self

,

x

)

:

       

return

self

.

net

(

x

)

.

view

(

x.

size

(

0

)

,

1

)

Test the network

noise_dim

=

100

generator

=

DCGenerator

(

noise_dim

=

noise_dim

,

channels

=

3

,

features_g

=

64

)

discriminator

=

DCDiscriminator

(

channels

=

3

,

features_d

=

64

)

Test the forward pass

noise

=

torch.

randn

(

2

,

noise_dim

)

fake_images

=

generator

(

noise

)

print

(

f

"Generated image shape: {fake_images.shape}"

)

 

torch.Size([2, 3, 64, 64])

decision

=

discriminator

(

fake_images

)

print

(

f

"Decision shape: {decision.shape}"

)

   

torch.Size([2, 1])

3.3 Complete DCGAN Training Code

Example

-- Training Configuration -------------------------------

device

=

torch.

device

(

"cuda"

if

torch.

cuda

.

is_available

(

)

else

"cpu"

)

print

(

f

"Using device: {device}"

)

NOISE_DIM

=

100

LEARNING_RATE

=

0.0002

BETA1

=

0.5

 

Adam parameters

Create the network

generator

=

DCGenerator

(

noise_dim

=

NOISE_DIM

)

.

to

(

device

)

discriminator

=

DCDiscriminator

(

)

.

to

(

device

)

Optimizer

g_optimizer

=

optim.

Adam

(

generator.

parameters

(

)

,

lr

=

LEARNING_RATE

,

betas

=

(

BETA1

,

0.999

)

)

d_optimizer

=

optim.

Adam

(

discriminator.

parameters

(

)

,

lr

=

LEARNING_RATE

,

betas

=

(

BETA1

,

0.999

)

)

criterion

=

nn.

BCELoss

(

)

-- Training Loop ----------------------------------------

fixed_noise

=

torch.

randn

(

64

,

NOISE_DIM

,

device

=

device

)

 

For visualization

def

train_dcgan

(

generator

,

discriminator

,

g_optimizer

,

d_optimizer

,

criterion

,

                num_epochs

,

device

,

fixed_noise

)

:

    G_losses

=

[

]

    D_losses

=

[

]

   

for

epoch

in

range

(

num_epochs

)

:

       

for

batch_idx

in

range

(

100

)

:  

Assume 100 batches per epoch

           

Train the discriminator

            discriminator.

zero_grad

(

)

           

Real images (assumed to exist)

           

real_images = ...

           

Simulated here with random noise

            real_images

=

torch.

randn

(

32

,

3

,

64

,

64

)

.

to

(

device

)

            batch_size

=

real_images.

size

(

0

)

            labels_real

=

torch.

ones

(

batch_size

,

1

)

.

to

(

device

)

            labels_fake

=

torch.

zeros

(

batch_size

,

1

)

.

to

(

device

)

           

Real image loss

            output

=

discriminator

(

real_images

)

            d_loss_real

=

criterion

(

output

,

labels_real

)

           

Generated image loss

            noise

=

torch.

randn

(

batch_size

,

NOISE_DIM

)

.

to

(

device

)

            fake_images

=

generator

(

noise

)

            output

=

discriminator

(

fake_images.

detach

(

)

)

            d_loss_fake

=

criterion

(

output

,

labels_fake

)

           

Total loss

            d_loss

=

d_loss_real + d_loss_fake

            d_loss.

backward

(

)

            d_optimizer.

step

(

)

           

Train the generator

            generator.

zero_grad

(

)

            noise

=

torch.

randn

(

batch_size

,

NOISE_DIM

)

.

to

(

device

)

            fake_images

=

generator

(

noise

)

            output

=

discriminator

(

fake_images

)

            g_loss

=

criterion

(

output

,

labels_real

)

 

Want the generated images to be judged as real

            g_loss.

backward

(

)

            g_optimizer.

step

(

)

           

Record the loss

           

if

batch_idx %

50

==

0

:

                G_losses.

append

(

g_loss.

item

(

)

)

                D_losses.

append

(

d_loss.

item

(

)

)

               

print

(

f

"[{epoch}/{num_epochs}][{batch_idx}/100] "

                      f

"D_loss: {d_loss:.4f} | G_loss: {g_loss:.4f}"

)

   

return

G_losses

,

D_losses

Start training

G_losses, D_losses = train_dcgan(generator, discriminator, g_optimizer,

                                  d_optimizer, criterion, 5, device, fixed_noise)

print

(

"DCGAN architecture is defined. Ready to start training!"

)


4. GAN Training Techniques

4.1 Common Problems and Solutions

Problem Reason Solution
Mode Collapse The generator only produces a limited variety of samples Use WGAN, add mini-batch discrimination, use label smoothing
The discriminator is too strong The generator's gradients vanish and it cannot learn Train the generator more times, lower the discriminator's learning rate, use LeakyReLU
Unstable training The GAN objective function is non-convex and hard to converge Use spectral normalization, gradient penalty, learning rate warmup
Poor generation quality Insufficient network capacity or insufficient training Increase network depth, use more training data, train for longer

4.2 Loss Function Improvements

The original GAN uses JS divergence, which suffers from vanishing gradients. WGAN uses the Wasserstein distance and is more stable:

Example

WGAN loss function (replacing BCE)

def

wgan_d_loss

(

real_pred

,

fake_pred

)

:

   

"""Discriminator loss: real samples score high, generated samples score low"""

   

return

(

torch.

mean

(

real_pred

)

mean

(

fake_pred

)

)

def

wgan_g_loss

(

fake_pred

)

:

   

"""Generator loss: make generated samples score high"""

   

return

-torch.

mean

(

fake_pred

)

Gradient Penalty - WGAN-GP

def

gradient_penalty

(

discriminator

,

real_images

,

fake_images

,

device

)

:

   

"""WGAN-GP gradient penalty term"""

    batch_size

=

real_images.

size

(

0

)

    alpha

=

torch.

rand

(

batch_size

,

1

,

1

,

1

)

.

to

(

device

)

   

Interpolate between real and generated images

    interpolated

=

alpha * real_images +

(

1

)

    interpolated.

requires_grad

=

True

   

Compute the discriminator output for the interpolated images

    pred

=

discriminator

(

interpolated

)

   

Compute gradients

    gradients

=

torch.

autograd

.

grad

(

        outputs

=

pred

,

        inputs

=

interpolated

,

        grad_outputs

=

torch.

ones_like

(

pred

)

,

        create_graph

=

True

,

        retain_graph

=

True

,

        only_inputs

=

True

   

)

[

0

]

   

Compute the gradient norm

    gradients

=

gradients.

view

(

batch_size

,

1

)

    gradient_norm

=

gradients.

norm

(

2

,

dim

=

1

)

    penalty

=

(

(

gradient_norm -

1

)

**

2

)

.

mean

(

)

   

return

penalty

4.3 Spectral Normalization

Spectral normalization can stabilize GAN training by controlling the discriminator's Lipschitz constant:

Example

import

torch.

nn

.

utils

.

spectral_norm

as

spectral_norm

A discriminator using spectral normalization

class

SNDiscriminator

(

nn.

Module

)

:

   

def

init

(

self

,

channels

=

3

,

features_d

=

64

)

:

       

super

(

)

.

init

(

)

       

self

.

net

=

nn.

Sequential

(

            spectral_norm

(

nn.

Conv2d

(

channels

,

features_d

,

4

,

2

,

1

)

)

,

            nn.

LeakyReLU

(

0.2

,

inplace

=

True

)

,

            spectral_norm

(

nn.

Conv2d

(

features_d

,

features_d *

2

,

4

,

2

,

1

)

)

,

            nn.

LeakyReLU

(

0.2

,

inplace

=

True

)

,

            spectral_norm

(

nn.

Conv2d

(

features_d *

2

,

features_d *

4

,

4

,

2

,

1

)

)

,

            nn.

LeakyReLU

(

0.2

,

inplace

=

True

)

,

            spectral_norm

(

nn.

Conv2d

(

features_d *

4

,

1

,

4

,

1

,

0

)

)

,

            nn.

Sigmoid

(

)

       

)

   

def

forward

(

self

,

x

)

:

       

return

self

.

net

(

x

)

.

view

(

x.

size

(

0

)

,

1

)


5. Conditional GAN (cGAN)

Conditional GANs allow specifying the class label of the generated data, enabling conditional generation.

5.1 cGAN Architecture

Example

import

torch

import

torch.

nn

as

nn

class

ConditionalGenerator

(

nn.

Module

)

:

   

"""Conditional generator: receives both noise and a class label"""

   

def

init

(

self

,

noise_dim

,

num_classes

,

embed_dim

,

img_channels

,

features_g

=

64

)

:

       

super

(

)

.

init

(

)

       

self

.

label_emb

=

nn.

Embedding

(

num_classes

,

embed_dim

)

       

Concatenate the noise and the label embedding

       

self

.

net

=

nn.

Sequential

(

            nn.

Linear

(

noise_dim + embed_dim

,

features_g *

8

4

4

)

,

            nn.

BatchNorm1d

(

features_g *

8

4

4

)

,

            nn.

ReLU

(

)

,

           

Then apply transposed convolutions (similar to DCGAN)

           

...

       

)

   

def

forward

(

self

,

noise

,

labels

)

:

       

Embed the class label into the same dimension as the noise

        label_embedding

=

self

.

label_emb

(

labels

)

       

Concatenate the noise and the label embedding

        x

=

torch.

cat

(

[

noise

,

label_embedding

]

,

dim

=

1

)

       

return

self

.

net

(

x

)

class

ConditionalDiscriminator

(

nn.

Module

)

:

   

"""Conditional discriminator: receives both an image and a class label"""

   

def

init

(

self

,

img_channels

,

num_classes

,

embed_dim

,

features_d

=

64

)

:

       

super

(

)

.

init

(

)

       

self

.

label_emb

=

nn.

Embedding

(

num_classes

,

embed_dim

)

       

Concatenate the image and the label embedding

       

self

.

net

=

nn.

Sequential

(

            nn.

Conv2d

(

img_channels + embed_dim

,

features_d

,

4

,

2

,

1

)

,

            nn.

LeakyReLU

(

0.2

)

,

           

...

       

)

   

def

forward

(

self

,

img

,

labels

)

:

       

Resize the label embedding to the same spatial size as the image

        label_embedding

=

self

.

label_emb

(

labels

)

       

Reshape for concatenation

        label_embedding

=

label_embedding.

unsqueeze

(

2

)

.

unsqueeze

(

3

)

        label_embedding

=

label_embedding.

expand

(

1

,

1

,

img.

size

(

2

)

,

img.

size

(

3

)

)

       

Concatenate the image and the label

        x

=

torch.

cat

(

[

img

,

label_embedding

]

,

dim

=

1

)

       

return

self

.

net

(

x

)


6. GAN Evaluation Metrics

6.1 Common Evaluation Metrics

Pointer Description Advantages Disadvantages
Inception Score (IS) Use Inception v3 to assess the quality and diversity of generated images Simple to compute, with some correlation to human judgment Does not evaluate overfitting, cannot detect mode collapse
Fréchet Inception Distance (FID) Compute the distance between real and generated images in feature space More sensitive to noise and mode collapse Requires a large number of samples and is slower to compute
Human Evaluation Human judgment of generated image quality Most accurately reflects human perception Subjective and time-consuming

6.2 FID Calculation Implementation

Example

import

numpy

as

np

from

scipy

import

linalg

def

calculate_fid

(

real_activations

,

fake_activations

)

:

   

"""
    Compute the Frechet Inception Distance
    real_activations: feature vectors of real images (N, dim)
    fake_activations: feature vectors of generated images (N, dim)
    """

   

Compute the mean and covariance

    mu1

,

sigma1

=

real_activations.

mean

(

axis

=

0

)

,

np.

cov

(

real_activations

,

rowvar

=

False

)

    mu2

,

sigma2

=

fake_activations.

mean

(

axis

=

0

)

,

np.

cov

(

fake_activations

,

rowvar

=

False

)

   

Compute the FID

    diff

=

mu1 - mu2

   

Compute the sum of the covariance matrices

    covmean

,

_

=

linalg.

sqrtm

(

sigma1.

dot

(

sigma2

)

,

disp

=

False

)

   

Avoid complex numbers

   

if

np.

iscomplexobj

(

covmean

)

:

        covmean

=

covmean.

real

    fid

=

diff.

dot

(

diff

)

trace

(

sigma1 + sigma2 -

2

)

   

return

fid

Simplified example: use random data

np.

random

.

seed

(

42

)

real_acts

=

np.

random

.

randn

(

1000

,

2048

)

 

Inception v3 output dimension

fake_acts

=

np.

random

.

randn

(

1000

,

2048

)

fid_score

=

calculate_fid

(

real_acts

,

fake_acts

)

print

(

f

"FID Score: {fid_score:.2f}"

)

A lower FID is better; typically below 50 indicates good generation quality


7. Common GAN Variants

GANs have produced many variants to date, suited to different application scenarios:

Model Full name Feature Applicable scenarios
DCGAN Deep Convolutional GAN Use convolutional networks to generate high-quality images Image generation
WGAN Wasserstein GAN Use the Wasserstein distance for more stable training Stable training
WGAN-GP WGAN with Gradient Penalty Gradient penalty replaces weight clipping Stable training
CGAN Conditional GAN Add conditional information for controllable generation Conditional generation
CycleGAN Cycle-Consistent GAN Unsupervised image-to-image translation Style transfer
StyleGAN Style-Based GAN Style control, high-quality face generation Face generation
BigGAN Big GAN Large-scale, high-quality image generation High-resolution images
ProGAN Progressive Growing GAN Progressively increase the resolution High-resolution generation