# nssharmaofficial/image-caption-generator architecture

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

- Interactive diagram: https://gitdiagram.com/nssharmaofficial/image-caption-generator
- Repository: https://github.com/nssharmaofficial/image-caption-generator

## Overview

##### Project Overview & Main Components
- Data & Preprocessing:
- Input image files from the Flickr30k dataset.
- Caption files and associated train/validation splits (train_list*.txt, val_list*.txt).
- A vocabulary creation module (vocab.py) that processes the raw captions (captions.txt) into word2index and index2word mappings, output to vocabulary.txt.
- Data Loader Component:
- The custom ImageCaptionDataset class in dataset.py that loads and preprocesses images and captions. This includes padding captions (using PADDING_INDEX) and preparing image tensors.
- Model Architecture:
- Encoder: Utilizes a pre-trained ResNet50 (with the final classification layer removed) plus a linear layer that transforms the extracted image features to a desired dimension (IMAGE_EMB_DIM).
- Embedding Layer: Transforms input word indices from captions into dense vector representations (WORD_EMB_DIM).
- Decoder: Comprises an LSTM for sequential processing and a fully connected (FC) layer followed by LogSoftmax to output probabilities for each word in the vocabulary.
- Training & Evaluation:
- Training script (train.py and train_whole_sequence.py) ties together data loading, model processing, and iteratively trains by generating captions word-by-word.
- Evaluation and visualization are handled by test_show.py, which also computes BLEU scores for quality assessment.
- Inference:
- A separate script (predict_sample.py) uses the trained model to generate captions for new sample images.

##### Important Architectural Patterns & Design Principles
- Separation of concerns: Distinguish modules for data handling (dataset, vocabulary), model computation (encoder, embedding, decoder), and separate scripts for training, evaluating, and inference.
- Modularity: Each script (e.g., vocab.py, train.py, test_show.py) performs a distinct role and can be individually maintained.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Image Caption Generator (PyTorch)"
        %% Data & Preprocessing Layer
        subgraph "Data & Preprocessing Layer"
            A["Dataset (Flickr30k Images)"]:::data
            B["Captions File (captions.txt)"]:::data
            subgraph "Data Splits"
                C1["train_list.txt"]:::data
                C2["train_list_sorted.txt"]:::data
                C3["val_list.txt"]:::data
                C4["val_list_sorted.txt"]:::data
            end
            D["Vocabulary Module"]:::data
            E["Data Loader (ImageCaptionDataset)"]:::data
        end

        %% Model Architecture Layer
        subgraph "Model Architecture Layer"
            F["Encoder (ResNet50+Linear)"]:::model
            G["Embedding Layer"]:::model
            H["Decoder (LSTM+FC+LogSoftmax)"]:::model
        end

        %% Training & Evaluation Layer
        subgraph "Training & Evaluation Layer"
            I["Training Module (train.py)"]:::process
            J["Whole Sequence Training (train_whole_sequence.py)"]:::process
            K["Evaluation & Visualization (test_show.py)"]:::process
        end

        %% Inference Layer
        subgraph "Inference Layer"
            L["Inference Module (predict_sample.py)"]:::process
        end

        %% External Configuration
        M["Configuration (config.py)"]:::config

        %% Data Flow Connections
        A --> D
        B --> D
        C1 --> D
        C2 --> D
        C3 --> D
        C4 --> D
        D --> E

        %% Model Flow Connections
        E --> F
        F --> G
        G --> H

        %% Training Connections
        E --> I
        I --> F
        I --> G
        I --> H
        I --> M
        I --> J
        I --> K

        %% Inference Connections
        L --> F
        L --> G
        L --> H
        L --> M
    end

    %% Click Events for Component Mapping
    click E "https://github.com/nssharmaofficial/image-caption-generator/blob/main/code/dataset.py"
    click D "https://github.com/nssharmaofficial/image-caption-generator/blob/main/code/vocab.py"
    click F "https://github.com/nssharmaofficial/image-caption-generator/blob/main/code/model.py"
    click G "https://github.com/nssharmaofficial/image-caption-generator/blob/main/code/model.py"
    click H "https://github.com/nssharmaofficial/image-caption-generator/blob/main/code/model.py"
    click I "https://github.com/nssharmaofficial/image-caption-generator/blob/main/code/train.py"
    click J "https://github.com/nssharmaofficial/image-caption-generator/blob/main/code/train_whole_sequence.py"
    click K "https://github.com/nssharmaofficial/image-caption-generator/blob/main/code/test_show.py"
    click L "https://github.com/nssharmaofficial/image-caption-generator/blob/main/code/predict_sample.py"
    click M "https://github.com/nssharmaofficial/image-caption-generator/blob/main/code/config.py"
    click C1 "https://github.com/nssharmaofficial/image-caption-generator/blob/main/train_list.txt"
    click C2 "https://github.com/nssharmaofficial/image-caption-generator/blob/main/train_list_sorted.txt"
    click C3 "https://github.com/nssharmaofficial/image-caption-generator/blob/main/val_list.txt"
    click C4 "https://github.com/nssharmaofficial/image-caption-generator/blob/main/val_list_sorted.txt"

    %% Styles
    classDef data fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef model fill:#A3E4D7,stroke:#117864,stroke-width:2px;
    classDef process fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef config fill:#F5B7B1,stroke:#C0392B,stroke-width:2px;
```
