Pix2Code: Learning to Compose Neural Visual Concepts as Programs

Antonia Wüst, Wolfgang Stammer, Quentin Delfosse, Devendra Singh Dhami, Kristian Kersting · DOI

Abstract

While deep learning has achieved remarkable success in visual perception, traditional neural architectures often struggle with abstract relational reasoning and the "entity generalization" gap—the ability to apply learned concepts to scenes containing more objects than seen during training. We introduce Pix2Code, a neuro-symbolic framework that addresses these challenges by learning visual concepts as executable $\lambda$-calculus programs. By synthesizing compositional logic from neural object extractions, Pix2Code achieves near-perfect generalization and provides a human-interpretable interface for inspecting and revising learned concepts.

Introduction

Modern visual reasoning models are frequently criticized for being "black boxes." When a model fails or relies on spurious correlations (shortcuts), it is difficult for researchers to diagnose the underlying logic or correct it without extensive retraining. Furthermore, pure neural approaches often fail to generalize to novel scene complexities; for instance, a model trained to identify a property in images with three objects may fail when presented with ten.

Pix2Code bridges this gap by combining the perceptual strengths of neural networks with the explicit, compositional logic of computer programs. By representing concepts as code, the model becomes count-invariant and inherently debuggable, allowing for a new paradigm of human-AI collaboration in visual concept learning.

Methods

Pix2Code utilizes a two-stage neuro-symbolic pipeline to transform raw pixels into logical concepts.

Neural Perception

The first stage involves an object extractor (based on the Pix2Seq architecture) that parses raw images into symbolic representations. Each detected object is characterized by a set of discrete and continuous attributes, including bounding box coordinates, color, shape, size, and material. This step abstracts away the high-dimensional pixel space into a structured format suitable for logical reasoning.

Program Synthesis and Library Learning

The core of Pix2Code is its program synthesis engine, which treats concept learning as a search for a $\lambda$-calculus program that can distinguish "positive" examples from "negative" ones. The framework employs a "wake-sleep" algorithm, inspired by DreamCoder, to iteratively refine its understanding. During the "wake" phase, the system searches for programs that explain the current tasks. During the "sleep" phase, it abstracts frequently recurring code patterns into new, higher-level primitives in a global library $L$.

The Pix2Code architecture: from raw images to object extraction, program synthesis, and final interpreted concept.

This library-building process allows the model to "learn to learn." Starting from basic primitives like forall, exists, and eq?, the system eventually develops complex abstractions such as same shape or all same color, enabling it to solve increasingly sophisticated relational tasks.

Experimental Evaluation

The authors evaluated Pix2Code against several benchmarks designed to test compositional reasoning and generalization:

  • RelKP: A new dataset based on Kandinsky Patterns with complex relational rules.
  • CURI-EG & CURI-Hans: Extensions of the CURI benchmark specifically designed to test Entity Generalization (scaling to more objects) and Confounding (testing reliance on shortcuts).
  • MS COCO: Real-world images used to validate the approach's scalability to natural environments.

Results and Key Insights

Pix2Code demonstrated significant performance gains over pure neural baselines, particularly in scenarios requiring logical consistency across varying scene scales.

Superior Generalization

On the RelKP dataset, Pix2Code achieved an accuracy of 90.05%, compared to just 59.69% for the neural baseline (CURI-B). Most notably, in Entity Generalization tests, Pix2Code maintained 100% accuracy when the number of objects in a scene increased from 5 to 10. Because the learned concept is a logical program (e.g., forall(objects, is_red)), the logic remains valid regardless of the number of entities present.

Interpretability and Interactive Revision (XIL)

Because the output is code, it can be translated into natural language using Large Language Models (LLMs), making the model's internal logic transparent to humans. This transparency allows for eXplanatory Interactive Learning (XIL). If a model learns a shortcut—such as using color to identify a concept that should be defined by shape—a human can intervene by removing the "color" primitive from the library. This forces the model to re-synthesize the concept using the correct logical features.

Performance comparison on Confounded and Counting tasks. Pix2Code + XIL shows perfect or near-perfect accuracy by leveraging human-guided revision.

As shown in the results, while the initial Pix2Code model was susceptible to biases in the "Confounded" dataset, the addition of XIL allowed it to reach 100% accuracy by correcting the underlying logic.

Discussion and Limitations

While Pix2Code offers a powerful alternative to black-box models, it faces certain trade-offs. The program synthesis process is computationally intensive; training on the CURI dataset required approximately 13.5 hours, whereas the neural baseline finished in 18 minutes. Additionally, the system's performance is strictly capped by the accuracy of the neural object extractor. If the "front-end" fails to detect an object, the "back-end" logic cannot compensate.

Future research will focus on improving the efficiency of the synthesis search and exploring more intuitive ways for humans to interact with the symbolic library, potentially moving toward direct natural language synthesis.

Summary updated

BibTeX
@inproceedings{wust2024pix,
  author = {Antonia Wüst and Wolfgang Stammer and Quentin Delfosse and Devendra Singh Dhami and Kristian Kersting},
  title = {{Pix2Code: Learning to Compose Neural Visual Concepts as Programs}},
  booktitle = {Conference on Uncertainty in Artificial Intelligence},
  year = {2024},
  url = {https://openreview.net/forum?id=j6MQQErg0p},
}
Figure