Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
pipeline_tag: image-to-text
|
| 6 |
+
tags:
|
| 7 |
+
- image-captioning
|
| 8 |
+
- pytorch
|
| 9 |
+
- lstm
|
| 10 |
+
- computer-vision
|
| 11 |
+
- nlp
|
| 12 |
+
- flickr8k
|
| 13 |
+
metrics:
|
| 14 |
+
- bleu
|
| 15 |
+
- rouge
|
| 16 |
+
- meteor
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
# Image Caption Generator (ResNet50 + LSTM)
|
| 20 |
+
|
| 21 |
+
A ResNet50 (frozen, transfer learning) + LSTM decoder model that generates natural-language captions for images. Trained on [Flickr8k](https://www.kaggle.com/datasets/adityajn105/flickr8k).
|
| 22 |
+
|
| 23 |
+
- **Full project code, training pipeline, and documentation:** https://github.com/adhamashraf7788/Image-Caption-Generator
|
| 24 |
+
- **Live interactive demo (Hugging Face Space):** https://huggingface.co/spaces/AdhamAshraf/image_caption_generator
|
| 25 |
+
|
| 26 |
+
## Files in this repo
|
| 27 |
+
|
| 28 |
+
```
|
| 29 |
+
vocab.json # vocabulary (shared across both checkpoints)
|
| 30 |
+
base_resnet_lstm/
|
| 31 |
+
βββ best_model.pt # baseline checkpoint
|
| 32 |
+
βββ config.yaml # baseline training config
|
| 33 |
+
resnet_lstm_regularized/
|
| 34 |
+
βββ best_model.pt # regularized checkpoint (recommended -- best results)
|
| 35 |
+
βββ config.yaml # regularized training config
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
Two checkpoints are provided:
|
| 39 |
+
|
| 40 |
+
| Checkpoint | BLEU-4 (beam-3) | Notes |
|
| 41 |
+
|---|---|---|
|
| 42 |
+
| `base_resnet_lstm/best_model.pt` | 0.1364 | Initial baseline |
|
| 43 |
+
| `resnet_lstm_regularized/best_model.pt` | **0.1557** | Added LSTM output dropout, weight decay, gradient clipping β recommended |
|
| 44 |
+
|
| 45 |
+
Both checkpoints share the same `vocab.json` (identical vocabulary, 2,662 tokens).
|
| 46 |
+
|
| 47 |
+
## Architecture
|
| 48 |
+
|
| 49 |
+
```
|
| 50 |
+
Image β ResNet50 (frozen, ImageNet-pretrained) β 2048-d feature
|
| 51 |
+
β Linear(2048 β 256) projection
|
| 52 |
+
β fed as first input step to a 1-layer LSTM (hidden_dim=512)
|
| 53 |
+
β LSTM generates caption word-by-word (beam search recommended, width 3)
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
Full architecture, preprocessing, and training details: see the [GitHub README](https://github.com/adhamashraf7788/Image-Caption-Generator#architecture).
|
| 57 |
+
|
| 58 |
+
## How to use
|
| 59 |
+
|
| 60 |
+
Requires the inference code from the [GitHub repo](https://github.com/adhamashraf7788/Image-Caption-Generator) (`src/inference/predict.py` and its dependencies) β these checkpoints are not standalone `transformers`-compatible weights, they're plain PyTorch `state_dict`s wrapped with config metadata.
|
| 61 |
+
|
| 62 |
+
```python
|
| 63 |
+
from huggingface_hub import hf_hub_download
|
| 64 |
+
from src.inference.predict import Predictor # from the GitHub repo's src/
|
| 65 |
+
|
| 66 |
+
checkpoint_path = hf_hub_download(
|
| 67 |
+
repo_id="AdhamAshraf/image-caption-generator",
|
| 68 |
+
filename="resnet_lstm_regularized/best_model.pt",
|
| 69 |
+
)
|
| 70 |
+
vocab_path = hf_hub_download(
|
| 71 |
+
repo_id="AdhamAshraf/image-caption-generator",
|
| 72 |
+
filename="vocab.json",
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
predictor = Predictor(checkpoint_path=checkpoint_path, vocab_path=vocab_path, device="cpu")
|
| 76 |
+
caption = predictor.predict("path/to/image.jpg")
|
| 77 |
+
print(caption)
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+
## Training data
|
| 81 |
+
|
| 82 |
+
[Flickr8k](https://www.kaggle.com/datasets/adityajn105/flickr8k) β 8,091 images, 5 human-written reference captions each. Split 80/10/10 (by image, not caption, to avoid leakage) using a fixed seed.
|
| 83 |
+
|
| 84 |
+
## Evaluation results (test set, 810 images)
|
| 85 |
+
|
| 86 |
+
| Metric | Baseline + greedy | Baseline + beam-3 | Regularized + greedy | **Regularized + beam-3** |
|
| 87 |
+
|---|---|---|---|---|
|
| 88 |
+
| BLEU-1 | 0.5127 | 0.5240 | 0.5444 | **0.5517** |
|
| 89 |
+
| BLEU-4 | 0.1221 | 0.1364 | 0.1435 | **0.1557** |
|
| 90 |
+
| ROUGE-L | 0.4177 | 0.4265 | 0.4434 | **0.4527** |
|
| 91 |
+
| METEOR | 0.3266 | 0.3267 | 0.3480 | **0.3528** |
|
| 92 |
+
|
| 93 |
+
Full evaluation methodology, qualitative examples, and failure-case analysis: see the [GitHub README](https://github.com/adhamashraf7788/Image-Caption-Generator#evaluation-metrics-and-results).
|
| 94 |
+
|
| 95 |
+
## Limitations
|
| 96 |
+
|
| 97 |
+
- Trained on a small (8k image) dataset; struggles with image content/styles underrepresented in Flickr8k (predominantly people, dogs, and outdoor scenes).
|
| 98 |
+
- Even the regularized model still shows some overfitting past its best epoch.
|
| 99 |
+
- Generated captions are sometimes fluent but not fully grounded in image-specific detail.
|
| 100 |
+
|
| 101 |
+
See the [GitHub README's Limitations section](https://github.com/adhamashraf7788/Image-Caption-Generator#known-limitations--next-steps) for a full discussion, including a documented failure case and how regularization + beam search improved it.
|