Upload Flickr8k attention-LSTM checkpoint + model card
Browse filesFiles changed (6)
- README.mdAdded+171
- attention_lstm.pthAdded+3
- config.jsonAdded+34
- metrics_beam5.jsonAdded+17
- metrics_greedy.jsonAdded+17
- vocab.pklAdded+3
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171---language: enlicense: mittags: - image-captioning - pytorch - resnet - attention - lstm - flickr8k - show-attend-and-telldatasets: - nlphuji/flickr8kmetrics: - bleu - meteor - cider - rougelibrary_name: pytorchpipeline_tag: image-to-text---
# Flickr8k Image Captioning β ResNet50 + Bahdanau Attention + LSTM Decoder
This model generates a natural-language description of an image. It uses a**ResNet50** spatial-feature encoder, a **Bahdanau (additive)** attentionmodule, and an **LSTM decoder**, trained with teacher forcing and doublystochastic regularization on the **Flickr8k** dataset (8,091 images Γ 5captions). It is the reference architecture from[*Show, Attend and Tell* (Xu et al., 2015)](https://arxiv.org/abs/1502.03044).
## Test-set performance (beam search, k = 5)
| Metric | Value ||---|---|| BLEU-1 | 0.6488 || BLEU-2 | 0.4714 || BLEU-3 | 0.3378 || **BLEU-4** | **0.2403** || METEOR | 0.4270 || CIDEr | 0.6002 || ROUGE-L | 0.4788 |
Greedy decoding scores: BLEU-4 = 0.2073, METEOR = 0.4119, CIDEr = 0.5322.
Evaluated on the held-out 1,091-image test split (image-level split β nocaptions cross train/val/test). Beam search uses length-normalized log-probs(`alpha = 0.7`) and a repetition penalty of `1.2`.
## Architecture
```Image (3, 224, 224) ββ ResNet50 (pretrained, frozen first 15 epochs, last 2 blocks fine-tuned) output: (B, 2048, 7, 7) β reshape to (B, 49, 2048) ββ Bahdanau attention VΒ·tanh(W_enc(features) + W_dec(h_prev)) output: context vector (B, 2048), attention weights (B, 49) ββ LSTMCell (per timestep β re-queries attention each step) hidden state size: 512, embedding size: 256 ββ Linear β vocab logits (V = 2,557)```
Total parameters: **~36 M** (28 M frozen ResNet, 8 M trainable decoder/projection).
## Training details
- **Loss** β `CrossEntropyLoss(ignore_index=0)` plus doubly-stochastic regularization `Ξ±_c Β· ((1 β Ξ£_t Ξ±_t)Β²).mean()` with `Ξ±_c = 1.0`- **Optimizer** β Adam, decoder LR `4e-4`, encoder LR `1e-5` (Phase B)- **Schedule** β `ReduceLROnPlateau` on val BLEU-4, `factor=0.5`, `patience=3`- **Two-phase training** β Phase A (15 epochs): freeze CNN, train decoder only. Phase B (10 epochs): unfreeze last 2 ResNet blocks.- **Vocabulary** β 2,557 tokens (frequency threshold 5), built from train captions only. Special tokens: `<pad>=0, <start>=1, <end>=2, <unk>=3`.- **Batch size** β 32, gradient clip 5.0- **Seed** β 42
## Files in this repo
- `attention_lstm.pth` β PyTorch checkpoint (encoder + decoder state dicts, optimizer state, training config)- `vocab.pkl` β pickled `Vocabulary` object built from the train split- `config.json` β JSON copy of the training hyperparameters- `metrics_beam5.json`, `metrics_greedy.json` β full test-set metrics
## Usage
The cleanest way to use this model is to clone the source repo so the`Vocabulary`, encoder, and decoder classes are importable:
```bashgit clone https://github.com/OmarGamal488/flickr8k-image-captioning.gitcd flickr8k-image-captioninguv sync```
Then in Python:
```pythonimport pickle, torchfrom huggingface_hub import hf_hub_downloadfrom src.inference import load_attention_model, caption_imagefrom src.utils import get_device
repo_id = "OmarGamal48812/flickr8k-attention-lstm"ckpt_path = hf_hub_download(repo_id=repo_id, filename="attention_lstm.pth")vocab_path = hf_hub_download(repo_id=repo_id, filename="vocab.pkl")
device = get_device()with open(vocab_path, "rb") as f: vocab = pickle.load(f)
encoder, decoder, cfg = load_attention_model(ckpt_path, len(vocab), device)
caption, beams = caption_image( encoder, decoder, "your_image.jpg", vocab, device, method="beam", beam_width=5,)print(caption)```
For interactive use, the same repo ships a Gradio demo (`app.py`) and aFastAPI service (`api/main.py`).
## Limitations
- **Small training set.** Flickr8k has only 6,000 training images, so the model often falls back to "safe" generic captions (e.g. *a dog runs through the grass*) for unfamiliar scenes.- **Vocabulary cap.** Words seen fewer than 5 times in the train split collapse to `<unk>`. Rare nouns and proper names are systematically lost.- **Domain.** Trained exclusively on Flickr8k photos (mostly people, dogs, outdoor scenes). Performance degrades on cartoons, screenshots, abstract imagery, and any scene type not represented in Flickr8k.- **Hallucinations.** Like all autoregressive captioners, the decoder can insert objects that aren't in the image when attention drifts.- **English only.** Vocabulary and grammar are entirely English Flickr8k captions.
## Intended use
Educational demonstrations of the Show-Attend-Tell architecture andresearch baselines. Not appropriate as the only data source foraccessibility tooling (alt-text generation should ideally use a modeltrained on a much larger dataset).
## Citation
If you use this checkpoint, please credit the underlying paper:
```bibtex@inproceedings{xu2015show, title = {Show, Attend and Tell: Neural Image Caption Generation with Visual Attention}, author = {Xu, Kelvin and Ba, Jimmy and Kiros, Ryan and Cho, Kyunghyun and Courville, Aaron and Salakhutdinov, Ruslan and Zemel, Richard and Bengio, Yoshua}, booktitle = {ICML}, year = {2015}}```
and the dataset:
```bibtex@article{hodosh2013framing, title = {Framing Image Description as a Ranking Task: Data, Models and Evaluation Metrics}, author = {Hodosh, Micah and Young, Peter and Hockenmaier, Julia}, journal = {Journal of Artificial Intelligence Research}, year = {2013}}```123version https://git-lfs.github.com/spec/v1oid sha256:91b41ebecea26453f6ce9ddab2702fb2db9f41dc17423a00a3d5d3ea9bfc8934size 22027784812345678910111213141516171819202122232425262728293031323334{ "images_dir": "data/raw/Images", "processed_dir": "data/processed",
"encoder_dim": 2048, "embed_size": 256, "hidden_size": 512, "attention_dim": 256, "dropout": 0.5, "rnn_type": "lstm",
"alpha_c": 1.0,
"batch_size": 32, "num_workers": 4, "num_epochs": 25, "decoder_lr": 4e-4, "encoder_lr": 1e-5, "weight_decay": 0.0, "grad_clip": 5.0, "scheduler_patience": 3, "scheduler_factor": 0.5,
"fine_tune_start_epoch": 16, "fine_tune_blocks": 2,
"seed": 42, "save_dir": "models", "run_name": "attention_lstm", "log_interval": 50, "val_bleu_subset": 200, "wandb_project": "flickr8k-captioning", "wandb_mode": "online"}1234567891011121314151617{ "checkpoint": "models/attention_lstm.pth", "split": "test", "n_images": 1091, "method": "beam", "beam_width": 5, "max_len": 20, "rnn_type": "lstm", "BLEU-1": 0.6488, "BLEU-2": 0.4714, "BLEU-3": 0.3378, "BLEU-4": 0.2403, "METEOR": 0.427, "CIDEr": 0.6002, "ROUGE-L": 0.4788, "wall_clock_s": 21.1}1234567891011121314151617{ "checkpoint": "models/attention_lstm.pth", "split": "test", "n_images": 1091, "method": "greedy", "beam_width": null, "max_len": 20, "rnn_type": "lstm", "BLEU-1": 0.6342, "BLEU-2": 0.4485, "BLEU-3": 0.3057, "BLEU-4": 0.2073, "METEOR": 0.4119, "CIDEr": 0.5322, "ROUGE-L": 0.4654, "wall_clock_s": 6.1}123version https://git-lfs.github.com/spec/v1oid sha256:30adbb8a77440e549df89caf254b77ebb0c269fdc6bec5ee3a3a79f310521c07size 126102README.mdAdded+171
attention_lstm.pthAdded+3
config.jsonAdded+34
metrics_beam5.jsonAdded+17
metrics_greedy.jsonAdded+17
vocab.pklAdded+3