# roshan2828/image-captioning architecture

> Architecture diagram of the GitHub repository roshan2828/image-captioning, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/roshan2828/image-captioning
- Repository: https://github.com/roshan2828/image-captioning

## Overview

This is a PyTorch image-captioning project: training turns Flickr8k images and captions into a trained encoder-decoder, and inference uses a model checkpoint to generate a caption for an image. The graph separates data preparation, model internals, training, and inference. The README describes validation-based early stopping, but the sampled training code monitors training loss only; the graph reflects the code. Relationships are limited to calls, reads, and writes visible in the excerpts or explicitly documented.

Main entry points: [`infer.py`](https://github.com/roshan2828/image-captioning/blob/main/infer.py) (Inference script), [`train.py`](https://github.com/roshan2828/image-captioning/blob/main/train.py) (Training script), [`dataset.py`](https://github.com/roshan2828/image-captioning/blob/main/dataset.py) (Image-caption dataset), [`model.py`](https://github.com/roshan2828/image-captioning/blob/main/model.py) (Caption model)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Caption data"]
  node_dataset["Image-caption dataset<br/>[dataset.py]"]
  node_vocab["Vocabulary builder<br/>[utils.py]"]
  node_captions["Caption file"]
  node_images["Image files"]
end

subgraph group_model["Caption model"]
  node_model["Caption model<br/>[model.py]"]
  node_encoder["ResNet encoder<br/>[model.py]"]
  node_attention["Spatial attention<br/>[model.py]"]
  node_decoder["LSTM decoder<br/>[model.py]"]
end

subgraph group_training["Training"]
  node_trainer["Training script<br/>[train.py]"]
  node_loss["Caption loss<br/>[train.py]"]
  node_checkpoint["Model checkpoint"]
end

subgraph group_inference["Inference"]
  node_inference["Inference script<br/>[infer.py]"]
  node_input_image["Test image"]
  node_caption_output["Generated caption"]
end

node_user(("User"))

node_trainer -->|"reads captions"| node_captions
node_trainer -->|"builds vocabulary"| node_vocab
node_trainer -->|"constructs dataset"| node_dataset
node_dataset -->|"reads captions"| node_captions
node_dataset -->|"loads images"| node_images
node_dataset -->|"encodes tokens"| node_vocab
node_trainer -->|"trains model"| node_model
node_model -->|"extracts features"| node_encoder
node_model -->|"generates logits"| node_decoder
node_decoder -->|"computes context"| node_attention
node_trainer -->|"computes loss"| node_loss
node_trainer -->|"saves weights"| node_checkpoint
node_user -.->|"starts inference"| node_inference
node_inference -->|"reads captions"| node_captions
node_inference -->|"builds vocabulary"| node_vocab
node_inference -->|"loads weights"| node_checkpoint
node_inference -->|"loads image"| node_input_image
node_inference -->|"generates caption"| node_model
node_inference -->|"prints caption"| node_caption_output

click node_trainer "https://github.com/roshan2828/image-captioning/blob/main/train.py"
click node_dataset "https://github.com/roshan2828/image-captioning/blob/main/dataset.py"
click node_vocab "https://github.com/roshan2828/image-captioning/blob/main/utils.py"
click node_model "https://github.com/roshan2828/image-captioning/blob/main/model.py"
click node_encoder "https://github.com/roshan2828/image-captioning/blob/main/model.py"
click node_attention "https://github.com/roshan2828/image-captioning/blob/main/model.py"
click node_decoder "https://github.com/roshan2828/image-captioning/blob/main/model.py"
click node_loss "https://github.com/roshan2828/image-captioning/blob/main/train.py"
click node_inference "https://github.com/roshan2828/image-captioning/blob/main/infer.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_dataset,node_vocab,node_captions,node_images,node_user toneBlue
class node_model,node_encoder,node_attention,node_decoder toneAmber
class node_trainer,node_loss,node_checkpoint toneMint
class node_inference,node_input_image,node_caption_output toneRose
```

## Components

### Caption data

- [Image-caption dataset](https://github.com/roshan2828/image-captioning/blob/main/dataset.py): `dataset.py`
- [Vocabulary builder](https://github.com/roshan2828/image-captioning/blob/main/utils.py): `utils.py`
- **Caption file**
- **Image files**

### Caption model

- [Caption model](https://github.com/roshan2828/image-captioning/blob/main/model.py): `model.py`
- [ResNet encoder](https://github.com/roshan2828/image-captioning/blob/main/model.py): `model.py`
- [Spatial attention](https://github.com/roshan2828/image-captioning/blob/main/model.py): `model.py`
- [LSTM decoder](https://github.com/roshan2828/image-captioning/blob/main/model.py): `model.py`

### Training

- [Training script](https://github.com/roshan2828/image-captioning/blob/main/train.py): `train.py`
- [Caption loss](https://github.com/roshan2828/image-captioning/blob/main/train.py): `train.py`
- **Model checkpoint**

### Inference

- [Inference script](https://github.com/roshan2828/image-captioning/blob/main/infer.py): `infer.py`
- **Test image**
- **Generated caption**

### Other components

- **User**

## Connections

- Training script → Caption file: reads captions (evidence: `train.py`)
- Training script → Vocabulary builder: builds vocabulary (evidence: `train.py`)
- Training script → Image-caption dataset: constructs dataset (evidence: `train.py`)
- Image-caption dataset → Caption file: reads captions (evidence: `dataset.py`)
- Image-caption dataset → Image files: loads images (evidence: `dataset.py`)
- Image-caption dataset → Vocabulary builder: encodes tokens (evidence: `dataset.py`)
- Training script → Caption model: trains model (evidence: `train.py`)
- Caption model → ResNet encoder: extracts features (evidence: `model.py`)
- Caption model → LSTM decoder: generates logits (evidence: `model.py`)
- LSTM decoder → Spatial attention: computes context (evidence: `model.py`)
- Training script → Caption loss: computes loss (evidence: `train.py`)
- Training script → Model checkpoint: saves weights (evidence: `train.py`)
- User → Inference script: starts inference
- Inference script → Caption file: reads captions (evidence: `infer.py`)
- Inference script → Vocabulary builder: builds vocabulary (evidence: `infer.py`)
- Inference script → Model checkpoint: loads weights (evidence: `infer.py`)
- Inference script → Test image: loads image (evidence: `infer.py`)
- Inference script → Caption model: generates caption (evidence: `infer.py`)
- Inference script → Generated caption: prints caption (evidence: `infer.py`)
