This project showcases a fundamental Generative Adversarial Network (GAN) architecture. Its purpose is to learn the underlying distribution of a given image dataset and then generate new, similar images. The GAN operates with two main components: a Generator that creates images, and a Discriminator that evaluates these images, trying to distinguish between real and fake ones. Through this adversarial training process, the Generator continually improves its ability to produce increasingly realistic images.
Before running this code, make sure you have the following Python libraries installed:
TensorFlow Keras (typically included with TensorFlow) NumPy Pillow (PIL) You can install them using pip:
pip install tensorflow numpy Pillow Dataset The code expects your training images to be located in a specific directory.
Path: The data_path variable is initially set to 'C:\GANs\images1'. Image Format: The code assumes RGB images, which will be resized to 128×128 pixels. Normalization: During loading, images are normalized to the range [0,1]. Crucially, before running the script, you'll need to update the data_path variable to point to your actual image directory.
The script is structured as follows:
data_path: Specifies the directory containing your training images. input_shape: Defines the target dimensions and color channels for the images (e.g., 128×128×3 for RGB). latent_dim: The size of the random noise vector fed into the generator (100). batch_size: The number of images processed in each training step (32). epochs: The total number of training iterations (1000). load_data(data_path): A helper function responsible for loading and preprocessing images from the specified directory. build_generator(): Constructs the generator model, utilizing Conv2DTranspose layers to upsample the latent noise into image data. build_discriminator(): Creates the discriminator model, using Conv2D layers to classify images as either real or fake. Loss Functions and Optimizers: Employs BinaryCrossentropy for calculating loss and Adam optimizers for both the generator and discriminator. train_step(images): The core training logic, encapsulating a single adversarial training step. Training Loop: Iterates through the defined number of epochs and batches, calling train_step and periodically saving generated images. How to Run Prepare your dataset: Place all your training images within a dedicated directory. Update data_path: Open the script and modify the data_path variable to correctly point to your dataset directory. Python
data_path = '/path/to/your/images' # Example for Linux/macOS
data_path = 'D:\MyImages\GAN_Dataset' # Example for Windows Execute the script: Run the Python script from your terminal: Bash
python your_script_name.py The training process will begin. Every 100 epochs, 32 generated images will be saved in the same directory as your script, named generated_image_epoch_XXX_Y.png (where XXX is the epoch number and Y is the image index).
As training progresses, you'll observe the generator's ability to create more realistic images improve. You can monitor this progress by examining the generated_image_epoch_XXX_Y.png files. Initially, generated images might appear noisy or abstract, but over time, they should start to resemble your training data.
Feel free to customize the GAN's behavior by adjusting the following parameters within the code:
input_shape: Modify this tuple if your images have different dimensions or are grayscale. latent_dim: Experiment with various latent dimensions to see their impact on generation quality. batch_size: Larger batch sizes may require more GPU memory but can sometimes lead to more stable training. epochs: Increase the number of epochs to achieve better generation quality, especially with more complex datasets. Model Architectures: You can modify the build_generator() and build_discriminator() functions to try out different layer configurations, activation functions, or regularization techniques (e.g., adding more Dropout layers or adjusting strides). Optimizers and Learning Rates: Adjust the learning rates for generator_optimizer and discriminator_optimizer to fine-tune the training process.