Abstract
Answering visual queries is a complex task that requires both visual processing and reasoning. End-to-end models, the dominant approach for this task, do not explicitly differentiate between the two, limiting interpretability and generalization. Learning modular programs presents a promising alternative, but has proven challenging due to the difficulty of learning both the programs and modules simultaneously. We introduce ViperGPT, a framework that leverages code-generation models to compose vision-and-language models into subroutines to produce a result for any query. ViperGPT utilizes a provided API to access the available modules, and composes them by generating Python code that is later executed. This simple approach requires no further training, and achieves state-of-the-art results across various complex visual tasks.
Introduction
Traditional computer vision models are typically "end-to-end," mapping images directly to answers in a single forward pass. While effective for simple pattern recognition (System 1 thinking), these models often struggle with complex, multi-step reasoning (System 2 thinking), such as performing arithmetic, handling long-range temporal dependencies in video, or incorporating external world knowledge.
Previous attempts at "Modular Neural Networks" sought to decompose tasks into sub-modules but faced significant training hurdles, as program generators and perceptual modules had to be learned concurrently. ViperGPT addresses this by leveraging the "prior knowledge" of code-generating Large Language Models (LLMs) to handle logic, while using off-the-shelf pretrained models for perception.
Methods
The ViperGPT architecture consists of three main components:
- Program Generator ($\pi$): An LLM (specifically GPT-3 Codex) generates Python code based on an API Specification—a set of class definitions and docstrings for available visual tools (e.g.,
find,compute_depth,verify_property). - API / Modules: A library of specialized pretrained models wrapped in a Pythonic interface. Key modules include GLIP for object detection (
find), MiDaS for depth estimation, BLIP-2 for localized visual questions, and GPT-3 for external knowledge. - Execution Engine ($\phi$): A standard Python interpreter executes the generated code, calling the modules as subroutines to produce the final result.
This approach is training-free, interpretable, and logic-complete, allowing the use of loops, conditionals, and mathematical operators.
Key Insights
- Code as Reasoning: Python code provides a robust and flexible representation for reasoning. It naturally handles mathematical operations (e.g., calculating muffins per kid) and logical control flow.
- Interventional Explainability: Since the model outputs code, researchers can perform "interventions" by disabling specific modules to measure their impact on performance, quantitatively determining which perceptual capabilities are critical for a task.
- In-Context Flexibility: ViperGPT can adapt to additional information provided in comments. For instance, context about geographic location can guide the model to generate logic specific to local traffic laws.
Results
ViperGPT achieves impressive zero-shot results, often surpassing models specifically trained on target datasets:
- RefCOCO: Achieved 72.0% IoU, significantly outperforming previous zero-shot models like ReCLIP (58.6%).
- OK-VQA: Reached 51.9% accuracy, surpassing the massive Flamingo-80B model (50.6%) without seeing any OK-VQA training images.
- GQA: Achieved 48.1%, a state-of-the-art result for zero-shot compositional reasoning.
- NExT-QA: Demonstrated that image-level perception modules can enable temporal reasoning through generated Python loops.
Conclusion
ViperGPT demonstrates that separating perception from reasoning via code generation is a powerful paradigm for visual AI. While success is tied to the LLM's ability to generate correct code and execution latency is higher than end-to-end models, the system's interpretability and zero-shot performance mark a significant step forward. Future work will focus on integrating more diverse modules and improving execution efficiency.
Summary updated