I'm currently adding LayoutLMv2 and LayoutXLM to HuggingFace Transformers. These models, built by Microsoft, have impressive capabilities for understanding document images (scanned documents, such as PDFs). LayoutLM, and its successor, LayoutLMv2, are extensions of BERT that incorporate layout and visual information, besides just text. LayoutXLM is a multilingual version of LayoutLMv2.
It would be really cool to have inference widgets for the following tasks:
- document image understanding
- document image visual question answering
- document image classification
Document image understanding
Document image understanding (also called form understanding) means understanding all pieces of information of a document image. Example datasets here are FUNSD, CORD, SROIE and Kleister-NDA.
The input is a document image:
![Schermafbeelding 2021-08-19 om 10 12 18](https://user-images.githubusercontent.com/48327001/130034903-13f88eaf-7a6e-4dca-9973-e1e83a41deba.png)
The output should be the same image, but with colored bounding boxes, indicating for example what part of the image are questions (blue), which are answers (green), which are headers (orange), etc.
![Schermafbeelding 2021-08-19 om 10 12 45](https://user-images.githubusercontent.com/48327001/130035118-39cce15b-a9a7-4e5a-9c5c-ce759b89d272.png)
LayoutLMv2 solves this as a NER problem, using LayoutLMv2ForTokenClassification
. First, an OCR engine is run on the image to get a list of words + corresponding coordinates. These are then tokenized, and together with the image sent through the LayoutLMv2 model. The model then labels each token using its classification head.
Document visual question answering
Document visual question answering means, given an image + question, generate (or extract) an answer. For example, for the PDF document above, a question could be "what's the date at which this document was sent?", and the answer is "January 11, 1999".
Example datasets here are DocVQA - on which LayoutLMv2 obtains SOTA performance, who might have guessed.
LayoutLMv2 solves this as a extractive question answering problem similar to SQuAD. I've defined a LayoutLMv2ForQuestionAnswering
, which predicts the start_positions
and end_positions
.
Document image classification
Document image classification is fairly simple: given a document image, classify it (e.g. invoice/form/letter/email/etc.). Example datasets here are [RVL-CDIP](https://www.cs.cmu.edu/~aharley/rvl-cdip/#:~:text=The%20RVL%2DCDIP%20(Ryerson%20Vision,images%2C%20and%2040%2C000%20test%20images.). For this, I have defined a LayoutLMv2ForSequenceClassification
, which just places a linear layer on top of the model in order to classify documents.
Remarks
I don't think we can leverage the existing 'token-classification', 'question-answering' and 'image-classification' pipelines, as the inputs are quite different (document images instead of text). To ease the development of new pipelines, I have implemented a new LayoutLMv2Processor
, which takes care of all the preprocessing required for LayoutLMv2. It combines a LayoutLMv2FeatureExtractor
(for the image modality) and LayoutLMv2Tokenizer
(for the text modality). I would also argue that if we have other models in the future, they all implement a processor that takes care of all the preprocessing (and possibly postprocessing). Processors are ideal for multi-modal models (they have been defined previously for CLIP and Wav2Vec2).