Spatial Attention and Syntax Rule Enhanced Tree Decoder for Offline Handwritten Mathematical Expression Recognition

Zihao Lin, Jinrong Li, Fan Yang, Shuangping Huang, Xu Yang, Jianmin Lin, Ming Yang

Abstract

Handwritten Mathematical Expression Recognition (HMER) is a complex task due to the inherent two-dimensional structure of mathematical notation. While tree-based decoders have emerged as a promising approach to capture these spatial relationships, they often struggle with parent node ambiguity and the generation of syntactically invalid expressions. This paper presents the Spatial Attention and Syntax Rule Enhanced Tree Decoder (SS-TD), which integrates spatial context and grammatical constraints into the decoding process. By utilizing spatial attention to resolve parent-child relationships and syntax masks to enforce mathematical rules, SS-TD achieves state-of-the-art performance among tree-based decoders on the CROHME benchmarks while significantly reducing model parameters and inference latency.

Introduction

Recognizing handwritten mathematical expressions is significantly more challenging than standard optical character recognition (OCR). Unlike linear text, mathematical notation is inherently two-dimensional, where symbols are organized through complex spatial relationships such as superscripts, subscripts, fractions (above/below), and radical signs (inside).

Recent advancements in the field have shifted from traditional linear string decoders toward tree-based decoders, which are designed to explicitly model the hierarchical structure of math. However, existing tree decoders face two primary hurdles: the inability to distinguish between identical symbols when predicting parent-child relationships and the tendency to generate mathematically impossible structures. The SS-TD model addresses these issues by incorporating spatial awareness and formal syntax rules into the decoding architecture.

Challenges in Tree-Based Decoding

Tree-based models typically predict a symbol and its relationship to a "parent" node. However, two main errors frequently occur:

  1. Parent Prediction Errors: In expressions containing multiple instances of the same symbol (e.g., an equation with multiple variables named "n"), models relying solely on semantic hidden states often fail to identify which specific "n" is the correct parent for a new child symbol.
  2. Ungrammatical Outputs: Without structural constraints, models may produce "hallucinated" structures, such as assigning a subscript to a symbol that does not support one or assigning multiple "Right" neighbors to a single parent node.

The SS-TD Architecture

The SS-TD model utilizes a DenseNet encoder to extract visual features and introduces a novel tree decoder with three primary enhancements:

Spatial Attention-based Parent Prediction

To resolve ambiguities between identical characters, SS-TD incorporates spatial attention maps. Instead of relying only on the semantic meaning of previous symbols, the decoder "looks" at the physical coordinates and visual location of symbols within the image. A gating mechanism is employed to balance these spatial cues with semantic information, ensuring the model identifies the correct geometric parent for every node.

Syntax Rule-based Relation Prediction

To ensure the output is mathematically valid, the authors introduced two types of Syntax Masks:

  • Static Masks: These act as a predefined grammar, restricting the allowed relationships for specific symbol classes (e.g., a plus sign '+' is restricted from having certain children like a 'subscript').
  • Dynamic Masks: This mechanism tracks the relationships already assigned to a parent node during the decoding process. It prevents the model from repeating a relationship that has already been filled, such as ensuring a symbol does not have two different nodes in the 'Superscript' position.

Attention Self-Regularization

The model employs Kullback-Leibler (KL) divergence to minimize the discrepancy between the attention distributions of parent and child decoders. This self-regularization technique helps the model converge faster and ensures more consistent focus during the decoding of complex structures.

Experimental Results

The SS-TD model was evaluated on the Competition on Recognition of Online Handwritten Mathematical Expressions (CROHME) datasets from 2014, 2016, and 2019. These benchmarks are the standard for the HMER field, involving over 100 math symbol classes.

Performance

SS-TD consistently outperformed the baseline tree-based model (DenseWAP-TD) across all benchmarks:

  • CROHME 2014: Achieved an Expression Rate (ExpRate) of 52.48%, a 3.38% improvement over the baseline.
  • CROHME 2019: Achieved 54.32%, outperforming the baseline by nearly 3%.

Ablation Studies and Efficiency

Ablation studies confirmed that spatial attention provided the most significant performance boost (approximately 1.5%), while syntax masks were crucial for narrowing the gap between raw tree accuracy and valid LaTeX string generation.

Beyond accuracy, the authors optimized the model's architecture by simplifying embedding and attention dimensions. This resulted in a 50% reduction in parameter count and a 50ms reduction in inference latency compared to previous tree-based architectures, making the model more suitable for real-time applications.

Conclusion

The SS-TD model demonstrates that integrating geometric spatial context and hard grammatical constraints can significantly improve the reliability of tree-based math decoders. While the model currently lags slightly behind the highest-performing string-based decoders—which often rely on massive data augmentation and complex beam search strategies—it sets a new state-of-the-art for structural tree-based recognition. Future research will focus on combining the structural robustness of SS-TD with the high-accuracy symbol recognition found in the latest string-based architectures.

Summary updated

BibTeX
@inproceedings{lin2022spatial,
  author = {Zihao Lin and Jinrong Li and Fan Yang and Shuangping Huang and Xu Yang and Jianmin Lin and Ming Yang},
  title = {{Spatial Attention and Syntax Rule Enhanced Tree Decoder for Offline Handwritten Mathematical Expression Recognition}},
  booktitle = {ICFHR},
  year = {2022},
  url = {https://openreview.net/forum?id=oax0hLB66c},
}
Figure