VOOZH about

URL: https://www.geeksforgeeks.org/machine-learning/deep-convolutional-gan-with-keras/

⇱ Deep Convolutional GAN with Keras - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Deep Convolutional GAN with Keras

Last Updated : 16 May, 2026

Deep Convolutional GAN (DCGAN) is a GAN architecture proposed by researchers from MIT and Facebook AI Research to improve the stability of GAN training using convolutional neural networks.

  • Uses convolutional layers for image generation tasks
  • Introduces architectural changes for stable GAN training
  • Widely used in computer vision and image synthesis
  • Effective for generating realistic images
  • Can be applied to datasets such as Fashion MNIST for clothing image generation

Architecture

Deep Convolutional GAN consists of two neural networks, a generator and a discriminator. The generator creates realistic images from random noise, while the discriminator identifies whether an image is real or generated.

👁 generator
Architecture

Generator

The generator converts a 100-dimensional noise vector into a 64×64×3 image using fractionally strided convolution layers. It uses Batch Normalization and ReLU activations for stable training and removes fully connected layers.

  • Converts noise into images
  • Uses fractionally strided convolutions
  • Applies Batch Normalization and ReLU
  • Gradually increases image dimensions

Discriminator

The discriminator acts as a convolutional classifier that determines whether an image is real or generated. It uses strided convolutions and LeakyReLU activations for improved feature learning.

  • Classifies images as real or fake
  • Uses strided convolution layers
  • Applies LeakyReLU activations
  • Removes fully connected layers

Implementation

In this implementation, DCGAN is built using Keras and TensorFlow on the Fashion MNIST dataset. Since Fashion MNIST contains grayscale images of size 28×28, the original DCGAN architecture is slightly modified from the standard 64×64×3 image setup.

Step1: Importing Required Libraries

  • tensorflow and keras are used for creating and training neural networks
  • numpy is used for numerical operations and array handling
  • matplotlib.pyplot is used for image visualization
  • tqdm displays training progress bars
  • IPython.display helps display generated images during training

Step 2: Loading the Fashion-MNIST Dataset

The Fashion-MNIST dataset is loaded using tf.keras.datasets, which provides direct access to the dataset without manual downloading. The dataset contains 28×28 grayscale images for training and testing.

  • Contains 60,000 training images and 10,000 test images
  • Each image has dimensions 28×28×1
  • Pixel values range from 0 to 255
  • Values are divided by 255 for normalization

Output:

((60000, 28, 28), (10000, 28, 28))

Step 3: Visualizing the Dataset

In this step, some images from the Fashion-MNIST dataset are visualized using the Matplotlib library.

  • Uses Matplotlib for image visualization
  • Displays sample Fashion-MNIST images
  • Helps understand the dataset before training

Output:

👁 datasetimage
Output

Step 4: Defining Training Parameters

In this step, training parameters such as batch size are defined and the dataset is divided into smaller batches for training.

  • Defines batch size for training
  • Splits dataset into batches
  • Randomly samples training data for each batch
  • Improves training efficiency and stability

Step 5: Defining the Generator

The generator converts a 100-dimensional noise vector into a (28,28,1) image using transpose convolution layers and Batch Normalization.

  • Reshapes input to (7,7,128)
  • Uses transpose convolutions for upsampling
  • Applies Batch Normalization
  • Generates grayscale images

Output:

👁 output
Output

Step 6: Defining the Discriminator

The discriminator takes a (28×28×1) image as input and outputs a scalar value indicating whether the image is real or generated.

  • Accepts grayscale images as input
  • Uses convolution layers for feature extraction
  • Classifies images as real or fake
  • Outputs a single prediction value

Output:

👁 discriminator
Discriminator

Step 7: Compiling the DCGAN Model

In this step, the discriminator is compiled first and its training is temporarily disabled while training the generator in the combined DCGAN model.

  • Compiles the discriminator model
  • Freezes discriminator weights during generator training
  • Combines generator and discriminator into DCGAN
  • Helps train generator to produce realistic images

Step 8: Defining the Training Procedure

In this step, the training process for the DCGAN model is defined. The tqdm package is used to visualize training progress during each epoch.

  • Defines GAN training loop
  • Trains generator and discriminator iteratively
  • Uses tqdm for progress visualization
  • Monitors training efficiently

Step 9: Generating and Saving Images

In this step, a function is created to generate and save images during training. These generated images can later be used to create a GIF showing the training progress.

Step 10: Training the DCGAN Model

Before training, the dataset is reshaped to include the color channel dimension and divided into batches. The DCGAN model is then trained for multiple epochs.

Output:

👁 output2
Output

Step 11: Creating GIF from Generated Images

In this step, a function is created to convert the saved generated images into a GIF for visualizing the training progress of the DCGAN model.

  • Uses saved images from each epoch
  • Combines images into an animated GIF
  • Helps visualize image generation improvements during training
👁 Image
Generated Images results

Download full code from here

Applications

DCGANs are widely used in image generation and computer vision tasks due to their ability to learn meaningful visual features.

  • Image generation and synthesis
  • Fashion and face image creation
  • Data augmentation for deep learning models
  • Image super-resolution and enhancement
  • Feature extraction for classification tasks
  • Art, animation, and creative content generation
Comment
Article Tags: