File size: 1,144 Bytes
8771bb9
 
7d04a2e
 
 
 
 
 
 
 
 
 
8771bb9
7d04a2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
license: apache-2.0
datasets:
- atasoglu/flickr8k-dataset
language:
- en
metrics:
- rouge
pipeline_tag: image-to-text
tags:
- image
- vision
---

Vision Encoder Decoder (ViT + BERT) model that fine-tuned on [flickr8k-dataset](https://huggingface.co/datasets/atasoglu/flickr8k-dataset) for image-to-text task.

Example:

```py
from transformers import VisionEncoderDecoderModel, ViTImageProcessor, BertTokenizer
import torch
from PIL import Image

# load models
feature_extractor = ViTImageProcessor.from_pretrained("atasoglu/vit-bert-flickr8k")
tokenizer = BertTokenizer.from_pretrained("atasoglu/vit-bert-flickr8k")
model = VisionEncoderDecoderModel.from_pretrained("atasoglu/vit-bert-flickr8k")

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

# load image
img = Image.open("example.jpg")

# encode (extracting features)
pixel_values = feature_extractor(images=[img], return_tensors="pt").pixel_values
pixel_values = pixel_values.to(device)

# generate caption
output_ids = model.generate(pixel_values)

# decode
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
print(preds)
```