Spaces:
Runtime error
Runtime error
Duplicate from nielsr/comparing-VQA-models
Browse filesCo-authored-by: Niels Rogge <nielsr@users.noreply.huggingface.co>
- .gitattributes +34 -0
- README.md +13 -0
- app.py +98 -0
- requirements.txt +2 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Comparing VQA Models
|
3 |
+
emoji: 📊
|
4 |
+
colorFrom: gray
|
5 |
+
colorTo: green
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.15.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: nielsr/comparing-VQA-models
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, AutoModelForCausalLM, BlipForQuestionAnswering, ViltForQuestionAnswering
|
3 |
+
import torch
|
4 |
+
|
5 |
+
torch.hub.download_url_to_file('http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg')
|
6 |
+
torch.hub.download_url_to_file('https://huggingface.co/datasets/nielsr/textcaps-sample/resolve/main/stop_sign.png', 'stop_sign.png')
|
7 |
+
torch.hub.download_url_to_file('https://cdn.openai.com/dall-e-2/demos/text2im/astronaut/horse/photo/0.jpg', 'astronaut.jpg')
|
8 |
+
|
9 |
+
git_processor_base = AutoProcessor.from_pretrained("microsoft/git-base-vqav2")
|
10 |
+
git_model_base = AutoModelForCausalLM.from_pretrained("microsoft/git-base-vqav2")
|
11 |
+
|
12 |
+
git_processor_large = AutoProcessor.from_pretrained("microsoft/git-large-vqav2")
|
13 |
+
git_model_large = AutoModelForCausalLM.from_pretrained("microsoft/git-large-vqav2")
|
14 |
+
|
15 |
+
blip_processor_base = AutoProcessor.from_pretrained("Salesforce/blip-vqa-base")
|
16 |
+
blip_model_base = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-base")
|
17 |
+
|
18 |
+
blip_processor_large = AutoProcessor.from_pretrained("Salesforce/blip-vqa-capfilt-large")
|
19 |
+
blip_model_large = BlipForQuestionAnswering.from_pretrained("Salesforce/blip-vqa-capfilt-large")
|
20 |
+
|
21 |
+
vilt_processor = AutoProcessor.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
22 |
+
vilt_model = ViltForQuestionAnswering.from_pretrained("dandelin/vilt-b32-finetuned-vqa")
|
23 |
+
|
24 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
25 |
+
|
26 |
+
git_model_base.to(device)
|
27 |
+
blip_model_base.to(device)
|
28 |
+
git_model_large.to(device)
|
29 |
+
blip_model_large.to(device)
|
30 |
+
vilt_model.to(device)
|
31 |
+
|
32 |
+
def generate_answer_git(processor, model, image, question):
|
33 |
+
# prepare image
|
34 |
+
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
35 |
+
|
36 |
+
# prepare question
|
37 |
+
input_ids = processor(text=question, add_special_tokens=False).input_ids
|
38 |
+
input_ids = [processor.tokenizer.cls_token_id] + input_ids
|
39 |
+
input_ids = torch.tensor(input_ids).unsqueeze(0)
|
40 |
+
|
41 |
+
generated_ids = model.generate(pixel_values=pixel_values, input_ids=input_ids, max_length=50)
|
42 |
+
generated_answer = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
43 |
+
|
44 |
+
return generated_answer
|
45 |
+
|
46 |
+
|
47 |
+
def generate_answer_blip(processor, model, image, question):
|
48 |
+
# prepare image + question
|
49 |
+
inputs = processor(images=image, text=question, return_tensors="pt")
|
50 |
+
|
51 |
+
generated_ids = model.generate(**inputs, max_length=50)
|
52 |
+
generated_answer = processor.batch_decode(generated_ids, skip_special_tokens=True)
|
53 |
+
|
54 |
+
return generated_answer
|
55 |
+
|
56 |
+
|
57 |
+
def generate_answer_vilt(processor, model, image, question):
|
58 |
+
# prepare image + question
|
59 |
+
encoding = processor(images=image, text=question, return_tensors="pt")
|
60 |
+
|
61 |
+
with torch.no_grad():
|
62 |
+
outputs = model(**encoding)
|
63 |
+
|
64 |
+
predicted_class_idx = outputs.logits.argmax(-1).item()
|
65 |
+
|
66 |
+
return model.config.id2label[predicted_class_idx]
|
67 |
+
|
68 |
+
|
69 |
+
def generate_answers(image, question):
|
70 |
+
answer_git_base = generate_answer_git(git_processor_base, git_model_base, image, question)
|
71 |
+
|
72 |
+
answer_git_large = generate_answer_git(git_processor_large, git_model_large, image, question)
|
73 |
+
|
74 |
+
answer_blip_base = generate_answer_blip(blip_processor_base, blip_model_base, image, question)
|
75 |
+
|
76 |
+
answer_blip_large = generate_answer_blip(blip_processor_large, blip_model_large, image, question)
|
77 |
+
|
78 |
+
answer_vilt = generate_answer_vilt(vilt_processor, vilt_model, image, question)
|
79 |
+
|
80 |
+
return answer_git_base, answer_git_large, answer_blip_base, answer_blip_large, answer_vilt
|
81 |
+
|
82 |
+
|
83 |
+
examples = [["cats.jpg", "How many cats are there?"], ["stop_sign.png", "What's behind the stop sign?"], ["astronaut.jpg", "What's the astronaut riding on?"]]
|
84 |
+
outputs = [gr.outputs.Textbox(label="Answer generated by GIT-base"), gr.outputs.Textbox(label="Answer generated by GIT-large"), gr.outputs.Textbox(label="Answer generated by BLIP-base"), gr.outputs.Textbox(label="Answer generated by BLIP-large"), gr.outputs.Textbox(label="Answer generated by ViLT")]
|
85 |
+
|
86 |
+
title = "Interactive demo: comparing visual question answering (VQA) models"
|
87 |
+
description = "Gradio Demo to compare GIT, BLIP and ViLT, 3 state-of-the-art vision+language models. To use it, simply upload your image and click 'submit', or click one of the examples to load them. Read more at the links below."
|
88 |
+
article = "<p style='text-align: center'><a href='https://huggingface.co/docs/transformers/main/model_doc/blip' target='_blank'>BLIP docs</a> | <a href='https://huggingface.co/docs/transformers/main/model_doc/git' target='_blank'>GIT docs</a></p>"
|
89 |
+
|
90 |
+
interface = gr.Interface(fn=generate_answers,
|
91 |
+
inputs=[gr.inputs.Image(type="pil"), gr.inputs.Textbox(label="Question")],
|
92 |
+
outputs=outputs,
|
93 |
+
examples=examples,
|
94 |
+
title=title,
|
95 |
+
description=description,
|
96 |
+
article=article,
|
97 |
+
enable_queue=True)
|
98 |
+
interface.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
git+https://github.com/huggingface/transformers.git
|
2 |
+
torch
|