# sriramsohanbasa/gan_implementation architecture

> Architecture diagram of the GitHub repository sriramsohanbasa/gan_implementation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sriramsohanbasa/gan_implementation
- Repository: https://github.com/sriramsohanbasa/gan_implementation

## Overview

##### Project Type and Purpose
- The project is a machine learning application focused on image synthesis using Deep Convolutional GANs (DCGAN). Its goal is to generate realistic images using the CIFAR-10 dataset.
- Key functionalities include data ingestion, defining and training the generator and discriminator models, and integrating logging with Weights & Biases (WandB).

##### File Structure
- Top-Level Files and Directories:
- main.py: Entry point for training the model.
- config.py: Contains configuration settings including paths (e.g., where generated images will be saved).
- data/: Contains the CIFAR-10 dataset batches.
- models/: Contains the GAN-related submodules – generator (models/generator.py), discriminator (models/discriminator.py), and a combined gan module (models/gan.py).
- utils/: Provides utility functions for callbacks, data loading (data_loader.py), image handling (image_utils.py), and training routines.
- logs/: Holds training logs showing events from model training.
- wandb/: Contains WandB logs and configurations for tracking experiments.
- generated_images/: Where output images are stored.
- This modular file structure separates data management, model definitions, training utilities, and experiment logging clearly.

##### README for Architectural Insights
- The README outlines the two key components: the Generator and the Discriminator.
- It describes the technical stack: Python (with PyTorch as the framework) and WandB for logging.
- It explains the role of each network:
▪ Generator: Converts a noise vector into a 32x32 image using upsampling layers, batch normalization, and activations like ReLU/Tanh.
▪ Discriminator: Uses convolutional layers and LeakyReLU activations to differentiate between real and generated images with a sigmoid output.
- The README also details training mechanics, loss functions, hyperparameters, and logging processes.

a. Main Components to Include:
▪ Data Management: The CIFAR-10 dataset (data/ directory), along with data processing and normalization in utils/data_loader.py.
▪ Model Components:
– Generator (models/generator.py) – accepts a latent noise vector and outputs a 32x32 image.
– Discriminator (models/discriminator.py) – evaluates images to classify them as real or generated.
– GAN orchestration (models/gan.py) which may combine the generator and discriminator for coordinated training.
▪ Training Infrastructure:
– The central training loop (main.py, assisted by utils/training.py) that alternates training the discriminator and generator.
– Handling of batch processing/resampling.
▪ Experiment Logging:
– WandB integration (with files in the wandb directory and configurations in config.py) for real-time logging and visualization.
– Additional logging files in logs/ and saving of generated images in generated_images/.
▪ Utility Modules:
– Callbacks (utils/callbacks.py)
– Image utilities (utils/image_utils.py) for processing output images.

b. Relationships and Interactions:
▪ Data from the CIFAR-10 dataset is ingested by the data loader → Preprocessed and passed into the training loop.
▪ The training loop calls the Discriminator and Generator alternately.
▪ Losses computed from these interactions drive the parameter updates.
▪ The training process logs metrics, images, and model checkpoints via WandB and local file logs.

c. Architectural Patterns and Design Principles:
▪ Separation of Concerns: Different modules handle distinct parts of the workflow (data, models, training, logging).
▪ Modular Design: Each core component (generator, discriminator, callbacks, etc.) is encapsulated in its own file, making the system easy to extend.
▪ Clear Data Flow: Data moves from the input (CIFAR-10) through processing, training, model updates, and logging.

d. Relevant Technologies/Frameworks:
▪ PyTorch for deep learning model implementation.
▪ WandB for experiment tracking and logging.
▪ Standard Python libraries and configuration management.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data Management
    subgraph "Data Management"
        CIFAR10["CIFAR-10 Data"]:::data
        DataLoader["Data Loader (utils/data_loader.py)"]:::utility
    end

    %% Model Components
    subgraph "Model Components"
        Generator["Generator (models/generator.py)"]:::model
        Discriminator["Discriminator (models/discriminator.py)"]:::model
        GAN["Combined GAN (models/gan.py)"]:::model
    end

    %% Training Infrastructure
    subgraph "Training Infrastructure"
        TrainingLoop["Main Training Loop (main.py)"]:::training
        TrainingUtils["Training Utilities (utils/training.py)"]:::training
        LossComp["Loss Computation"]:::training
    end

    %% Experiment Logging
    subgraph "Experiment Logging"
        Config["Logging Config (config.py)"]:::logging
        WandB["WandB Logger (wandb/)"]:::logging
        LocalLogs["Local Logs (logs/)"]:::logging
        GeneratedImages["Generated Images Output"]:::logging
    end

    %% Utility Modules
    subgraph "Utility Modules"
        Callbacks["Callbacks (utils/callbacks.py)"]:::utility
        ImageUtils["Image Processing Utilities (utils/image_utils.py)"]:::utility
    end

    %% Data Flow
    CIFAR10 -->|"feeds"| DataLoader
    DataLoader -->|"preprocesses"| TrainingLoop
    TrainingLoop -->|"trains"| Generator
    TrainingLoop -->|"trains"| Discriminator
    TrainingLoop -->|"orchestrates"| GAN
    TrainingLoop -->|"computes_loss"| LossComp
    LossComp -->|"feedback"| Generator
    LossComp -->|"feedback"| Discriminator
    TrainingLoop -->|"uses_util"| TrainingUtils
    TrainingLoop -->|"invokes"| Callbacks
    TrainingLoop -->|"processes"| ImageUtils
    TrainingLoop -->|"logs_metrics"| WandB
    TrainingLoop -->|"saves_logs"| LocalLogs
    TrainingLoop -->|"saves_images"| GeneratedImages
    Config -->|"configures"| WandB

    %% Click Events
    click CIFAR10 "https://github.com/sriramsohanbasa/gan_implementation/tree/main/data/"
    click DataLoader "https://github.com/sriramsohanbasa/gan_implementation/blob/main/utils/data_loader.py"
    click Generator "https://github.com/sriramsohanbasa/gan_implementation/blob/main/models/generator.py"
    click Discriminator "https://github.com/sriramsohanbasa/gan_implementation/blob/main/models/discriminator.py"
    click GAN "https://github.com/sriramsohanbasa/gan_implementation/blob/main/models/gan.py"
    click TrainingLoop "https://github.com/sriramsohanbasa/gan_implementation/blob/main/main.py"
    click TrainingUtils "https://github.com/sriramsohanbasa/gan_implementation/blob/main/utils/training.py"
    click Config "https://github.com/sriramsohanbasa/gan_implementation/blob/main/config.py"
    click WandB "https://github.com/sriramsohanbasa/gan_implementation/tree/main/wandb/"
    click LocalLogs "https://github.com/sriramsohanbasa/gan_implementation/tree/main/logs/"
    click Callbacks "https://github.com/sriramsohanbasa/gan_implementation/blob/main/utils/callbacks.py"
    click ImageUtils "https://github.com/sriramsohanbasa/gan_implementation/blob/main/utils/image_utils.py"

    %% Styles
    classDef data fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef model fill:#ccffcc,stroke:#009900,stroke-width:2px;
    classDef training fill:#cce5ff,stroke:#003366,stroke-width:2px;
    classDef logging fill:#fff2cc,stroke:#ff9900,stroke-width:2px;
    classDef utility fill:#e6ccff,stroke:#663399,stroke-width:2px;
```
