adrienckr/Zero-and-Few-Shot-Visual-Question-Answering

Investigating zero- and few-shot prompting for VQA using state-of-the-art vision-language models.

0

stars

0

commits

Python

primary language

Feb 9, 2025

updated

README

Investigating Prompting Techniques for Zero- and Few-Shot Visual Question Answering

VQA Prompt Teaser

Table of Contents

Approach

We explore fine-tuning-free prompting techniques applied to vision-language models, specifically state-of-the-art BLIP2, Kosmos2, OpenFlamino and multimodal instruction-tuned LLaVa. We mainly focus on the following prompting approaches:

  • Zero- and few-shot prompt templates (commonly explored in NLP).
  • Chain-of-thought reasoning for VQA.
  • A two-step VQA method, proposing captioning as additional visual context for VLMs.

Existing vision-language models (VLMs) already show good zero-shot VQA performance. Our prompting techniques (especially captioning in few-shot vqa)lead to a substantial performance increase across benchmarks. However, though instruction-tuned models are claimed to show strong reasoning abilities, our tests found these reasoning abilities, particularly the chain-of-thought, to be deficient in diverse benchmarks. We hope our work will inspire future research in this direction.

VQA Formats

We supports the following VQA formats:

FormatDescriptionExample
Standard VQAStandard VQA task format.Question: "What is the primary activity of the people in the scene?"
Answer: "Dancing"
Caption VQABegins with a model-generated caption, then standard VQA format.Context: A group of people in traditional attire are dancing around a bonfire.
Question: "What is the primary activity of the people in the scene?"
Answer: "Dancing"
Chain-of-thought VQAImplements the chain-of-thought format.Question: "What is the primary activity of the people in the scene? Let's think step-by-step."
Answer: "First, considering there's a bonfire, this often signifies a gathering or festivity. Next, seeing people in traditional attire implies a cultural event. Merging these observations, the primary activity is dancing around the bonfire."

Prompt Templates

We have a list of prompt templates that can be used with different VQA formats. Please check the prompts/templates/{dataset_name}.

VQA Prompt Templates

Datasets

Download and unzip the files into the dataset/ folder for the VQA datasets. For Winoground, use the Hugging Face datasets library.

OK-VQAAOK-VQAGQAWinogroundVQAv2
SourceallenaiallenaiStanfordHugging FaceVQA

Usage

Running the inference code

To run the Standard VQA, use the following command:

python3 main.py --dataset_name okvqa \
  --model_name blip2_t5_flant5xxl \
  --vqa_format standard_vqa \
  --prompt_name prefix_your_task_knowledge_qa_short_answer

To run the Caption VQA, use the following command:

python3 main.py --dataset_name okvqa \
  --model_name blip2_t5_flant5xxl \
  --vqa_format caption_vqa \
  --prompt_name prefix_your_task_knowledge_qa_short_answer,prefix_promptcap

To run the Chain-of-thought VQA, use the following command:

python3 main.py --dataset_name okvqa \
  --model_name blip2_t5_flant5xxl \
  --vqa_format cot_vqa \
  --prompt_name prefix_think_step_by_step_rationale

Running few-shot inference

Please prepare examplar dataset dataset_zoo/nearest_neighbor.py and run the following command:

python3 main.py \
  --dataset_name okvqa \
  --model_name blip2_t5_flant5xxl \
  --vqa_format standard_vqa \
  --prompt_name prefix_your_task_knowledge_qa_short_answer \
  --vicuna_ans_parser --few_shot

Running Vicuna answer extraction

Considering the constraints of VQA accuracy metrics in the context of open-ended answer generation, we offer utility scripts in evals/vicuna_llm_evals.py. Using Vicuna LLM, these scripts process generated answers to align with reference responses and subsequently evaluate them based on the conventional VQA metric.

python3 main.py \
  --dataset_name okvqa \
  --model_name blip2_t5_flant5xxl \
  --vqa_format standard_vqa \
  --prompt_name prefix_your_task_knowledge_qa_short_answer \
  --vicuna_ans_parser

Results

We report the baseline and best setting results. Please check the paper for more results.

OKVQA

BLIP2 Flan-T5BLIP2 OPTKosmos2OpenFlamingoLLaVA
Baseline50.1342.740.3318.2944.84
Best50.5546.2943.0942.4846.86

AOKVQA

BLIP2 Flan-T5BLIP2 OPTKosmos2OpenFlamingoLLaVA
Baseline51.2045.5740.8517.2752.69
Best54.9849.3943.6044.1352.32

GQA

BLIP2 Flan-T5BLIP2 OPTKosmos2OpenFlamingoLLaVA
Baseline44.4638.4637.3326.3738.40
Best47.0141.9940.1341.0042.65

VQAv2

BLIP2 Flan-T5BLIP2 OPTKosmos2OpenFlamingoLLaVA
Baseline66.6654.5353.5235.4156.2
Best71.3762.8157.3358.065.32

Citing

Please email rabiul.awal [at] mila [dot] quebec for any questions. You can also open an issue or pull request to add more prompting techniques or new multi-modal vision-language models.

If you find this code useful, please cite our paper:

@article{awal2023investigating,
  title={Investigating Prompting Techniques for Zero-and Few-Shot Visual Question Answering},
  author={Awal, Rabiul and Zhang, Le and Agrawal, Aishwarya},
  journal={arXiv preprint arXiv:2306.09996},
  year={2023}
}

Acknowledgments

The codebase is build on top of transformers, lavis, llava and fastchat repositories. We thank the authors for their amazing work.

adrienckr/Zero-and-Few-Shot-Visual-Question-Answering

Investigating zero- and few-shot prompting for VQA using state-of-the-art vision-language models.

0

stars

0

commits

Python

primary language

Feb 9, 2025

updated

README

Investigating Prompting Techniques for Zero- and Few-Shot Visual Question Answering

VQA Prompt Teaser

Table of Contents

Approach

We explore fine-tuning-free prompting techniques applied to vision-language models, specifically state-of-the-art BLIP2, Kosmos2, OpenFlamino and multimodal instruction-tuned LLaVa. We mainly focus on the following prompting approaches:

  • Zero- and few-shot prompt templates (commonly explored in NLP).
  • Chain-of-thought reasoning for VQA.
  • A two-step VQA method, proposing captioning as additional visual context for VLMs.

Existing vision-language models (VLMs) already show good zero-shot VQA performance. Our prompting techniques (especially captioning in few-shot vqa)lead to a substantial performance increase across benchmarks. However, though instruction-tuned models are claimed to show strong reasoning abilities, our tests found these reasoning abilities, particularly the chain-of-thought, to be deficient in diverse benchmarks. We hope our work will inspire future research in this direction.

VQA Formats

We supports the following VQA formats:

FormatDescriptionExample
Standard VQAStandard VQA task format.Question: "What is the primary activity of the people in the scene?"
Answer: "Dancing"
Caption VQABegins with a model-generated caption, then standard VQA format.Context: A group of people in traditional attire are dancing around a bonfire.
Question: "What is the primary activity of the people in the scene?"
Answer: "Dancing"
Chain-of-thought VQAImplements the chain-of-thought format.Question: "What is the primary activity of the people in the scene? Let's think step-by-step."
Answer: "First, considering there's a bonfire, this often signifies a gathering or festivity. Next, seeing people in traditional attire implies a cultural event. Merging these observations, the primary activity is dancing around the bonfire."

Prompt Templates

We have a list of prompt templates that can be used with different VQA formats. Please check the prompts/templates/{dataset_name}.

VQA Prompt Templates

Datasets

Download and unzip the files into the dataset/ folder for the VQA datasets. For Winoground, use the Hugging Face datasets library.

OK-VQAAOK-VQAGQAWinogroundVQAv2
SourceallenaiallenaiStanfordHugging FaceVQA

Usage

Running the inference code

To run the Standard VQA, use the following command:

python3 main.py --dataset_name okvqa \
  --model_name blip2_t5_flant5xxl \
  --vqa_format standard_vqa \
  --prompt_name prefix_your_task_knowledge_qa_short_answer

To run the Caption VQA, use the following command:

python3 main.py --dataset_name okvqa \
  --model_name blip2_t5_flant5xxl \
  --vqa_format caption_vqa \
  --prompt_name prefix_your_task_knowledge_qa_short_answer,prefix_promptcap

To run the Chain-of-thought VQA, use the following command:

python3 main.py --dataset_name okvqa \
  --model_name blip2_t5_flant5xxl \
  --vqa_format cot_vqa \
  --prompt_name prefix_think_step_by_step_rationale

Running few-shot inference

Please prepare examplar dataset dataset_zoo/nearest_neighbor.py and run the following command:

python3 main.py \
  --dataset_name okvqa \
  --model_name blip2_t5_flant5xxl \
  --vqa_format standard_vqa \
  --prompt_name prefix_your_task_knowledge_qa_short_answer \
  --vicuna_ans_parser --few_shot

Running Vicuna answer extraction

Considering the constraints of VQA accuracy metrics in the context of open-ended answer generation, we offer utility scripts in evals/vicuna_llm_evals.py. Using Vicuna LLM, these scripts process generated answers to align with reference responses and subsequently evaluate them based on the conventional VQA metric.

python3 main.py \
  --dataset_name okvqa \
  --model_name blip2_t5_flant5xxl \
  --vqa_format standard_vqa \
  --prompt_name prefix_your_task_knowledge_qa_short_answer \
  --vicuna_ans_parser

Results

We report the baseline and best setting results. Please check the paper for more results.

OKVQA

BLIP2 Flan-T5BLIP2 OPTKosmos2OpenFlamingoLLaVA
Baseline50.1342.740.3318.2944.84
Best50.5546.2943.0942.4846.86

AOKVQA

BLIP2 Flan-T5BLIP2 OPTKosmos2OpenFlamingoLLaVA
Baseline51.2045.5740.8517.2752.69
Best54.9849.3943.6044.1352.32

GQA

BLIP2 Flan-T5BLIP2 OPTKosmos2OpenFlamingoLLaVA
Baseline44.4638.4637.3326.3738.40
Best47.0141.9940.1341.0042.65

VQAv2

BLIP2 Flan-T5BLIP2 OPTKosmos2OpenFlamingoLLaVA
Baseline66.6654.5353.5235.4156.2
Best71.3762.8157.3358.065.32

Citing

Please email rabiul.awal [at] mila [dot] quebec for any questions. You can also open an issue or pull request to add more prompting techniques or new multi-modal vision-language models.

If you find this code useful, please cite our paper:

@article{awal2023investigating,
  title={Investigating Prompting Techniques for Zero-and Few-Shot Visual Question Answering},
  author={Awal, Rabiul and Zhang, Le and Agrawal, Aishwarya},
  journal={arXiv preprint arXiv:2306.09996},
  year={2023}
}

Acknowledgments

The codebase is build on top of transformers, lavis, llava and fastchat repositories. We thank the authors for their amazing work.

Languages

Python

99.5%