HATFormer: Historic Handwritten Arabic Text Recognition with Transformers

## Abstract

Abstract Arabic handwritten text recognition (HTR) is challenging, especially for historical texts, due to diverse writing styles and the intrinsic features of Arabic script.
Additionally, Arabic handwriting datasets are smaller compared to English ones, making it difficult to train generalizable Arabic HTR models. To address these challenges, we propose HATFormer , a transformer-based encoder–decoder architecture that builds on a state-of-the-art English HTR model.
By leveraging the transformer’s attention mechanism, HATFormer captures spatial contextual information to address the intrinsic challenges of Arabic script through differentiating cursive characters, decomposing visual representations, and identifying diacritics.
Our customization to historical handwritten Arabic includes an image processor for effective ViT information preprocessing, a text tokenizer for compact Arabic text representation, and a training pipeline that accounts for a limited amount of historic Arabic handwriting data. HATFormer achieves a character error rate (CER) of 8.6% on the largest public historical handwritten Arabic dataset, with a 51% improvement over the best baseline in the literature. HATFormer also attains a comparable CER of 4.2% on the largest private non-historical dataset.
Our work demonstrates the feasibility of adapting an English HTR method to a low-resource language with complex, language-specific challenges, contributing to advancements in document digitization, information retrieval, and cultural preservation.

## 1 Introduction

Global archives contain hundreds of millions of manuscript pages written in the Arabic alphabet, primarily from the 19th and early 20th centuries, with around 25 million images from the Middle East and North Africa alone. For historians, the laborious process of sifting through these pages for relevant data is impractical due to time and resource constraints. Existing handwritten text recognition (HTR) systems for non-historical Arabic texts fail to effectively render these historical documents into a searchable format. Developing a dedicated HTR system for historical Arabic manuscripts would revolutionize digital humanities, enabling rapid data search and retrieval while facilitating the creation of advanced large language models for research, thus opening new avenues for historical and humanities scholarship.

This paper introduces HATFormer, a transformer-based historical Arabic HTR system that leverages self-attention mechanisms to capture long-range dependencies, outperforming traditional HTR methods for complex scripts like Arabic.
HTR systems such as Shi et al. () have traditionally relied on convolutional neural networks (CNNs) (LeCun et al., ) for feature extraction and recurrent neural networks (RNNs) (Rumelhart et al., ) for text generation. However, RNN-based methods often struggle to capture long-range dependencies, which are more crucial for handling Arabic scripts than for English scripts.
Recently, transformer (Vaswani et al., ) methods have shown to be promising for modern and historical English HTR tasks, with Li et al. (), Fujitake (), and Parres & Paredes () achieving state-of-the-art character error rates (CER) of 2.9%, 2.4%, and 2.7%, respectively.
HATFormer builds on the success of pretrained vision and text transformers in HTR, introducing key adaptations to handle the intrinsic challenges of Arabic for more accurate recognition of historical text.

We will show through experimental verification that the inductive bias of the transformer’s attention mechanism effectively addresses the following three intrinsic challenges (Najam & Faizullah, ; Faizullah et al., ) of Arabic script absent in English.
First, Arabic is written in cursive, making characters visually harder to distinguish. The attention mechanism allows the model to better differentiate between connected characters. Second, Arabic characters are context-sensitive, meaning a character’s shape can change depending on its position in a word and adjacent characters. Attention helps accurately decompose these visual representations by focusing on the relevant context within the sequence. Third, the Arabic language includes diacritics, which are markings above or below characters that can completely alter the semantics of a word. Attention enables the model to effectively identify diacritics by considering their contextual influence on surrounding characters.

In addition to the intrinsic challenges of Arabic scripts, Arabic handwritten datasets, especially historical ones, are significantly smaller than those available for languages like English. Many HTR works (Li et al., ; Wigington et al., ; Zhang et al., ) focus on languages using the modern Latin alphabet, such as English and French, where large amounts of training data are readily available. Common datasets include IAM (Marti & Bunke, ) and RIMES (Grosicki et al., ), containing over 1,500 and 12,500 pages, respectively. In contrast, the largest public dataset for handwritten Arabic (Saeed et al., ) consists of just over 1,600 pages, while another widely used dataset, KHATT (Mahmoud et al., ), contains only 1,000 pages.

We base our approach on TrOCR (Li et al., ) and leverage domain knowledge of the Arabic language to identify key factors in building an effective historical Arabic HTR system.
We incorporate a novel image preprocessor and synthetic dataset generator to enhance performance by minimizing horizontal information loss and expanding the training dataset with realistic synthetic images.
We perform extensive evaluation and cross-dataset experiments on HATFormer.
We will release the image preprocessor, tokenizer, model weights, and source code for our HTR system, along with a detailed guide for researchers to interface our system with existing text detection packages for page-level HTR evaluations and practical deployment. Additionally, we will release our dataset of realistic synthetic Arabic images and its generation source code, as well as provide an OCR Error Diagnostic App and its source code to benefit both machine learning and history studies researchers. The contributions of our work are threefold.

- 1.
Our proposed HATFormer for historical Arabic HTR outperforms the state of the art across various Arabic handwritten datasets. It achieves a CER of 8.6% and 4.2% on the largest public and private handwritten Arabic datasets, respectively.
- 2.
Our method has proven effective by leveraging the attention mechanism to address three intrinsic challenges of the Arabic language.
- 3.
Our historical Arabic HTR system and OCR Error Diagnostic App will aid humanity researchers by automatically transcribing historical Arabic documents and debugging common recognition errors, thereby significantly enhancing the accessibility of these documents.

## 2 Related Works

Handwritten Text Recognition (HTR).
Handcrafted features were historically used for optical character recognition (OCR) and HTR (Balm, ), but deep learning methods gradually took over due to their improved performance.
Common deep learning methods adopt the encoder–decoder paradigm where visual signals are encoded into a feature representation and the feature is decoded for text generation. Graves & Schmidhuber () proposed using a long short-term memory (LSTM) (Hochreiter & Schmidhuber, ) multidimensional recurrent neural network (MDRNN) (Graves et al., ) for feature extraction and a connectionist temporal classification (CTC) layer for decoding (Graves et al., ). Notably, Shi et al. () introduced the convolutional recurrent neural network (CRNN) architecture for OCR, where a CNN was used to extract visual features from images, and a stacked bidirectional LSTM (BLSTM) (Graves & Schmidhuber, ; Graves et al., ) was used as the decoder. Puigcerver (); Wang & Hu () respectively adapted the encoder to use a CNN and modified recurrent convolutional neural network (RCNN) (Liang & Hu, ). Newer approaches (Michael et al., ; Wang et al., ) attempt to incorporate the attention mechanism (Bahdanau et al., ) into the HTR pipeline.

Transformers for HTR.
Transformers (Vaswani et al., ) have recently been applied to HTR with earlier works using architectures consisting of a CNN-feature-extractor encoder and a transformer-encoder–decoder-hybrid decoder, which later was simplified to a transformer-only encoder–decoder pair or a transformer-decoder-only architecture. Wick et al. () proposed a hybrid system that uses a CNN feature extractor and multiple encoder–decoder transformers for bidirectional decoding. Li et al. () proposed a transformer-only method utilizing pretrained vision transformers (ViT) (Dosovitskiy et al., ), specifically BEiT (Bao et al., ), as its encoder using raw pixels as input and text transformers, specifically RoBERTa (Liu et al., ), as the decoder. Fujitake () proposed a transformer-decoder-only method, using GPT (Radford et al., ; ) in particular, with raw pixel inputs.
However, the decoder-only method, in general, requires more labeled data for training end-to-end, whereas a pretrained encoder could be used as an initialization step for visual feature extraction.
Our approach does not use a dedicated CNN feature extractor and builds upon the transformer-only encoder–decoder architecture.
ViTs have been shown to outperform CNNs and can benefit from large-scale pretraining for downstream tasks with low resources (Dosovitskiy et al., ), like Arabic HTR.

Arabic HTR. Arabic handwriting poses unique challenges to HTR systems, such as cursive writing, connected letters, and context-dependent character shapes. One of the earliest approaches to Arabic HTR is Graves & Schmidhuber (), which proposes using multidimensional LSTM (MDLSTM)
and CTC decoding. Shtaiwi et al. (); Lamtougui et al. (); Saeed et al. () proposed using a CNN and BLSTM architecture, with Shtaiwi et al. (); Saeed et al. () based upon the Start, Follow, Read network (Wigington et al., ). As with traditional English HTR, many Arabic HTR systems are starting to use the transformer architecture. Mostafa et al. () proposed a method that combines a ResNet-101 (He et al., ) for feature extraction and an encoder–decoder transformer for text prediction.
Momeni & BabaAli () proposed a system that solely uses transformers, similar to Li et al. (), but also introduces transducers (Graves, ) for HTR, removing the need for external postprocessing language models.
We continue using transformers for HTR and leverage the most recent advancements to further improve recognition performance on Arabic texts.

## 3 Background and Preliminaries

This section provides background information about the
components that HATFormer is built on.

Arabic-Character Encoding. Arabic characters can be efficiently represented in tokens for learning using byte-level byte pair encoding (BBPE) (Radford et al., ). It is a tokenization technique that compresses a string into a reversible compact representation by leveraging the UTF-8 encoding standard using a vocabulary dictionary. To train the vocabulary dictionary, it is initialized with all 256 possible byte values as base tokens, allowing it to tokenize any Unicode character
and eliminating the need for a task-specific vocabulary. They are then iteratively merged based on the most frequent token pairs in a corpus to form new tokens. This iterative process expands the vocabulary, allowing for more efficient encoding of frequent patterns.

TrOCR.
The TrOCR framework (Li et al., ) for predicting text from images will be used as the base architecture for this work. TrOCR employs a transformer-only encoder–decoder architecture, specifically using a pretrained ViT as the encoder and a pretrained text transformer as the decoder.
The encoder takes an input image of shape $3\times H_{0}\times W_{0}$, which is resized to a fixed shape of $3\times H\times W$. The resized image is then decomposed into a sequence of $N=HW\big{/}P^{2}$ patches, where each patch has a shape of $3\times P\times P$. The encoder will use the patches with added positional embeddings as input to generate encoder embeddings.
The decoder employs masked attention on the ground-truth text tokens
to ensure it does not access more information during training than during prediction.
The ground-truth text tokens are then combined with the encoder embedding using cross-attention.
A linear layer projects the hidden states from the decoder to match the vocabulary size, and the probabilities over the vocabulary are computed using the softmax function. Beam search is used to generate the final output.

## 4 Proposed Method for Historical Arabic HTR

In this section, we present HATFormer, which tackles the unique challenges of Arabic handwriting recognition, particularly for historical documents. We describe the main components of our method, including an image processor for effective ViT information preprocessing, a text tokenizer for compact Arabic text representation, and a training pipeline that accounts for the limited availability of historic Arabic handwriting data.

Architecture and Unit of Analysis.
Prediction for HTR involves recognizing and converting a text image into machine-readable characters. As illustrated in Figure 1, HATFormer follows TrOCR’s transformer encoder–decoder architecture for HTR text prediction.

Figure: Figure 1: The architecture of HATFormer. The input text-line image is processed by our BlockProcessor and the BEiT vision transformer. The ground-truth text string is tokenized using our Arabic BBPE tokenizer. The RoBERTa transformer is used for text prediction. HATFormer addresses the three intrinsic challenges of Arabic scripts by leveraging attention and is able to work on smaller datasets with the help of our synthetic image training pipeline.
Refer to caption: x1.png

We focused on line-level images as in Li et al. (); Momeni & BabaAli (),
which is more challenging than the word- and character-level predictions but less complex than the paragraph- and page-level predictions. This approach allows us to focus on Arabic HTR without the additional complexities of text document structure. HATFormer can be easily integrated with existing layout detection methods, enabling full-page prediction capabilities.

### 4.1 BlockProcessor for Effective ViT Information Preprocessing

We introduce a BlockProcessor to best prepare each text-line image for effective ViT comprehension by applying image-processing insights and leveraging ViT’s blocking and indexing behaviors.
The proposed BlockProcessor works by first horizontally flipping a text-line image, then standardizing its height to 64 pixels, and finally warping it to fill in the ViT’s 384$\times$384-pixel image container from left to right and top to bottom.
The ViT’s image container will allow up to six nonoverlapping complete rows that are 384 pixels wide, accommodating line images with varying widths for up to 2,304 pixels.
For shorter images, zeros will be padded.
Figure 2(c) shows an output of the proposed BlockProcessor respecting the input image’s aspect ratio to allow potential perfect reconstruction.
In contrast, images in Figure 2(d), (b), and (f) show significant information loss due to the direct use of ViT’s image preprocessor.
We provide analysis below and justify the system design of BlockProcessor.

Aspect Ratio. ViT resizes input images to 384$\times$384 without respecting their original aspect ratios.
This leads to an inefficient representation of text-line images from the Muharaf dataset, which has an average image width of 614 pixels after standardizing their heights to 64 pixels.
A direct application of ViT image preprocessing will lead to horizontal compression of 1.6 times on average, losing the clarity of the strokes in the horizontal direction for confident recognition.
Figure 2(d) shows a ViT-resized text-line image and Figure 2(b) shows the image content if the resized image is rescaled back to its original shape.

Figure: Figure 2: Left: Our proposed BlockProcessor respects the aspect ratio of (a) an original image and chunks it to fit within (c) a 384$\times$384-pixel ViT image container. In contrast, the base ViT image processor naively resizes images to (d) fully occupy its square image container, resulting in (f) significant horizontal information loss of the vertical strokes when compared to (e) the raw version. Right: (g) Synthetic image generation pipeline. Realistic-looking text-line images are generated by randomly selecting words from a large Arabic corpus, rendering with a random font, paper background, and image augmentation.
Refer to caption: x2.png

As the zoomed-in reconstruction block in Figure 2(f) reveals, the vertical strokes suffer the most severe blurring, making it difficult for any observer to confidently determine the exact thickness of a stroke at different vertical heights.

ViT Blocking & Indexing.
In BlockProcessor, both horizontal flipping of text-line images and the standardization of their heights to 64 pixels are designed to better leverage ViT’s blocking and indexing behaviors for more efficient transformer training.
First, Arabic text-line images read from right to left, so flipping them horizontally can avoid representing the end of a sentence with beginning ViT image tokens. Even though positional embeddings will help with ordering, we opt not to add extra workload to the attention layers as it will potentially require more training data.
Second, we standardize the line image to an integer multiple of the ViT’s patch height of 16 pixels.
This resizing choice ensures that when Arabic texts are written in the middle of a text line, the corresponding ViT image tokens with foreground text will always have similar indices.
Without resizing the height to an integer multiple of 16 pixels, the boundary of a text line and the boundary of a ViT block will misalign at varying pixel counts for different rows. This will cause foreground text to appear in all ViT image tokens, increasing the learning complexity for the attention layers.

### 4.2 Arabic BBPE Text Tokenizer for Compact Arabic Text Representation

Text representation is integral for language modeling. Radford et al. () showed the impact of using a byte-level representation for text with byte-level byte pair encoding (BBPE). This led to an optimal balance of token sequence length and vocabulary size. To efficiently represent Arabic text, we trained our own custom BBPE dictionary on a combined corpus from Abbas & Smaili (); Abbas et al. (); Saad & Alijla ().
As the base BBPE dictionary from Radford et al. () is skewed toward ASCII characters, our experiments show that Arabic text is represented with over 300% more tokens compared to our custom BBPE dictionary. The more compact representation from the custom BBPE dictionary results in a less complicated classification problem, resulting in higher accuracy along with our BlockProcessor, as we will discuss in Section 5.5.

### 4.3 Training on Realistic Synthetic and Real-World Textline Images

Our proposed method involves a two-stage training/fine-tuning process, i.e., training on a large synthetic dataset followed by fine-tuning on a real-world Arabic handwritten dataset.

Stage 1–Training on Large Synthetic Printed Dataset. To address the scarcity of historical handwritten Arabic data and capture key intrinsic features of Arabic scripts, we first trained HATFormer on a large dataset of one million synthetic text-line images. This approach mitigates the impact posed by the limited availability of historical handwritten Arabic data. The synthetic images contain all three inherent characteristics of Arabic, i.e., cursive writing, context-dependent character shapes, and diacritics. This enables our system to learn these challenging characteristics of Arabic script before being trained on a downstream task. Synthetic training provides the necessary data for the encoder to learn the visual features of Arabic, leading to more effective generalization. The synthetic image generation pipeline will be described in Section 5.1 and shown in Figure 2.

Stage 2–Fine-Tuning on Real Handwritten Dataset. We fine-tune HATFormer on real Arabic handwritten datasets, primarily focusing on the Muharaf dataset containing 36,000 text-line images due to its relevance to historical handwritten texts.
To achieve strong performance on Arabic HTR, we leverage a technical insight for large-scale training from Hao et al. (); Mosbach et al. ().
Traditional machine learning theory suggests that when the validation loss flattens, the model has converged, and no further learning occurs (Mohri et al., ; Jo, ). However, Mosbach et al. () demonstrated that transformers
can continue to improve in task performance long after the validation loss has plateaued.
Mosbach et al. () indicates that achieving a near-perfect training loss can serve as a strong baseline for model performance.
In Stage 2, we train past the plateau of the validation loss and approach a near-perfect training loss while monitoring the validation CER as the stopping criteria, which can take twice as long as the minimum validation loss.

## 5 Experimental Results

We present the experimental results for HATFormer on three Arabic handwritten datasets and compare it with other Arabic HTR baselines. We also conduct ablation studies to assess the effectiveness of each component and analyze various parameters of HATFormer.

### 5.1 Synthetic & Real-World Arabic Datasets

Synthetic Stage 1 Training Dataset.
For our Stage 1 training dataset, we generated 1,000,065 synthetic images of Arabic text lines. We first randomly sampled between 1–20 words from an Arabic corpus containing 8.2 million words. The sampled words were then paired with one of 54 Arabic text fonts on a background chosen from 130 paper background images and one of eight image augmentations to generate synthetic line images. Our ablation study in Section 5.5 will show that English OCR initialization is insufficient and synthetic Arabic training is required.

Arabic HTR Datasets.
The Muharaf dataset (Saeed et al., ) is a collection of historical handwritten Arabic manuscripts that span from the early 19th century to the early 21st century. The dataset contains over 36,000 text line images, which vary significantly in quality, from clear writing on clean white backgrounds to illegible sentences on creased pages with ink bleeds.
The KHATT dataset (Mahmoud et al., ) is a collection of Arabic handwriting samples with over 6,600 segmented line images. All images have black text on a clean white background.
The MADCAT dataset (Lee et al., ; ; ) is a collection of 740,000 handwritten Arabic line images created under controlled conditions, with writing speed, implementation, and paper-type specifications. All images have black text on a clean, white background.

### 5.2 Experimental Conditions

We initialized our model from HuggingFace’s trocr-base-stage1 334M parameter model. We use BEiT (Bao et al., ) and RoBERTa (Liu et al., ) as the encoder and decoder, respectively, since TrOCR (Li et al., ) empirically showed that they achieved the best CER performance. We used a batch size of 15 with a learning rate of $5\times 10^{-5}$ and linear warmup of 20,000 steps for synthetic Stage 1 training. For Stage 2 fine-tuning, we used a batch size of 30 with a learning rate of $10^{-4}$ and linear warmup of 2,000 steps. The warmup was followed by an inverse square root schedule for both Stage 1 training and Stage 2 fine-tuning. We trained on 2 to 4 A100 or H100 GPUs.

We did Stage 1 training on a train–validation–test dataset split of 90–9–1 for 1,000,065 synthetic line images.
We fine-tuned using a split of 85–15–5 for 25,767 line images from the Muharaf dataset, the author recommended 72–14–14 split for 6,687 line images from the KHATT dataset, and a 72–18–10 split for 741,877 line images from the MADCAT dataset. We used the traditional validation loss as the early stopping criterion during our Stage 1 training step. However, we used the overtraining technique during our Stage 2 fine-tuning and utilized the validation CER for early stopping as explained in Section 4.3.

### 5.3 Main Results

We first compare the performance of HATFormer against state-of-the-art baselines across the Muharaf, KHATT, and MADCAT datasets. We evaluate the HTR performance using the character error rate (CER) (Levenshtein, ), which is widely used for assessing the accuracy of OCR and HTR systems (Neudecker et al., ). It is defined as $\text{CER}=(S+D+I)\big{/}N$, where $S$ is the number of substitutions, $D$ is the number of deletions, $I$ is the number of insertions, and $N$ is the total number of characters in the original text. The CER is based on the edit distance, which calculates the number of aforementioned operations required to transform the predicted text into the original text.
We also performed cross-dataset comparisons to evaluate HATFormer’s ability to generalize across different datasets.

Table 2 reports the CER for HATFormer and several existing baselines across the three datasets.

Table: Table 1: Performance on Arabic Handwritten Datasets.

Since the public source code for existing Arabic HTR baseline models is not available, except Saeed et al. (), we compared our results to the reported numbers obtained from their papers. For Saeed et al. (), we retrained their model on each dataset for a fair comparison. It is important to note that the dataset splits used in these baselines may differ from those in our experiments, potentially affecting direct comparisons. Additionally, we conducted experiments on two variants of Muharaf, the entire dataset and a subset containing only Arabic characters. This allows us to investigate the impact of non-Arabic characters on HTR performance. For clarity, our analysis will refer to the Arabic-only subset as Muharaf.

We first compared with CNN and RNN-based methods. HATFormer achieves a CER of 8.6% and 15.4% on the Muharaf and KHATT datasets, respectively, as compared to Saeed et al. () who achieved a CER of 17.6% and 14.1%. Lamtougui et al. () achieved a CER of 19.9% on the KHATT dataset. These results indicate that the transformer architecture can significantly outperform traditional HTR methods based on CRNNs with a 23–51% improvement in CER for handwritten Arabic. This aligns with computer vision and natural language processing trends, where transformers are increasingly favored due to their superior ability to take care of contextual information.

While Saeed et al. () slightly outperformed our method on the KHATT dataset, HATFormer remains comparable. We attribute this to differences in the dataset characteristics, which may favor Saeed et al. ()’s hybrid architecture. Our results imply that CNNs and RNNs are no longer required for HTR. We enable a fully transformer-based model that can surpass these hybrid architectures by utilizing vision transformers as standalone feature extractors combined with a text transformer decoder. We credit the effectiveness of our model to the attention mechanism, which allows for learning contextual information
critical for language modeling. Figure 3 illustrates how the attention mechanism captures character relationships. See Appendix A for a more detailed description and analysis of the attention maps.

Figure: Figure 3: Self- and Cross-attention map visualizations. Yellow highlights areas of greater attention, with attention maps overlaid onto the input image for easier comparison. Left: ViT encoder self-attention maps for selected patch tokens. The top of each column shows the relevant patch, followed by attention maps showing what the transformer attends to as it progresses through its subsequent layers. The leftmost column shows the attention for a diacritic patch. Right: RoBERTa decoder cross-attention maps for selected ground truth text tokens. Each row represents consecutive text tokens, read from right to left, with the decoded token string above each map. The attention maps reveal the model’s ability to attend to relevant image regions for each token. It can handle a diverse range of text, from small diacritics to complex compounded characters, demonstrating the model’s ability to overcome the inherent challenges of Arabic script.
Refer to caption: extracted/5897575/figs/attention_maps.png

We also compared our approach with transformer-based methods. HATFormer achieves a CER of 15.4%, compared with Momeni & BabaAli (), who achieved a CER of 18.5% on the KHATT dataset. Our 17% improvement in CER demonstrates the effectiveness of our preprocessing and overtraining methods. Our preprocessing pipeline mitigates information loss caused by horizontal image compression, resulting in a CER improvement discussed in Section 5.5, while our overtraining strategy establishes a strong baseline, ultimately leading to better performance.

HATFormer achieves a CER of 4.2%, comparable to other baseline Arabic HTR models on the MADCAT dataset. The 1.5% CER achieved by Rawls et al. () may be due to several factors, specifically text normalization during evaluation (Rawls et al., ), which can significantly improve performance as shown in Section 5.5. MADCAT also presents many unique dataset-specific complexities and requires distinct preprocessing techniques, as highlighted by Abandah & Al-Hourani (). Furthermore, as both KHATT and MADCAT are non-historical datasets, they pose a different set of challenges compared to the historical Arabic texts that are the main focus of our work. While we included MADCAT and KHATT in our evaluation for a more complete comparison with existing Arabic HTR systems, we did not specifically optimize for them, as our primary goal is to enhance the performance of historical Arabic HTR.

### 5.4 Cross-dataset Evaluation

We conducted cross-dataset evaluations to explore the generalization ability of our model. Table 2 shows the results of cross-dataset evaluation. This table reveals the importance of using historical handwriting data for a strong general Arabic HTR model. While training on modern Arabic handwriting using either KHATT or MADCAT gives a high CER of 40% on Muharaf, training on historical Muharaf data gives a lower CER of 26% on modern Arabic handwriting. Hence, this shows that our model can perform well on the historic Muharaf handwriting and generalize to in-the-wild, unseen modern handwritten Arabic.

We also ran cross-dataset evaluations using Saeed et al. ()’s HTR system. As seen in Table 2, our proposed model outperforms their approach in every evaluation except one. Notably, when training on Muharaf (Arabic only) and testing on KHATT, our model outperforms (Saeed et al., ) by 16.7% at a CER of 27.5% compared to 33%, which further shows our model’s capability to generalize better compared to other methods.

### 5.5 Ablation Study

In our ablation study, we quantify the impact of each component of our model by starting with our best model and removing each component one at a time, as shown in Table 4

Table: Table 3: Ablation Study on Muharaf.

.

Baseline Model (A). Our baseline model achieved a CER of 8.6%. This served as the benchmark against which we compared the performance of the ablated models.

Overtraining (B). When we only trained to the minimum validation loss, we observed a slight increase in CER by 1.3%. This result is consistent with Hao et al. (); Mosbach et al. () suggesting that our model was not fully trained.

BlockProcessor (C). Removing the BlockProcessor led to a degradation in performance, increasing CER by 11.4%.
This indicates the importance of minimizing the horizontal information loss of line images for efficient ViT feature extraction.

Modified Text Tokenizer (D). By using the ASCII-biased BBPE tokenizer from Radford et al. (), we observed a decrease in CER of 10.9%. While performance improved when the Arabic text tokenizer was removed, the improvement was smaller than the performance loss due to removing the BlockProcessor, indicating a possible synergy between the two modules.

Synthetic Stage-1 Fine-Tuning (E). Removing the synthetic Stage-1 fine-tuning resulted in a substantial increase in CER by 4.2%. This demonstrates the effectiveness of the Stage-1 fine-tuning step that allows the model to better address the three inherent challenges of Arabic.

Pretrained Weights (F). When the training of HATFormer was started from randomly initialized weights, the model’s performance plummeted to a CER of 86.0%. Despite the major differences between English and Arabic scripts, leveraging TrOCR’s synthetic pretraining checkpoint for English OCR led to better results.

Arabic Specific Postprocessing Normalization. We leveraged Arabic domain knowledge to group our model errors into normalization categories: replace without context, replace with context, and remove diacritics. The replace without context category normalizes characters to a single form that are phonetically similar and generally do not change the meaning of the word. The replace with context category is where more aggressive normalization is applied. Characters that are similar but can change the meaning of the word are converted to a single form. Remove diacritics is relevant to applications such as historical informational archival and search, where normalizing certain characters into a single form is acceptable. Diacritics in some cases can be sparsely used and be removed in an Arabic OCR system. Table 4 shows that the model performance in terms of CER improves by 1.9% points or an additional $\sim$0.6% per postprocessing for each category.

### 5.6 Factor/Sensitivity Study

We analyze the impact of various parameters on model performance.(^1^11One notable parameter that was infeasible to study was decreasing the ViT patch size due to training computational complexity. Reducing patch size in ViT’s results in a longer sequence and the attention mechanism requires quadratic cost $O(n^{2})$ with respect to sequence length $n$.)

Stage-1 Synthetic Dataset Size. Figure 4(a)

Figure: (a)
Refer to caption: x3.png

shows the impact of the synthetic Stage-1 training dataset size on the final performance of HATFormer on the Muharaf dataset. As the size of the synthetic dataset increases, the CER decreases, demonstrating improved generalization. Specifically, datasets of 500k images and 1M images yield the best performance, with the CER dropping below 10%. This trend suggests that a larger synthetic Stage-1 training dataset enhances the model’s ability to effectively handle the inherent challenges of Arabic, ultimately leading to better CER performance in downstream HTR tasks.

Consecutive Whitespaces. The reported evaluation results throughout this paper were derived by removing consecutive whitespaces at test time. This is in line with the default CER score implementation in the HuggingFace evaluate library (v0.4.2). We observed that performing this normalization during the training stage instead of inference time leads to an additional 0.2% CER improvement.

Inference Beam Width. Figure 4(b) shows the effect of beam width on CER and generation speed. The CER improves until the beam width is three and stabilizes beyond this point. Hence, we used a beam width of three in our reported results.
The inference speed was measured in tokens per second over the Muharaf test set (total time / total tokens) on a single A10 (24GB) GPU with a batch size of 1. From the inference speeds we can see that our model can be used in a low-resource environment.

Inference Length Penalty.
The length penalty parameter in beam search biases the generated output sequence length, where negative values encourage shorter sequences and positive values encourage longer ones. In Figure 4(c), we empirically show that on the Muharaf dataset, our model performs optimally using a length penalty between 0.2 and 0.8.

## 6 Conclusion

In this paper, we have presented HATFormer, a dedicated Arabic handwritten text recognition system harnessing the transformer’s attention mechanism to address the unique challenges of the Arabic language. Our system integrates training methods with image and text processing techniques designed for Arabic HTR. Experiments show that HATFormer outperforms baseline methods across multiple real-world datasets, highlighting the effectiveness of our approach.

## Appendix A Attention Maps

We analyze the effectiveness of using self-attention for Arabic HTR, including visualizing the self-attention maps of our vision transformer encoder. We also visualize the cross-attention maps corresponding to a selected ground truth token.

Our visualization scheme for the vision transformer involves selecting a patch of interest in the image and then visualizing how it attends to other patches. In the heatmaps, a brighter color (yellow) indicates that the selected patch pays more attention to this patch. We accumulate the self-attention heatmaps from the previous layers (taking into account residual connections) in the next layer to get a more holistic view of the attention flow.
In the following text, we discuss insights from the attention maps and how our transformer model deals with the intricacies of Arabic handwriting and addresses key Arabic script challenges that we outlined in Section 3.

Cursive. Figure 3 demonstrates that when a patch containing a cursive line is selected, the attention map first highlights the relevant strokes of that character. This indicates that the network learns to distinguish individual characters and strokes before applying broader, global attention, effectively connecting relevant patches. The cross-attention map further shows that the model accurately identifies character boundaries. The model successfully segments the entire word in the image for tokens with complex cursive dependencies.

Context-Sensitive. A character in Arabic can take multiple forms depending on its position within a word and the surrounding characters. The cross-attention maps in Figure 3 demonstrate that even when words are split into multiple tokens, the model can accurately differentiate between word pieces and segment each part. These maps reveal that the model effectively learns the complex morphological rules of Arabic script and can distinguish between different positional forms of the same character.

Diacritics. Figure 3 demonstrates that the diacritic patch can attend to the character patches it is associated with. Importantly, both self-attention and cross-attention maps indicate that diacritic marks are not treated as noise but carry a strong signal. The self-attention maps reveal that the model can associate the relevant character corresponding with the diacritic. The cross-attention maps show the model correctly identifying the position of diacritics within the corresponding word. These maps highlight the network’s ability to incorporate these small marks into the final token predictions rather than ignoring them.

## Peer Reviews

confidence: 5
contribution: 2
initial_rating: 5
presentation: 2
questions: L56 : "First, Arabic is written in cursive, making characters visually harder to distinguish"  English is also cursive
L68 : The number of handwritten pages in the RIMES data base is not 12500, because only a fraction of the database is fully handwritten
The OpenHART database is not mentioned in this section but it is composed of more than 40 000 annotated pages of handwritten Arabic https://catalog.ldc.upenn.edu/LDC2012T15. However, it is used in the experiments.


Related work section

References to full page recognition (for example DAN Coquenet, D., Chatelain, C., Paquet, T.: DAN: a segmentation-free document attention network for handwritten document recognition. IEEE Trans. Pattern Anal. Mach. Intell. 45(7), 8227–8243 (2023). https://doi.org/10.1109/TPAMI.2023.3235826)  are missing


L200 : "the proposed BLOCKPROCESSOR works by first horizontally flipping a text-line image"  : since the usage of the pre-trained English weights is capital, we can see that changing the order of the patches in the Vit would be doomed to failure. However, the approach of returning the image of the line is not very satisfactory because, on the one hand, it does not adapt the model to the specific characteristics of Arabic, even though the authors' objectives are to take these specific characteristics into account, and on the other hand, it poses serious problems in the event of mixing text in Arabic and Latin alphabets.

L201 : "finally warping it to fill in the ViT’s 384×384-pixel image container from left to right and top to bottom" : this means that the system could decode paragraphs
soundness: 2
strengths: - Original use of line warping: The paper presents an innovative approach using line warping techniques to adapt elongated rectangular text lines into a square format suitable for Vision Transformers (ViTs). This adaptation is a solution to the challenges posed by the non-uniformity and the specific shape of handwritten text.

- Extensive pre-training on Arabic data: HATFORMER benefits from extensive pre-training on a synthetic Arabic dataset, which is critical given the limited availability of such data compared to English datasets. Note that using the English pre-training is still beneficial as showed in Table3

- Performance: The model outperforms a state-of-the-art model on one dataset, and is on par on 2 other datasets.
- Open-source : the code and dataset will be released
summary: This paper presents an improvement on the transformer-based encoder-decoder architecture of the TrOCR OCR model. These improvements are designed to address the challenges of Arabic handwritten text recognition, particularly for historical texts. The architecture includes a custom image processor for ViT information pre-processing, a specialized text tokenizer for Arabic text representation, and a training pipeline optimised for limited historical Arabic data. The system is compared with several other HTR systems based on 3 public databases and generally outperforms them.
weaknesses: Ablation study results: The ablation study presents some unusual results that need further investigation. The finding that the removal of block processing leads to a significant performance degradation suggests that the novel image packing technique is beneficial. However, the fact that removing the custom byte pair encoding (BBPE) improves the results - almost to the level of the original model - raises questions. This behaviour is counterintuitive and should be investigated further. The authors should conduct additional experiments to clarify these results and rule out the possibility of bugs or unintended interactions within the model.

Generalisability: Although the aim is to develop a generic HTR model, the system has not been trained on all available datasets to assess its generalisation ability. Testing this generic model on different datasets would provide valuable insights into its robustness and adaptability to different handwriting styles and historical contexts.

---

confidence: 2
contribution: 2
initial_rating: 5
presentation: 3
questions: * When you retrained the Saeed 2024 model for evaluation, did you also perform pretraining with synthetic printed data?
soundness: 3
strengths: * The authors described the BlockProcessor which helps preprocess the handwritten texts images for the ViT, substantially improves the performance. Although the authors applied this processor to Arabic, it could potentially help with other scripts
* The proposed model had significant improvement in the Muharaf dataset compared to existing methods (CER=17.6 -> 8.6)
* The model trained with the Muharaf dataset could also be generalised to other datasets
summary: This paper presents a transformer based method, HATFormer, to analyze historic handwritten Arabic. The authors proposed the BlockProcessor that effectively allows the vision transformer to learn from Muharaf Arabic dataset. The method beats existing CRNN methods in the Muharaf dataset.
weaknesses: * The authors emphasized the importance of pre-training with synthetic data however details regarding of this pre-training procedures was very limited.
* The authors mentioned the model was optimised for the Muharaf dataset, but, only provided one existing method for comparisons using this dataset

---

confidence: 4
contribution: 3
initial_rating: 6
presentation: 3
questions: My comments are explained in the weaknesses section.
soundness: 3
strengths: This paper improves the results of the current state of the art in this field.
summary: This manuscript presents a historical handwritten Arabic line recognition based on transformers. This manuscript is well written and organised. This approach is evaluated on some datasets and promising results are reported.

However, there are some problems with the presentation and experimental results.
weaknesses: - This approach focuses on the extracted line to recognise the documents and this should appear in the title. It is part of a document analysis system and this may lead to errors in the related subsystems.

- It is better to explain the records in detail in the appendix. The reader does not get an idea of which datasets are historical and how the datasets differ from other document analysis datasets. Also, we do not have detailed information about the generated dataset, which should be explained in detail.

- I can see that the authors provide an analysis in the appendix to show the three intrinsic challenges. But that is not enough. They should provide a comprehensive analysis to show how effective the method is in overcoming the challenges.

- I can not see any analysis that shows the limitations of the approach. The flaws of the approach are not shown in the experimintal results.

---

confidence: 4
contribution: 2
initial_rating: 5
presentation: 3
questions: I don't have particular questions to the authors but I believe that one way to strengthen the manuscript could be to open-source the model. UPD: authors provided the link to their model, and I am increasing the rating based on that.
soundness: 2
strengths: The paper describes the approach to obtaining state-of-the art performance in a particular domain of OCR on historic datasets in Arabic script. The writing is clear and the approach seems to be easy to reproduce.
summary: This paper describes a model for handwritten OCR of historic texts in Arabic script. The proposed model uses finetunes the TrOCR approach, first on a large synthetic and then on a small real dataset. To feed the large non-square images to the model, they are written in multiple lines when presented to the vision encoder. The model seems to obtain state-of-the-art results on 2/4 datasets and shows strong performance in cross-dataset evaluation.
weaknesses: Novelty: The main novelty cited in the paper are the BlockProcessor and Arabic BBPE tokenizer. However, block processing of the images for transformer vision encoder has already been studied in several works (ex. Fuyu-8B in https://www.adept.
ai/blog/fuyu-8b or "Representing Online Handwriting for Recognition in Large Vision-Language Models", Fadeeva et al.) and the BPE tokenizer is a standard solution for VLMs. Furthermore, according to Table 3, the use of BlockProcessor actually decreases the model performance. The paper could be strengthened by offering a comparison to those approaches, or to other approaches targeted at solving a similar problem differently (ex. Pack'n'Patch in https://arxiv.org/abs/2307.06304 or Pix2Struct in https://arxiv.org/abs/2210.03347) 

Beyond the aforementioned improvements, the proposed approach performs a (two-stage) fine-tuning of the base model, which does yield good performance, but may not be a significant enough contribution in itself. The paper could be strengthened by releasing the model weights or training code, which would be an additional contribution. Alternatively, perhaps open-sourcing some parts, like the synthetic data generation pipeline or dataset, could also be a good contribution. UPD: authors provided the link to their model, and I am increasing the rating based on that.

Meta-review: The paper introduces an approach using line warping techniques to adapt elongated rectangular text lines into a square format suitable for ViTs, addressing challenges posed by the non-uniformity of handwritten text. It benefits from extensive pre-training on a synthetic Arabic dataset, which is crucial given the limited availability of such data. English pre-training also proves beneficial. The model outperforms a state-of-the-art model on one dataset and performs on par with two other datasets. The code and dataset will be made publicly available.

However, the proposed approach involves a two-stage fine-tuning of the base model, which yields good performance but may not be a significant enough contribution on its own. While the authors provide an analysis in the appendix to show three intrinsic challenges, it is not enough. A comprehensive analysis is needed to demonstrate how effective the method is in overcoming these challenges. There is no analysis showing the limitations of the approach. The flaws are not highlighted in the experimental results.

Decision: Reject