Abstract
Graphic layout generation plays a significant role in user engagement and information perception. Existing methods primarily treat layout generation as a numerical optimization task, focusing on quantitative aspects while overlooking the semantic relationships between elements. In this paper, we propose LayoutNUWA, the first model that treats layout generation as a code generation task to enhance semantic information and harness the hidden layout expertise of large language models (LLMs). We develop a Code Instruct Tuning (CIT) approach comprising three modules: Code Initialization (CI), Code Completion (CC), and Code Rendering (CR). This framework ensures a highly interpretable and transparent layout generation procedure. We attain significant state-of-the-art performance, including over 50% improvements on complex datasets, showcasing the strong capabilities of LayoutNUWA.
Introduction
Creating aesthetically pleasing and functional layouts—such as mobile UIs or magazine pages—requires a delicate balance of spatial precision and semantic understanding. Traditionally, layout generation models have operated within a numerical optimization paradigm, representing layouts as tuples of $(category, x, y, width, height)$. However, this approach often fails to capture the logical dependencies between elements, such as the typical placement of a "Title" above "Text" or a "Button" at the bottom of a screen.
LayoutNUWA shifts this perspective by reframing layout generation as a code generation task. The core hypothesis is that LLMs possess latent structural expertise acquired from seeing millions of lines of HTML and SVG code during pre-training. By leveraging this "hidden expertise," LayoutNUWA can reason about spatial relationships through semantic tags rather than just raw coordinates.
Methodology: Code Instruct Tuning (CIT)
The proposed framework, Code Instruct Tuning (CIT), enables LLMs to generate structured layouts through three interconnected modules:
- Code Initialization (CI): This module converts user-provided constraints (e.g., element categories and counts) into a masked HTML template. To bridge the gap between continuous spatial coordinates and discrete text tokens, the authors employ Adaptive Quantization (via k-means clustering) to map positions and sizes to standardized string representations.
- Code Completion (CC): Acting as the central reasoning engine, a pre-trained LLM (such as LLaMA2) fills in the masked values (
<M>) within the HTML structure. To ensure the model remains invariant to the order of input elements, a self-consistency strategy is used, involving the random permutation of elements during the training phase. - Code Rendering (CR): The completed code, containing absolute coordinates, is directly rendered into a visual layout. This approach eliminates the need for complex post-processing and ensures high interpretability, as the generated code directly maps to the visual output.
Experimental Evaluation
LayoutNUWA was evaluated on three diverse benchmarks representing different layout challenges:
- RICO: Over 66,000 mobile UI layouts.
- PubLayNet: Over 360,000 document layouts (scientific papers and reports).
- Magazine: A high-flexibility dataset of approximately 4,000 sophisticated magazine designs, representing a "low-resource" but high-complexity task.
Key Results
LayoutNUWA achieved significant state-of-the-art (SOTA) results across all metrics. Notably, on the Magazine dataset, the model improved the FID score—a metric for layout realism—by over 50% compared to the previous leading model, LayoutDM. Furthermore, in head-to-head comparisons with zero-shot GPT-4, LayoutNUWA maintained a near-zero failure rate (0.0% to 0.3%), whereas un-tuned models often failed to produce valid or logical structures. Human evaluations also confirmed a strong preference for LayoutNUWA’s designs in terms of both quality and diversity.
Discussion and Insights
The success of LayoutNUWA highlights several critical insights for the field of automated design:
- Semantic Context Matters: Using descriptive tags like
data-category="title"provides richer context than integer IDs, allowing the model to apply its learned knowledge of design hierarchy. - Domain Agnosticism: Models trained across multiple domains (UI, documents, magazines) often outperformed domain-specific models, suggesting that LLMs are learning universal principles of layout and composition.
- Interpretability: By outputting code, the model provides a transparent intermediate representation that can be easily inspected or manually adjusted by designers.
Conclusion
LayoutNUWA demonstrates that the structural knowledge embedded in LLMs can be effectively unlocked for spatial tasks through code-based instruction tuning. While challenges remain regarding inference speed due to the autoregressive nature of LLMs, the significant gains in layout quality and semantic coherence mark a major step forward in AI-assisted design. Future research will explore the integration of additional design attributes, such as color and typography, into this code-centric framework.
Summary updated