ViUniT: Visual Unit Tests for More Robust Visual Programming

Artemis Panagopoulou, Honglu Zhou, Silvio Savarese, Caiming Xiong, Chris Callison-Burch, Mark Yatskar, Juan Carlos Niebles

Abstract

Programming based approaches to reasoning tasks have substantially expanded the types of questions models can answer about visual scenes. Yet on benchmark visual reasoning data, when answering correctly, such models produce incorrect programs 33% of the time. These models are often right for the wrong reasons and risk unexpected failures on new data. We propose Visual Unit Testing (ViUniT), a framework to improve the reliability of visual programs by automatically generating unit tests. In our framework, a unit test is represented as a novel image and answer meant to verify logical correctness of a program produced for a given query. Our method leverages a language model to create unit tests in the form of image descriptions and expected answers and image synthesis to produce corresponding images. We conduct a comprehensive analysis of what constitutes an effective visual unit test suite, exploring unit test generation, sampling strategies, image generation methods, and varying the number of programs and unit tests. Additionally, we introduce four applications of visual unit tests: best program selection, answer refusal, re-prompting, and unsupervised reward formulations for reinforcement learning. Experiments with two models across three datasets in visual question answering and image-text matching demonstrate that ViUniT improves model performance by 11.4%. Notably, it enables 7B open-source models to outperform gpt-4o-mini by an average of 7.7% and reduces the occurrence of programs that are correct for the wrong reasons by 40%.

Introduction

Visual Programming has emerged as a powerful paradigm for solving complex reasoning tasks. Instead of relying on a single "black box" model to answer a question, a Large Language Model (LLM) generates a Python script that orchestrates specialized tools—such as object detectors or croppers—to derive an answer. However, a significant challenge persists: these models are frequently "right for the wrong reasons." For instance, with CodeLlama-7B, approximately 33% of correct answers on benchmarks are produced by logically flawed programs that succeed through lucky guesses or tool biases. ViUniT addresses this by treating AI-generated code like traditional software, introducing automated unit testing to verify program logic.

The ViUniT Framework

ViUniT generates a suite of synthetic unit tests for every query to ensure the logical robustness of generated programs. The framework operates through four primary stages:

  1. Candidate Generation: For a given query (e.g., "Is the chair made of the same material as the table?"), an LLM generates multiple text-based unit test scenarios, such as "A wooden chair and a wooden table" with an expected answer of "Yes."
  2. Coverage Sampling: To maximize testing efficacy, ViUniT selects a diverse subset of candidates using an optimization criterion that prioritizes varied answers and high visual diversity.
  3. Image Synthesis: Selected descriptions are transformed into synthetic images using text-to-image diffusion models like SDXL.
  4. Scoring and Selection: The candidate visual programs are executed against these synthetic tests. Programs that consistently yield the expected answers are deemed robust and selected for final execution on the real input image.
The ViUniT framework overview, illustrating the pipeline from query to unit test generation, image synthesis, and final program selection.

Applications of Visual Unit Testing

The authors propose four distinct ways to leverage these visual unit tests to improve model reliability:

  • Best Program Selection: Choosing the program with the highest pass rate from a candidate pool.
  • Answer Refusal: Falling back to a standard Vision-Language Model (VLM) if no program passes the unit tests, preventing unreliable outputs.
  • Re-prompting: Utilizing test failures as feedback to help the LLM debug and rewrite its code.
  • Unsupervised RL: Employing the unit test pass rate as a reward signal for fine-tuning models via Reinforcement Learning.
The re-prompting mechanism in ViUniT, where unit test outputs are used to provide feedback to the program generator for iterative refinement.

Experimental Evaluation

ViUniT was evaluated on three major compositional reasoning benchmarks: GQA (spatial/attribute reasoning), SugarCREPE (compositionality errors), and Winoground (relational reasoning).

The results demonstrate significant gains:

  • Performance Boost: ViUniT improved model performance by an average of 11.4 points.
  • Model Efficiency: Open-source 7B models (e.g., CodeLlama, CodeGemma) equipped with ViUniT outperformed GPT-4o-mini by an average of 7.7 points, suggesting that verification can compensate for smaller model scales.
  • Logical Fidelity: Human evaluation confirmed a 40% reduction in "right for the wrong reasons" errors.
  • Debugging Efficacy: Re-prompting with unit test feedback resulted in programs that were 20% more accurate than simple re-prompting without feedback.

Conclusion and Future Work

ViUniT demonstrates that synthetic vision can serve as a powerful proxy for code verification in visual programming. While highly effective, the framework's performance is currently bounded by the quality of the Text-to-Image (T2I) generator. If the diffusion model fails to render a specific object mentioned in the unit test description, the test becomes invalid. Future work will focus on improving spatial controls in image synthesis and expanding the framework to tasks like robotic planning and video reasoning.

Summary updated

BibTeX
@inproceedings{panagopoulou2025viunit,
  author = {Artemis Panagopoulou and Honglu Zhou and Silvio Savarese and Caiming Xiong and Chris Callison-Burch and Mark Yatskar and Juan Carlos Niebles},
  title = {{ViUniT: Visual Unit Tests for More Robust Visual Programming}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2025},
  url = {https://openreview.net/forum?id=FY6dyqrOFT},
}
Figure