Abstract
Automated reverse engineering of HTML/CSS code from UI screenshots is an important yet challenging problem with broad applications in website development and design. In this paper, we propose a novel vision-code transformer (ViCT) composed of a vision encoder processing the screenshots and a language decoder to generate the code. They are initialized by pre-trained models such as ViT/DiT and GPT-2/LLaMA but aligning the two modalities requires end-to-end finetuning, which aims to minimize the visual discrepancy between the code-rendered webpage and the original screenshot. However, the rendering is non-differentiable and causes costly overhead. We address this problem by actor-critic fine-tuning where a visual critic without rendering (ViCR) is developed to predict visual discrepancy given the original and generated code. To train and evaluate our models, we created two synthetic datasets of varying complexity, with over 75,000 unique (code, screenshot) pairs. We evaluate the UI-to-Code performance using a combination of automated metrics such as MSE, BLEU, IoU, and a novel htmlBLEU score. ViCT outperforms a strong baseline model DiT-GPT2, improving IoU from 0.64 to 0.79 and lowering MSE from 12.25 to 9.02. With much lower computational cost, it can achieve comparable performance as when using a larger decoder such as LLaMA.
Introduction
Reverse-engineering a User Interface (UI) is a complex multi-modal task requiring a model to understand spatial relationships from an image and translate them into structured code syntax. While Large Language Models (LLMs) excel at generating code, they often struggle with the precise visual nuances of a layout. Standard training approaches minimize text-based differences between generated and ground-truth code. However, in web development, distinct code snippets can produce identical visual results. Optimizing for visual accuracy typically requires rendering code into an image for comparison—a process that is slow, computationally expensive, and non-differentiable, preventing standard backpropagation.
The ViCT and ViCR Framework
To overcome the rendering bottleneck, we introduce a two-part framework that enables efficient reinforcement learning for pixel-perfect code generation.
- ViCT (Vision-Code Transformer): Acting as the "Actor," ViCT utilizes a vision encoder (such as ViT or DiT) to process the screenshot and a language decoder (such as GPT-2 or LLaMA) to generate markup. These components are linked via cross-attention layers, allowing the decoder to attend to specific image regions during code generation.
- ViCR (Visual Critic without Rendering): Acting as the "Critic," this model is trained to predict the visual discrepancy (specifically Intersection over Union, or IoU) between the original screenshot and the hypothetical output of the generated code. By using a learned critic instead of a physical browser renderer, we bypass the non-differentiability problem.
Datasets and Evaluation
Due to the scarcity of high-quality screenshot-to-code pairs, we developed two synthetic benchmarks:
- RUID (Random UI Dataset): 25,000 pairs featuring basic shapes and styled buttons.
- RUID-Large: 50,000 pairs using realistic HTML elements like
div,input, andselectto simulate complex interfaces.
We also propose htmlBLEU, a metric that weights HTML keywords and DOM structures more heavily than standard BLEU. Our analysis shows a high correlation (0.764) between htmlBLEU and Mean Squared Error (MSE), confirming that standard NLP metrics are insufficient for visual code evaluation.
Key Results
Our experiments demonstrate that ViCR fine-tuning significantly boosts performance. On the RUID dataset, ViCT improved its IoU from 0.64 to 0.79 and reduced MSE from 12.25 to 9.02 compared to the baseline. Furthermore, ViCT achieved results comparable to much larger models like LLaMA at a fraction of the computational cost.
Qualitatively, ViCT shows superior accuracy in element placement and styling compared to general-purpose vision-language models. As shown in the comparison below, while models like MiniGPT-4 may hallucinate or produce generic text, ViCT maintains high structural fidelity to the original UI.
Conclusion
By replacing physical rendering with a learned visual critic, we have made RL-based fine-tuning for UI generation feasible at scale. Our findings suggest that Document Image Transformers (DiT) are particularly effective for this task due to the structural similarities between UIs and documents. Future work will focus on scaling these models to handle more diverse, real-world web designs and interactive elements like JavaScript.
Summary updated