File size: 4,564 Bytes
cef7c92 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | ---
language:
- en
license: mit
tags:
- vision
- image-to-text
- image-captioning
- gpt2
- vision-transformer
- flickr8k
pipeline_tag: image-to-text
datasets:
- jxie/flickr8k
---
# Vision-GPT: Image Captioning Model
A multimodal model combining Vision Transformer (ViT-B/16) and GPT-2 for image captioning, trained on Flickr8K dataset.
## Model Description
This model generates natural language captions for images by:
1. Encoding images using a pre-trained ViT-B/16 vision encoder
2. Projecting visual features into GPT-2's embedding space
3. Generating captions autoregressively with GPT-2
## Training Details
- **Dataset**: Flickr8K (all splits: train, validation, test)
- **Vision Encoder**: ViT-B/16 (frozen)
- **Language Model**: GPT-2 (frozen backbone, trainable projection)
- **Training**: Only the vision-to-text projection layer is trained
## Model Versions
### π¦ FP32 (Full Precision)
- **Size**: ~0.83 GB
- **Precision**: 32-bit floating point
- **Use case**: Maximum accuracy, research
### π¦ FP16 (Half Precision)
- **Size**: ~0.83 GB
- **Precision**: 16-bit floating point
- **Use case**: Faster inference, reduced memory (~50% smaller)
## Usage
### Installation
```bash
pip install torch torchvision transformers pillow huggingface_hub
```
### Loading the Model (FP32)
```python
import torch
from transformers import GPT2Tokenizer
from PIL import Image
from torchvision import transforms
# Load checkpoint
checkpoint = torch.load("model_fp32/model_checkpoint.pth", map_location="cpu")
# Load tokenizer
tokenizer = GPT2Tokenizer.from_pretrained("model_fp32/tokenizer")
# Load your model architecture (you need to define this)
# model = YourVisionGPTModel(config)
# model.load_state_dict(checkpoint['model_state_dict'])
# model.eval()
print("Model loaded successfully!")
```
### Loading the Model (FP16)
```python
# Load FP16 checkpoint
checkpoint = torch.load("model_fp16/model_checkpoint.pth", map_location="cpu")
# Load model and convert to FP16
# model = YourVisionGPTModel(config)
# model.load_state_dict(checkpoint['model_state_dict'])
# model.half() # Convert to FP16
# model.eval()
# For inference with FP16, also convert input images to FP16
```
### Image Preprocessing
```python
image_transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.Lambda(lambda x: x.convert('RGB')),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
),
])
# Load and preprocess image
image = Image.open("your_image.jpg")
image_tensor = image_transform(image).unsqueeze(0) # Add batch dimension
```
### Generate Caption
```python
# Generate caption
with torch.no_grad():
# Forward pass
generated_ids = model.generate(
image_tensor,
max_length=50,
num_beams=5,
temperature=0.7
)
# Decode caption
caption = tokenizer.decode(generated_ids[0], skip_special_tokens=True)
print(f"Generated caption: {caption}")
```
## Model Architecture
```
βββββββββββββββββββ
β Input Image β
β (224x224) β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β ViT-B/16 β
β (frozen) β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β Projection β
β (trainable) β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β GPT-2 β
β (frozen) β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β Caption Output β
βββββββββββββββββββ
```
## Limitations
- Trained only on Flickr8K (limited domain)
- English captions only
- Input images must be 224x224
- May generate generic captions for out-of-domain images
## Citation
If you use this model, please cite:
```bibtex
@misc{vision-gpt-flickr8k,
author = {gurumurthy3},
title = {Vision-GPT: Image Captioning with ViT and GPT-2},
year = {2025},
publisher = {Hugging Face},
howpublished = {\url{https://huggingface.co/gurumurthy3/vision-gpt-flickr8k}}
}
```
## License
MIT License
## Acknowledgments
- Vision Transformer (ViT): Dosovitskiy et al.
- GPT-2: OpenAI
- Flickr8K Dataset: Hodosh et al.
|