Saturday, December 13, 2025

thumbnail

Generating Art with GANs: A Practical Walkthrough for Beginners

 Generating Art with GANs: A Practical Walkthrough for Beginners


Generative Adversarial Networks (GANs) are a type of deep learning model that can generate realistic images, artwork, and other data. GANs consist of two neural networks that compete with each other:


Generator – Creates fake images from random noise


Discriminator – Tries to distinguish between real and fake images


This adversarial setup helps the generator improve until it produces realistic outputs.


1. How GANs Work

Random Noise z → Generator → Fake Image

Real Image + Fake Image → Discriminator → Real/Fake Prediction

Loss Backpropagation → Update Both Networks



Generator goal: Fool the discriminator


Discriminator goal: Correctly identify real vs fake images


2. Setup Environment


Install the required libraries:


pip install torch torchvision matplotlib


3. Import Libraries

import torch

import torch.nn as nn

import torch.optim as optim

from torchvision import datasets, transforms

import matplotlib.pyplot as plt


4. Prepare Dataset


We'll use MNIST (28x28 grayscale digits) for simplicity:


transform = transforms.Compose([

    transforms.ToTensor(),

    transforms.Normalize((0.5,), (0.5,))

])


train_loader = torch.utils.data.DataLoader(

    datasets.MNIST(root='./data', train=True, download=True, transform=transform),

    batch_size=128,

    shuffle=True

)


5. Define the Generator

class Generator(nn.Module):

    def __init__(self, latent_dim=100):

        super(Generator, self).__init__()

        self.model = nn.Sequential(

            nn.Linear(latent_dim, 256),

            nn.ReLU(True),

            nn.Linear(256, 512),

            nn.ReLU(True),

            nn.Linear(512, 1024),

            nn.ReLU(True),

            nn.Linear(1024, 28*28),

            nn.Tanh()

        )


    def forward(self, z):

        return self.model(z).view(-1, 1, 28, 28)


6. Define the Discriminator

class Discriminator(nn.Module):

    def __init__(self):

        super(Discriminator, self).__init__()

        self.model = nn.Sequential(

            nn.Linear(28*28, 512),

            nn.LeakyReLU(0.2, inplace=True),

            nn.Linear(512, 256),

            nn.LeakyReLU(0.2, inplace=True),

            nn.Linear(256, 1),

            nn.Sigmoid()

        )


    def forward(self, x):

        x = x.view(-1, 28*28)

        return self.model(x)


7. Initialize Models and Optimizers

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")


latent_dim = 100

G = Generator(latent_dim).to(device)

D = Discriminator().to(device)


criterion = nn.BCELoss()

optimizer_G = optim.Adam(G.parameters(), lr=0.0002)

optimizer_D = optim.Adam(D.parameters(), lr=0.0002)


8. Training Loop

epochs = 20


for epoch in range(epochs):

    for i, (imgs, _) in enumerate(train_loader):

        real_imgs = imgs.to(device)

        batch_size = real_imgs.size(0)


        # Adversarial labels

        valid = torch.ones(batch_size, 1).to(device)

        fake = torch.zeros(batch_size, 1).to(device)


        # ---------------------

        # Train Generator

        # ---------------------

        optimizer_G.zero_grad()

        z = torch.randn(batch_size, latent_dim).to(device)

        gen_imgs = G(z)

        g_loss = criterion(D(gen_imgs), valid)

        g_loss.backward()

        optimizer_G.step()


        # ---------------------

        # Train Discriminator

        # ---------------------

        optimizer_D.zero_grad()

        real_loss = criterion(D(real_imgs), valid)

        fake_loss = criterion(D(gen_imgs.detach()), fake)

        d_loss = (real_loss + fake_loss) / 2

        d_loss.backward()

        optimizer_D.step()


    print(f"Epoch [{epoch+1}/{epochs}]  D Loss: {d_loss.item():.4f}  G Loss: {g_loss.item():.4f}")


9. Generate and Visualize Art

G.eval()

with torch.no_grad():

    z = torch.randn(64, latent_dim).to(device)

    samples = G(z).cpu()


fig, axes = plt.subplots(8, 8, figsize=(8, 8))

for i, ax in enumerate(axes.flat):

    ax.imshow(samples[i].view(28,28), cmap='gray')

    ax.axis('off')

plt.show()


10. Key Tips for Beginners


Normalize inputs to [-1, 1] for Tanh output


Start with small datasets like MNIST or FashionMNIST


Use LeakyReLU in the discriminator


Balance training: too strong discriminator → generator struggles


Monitor loss curves but rely on visual inspection for quality


11. Next Steps / Extensions


Use CNN-based DCGAN for high-quality images


Train on datasets like CelebA or CIFAR-10


Experiment with Conditional GANs (cGANs) to generate specific classes


Apply progressive growing GANs for large images


Save and explore latent space interpolation


12. Conclusion


This beginner-friendly walkthrough shows how to implement a basic GAN to generate art-like images. GANs are powerful tools for creative AI applications, and mastering them opens the door to advanced generative models, including style transfer, image-to-image translation, and photorealistic art generation.

Learn Generative AI Training in Hyderabad

Read More

Implementing a VAE for Image Generation: A Hands-On Example

How to Use DALL·E for Text-to-Image Creation: A Beginner’s Guide

Creating Music with AI: A Practical Introduction to AI Music Generation

Exploring Style Transfer with Neural Networks: A Hands-On Guide

Visit Our Quality Thought Training Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive