PosFormer: Recognizing Complex Handwritten Mathematical Expression with Position Forest Transformer

Tongkun Guan, Chengyu Lin, Wei Shen, Xiaokang Yang

Abstract

Handwritten Mathematical Expression Recognition (HMER) has wide applications in human-machine interaction scenarios, such as digitized education and automated offices. Recently, sequence-based models with encoder-decoder architectures have been commonly adopted to address this task by directly predicting LaTeX sequences of expression images. However, these methods only implicitly learn the syntax rules provided by LaTeX, which may fail to describe the position and hierarchical relationship between symbols due to complex structural relations and diverse handwriting styles. To overcome this challenge, we propose a position forest transformer (PosFormer) for HMER, which jointly optimizes two tasks: expression recognition and position recognition, to explicitly enable position-aware symbol feature representation learning. Specifically, we first design a position forest that models the mathematical expression as a forest structure and parses the relative position relationships between symbols. Without requiring extra annotations, each symbol is assigned a position identifier in the forest to denote its relative spatial position. Second, we propose an implicit attention correction module to accurately capture attention for HMER in the sequence-based decoder architecture. Extensive experiments validate the superiority of PosFormer, which consistently outperforms the state-of-the-art methods 2.03%/1.22%/2.00%, 1.83%, and 4.62% gains on the single-line CROHME 2014/2016/2019, multi-line M(^{2})E, and complex MNE datasets, respectively, with no additional latency or computational cost.

Introduction

Recognizing handwritten mathematical expressions is significantly more challenging than standard optical character recognition (OCR) due to the inherently two-dimensional and hierarchical nature of mathematical notation. Unlike linear text, math formulas involve complex spatial relationships such as fractions, exponents, and nested radicals.

Current state-of-the-art HMER models typically employ an image-to-sequence approach, treating the problem as a translation task from an image to a 1D LaTeX string. However, these models often only implicitly learn syntax rules. In the presence of deeply nested expressions or varied handwriting styles, they frequently suffer from "alignment drift," where the model's attention fails to track the correct spatial location for the next symbol in the sequence.

Methodology

To address these challenges, we introduce PosFormer, a novel architecture that enhances the standard Transformer encoder-decoder framework with two primary innovations: the Position Forest (PF) and the Implicit Attention Correction (IAC) module.

Architecture Overview

The PosFormer architecture utilizes a DenseNet encoder to extract visual features from the input image. These features are then processed by a Transformer-based decoder. A key distinction of PosFormer is its dual-head output: one for standard expression recognition and another for position forest recognition, which is utilized during the training phase to guide feature learning.

The PosFormer architecture, featuring a DenseNet encoder and a decoder with Implicit Attention Correction (IAC) and dual recognition heads.

Position Forest (PF) Coding

The core innovation is the Position Forest, which explicitly models the 2D spatial structure of formulas. Instead of treating LaTeX as a simple 1D sequence, each symbol is assigned a position identifier describing its "nested level" and "relative position" (e.g., middle, superscript, subscript, numerator, or denominator). This transforms the sequence into a structural map during training, forcing the model to learn the underlying hierarchy of the expression.

Position Forest Coding: Mapping a LaTeX sequence to a hierarchical forest structure with relative position identifiers.

Implicit Attention Correction (IAC)

To mitigate the "coverage problem"—where models skip symbols or repeatedly attend to the same region—we propose the IAC module. When the model identifies an "entity symbol" (like a variable or digit), the IAC module effectively suppresses the attention weights for that region in subsequent decoding steps. This mechanism ensures the model systematically focuses on unparsed parts of the expression.

Importantly, the Position Forest heads are auxiliary tasks used only during training. During inference, these components are discarded, allowing PosFormer to deliver higher accuracy with the same speed and memory footprint as a standard Transformer.

Experimental Results

PosFormer was evaluated across several benchmarks, including the standard CROHME (2014/2016/2019) datasets, the large-scale HME100k dataset, and the multi-line $M^2E$ dataset. To further test structural robustness, we introduced the MNE (Multi-level Nested Expression) dataset, specifically designed for deeply nested formulas.

PosFormer consistently outperformed previous state-of-the-art (SOTA) methods:

  • CROHME Benchmarks: Achieved absolute gains in Expression Recognition Rate (ExpRate) of 2.03%, 1.22%, and 2.00% on the 2014, 2016, and 2019 datasets, respectively.
  • Complex Structures: On the MNE dataset, PosFormer demonstrated a significant 4.62% improvement over baselines, validating its effectiveness in handling structural depth.
  • Multi-line Expressions: On the $M^2E$ dataset, it reached an ExpRate of 58.33%, a 1.83% improvement over the previous best model.

Conclusion

PosFormer demonstrates that explicit structural modeling is superior to implicit learning for complex HMER tasks. By incorporating a "forgetting" mechanism via IAC and a spatial "map" via the Position Forest, the model achieves robust performance across diverse and nested mathematical expressions. Future work may explore the integration of linguistic context and the application of the Position Forest concept to other structured document analysis tasks like table recognition.

Summary updated

BibTeX
@inproceedings{guan2024posformer,
  author = {Tongkun Guan and Chengyu Lin and Wei Shen and Xiaokang Yang},
  title = {{PosFormer: Recognizing Complex Handwritten Mathematical Expression with Position Forest Transformer}},
  booktitle = {European Conference on Computer Vision},
  year = {2024},
  url = {https://openreview.net/forum?id=slZfGFor1A},
}
Figure