File size: 1,102 Bytes
fdef93d
 
b632735
41dc5c5
b632735
 
 
 
 
 
 
fdef93d
b632735
fdef93d
 
b632735
 
 
fdef93d
 
b632735
 
 
fdef93d
b632735
 
 
 
 
 
fdef93d
93c57d7
b632735
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
## EXAMPLE
```python
import requests
import torch
from PIL import Image
from transformers import (
    VisionEncoderDecoderModel, 
    ViTFeatureExtractor, 
    PreTrainedTokenizerFast,
)

# device setting
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# load feature extractor and tokenizer
encoder_model_name_or_path = "ddobokki/vision-encoder-decoder-vit-gpt2-coco-ko"
feature_extractor = ViTFeatureExtractor.from_pretrained(encoder_model_name_or_path)
tokenizer = PreTrainedTokenizerFast.from_pretrained(encoder_model_name_or_path)

# load model
model = VisionEncoderDecoderModel.from_pretrained(encoder_model_name_or_path)
model.to(device)

# inference
url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
with Image.open(requests.get(url, stream=True).raw) as img:
    pixel_values = feature_extractor(images=img, return_tensors="pt").pixel_values

generated_ids = model.generate(pixel_values.to(device),num_beams=5)
generated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)

>> ['๊ณ ์–‘์ด ๋‘๋งˆ๋ฆฌ๊ฐ€ ๋‹ด์š” ์œ„์— ๋ˆ„์›Œ ์žˆ๋‹ค.']
```