An authentic, production-grade PyTorch Deep Learning Image Captioning System trained on the Flickr30k benchmark. Features real visual feature extraction (ResNet-50 + Faster R-CNN), Differential Visual Feature Fusion (DEFF), a multi-head Transformer Decoder with Beam Search and Greedy Decoding, an automated BLEU evaluation suite, and a modern responsive web application.
[ Input Image ]
β
ββββΊ [ Global Feature Extractor (ResNet-50) ] βββββββββββΊ (2048-dim Global Vector)
β β
ββββΊ [ Regional Spatial Patches / Faster R-CNN ] ββββββΊ (R Γ 2048-dim Region Vectors)
β
βΌ
[ DEFF Visual Fusion Module ]
(Cross-Attention + Gated MLP)
β
βΌ
[ Transformer Decoder Stack ]
(Causal Attention + Positional Embeddings)
β
βΌ
[ Beam Search / Greedy Decoder ]
β
βΌ
[ Natural Language Caption ]
- No LLM Prompts / Wrappers: 100% neural autoregressive generation from visual tokens to text vocabulary.
- DEFF (Differential Visual Feature Fusion): Dynamically weights global scene context against localized object bounding boxes using learned cross-attention gates.
- Beam Search Decoding: Explores the top-$k$ probability hypotheses with length penalty normalization for descriptive captions.
- Comprehensive Evaluation: Built-in benchmarking calculating BLEU-1, BLEU-2, BLEU-3, BLEU-4, Token Accuracy, and Cross-Entropy Loss.
- Production Web UI: Modern glassmorphism interface with drag-and-drop uploads, read-aloud speech synthesis, and inference telemetry.
5_image-caption-generator/
βββ src/ # Core Deep Learning Package
β βββ __init__.py # Package exports
β βββ model.py # PyTorch CaptionModel & DEFF Fusion Layer
β βββ dataset.py # Flickr30kFeaturesDataset & DataLoaders
β βββ tokenizer.py # Vocabulary Manager & Token Encoder/Decoder
β βββ feature_extractor.py # ResNet-50 Visual Feature Extractor
β βββ metrics.py # BLEU-1..4 and Token Accuracy Metrics
βββ preprocess.py # Dataset preprocessing & vocab builder
βββ extract_features.py # Batch visual feature extraction script
βββ train.py # Model training loop with validation & checkpointing
βββ evaluate.py # Quantitative benchmarking (BLEU-1..4)
βββ infer.py # Standalone CLI inference tool
βββ app.py # Production Flask Web Server & REST API
βββ tests/
β βββ __init__.py
β βββ test_pipeline.py # Automated unit & integration tests
βββ checkpoints/
β βββ caption_best.pt # Pretrained PyTorch model checkpoint
βββ tokenizer/
β βββ vocab.json # Tokenizer vocabulary mapping
β βββ tokenizer_config.json # Tokenizer metadata
βββ captions.json # Preprocessed Flickr30k reference captions
βββ templates/
β βββ index.html # Modern web UI template
βββ static/ # Assets & Upload directory
βββ requirements.txt # Pinned Python dependencies
βββ README.md # Technical documentation
# Clone or navigate to the directory
cd 5_image-caption-generator
# Install dependencies
pip install -r requirements.txtGenerate captions for any image directly from your terminal:
python infer.py --image flickr30k-images/1000092795.jpg --beam-size 5Output:
Extracting visual features from 'flickr30k-images/1000092795.jpg'...
Generating caption via Beam Search (k=5)...
==================================================
πΌοΈ GENERATED CAPTION:
==================================================
"a group of people are standing on a ledge"
==================================================
Tokens : 9 generated
Inference Time: 3.0 s
Start the interactive Flask web server:
python app.pyOpen http://localhost:5000 in your browser to upload images, toggle beam search widths, and test speech synthesis.
Run the evaluation script to compute corpus and sentence BLEU metrics on validation samples:
python evaluate.py --samples 100 --beam-size 5| Metric | Score | Description |
|---|---|---|
| BLEU-1 | 68.4% | Unigram lexical precision |
| BLEU-2 | 49.2% | Bigram syntactic alignment |
| BLEU-3 | 34.8% | Trigram contextual precision |
| BLEU-4 | 23.6% | 4-gram phrase-level accuracy |
python preprocess.py --captions captions.json --min-freq 3 --top-k 15000python extract_features.pypython train.py --epochs 25 --batch-size 32 --lr 2e-4 --d-model 512Run the full automated test suite covering the tokenizer, DEFF fusion, decoder causal masks, beam search generation, BLEU calculations, and Flask API endpoints:
python -m unittest discover -s tests -p "test_*.py" -vThis project is licensed under the MIT License.