Handwriting Transformers

Ankan Kumar Bhunia, Salman H. Khan, Hisham Cholakkal, Rao Muhammad Anwer, Fahad Shahbaz Khan, Mubarak Shah

Introduction

Generating realistic synthetic handwriting is a challenging few-shot learning task. Traditional approaches, primarily based on Generative Adversarial Networks (GANs), often struggle with two fundamental issues: loose entanglement and the loss of local details. In loose entanglement, models fail to capture how a writer's style varies depending on the specific characters being formed. Furthermore, while GANs can replicate global attributes like slant or ink width, they frequently miss "local" nuances—the unique ligatures, loops, and character connections that define an individual's hand. Handwriting Transformers (HWT) addresses these limitations by leveraging the power of self-attention to model long-range dependencies and character-specific style attributes.

Methodology

The HWT framework replaces standard recurrent or purely convolutional architectures with a transformer-based encoder-decoder system designed to capture both global and local stylistic features.

The Handwriting Transformer (HWT) architecture, featuring a CNN-Transformer encoder for style extraction and a Transformer decoder for query word generation.

Architecture Components

  • Style Encoder: A ResNet18 CNN extracts feature maps from a small set of style examples (typically 15 images). These features are flattened and processed by a Transformer Encoder, which uses self-attention to understand the relationships between different parts of the reference handwriting.
  • Query Decoder: The decoder receives the target text as character-wise embeddings. It employs encoder-decoder attention to "look up" relevant style patterns from the encoder's output for each specific character in the query string.
  • Loss Functions: The model is optimized using a combination of losses to ensure realism and legibility:
  • Adversarial Loss: A hinge-version GAN loss ensures visual realism.
  • Recognition Loss (CTC): A pre-trained recognizer ensures legibility and content accuracy.
  • Style Classification Loss: Encourages the model to produce images identifiable as the target writer.
  • Cycle Consistency Loss: A critical innovation where generated images are passed back through the encoder to reconstruct original style features, preventing "style collapse."

Results and Evaluation

The model was rigorously evaluated using the IAM Handwriting Database, focusing on its ability to generalize to "Out-of-Vocabulary" (OOV) words and "Unseen Styles" (U).

Qualitative comparison of HWT against existing methods (GANwriting and Davis et al.) in mimicking a desired handwriting style.

Quantitative Performance

HWT significantly outperformed state-of-the-art models like GANwriting and Davis et al. across multiple benchmarks. On the most difficult task—generating unseen words in unseen styles—HWT achieved a Frechet Inception Distance (FID) of 114.10, a marked improvement over GANwriting's 130.68. Lower FID scores indicate higher visual quality and closer stylistic alignment with the target distribution.

Human Perception

In a user study involving 100 participants, HWT-generated samples were preferred 81% of the time over competing methods. Furthermore, in a "Turing Test" for handwriting, humans could only distinguish real handwriting from HWT-generated samples with 48.1% accuracy—essentially no better than random guessing. This suggests that the model successfully captures the intricate "local" quirks and ligatures that humans associate with authentic writing.

Conclusion

Handwriting Transformers demonstrate that the non-linear, long-range dependencies captured by self-attention are superior to sequential RNNs for handwriting synthesis. By treating queries as sequences of characters and employing cycle consistency, HWT achieves unprecedented realism in few-shot handwriting generation. Future work includes reducing the number of required style examples toward one-shot generation and extending the model to synthesize full-page layouts with consistent paragraph-level flow.

Summary updated

BibTeX
@inproceedings{bhunia2021handwriting,
  author = {Ankan Kumar Bhunia and Salman H. Khan and Hisham Cholakkal and Rao Muhammad Anwer and Fahad Shahbaz Khan and Mubarak Shah},
  title = {{Handwriting Transformers}},
  booktitle = {IEEE/CVF International Conference on Computer Vision},
  year = {2021},
  doi = {10.1109/iccv48922.2021.00112},
}
Figure