Abstract: Sanjay Subramanian, Medhini Narasimhan, Kushal Khangaonkar, Kevin Yang, Arsha Nagrani, Cordelia Schmid, Andy Zeng, Trevor Darrell, Dan Klein. Proceedings of the 61st Annual Meeting of the Association for Computational Linguistics (Volume 2: Short Papers).
2023.
Introduction
Visual Question Answering (VQA) is a cornerstone task in artificial intelligence, requiring the seamless integration of computer vision and natural language processing. Solving complex VQA queries involves grounding language in visual data, performing spatial reasoning, and executing multi-step logical operations. While end-to-end deep learning models have made significant strides, they often function as "black boxes," making their reasoning processes difficult to interpret and their skills hard to extend without massive retraining.
CodeVQA addresses these challenges by transforming VQA from a black-box learning problem into a transparent program synthesis task. By prompting a Large Language Model (LLM) to generate Python code that orchestrates specialized visual "primitive" functions, the system can solve complex, multi-step reasoning questions without requiring any additional model training or fine-tuning.
The CodeVQA Framework
The CodeVQA framework operates in a few-shot setting, leveraging the code-writing abilities of modern LLMs to act as a flexible, zero-training orchestrator for visual reasoning.
As illustrated in the pipeline above, the system consists of three primary components:
- The Orchestrator (LLM): Utilizing
code-davinci-002(Codex), the system generates a Python script based on the input question and approximately 50 human-annotated "in-context" examples. - Visual Primitives (APIs): These are Python functions that wrap around off-the-shelf Vision-Language Models like BLIP. Key primitives include
query(image, question)for answering simple sub-questions,get_pos(image, text)for returning object coordinates via GradCAM, andfind_matching_image(images, text)for selecting relevant images from a set. - Execution Engine: A standard Python interpreter runs the generated code. The script can utilize loops, arithmetic, and conditional logic to arrive at a final answer, making the entire reasoning process fully interpretable.
Benchmarking Complex Reasoning
The authors evaluated CodeVQA on three benchmarks known for requiring sophisticated multi-step logic:
- GQA: Focuses on multi-hop spatial reasoning and attribute identification in single images.
- COVR: Requires reasoning over sets of images (e.g., "How many images contain exactly 2 pink shoes?").
- NLVR2: Involves determining if a natural language statement is true or false based on a pair of images.
Interpretable Multi-Image Reasoning
A key advantage of CodeVQA is its ability to handle multi-image reasoning through explicit programming logic. While traditional models struggle to aggregate information across multiple visual inputs in a single prompt, CodeVQA can generate a loop to process each image individually and track results with variables.
As shown in the comparison above, CodeVQA correctly identifies whether two different images share a specific attribute (e.g., train color) by querying each image and comparing the results using an if statement. In contrast, caption-based baselines often fail because critical details are lost during the translation from image to text, or the LLM fails to synthesize the disparate captions correctly.
Key Results and Insights
CodeVQA consistently outperformed "Few-shot PnP-VQA," a strong baseline that uses an LLM to answer questions based on generated image captions but lacks code generation capabilities:
- COVR: Achieved 50.7% accuracy, a significant +4.9% improvement over the baseline. The advantage was most pronounced on complex instances involving 4 or 5 images.
- GQA: Achieved 49.0% accuracy, a +2.4% improvement. Analysis showed the biggest gains were in "spatial" questions (roughly +30% improvement), as the system could use simple arithmetic on coordinates (e.g.,
if object1_x < object2_x) rather than relying on the LLM's internal spatial intuition. - NLVR2: Achieved 64.0% accuracy, slightly outperforming the baseline.
Conclusion and Future Work
CodeVQA demonstrates that LLMs can effectively orchestrate visual tools to solve complex VQA tasks without task-specific fine-tuning. While the system currently lags behind fully supervised state-of-the-art models and introduces higher latency due to API calls, its modularity allows for "plug-and-play" upgrades. For example, one can swap out a visual primitive for a more advanced model without retraining the rest of the system. Future work may involve expanding the primitive library and extending the framework to support multilingual queries.
Summary updated