Update
Browse files- README.md +1 -1
- app.py +55 -81
- requirements.txt +2 -2
README.md
CHANGED
@@ -4,7 +4,7 @@ emoji: 📊
|
|
4 |
colorFrom: gray
|
5 |
colorTo: green
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
duplicated_from: nielsr/comparing-VQA-models
|
|
|
4 |
colorFrom: gray
|
5 |
colorTo: green
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.35.2
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
duplicated_from: nielsr/comparing-VQA-models
|
app.py
CHANGED
@@ -1,98 +1,72 @@
|
|
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 |
-
|
49 |
-
|
50 |
-
|
51 |
generated_ids = model.generate(**inputs, max_length=50)
|
52 |
-
generated_answer = processor.batch_decode(generated_ids,
|
53 |
-
|
54 |
-
return generated_answer
|
55 |
|
56 |
|
|
|
57 |
def generate_answer_vilt(processor, model, image, question):
|
58 |
-
|
59 |
-
|
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 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
outputs=outputs,
|
93 |
-
examples=examples,
|
94 |
-
title=title,
|
95 |
-
description=description,
|
96 |
-
article=article,
|
97 |
-
enable_queue=True)
|
98 |
-
interface.launch(debug=True)
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import torch
|
3 |
+
from transformers import (AutoProcessor, BlipForQuestionAnswering,
|
4 |
+
ViltForQuestionAnswering)
|
5 |
+
|
6 |
+
torch.hub.download_url_to_file(
|
7 |
+
'http://images.cocodataset.org/val2017/000000039769.jpg', 'cats.jpg')
|
8 |
+
torch.hub.download_url_to_file(
|
9 |
+
'https://huggingface.co/datasets/nielsr/textcaps-sample/resolve/main/stop_sign.png',
|
10 |
+
'stop_sign.png')
|
11 |
+
torch.hub.download_url_to_file(
|
12 |
+
'https://cdn.openai.com/dall-e-2/demos/text2im/astronaut/horse/photo/0.jpg',
|
13 |
+
'astronaut.jpg')
|
14 |
+
|
15 |
+
blip_processor_large = AutoProcessor.from_pretrained(
|
16 |
+
'Salesforce/blip-vqa-capfilt-large')
|
17 |
+
blip_model_large = BlipForQuestionAnswering.from_pretrained(
|
18 |
+
'Salesforce/blip-vqa-capfilt-large')
|
19 |
+
|
20 |
+
vilt_processor = AutoProcessor.from_pretrained(
|
21 |
+
'dandelin/vilt-b32-finetuned-vqa')
|
22 |
+
vilt_model = ViltForQuestionAnswering.from_pretrained(
|
23 |
+
'dandelin/vilt-b32-finetuned-vqa')
|
24 |
+
|
25 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
blip_model_large.to(device)
|
28 |
vilt_model.to(device)
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
@torch.inference_mode()
|
32 |
def generate_answer_blip(processor, model, image, question):
|
33 |
+
inputs = processor(images=image, text=question,
|
34 |
+
return_tensors='pt').to(device)
|
|
|
35 |
generated_ids = model.generate(**inputs, max_length=50)
|
36 |
+
generated_answer = processor.batch_decode(generated_ids,
|
37 |
+
skip_special_tokens=True)
|
38 |
+
return generated_answer[0]
|
39 |
|
40 |
|
41 |
+
@torch.inference_mode()
|
42 |
def generate_answer_vilt(processor, model, image, question):
|
43 |
+
encoding = processor(images=image, text=question,
|
44 |
+
return_tensors='pt').to(device)
|
45 |
+
outputs = model(**encoding)
|
|
|
|
|
|
|
46 |
predicted_class_idx = outputs.logits.argmax(-1).item()
|
|
|
47 |
return model.config.id2label[predicted_class_idx]
|
48 |
|
49 |
|
50 |
def generate_answers(image, question):
|
51 |
+
answer_blip_large = generate_answer_blip(blip_processor_large,
|
52 |
+
blip_model_large, image, question)
|
53 |
+
answer_vilt = generate_answer_vilt(vilt_processor, vilt_model, image,
|
54 |
+
question)
|
55 |
+
return answer_blip_large, answer_vilt
|
56 |
+
|
57 |
+
|
58 |
+
demo = gr.Interface(
|
59 |
+
fn=generate_answers,
|
60 |
+
inputs=[gr.Image(type='pil'),
|
61 |
+
gr.Textbox(label='Question')],
|
62 |
+
outputs=[
|
63 |
+
gr.Textbox(label='Answer generated by BLIP-large'),
|
64 |
+
gr.Textbox(label='Answer generated by ViLT')
|
65 |
+
],
|
66 |
+
examples=[
|
67 |
+
['cats.jpg', 'How many cats are there?'],
|
68 |
+
['stop_sign.png', "What's behind the stop sign?"],
|
69 |
+
['astronaut.jpg', "What's the astronaut riding on?"],
|
70 |
+
],
|
71 |
+
title='Interactive demo: comparing visual question answering (VQA) models')
|
72 |
+
demo.queue().launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,2 +1,2 @@
|
|
1 |
-
|
2 |
-
|
|
|
1 |
+
torch
|
2 |
+
transformers
|