Commit
·
f067fcd
1
Parent(s):
b309c4b
readme updated
Browse files
README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
tags:
|
| 3 |
+
- image-captioning
|
| 4 |
+
license: apache-2.0
|
| 5 |
+
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
# nlpconnect/vit-gpt2-image-captioning
|
| 9 |
+
|
| 10 |
+
This is an image captioning model training by @ydshieh in flax, this is pytorch version of https://huggingface.co/ydshieh/vit-gpt2-coco-en-ckpts model.
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# Sample running code
|
| 14 |
+
|
| 15 |
+
```python
|
| 16 |
+
|
| 17 |
+
from transformers import VisionEncoderDecoderModel, ViTFeatureExtractor, AutoTokenizer
|
| 18 |
+
|
| 19 |
+
model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 20 |
+
feature_extractor = ViTFeatureExtractor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 21 |
+
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
| 22 |
+
|
| 23 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 24 |
+
model.to(device)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
max_length = 16
|
| 29 |
+
num_beams = 4
|
| 30 |
+
gen_kwargs = {"max_length": max_length, "num_beams": num_beams}
|
| 31 |
+
def predict_step(image_paths):
|
| 32 |
+
images = []
|
| 33 |
+
for image_path in image_paths:
|
| 34 |
+
i_image = Image.open(image_path)
|
| 35 |
+
if i_image.mode != "RGB":
|
| 36 |
+
i_image = i_image.convert(mode="RGB")
|
| 37 |
+
|
| 38 |
+
images.append(i_image)
|
| 39 |
+
|
| 40 |
+
pixel_values = feature_extractor(images=images, return_tensors="pt").pixel_values
|
| 41 |
+
pixel_values = pixel_values.to(device)
|
| 42 |
+
|
| 43 |
+
output_ids = model.generate(pixel_values, **gen_kwargs)
|
| 44 |
+
|
| 45 |
+
preds = tokenizer.batch_decode(output_ids, skip_special_tokens=True)
|
| 46 |
+
preds = [pred.strip() for pred in preds]
|
| 47 |
+
return preds
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
predict_step(['doctor.e16ba4e4.jpg'] # ['a woman in a hospital bed with a woman in a hospital bed']
|
| 51 |
+
|
| 52 |
+
```
|
| 53 |
+
|