Spatial Dual-Modality Graph Reasoning for Key Information Extraction
- Publication
- Publication venue not yet verified
- First submitted to arXiv
- arXiv:2103.14470
Cite this paper · BibTeX
The citation identifies the linked source version. It cites the original paper and its authors.
@misc{documentunderstandingsdmgr2021,
title = {{Spatial Dual-Modality Graph Reasoning for Key Information Extraction}},
author = {Hongbin Sun and Zhanghui Kuang and Xiaoyu Yue and Chenhao Lin and Wayne Zhang},
year = {2021},
eprint = {2103.14470},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2103.14470}
}
Open PaddleOCR's list of algorithms, scroll to the "key information extraction" section, and you'll find three names: LayoutXLM, VI-LayoutXLM and SDMGR.
The first two are multilingual versions of Microsoft's LayoutLM family and need pretraining on huge numbers of documents first. SDMGR is different. It's a small graph neural network with no pretraining, and a single 12GB graphics card is enough to train it from scratch. Give it a photo of a receipt, plus every text box OCR detected and the text inside each one, and it decides for each box whether it's the store name, the date, the subtotal, the tax, the total, or irrelevant noise.
SDMG-R comes from SenseTime Research. Its code lives in OpenMMLab's MMOCR, and PaddleOCR later made a reproduction too.
Alibaba had already done document information extraction with graphs two years earlier, likewise wiring the text boxes into a graph and letting them exchange messages through graph convolution. This paper uses that approach as its baseline, and reading the two side by side makes it very clear what graph models changed over those two years.
Defining the Problem
The input to key information extraction (KIE) is a document image and a set of text boxes from OCR. Each box carries its top-left coordinates, width, height and recognized string, and the task is to assign every box to one of a set of predefined classes.
The old-school approach is templates. For each kind of receipt you write down "the total is on this line, after this keyword" and search by the rules. The authors have two criticisms of it. If OCR misreads just a few characters, the keywords no longer match; and at a store nobody has seen before, the template stops working altogether.
The other route is to string the OCR output together in order into one long sequence and treat it as named entity recognition (NER), which is what CloudScan does.

The top half is the sequence version. "Subtotal" is followed by "$11.98", and the model can only look at what comes before and after. If the receipt is photographed at an angle, words on the same line are no longer horizontal in the image, and the order of the string gets scrambled; even with a perfectly straight shot, the relationship between different lines is reduced to which comes first.
The bottom half is what the authors want, with every box able to look directly at its neighbors above, below, left and right. That "$1.05" sits right below "Subtotal" is a clue in itself.
The public data has problems too. In the public KIE datasets of the time, such as ICDAR 2019's SROIE, the training and test sets share many templates. A model that scores high on such data may simply have memorized the layouts of a few stores. What real applications run into is often a store nobody has seen before.
How do you use the text, the image and the spatial relations between boxes all at once? And how do you show that the model holds up on templates it has never seen?
Solving the Problem
The Whole Model

The model has three stages, from left to right: dual-modality fusion, graph reasoning and classification. Let's pick one box on the receipt, say the total "$30,473.00", and follow it all the way through.
Reading the Text: A Character-Level Bi-LSTM
First, the text in the box. The authors prepare a dictionary of 91 characters, covering 0 to 9, upper- and lowercase English letters, and symbols closely tied to receipt fields, such as "$", "€", "¥", ":", "%" and "#"; every other character is mapped to "unknown".
Each of the 10 characters in "$30,473.00" becomes a 91-dimensional one-hot vector, is multiplied by a projection matrix that squeezes it down to 32 dimensions, and goes into the Bi-LSTM in order:
is the one-hot vector of the -th character in the -th box. After reading the whole string, you get a 256-dimensional text feature .
The upside of working at the character level is that unseen words don't scare it. Store names and item names on receipts come in every imaginable form, and no vocabulary could ever hold them all; subtotals also get printed as all kinds of abbreviations, and the receipt in the architecture figure above says "SUBTIL". To a word vocabulary, that's an unseen word; a character model at least has a chance of noticing it looks a lot like SUBTOTAL.
Looking at the Image: U-Net Plus RoI Pooling
Next, the image. The whole receipt is resized to and fed into a U-Net, whose last feature map is taken with its channels reduced to 256. Then, using the position of the "$30,473.00" box, RoI Pooling on the feature map gives a 256-dimensional visual feature .
The weight and size of the type, whether there's a border, how dark the background is, all stay in this vector. Some receipts print the total bigger and bolder, which you can't read from the string alone.
Fusion: Kronecker Product and Block Decomposition
The two 256-dimensional vectors have to become one node feature. The most direct options are to concatenate them, or to project both and add. The authors wanted finer interaction, with every dimension of the text able to pair with every dimension of the image, so they use the Kronecker product:
multiplies every pair of elements from the two vectors, 256 by 256, giving a 65,536-dimensional vector. then projects it back to 256 dimensions.
is a matrix, so this one layer alone has 16.77 million parameters.
The authors borrow BLOCK fusion from VQA and apply a block-term decomposition to the corresponding third-order tensor :
In plain terms, text and image are each first projected to dimensions and cut into 20 chunks of 52 dimensions; the -th text chunk interacts bilinearly only with the -th image chunk, handled by a small core tensor ; and the 20 results are joined and projected back to 256 dimensions.
Let's count the parameters. The 20 core tensors take , about 2.81 million; the three projections are each , about 0.8 million together. The total is roughly 3.61 million, a bit more than a fifth of the full version (worked out for this note from the dimensions the paper gives).
After fusion, "$30,473.00" becomes a 256-dimensional node that holds both what it says and what it looks like.
Building Edges: The Same Five Numbers as Two Years Ago
Next comes the graph. Each text box is a node, and there's an edge between every two boxes, which makes it a fully connected graph; MMOCR's code likewise computes relations for every pair of boxes in each graph.
The raw edge feature is five numbers:
and are the horizontal and vertical distances between the top-left corners of the two boxes, divided by a constant ; the last three terms are the aspect ratio of the source box and the height and width of the target box, all divided by the source box's height.
If you've read the Alibaba paper, you'll notice these five numbers are almost exactly the same. Both papers use the distances between boxes, the source box's aspect ratio and the relative sizes of the two boxes. The difference is that this paper divides by an extra constant, so the distances don't grow large enough to swamp the other three terms.
Reasoning: Edge Weights That Change with the Nodes
The five numbers first go through a linear layer that lifts them to 256 dimensions, followed by L2 normalization, which the paper says is for training stability. Then the two end nodes and the edge are concatenated and handed to a two-layer MLP , which outputs a scalar score:
is concatenation, and has dimensions. A softmax over all neighbors turns the scores into edge weights, which are then used to sum up the neighbors' messages. For readability, the weighted sum is written here as ; the paper puts it all in one equation:
The superscript is the layer index, and projects the 768-dimensional message back to 256 dimensions.
The score is computed from the features of the two end nodes. By the second layer, the nodes have already absorbed one round of neighbors, so the weight of the same edge changes along with them; the authors call this dynamic edge weighting. The idea comes from GAT, with the spatial relations between boxes added to what the score is computed from.
The update is residual, with the new node equal to the old node plus the neighbors' message. The Alibaba paper has no such residual; its new node is computed entirely from that round's neighbor messages.
This reasoning runs for layers.
Classification: Labeling the Keywords Too
The node features of the last layer go straight into a classifier, with one cross-entropy per box:
Compared with the Alibaba paper, a whole BiLSTM-CRF stage is gone. That paper appended the graph embedding to every token and then did token-by-token IOB tagging; this one treats a box as a single unit and classifies it directly. The model gets much simpler, and the price is that each box has to correspond exactly to one field, which will come up again later.
On a receipt, the word "Total:" itself isn't the information to extract; what you want is the amount next to it. The authors still label such keywords as separate key classes, such as Total key and Tax key, so the model learns to recognize both the keywords and the values. The experiments will show this trick is worth 4.4 points.
Training uses PyTorch and a single 12GB Titan X, from scratch, with Adam, batch size 4, 60 epochs and a learning rate of , divided by ten at epochs 40 and 50. During training, images are randomly cropped with probability one half, without cutting through any text box.
WildReceipt: An Exam on Stores Never Seen
Besides the method, the other half of this paper is a dataset.
The authors pulled about 4,300 images from a search engine with keywords such as receipt and invoice, then removed images with several receipts in one shot, images that weren't receipts, and images that were unreadable, incomplete or not in English, keeping 1,740. They first annotated each text box and its text, then labeled every box with one of 25 classes.

The Subway receipt on the left shows how fine the annotation is. Each box gets one class number; item names are 11, item prices 15, the subtotal word 18 and the subtotal amount 17. The two on the right are what the "Wild" in the dataset's name means, crumpled, tilted, partly covered by fingers, a far cry from the flat scans in SROIE.
The 25 classes are 12 key-value pairs plus an others class, covering store name, address, phone, date, time, item, quantity, unit price, subtotal, tax, tip and total. There are 68,975 text boxes in all, about 39 per image on average, and 26,623 of them are others. The classes are badly unbalanced, with 8,634 item-name boxes but only 165 tip values.
The training set has 1,268 images and 847 templates; the test set has 472 images and 345 templates. The authors split by store name and near-duplicate image retrieval to make sure no test template ever appears in the training set, and this is the biggest difference between WildReceipt and SROIE.
Scoring uses F1, averaged over the 12 value classes only. The key classes and others are trained on but not scored, because the values are all that actually gets handed over.
Discussion
The Competition
There are three baselines:
- Chargrid: lays characters out on a 2D grid and segments it with a CNN.
- Chargrid-UNet: the same, with the backbone swapped for the U-Net SDMG-R uses, to rule out backbone differences.
- VRD: the authors' reproduction of Graph Convolution for VRD by Liu et al. at Alibaba, which builds a graph from text boxes and puts a CRF after the graph convolution, with no visual features.
Three Conditions, Three Scores
The authors tested three input conditions on WildReceipt, from ideal to realistic. Mean F1 (%) compiled from Tables V, VI and VII of the paper:
| Method | GT box + GT text | GT box + OCR text | OCR box + OCR text |
|---|---|---|---|
| Chargrid | 76.9 | 71.4 | 60.0 |
| Chargrid-UNet | 79.0 | 71.6 | 58.7 |
| VRD | 85.7 | 79.8 | 77.2 |
| SDMG-R | 88.7 | 82.4 | 79.3 |
The first column uses the annotated boxes and text, so the model only has to classify. SDMG-R is 3.0 points above VRD and 11.8 above Chargrid.
The second column swaps the text for Google OCR's output while keeping the annotated boxes. Every model drops 5.5 to 7.4 points, SDMG-R from 88.7 to 82.4. The authors specifically note that symbols crucial to the fields, such as "$", "€" and "¥", are exactly where OCR slips most often.
The third column swaps the boxes for OCR detections as well, with each detected box inheriting the class of the annotated box it overlaps most by IoU. This is the setting closest to real use; Chargrid falls to 60, and SDMG-R is left with 79.3.
Both graph models beat Chargrid in all three columns, and in the third, SDMG-R leads Chargrid by 19.3 points. Convolution on a grid only sees what's nearby, while on a graph every box can connect directly to any other box on the whole receipt.
The Money Fields
Break the first column down, and the gap concentrates in the fields that deal with money:
| Method | Subtotal | Tax | Tip | Total |
|---|---|---|---|---|
| Chargrid | 68.0 | 68.1 | 20.5 | 70.1 |
| VRD | 82.4 | 84.2 | 56.0 | 79.5 |
| SDMG-R | 87.9 | 89.5 | 67.9 | 82.4 |
Compiled from Table V of the paper; ground-truth boxes and text, F1 (%).
The values of these four fields are all amounts, impossible to tell apart from the string alone; the only way is to see which keyword each one sits next to. SDMG-R beats VRD by nearly 12 points on tips; tip values number only 165 in the whole dataset, the least data and the hardest field.
Among the other fields, SDMG-R and VRD are only 0.2 points apart on time and quantity, and Chargrid-UNet scores highest on store name. All of this is the first column.
Move to the realistic condition of the third column, and SDMG-R's average lead over VRD shrinks to 2.1 points; it loses to VRD on five classes, phone, date, time, tax and total, with tax at 79.8 against 82.3 and total at 73.4 against 75.7. The paper's word for this is "impressive margins" (and the total, the one field everyone wants most, is on the losing side).
SROIE's 87.1
The authors also compared on SROIE, where SDMG-R gets 87.1, VRD 84.9 and Chargrid 80.9.
That 87.1 looks far below LayoutLM's 95 on SROIE. As early as November 2021, someone asked in MMOCR's issue #560 why it was 9 points below PICK's 96.12. The first author replied that "the reason may be the different f1 metric calculations."
It is indeed different. SROIE's official test annotations were never made public, so the authors annotated the test set themselves, and 87.1 is the F1 for classifying each box on that self-annotated test set; LayoutLM-style methods compare the extracted field strings. The two sets of numbers don't belong in the same table.
Ablation: What Hurts Most to Remove
Table IX of the paper removes one component at a time on WildReceipt (mean F1; the full model gets 88.7):
- Without graph reasoning (classifying the fused node features directly): 77.2, down 11.5. Message passing between boxes is the biggest contributor.
- Without text features: 80.1, down 8.6.
- Without spatial relations (edge features all set to 0): 81.8, down 6.9.
- Without key classes (labeling values only): 84.3, down 4.4.
- Without visual features: 86.4, down 2.3.
Visual features come last. SDMG-R without the image gets 86.4 and VRD gets 85.7, only 0.7 apart; both see only the text and the box positions, and their structures are similar. Put those two numbers side by side and most of the 3 points SDMG-R gains over VRD come from the image. MMOCR's later official models bear out this order of magnitude, with a Macro F1 of 0.890 for the version with images and 0.873 for the text-only version, a gap of 1.7 points.
The key-class trick is a bargain. Label a few extra words like "Total:" and "Tax" when annotating, words that don't count at all in scoring, and the value scores go up 4.4 points.
The number of reasoning layers was tested too, with one layer giving 84.9, two 88.7 and three 87.6. As in the Alibaba paper, two layers work best, and a third starts to overfit.
Fusion Methods
Table X of the paper compares three fusion methods, each swept over a set of hidden sizes:
- ConcatMLP (concatenate, then an MLP): 0.854 to 0.867.
- LinearSum (project each, then add): 0.845 to 0.861.
- The paper's block decomposition: block sizes 26, 52 and 78 give 0.859, 0.887 and 0.857.
Only the block size 52 setting beats ConcatMLP's best of 0.867; 26 and 78 both lose to it. The paper's verdict on this table is "very effective, and obviously outperforms" (one win out of three settings). Move the block size from 52 to 78 and the score drops 3 points, so on your own data you'll most likely have to sweep this hyperparameter again.
What the Graph Learned

Take the first example, where the query is the total "$30,473.00".
In the first layer, the edge weights land on the two "$28,250.00" above it, that is, the other amounts. This layer is probably still working out "I'm an amount; what other amounts are around me?" In the second layer, the weight shifts to "TOTAL" on the left of the same line (0.463) and "SALES TAX" on the line above (0.377). The answer is written in those two words; the one on its left tells it that it's the total.
The second and third examples query the keyword "SUBTOTAL" itself, and in the second layer it looks at "SALES TAX" and "TOTAL" below it. A "subtotal, tax, total" stack running top to bottom is itself the typical receipt layout, and the second layer has learned that arrangement.
This is very close to what the Alibaba paper observed; it, too, found that when layouts vary, attention concentrates on the neighbors to the left and above. The two papers describe position with the same five numbers, and what they learn isn't far apart either.
After the Code
SDMG-R's code lives in MMOCR, and the first author answers issues personally.
Edges aren't updated layer by layer. In June 2022, someone pointed out in issue #1122 that in MMOCR's implementation, the new edge features computed at each layer were never passed to the next. The first author confirmed it was a bug two days later, and the fix was merged in July. The model weights MMOCR currently provides are dated August 2022, after the fix.
Classification only, no pairing. As early as November 2021, someone asked in issue #553 about invoices with a pile of items and prices. SDMG-R labels which boxes are items and which are prices, but which price belongs to which item? The maintainer answered bluntly, "SDMGR only tells which text boxes should be the key or value, but can't find the relations between boxes." MMOCR later added an open-set version with an extra classifier that decides whether two boxes are linked. But in August 2022, someone in issue #1321 found that WildReceipt's open-set annotations link all the items and all the prices into one big cluster instead of pairing them row by row, and the person who replied thought the only way to get item lists out was to re-annotate the data yourself. Over at PaddleOCR, someone asked the same question in October 2023 (#11071) and never got an official answer.
The boxes have to be cut right. As mentioned earlier, SDMG-R gives each box only one class. In January 2022, someone reported in PaddleOCR's issue #5199 that training on ID-card data worked well, but whenever the upstream OCR put "姓名" ("name") and "张三" (a stand-in name, like John Doe) into the same box, SDMGR could only classify the whole box as others. The only remedies anyone in the discussion could think of were patching things afterward with regular expressions or training a better detection model.
PaddleOCR's version trains but doesn't deploy. In PaddleOCR's documentation, training, evaluation and prediction all have commands, and the WildReceipt hmean is 86.70%; but in the inference and deployment section, Python inference, C++ inference, serving deployment and "more deployment options" all read "暂不支持" ("not supported yet"). In 2023 someone asked when Python inference would arrive (#9390), and the issue was closed automatically without an official reply; in July 2024 someone reported that exporting an inference model failed, even with the officially provided trained model (#13556), and the maintainer's reply was that some models don't yet support exporting to a static graph, so you'd have to change the model structure or wait for the framework to support it. That one was eventually closed by a bot as well (on the list, not in production).
The dataset's license. In February 2022, someone asked in MMOCR whether WildReceipt could be used commercially (issue #777). A maintainer tagged the first author, nothing more came of it, and the issue is still open. In MMOCR's dataset documentation, the license field says N/A. Given that the images were downloaded from a search engine, this was never going to be an easy question.
Conclusion
To sum up:
- The nodes gain an image. Text goes through a character-level Bi-LSTM and the image through a U-Net plus RoI Pooling, and the two are fused with a Kronecker product approximated by block decomposition. The ablation puts the image's contribution at about 2.3 points, less than the text or the spatial relations, but it is exactly what separates this paper from the Alibaba one.
- The edges are still those five numbers. The distances and relative sizes between boxes are embedded and used to score neighbors, and the edge weights change layer by layer with the nodes; two layers of reasoning work best.
- The exam moves to stores never seen. WildReceipt's test templates never appear in training, and scoring counts only F1 over the 12 values. In the most realistic setting, OCR boxes plus OCR text, SDMG-R gets 79.3, 2.1 points above VRD.
Side by side with the paper from two years earlier, what changed is actually quite clear. The nodes went from text alone to text plus image, the output from token-by-token tagging to whole-box classification, and the exam from the same batch of templates to a different store. The skeleton of the graph itself barely changed.
It also leaves the ceiling of graph models in plain sight. Every box can be labeled "item" or "price," but how to match the dozen or so items on a receipt back to their own prices, row by row, is something WildReceipt's annotations never taught, and this graph still hasn't learned it.