Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added gqa as eval dataset #299

Open
wants to merge 4 commits into
base: reduce-scope-mllm
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions open_flamingo/eval/eval_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
"okvqa",
"vizwiz",
"textvqa",
"gqa",
"mantiseval",
"hateful_memes",
"imagenet",
]
Expand Down Expand Up @@ -104,16 +106,28 @@ def get_img_path(self, question):
)
elif self.dataset_name == "vizwiz":
return os.path.join(self.image_dir_path, question["image_id"])
elif self.dataset_name == "textvqa":
elif self.dataset_name == "textvqa" or self.dataset_name == "gqa":
return os.path.join(self.image_dir_path, f"{question['image_id']}.jpg")
elif self.dataset_name == "mantiseval":
img_paths = []
for img_id in question['image_id']:
img_paths.append(os.path.join(self.image_dir_path, f"{img_id}.jpg"))
return img_paths
else:
raise Exception(f"Unknown VQA dataset {self.dataset_name}")

def __getitem__(self, idx):
question = self.questions[idx]
img_path = self.get_img_path(question)
image = Image.open(img_path)
image.load()
if self.dataset_name == "mantiseval":
image = []
for path in img_path:
img = Image.open(path)
img.load()
image.append(img)
else:
image = Image.open(img_path)
image.load()
results = {
"image": image,
"question": question["question"],
Expand Down
13 changes: 12 additions & 1 deletion open_flamingo/eval/eval_models/blip.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from transformers import Blip2Processor, Blip2ForConditionalGeneration
from eval_models.eval_model import BaseEvalModel
from utils import unwrap_model
from utils import unwrap_model, combine_images
from transformers.modeling_outputs import CausalLMOutputWithPast


Expand All @@ -27,9 +27,14 @@ def required_args(self):

def prepare_images(self, batch: List[List[Image.Image]]) -> torch.Tensor:
batch_images = None
for i in range(len(batch)):
if len(batch[i]) > 1:
batch[i] = combine_images(batch[i])
"""
assert all(
len(example) == 1 for example in batch
), "BLIP-2 only supports one image per example"
"""
for example in batch:
if batch_images is None:
batch_images = self.processor.image_processor(
Expand Down Expand Up @@ -108,6 +113,12 @@ def get_vizwiz_prompt(self, question, answer=None) -> str:

def get_textvqa_prompt(self, question, answer=None) -> str:
return f"Question:{question} Short answer:{answer if answer is not None else ''}"

def get_gqa_prompt(self, question, answer=None) -> str:
return f"Question:{question} Short answer:{answer if answer is not None else ''}"

def get_mantiseval_prompt(self, question, answer=None) -> str:
return f"Question:{question} Short answer:{answer if answer is not None else ''}"

def get_coco_prompt(self, caption=None) -> str:
return f"A photo of {caption if caption is not None else ''}"
Expand Down
6 changes: 6 additions & 0 deletions open_flamingo/eval/eval_models/open_flamingo.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ def get_vizwiz_prompt(self, question, answer=None) -> str:

def get_textvqa_prompt(self, question, answer=None) -> str:
return f"<image>Question:{question} Short answer:{answer if answer is not None else ''}{'<|endofchunk|>' if answer is not None else ''}"

def get_gqa_prompt(self, question, answer=None) -> str:
return f"<image>Question:{question} Short answer:{answer if answer is not None else ''}{'<|endofchunk|>' if answer is not None else ''}"

def get_mantiseval_prompt(self, question, answer=None) -> str:
return f"<image>Question:{question} Short answer:{answer if answer is not None else ''}{'<|endofchunk|>' if answer is not None else ''}"

def get_coco_prompt(self, caption=None) -> str:
return f"<image>Output:{caption if caption is not None else ''}{'<|endofchunk|>' if caption is not None else ''}"
Expand Down
Loading
Loading