update README.md
Browse files
README.md
CHANGED
|
@@ -10,4 +10,39 @@ pipeline_tag: image-to-text
|
|
| 10 |
tags:
|
| 11 |
- image
|
| 12 |
- vision
|
| 13 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
tags:
|
| 11 |
- image
|
| 12 |
- vision
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
Vision Encoder Decoder (ViT + GPT2) model that fine-tuned on [flickr8k-dataset](https://huggingface.co/datasets/atasoglu/flickr8k-dataset) for image-to-text task.
|
| 16 |
+
|
| 17 |
+
Example:
|
| 18 |
+
|
| 19 |
+
```py
|
| 20 |
+
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
|
| 21 |
+
import torch
|
| 22 |
+
from PIL import Image
|
| 23 |
+
|
| 24 |
+
# load models
|
| 25 |
+
feature_extractor = ViTImageProcessor.from_pretrained("atasoglu/vit-gpt2-flickr8k")
|
| 26 |
+
tokenizer = AutoTokenizer.from_pretrained("atasoglu/vit-gpt2-flickr8k")
|
| 27 |
+
model = VisionEncoderDecoderModel.from_pretrained("atasoglu/vit-gpt2-flickr8k")
|
| 28 |
+
|
| 29 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 30 |
+
model.to(device)
|
| 31 |
+
|
| 32 |
+
# load image
|
| 33 |
+
img = Image.open("example.jpg")
|
| 34 |
+
|
| 35 |
+
# encode (extracting features)
|
| 36 |
+
pixel_values = feature_extractor(images=[img], return_tensors="pt").pixel_values
|
| 37 |
+
pixel_values = pixel_values.to(device)
|
| 38 |
+
|
| 39 |
+
# generate caption
|
| 40 |
+
output_ids = model.generate(pixel_values)
|
| 41 |
+
|
| 42 |
+
# decode
|
| 43 |
+
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
| 44 |
+
|
| 45 |
+
print(preds)
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
For more, see [this](https://ankur3107.github.io/blogs/the-illustrated-image-captioning-using-transformers/) awesome blog.
|