Document Question Answering

Document Question Answering is a task that involves providing answers to questions posed about document images.

Verified

On this page

Definition

Document Question Answering, also referred to as Document Visual Question Answering, is a task that involves providing answers to questions posed about document images. For this task, the output is an answer expressed in natural language. The modalities used by models supporting this task include text, the positions of words (bounding boxes), and the image itself. The Document Question Answering task is a multimodal task.

At a glance

A pipeline for document question answering receives a model and the image + question combination. The results of that pipeline can also be replicated manually.

Diagram: Take an image and a question, and prepare them for the model using the processor from the model being used. > Forward the result of preprocessing through the model. > In the guide's example of manually replicating the pipeline results, the model returns start logits and end logits, where the start logits indicate which token is at the start of the answer. > In that manual replication example, the end logits indicate which token is at the end of the answer, and both the start logits and end logits have shape (batch_size, sequence_length). > In that manual replication example, take an argmax on the last dimension of both the start logits and end logits to get the predicted start index and end index. > Decode the answer with the tokenizer.1Take an image and a question, and prepare themfor the model using the processor from the model being used.2Forward the result of preprocessing through the model.3In the guide's example of manually replicatingthe pipeline results, the model returns startlogits and end logits, where the start logitsindicate which token is at the start of theanswer.4In that manual replication example, the endlogits indicate which token is at the end of theanswer, and both the start logits and end logits have shape (batch_size, sequence_length).5In that manual replication example, take anargmax on the last dimension of both the startlogits and end logits to get the predicted start index and end index.6Decode the answer with the tokenizer.

How it works

In LayoutLMv2, the positions of the start and end tokens of the answer are what is predicted. In the LayoutLMv2 setup used in the guide, the context comes from the output of an OCR engine, here Google's Tesseract. LayoutLMv2 depends on detectron2, torchvision and tesseract.

LayoutLMv2Processor internally combines an image processor that can handle image data and a tokenizer that can encode text data. By default, the image processor resizes images to 224x224 and makes sure they have the correct order of color channels. The image processor applies OCR with tesseract to get words and normalized bounding boxes.

Text preprocessing involves converting the words and boxes obtained to token-level input_ids, attention_mask, token_type_ids and bbox. In Transformers, for xxxForQuestionAnswering models, the labels consist of start positions and end positions. In the guide's answer-finding helper function, if more than one match was found, only the first one is returned.

The LayoutLMv2 checkpoint used in the guide has been trained with max_position_embeddings = 512. Evaluation for document question answering requires a significant amount of postprocessing. Extractive question answering is typically evaluated using F1/exact match.

Example

The guide illustrates how to fine-tune LayoutLMv2 on the DocVQA dataset. The guide also illustrates how to use a fine-tuned model for inference. The guide uses a small sample of preprocessed DocVQA that can be found on the Hub.

That dataset is already split into train and test sets. The image field is a PIL.Image.Image object containing the document image. The query field is the question string, a natural language asked question, in several languages.

The answers field is a list of correct answers provided by human annotators. The words and bounding boxes fields are the results of OCR, and the guide does not use them here. The guide leaves only English questions and drops the answer feature, which appears to contain predictions by another model.

For training, the model is loaded with AutoModelForDocumentQuestionAnswering using the same checkpoint as in the preprocessing. The training hyperparameters are defined in TrainingArguments. Call train() to finetune the model.

In the inference example, the question asked is Who is ‘presiding’ TRRF GENERAL SESSION (PART 1)? The answers given by the annotators for that example are TRRF Vice President and lee a. waller. In this example, the answer output by the pipeline is Lee A. Waller.

Sources

This page is written from the official sources below. Open them to check the original text.

Verified ·

This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.