AnyText: Multilingual Visual Text Generation and Editing

Yuxiang Tuo, Wangmeng Xiang, Jun-Yan He, Yifeng Geng, Xuansong Xie

TL;DR

AnyText is a novel diffusion-based framework designed to solve the "gibberish text" problem in AI-generated images. By combining a specialized auxiliary latent module for spatial control with an OCR-based text embedding module, it enables the generation and editing of accurate, legible, and aesthetically integrated text across multiple languages and irregular shapes. Notably, it is the first work to specifically address multilingual visual text generation.

The Challenge of Visual Text Generation

Users of popular text-to-image models often encounter a glaring flaw: while these models can generate breathtaking landscapes and photorealistic subjects, they are notoriously bad at spelling. Text in generated images frequently appears as blurred, nonsensical squiggles.

The authors identify three primary reasons for this limitation:

  1. Tokenizer Limitations: Most models use CLIP-based text encoders that are "character-blind." They treat words as semantic tokens rather than sequences of specific visual characters.
  2. Loss Function Gaps: Standard diffusion training optimizes for overall image quality but lacks specific supervision for the fine-grained, structural details required in text regions.
  3. Data Scarcity: There is a significant lack of large-scale, high-quality datasets that pair images with precise OCR (Optical Character Recognition) annotations, especially for non-Latin scripts like Chinese, Japanese, or Korean.

The AnyText Architecture

To address these issues, AnyText introduces a two-pronged architecture designed to seamlessly integrate text into images:

  • Auxiliary Latent Module: This module provides crucial spatial and structural guidance. It takes three inputs: a glyph image (representing the visual structure of the characters), a position map (defining where the text goes, accommodating curved or irregular shapes), and a masked image (used for text editing tasks). These inputs are encoded into the latent space to guide the UNet generator.
  • Text Embedding Module: Instead of relying solely on a standard text tokenizer, AnyText innovatively uses a pre-trained OCR model to encode the visual "strokes" of the text. These visual embeddings are fused with the standard image caption tokens via cross-attention. This ensures the model fundamentally understands the visual structure of the characters it is rendering.
  • Text Perceptual Loss: To guarantee the final output is readable, the authors implemented a specialized loss function. It compares the features of the generated text region against the intended text using an OCR recognition model, penalizing the generator if the output is illegible or incorrect.

New Datasets: AnyWord-3M

To train this robust model, the researchers contributed a massive new dataset to the community: AnyWord-3M. This large-scale multilingual dataset contains 3 million image-text pairs with precise OCR annotations, encompassing a diverse mix of English, Chinese, and other languages. Alongside this, they introduced the AnyText-benchmark, a new evaluation suite used to rigorously test sentence accuracy and image quality.

Key Insights and Results

Examples of AnyText generation and editing capabilities

One of the most clever innovations in this paper is using an OCR model in reverse. Usually, OCR is used for reading; here, its internal feature representations are used to teach the diffusion model how to write. Furthermore, unlike previous methods that struggled with anything other than horizontal bounding boxes, AnyText can render text into deformed regions—such as text curved around a coffee mug or slanted on a perspective-heavy sign.

AnyText significantly outperforms previous state-of-the-art models like ControlNet and TextDiffuser:

  • Quantitative Superiority: In English text generation, AnyText achieved a Sentence Accuracy of 72.39%, compared to just 59.21% for TextDiffuser. In Chinese, the gap is even more pronounced: 69.23% accuracy for AnyText versus a mere 6.05% for TextDiffuser.
  • Qualitative Integration: As seen in the generated examples, the model excels at seamless integration. Instead of looking like text was artificially pasted on top of an image, AnyText's results look carved into ice, frosted onto cakes, or glowing in neon, perfectly respecting the lighting and texture of the surrounding environment.
  • Plug-and-Play Versatility: AnyText is not just a standalone model; it can be integrated into existing community diffusion models to grant them accurate "writing" capabilities without destroying their unique artistic styles.

Limitations and Future Work

While AnyText represents a major leap forward, the authors note a few remaining hurdles. The model still struggles to render extremely small fonts clearly, likely due to the resolution limits inherent in the latent diffusion process. Additionally, while the model is excellent at determining "what" to write and "where" to write it, fine-grained control over specific font styles or colors via text prompts remains an area for future exploration. Finally, the auxiliary modules add a slight computational overhead, though this is generally negligible for most practical applications.

Summary updated

BibTeX
@inproceedings{tuo2024anytext,
  author = {Yuxiang Tuo and Wangmeng Xiang and Jun-Yan He and Yifeng Geng and Xuansong Xie},
  title = {{AnyText: Multilingual Visual Text Generation and Editing}},
  booktitle = {International Conference on Learning Representations},
  year = {2024},
  url = {https://openreview.net/forum?id=ezBH9WE9s2},
}
Figure