Package: docile-benchmark
DocILE: Document Information Localization and Extraction BenchmarkRepository to work with theDocILE dataset and benchmark, used in the DocILE'23 CLEF Lab and ICDAR Competition.
The competition deadline is on May 10, 2023 and comes with a$9000 prize pool.The repository consists of:A python library,docile, making it easy to load the dataset, work with its annotations, pdfs and pre-computed OCR and run the evaluation.An interactivedataset browser notebookto visualize the document annotations, predictions and evaluation results.Training and inference scripts forBaseline methodsprovided together with pretrained checkpoints and results on test and validation sets.Tutorialsto get quickly started with the repo.Table of Contents:Download the datasetInstallationPredictions format and running evaluationTasks and evaluation metricsPre-computed OCRDevelopment instructionsDataset and benchmark paperDownload the datasetFirst you need to obtain a secret token by following the instructions athttps://docile.rossum.ai/. Then download and unzip the dataset by running:./download_dataset.shTOKENannotated-trainvaldata/docile--unzip
./download_dataset.shTOKENtestdata/docile--unzip
./download_dataset.shTOKENsyntheticdata/docile--unzip
./download_dataset.shTOKENunlabeleddata/docile--unzipRun./download_dataset.sh --helpfor more options, including how to only show urls (to download
with a different tool than curl), how to download smaller unlabeled/synthetic chunks or unlabeled
dataset without pdfs (with pre-computed OCR only).You can also work with the zipped datasets when you turn off image caching (checkLoad and sample datasettutorial for details).InstallationOption 1: Install as a libraryInstall the library with:pipinstalldocile-benchmarkTo convert pdfs into images, the library useshttps://github.com/Belval/pdf2image. On linux you might need to install:aptinstallpoppler-utilsAnd on macOS:brewinstallpopplerNow you have all dependencies to work with the dataset annotations, pdfs, pre-comptued OCR and to run the evaluation. You can install extra dependencies by running the following (although using one of the provided dockerfiles, as explained below, might be easier in this case):pipinstall"docile-benchmark[interactive]"pipinstall"docile-benchmark[ocr]"The first line installs additional dependencies allowing you to use the interactive dataset browser indocile/tools/dataset_browser.pyand thetutorials. The second line let's you rerun the OCR predictions from scratch (e.g., if you'd like to run it with different parameters) but to make it work, you might need additional dependencies on your system. Checkhttps://github.com/mindee/doctrfor the installation instructions (for pytorch).Option 2: Use dockerThere are two Dockerfiles available with the dependencies preinstalled:Dockerfileis a lighter, CPU-only, version with all necessary dependencies to use the dataset with the pre-computed OCR and interactive browser.Dockerfile.gpuhas CUDA GPU support and contains additional dependencies needed to recompute OCR predictions from scratch (not needed for standard usage).You can use docker compose to manage the docker images. First update the settings indocker-compose.ymland the port for jupyter lab in.env. Then build the image with:dockercomposebuildjupyter[-gpu]wherejupyterusesDockerfileandjupyter-gpuusesDockerfile.gpu. You can then start the jupyter server:dockercomposeup-djupyter[-gpu]Jupyter lab can be then accessed athttps://127.0.0.1:${JUPYTER_PORT}(retrieve the token from logs withdocker compose logs jupyter[-gpu]). You can also login to the container with:dockercomposeexecjupyterbashAfter that runpoetry shellto activate the virtual environment with thedocilelibrary and its dependencies installed.Predictions format and running evaluationTo evaluate predictions for tasks KILE or LIR on the test set, you have to make a submission to the benchmark on theRobust Reading Competitionwebsite. To evaluate on the validation set, use the following command:docile_evaluate\--taskLIR\--dataset-pathpath/to/dataset[.zip]\--splitval\--predictionspath/to/predictions.json\--evaluate-x-shot-subsets"0,1-3,4+"\# default, show evaluation for 0-shot, few-shot and many-shot layout clusters--evaluate-synthetic-subsets\# optional, show evaluation on layout clusters with available synthetic data--evaluate-fieldtypes\# optional, show breakdown per fieldtype--evaluate-also-text\# optional, evaluate if the text prediction is correct--store-evaluation-resultLIR_val_eval.json# optional, it can be loaded in the dataset browserRundocile_evaluate --helpfor more information on the options. You can also rundocile_print_evaluation_report --evaluation-result-path LIR_val_eval.jsonto print the results of a previously computed evaluation. It is also possible to run evaluation or load the evaluation result directly from code which provides even more options, such as computing metrics just for a single document, specific subset of documents (i.e., documents belonging to the same layout cluster) etc. Checkdocile/evaluation/evaluate.pymodule for more details.Predictions need to be stored in a single json file (for each task separately) containing a mapping fromdocidto the predictions for that document, i.e.:{"docid1":[{"page":0,"bbox":[0.2,0.1,0.4,0.5],"fieldtype":"line_item_order_id","line_item_id":3,"score":0.8,"text":"Order 38","use_only_for_ap":true},"..."],"docid2":[{"...":"..."},"..."],"..."}Explanation of the individual fields of the predictions:page: page index (from zero) the prediction belongs tobbox: relative coordinates (from 0 to 1) representing theleft,top,right,bottomsides of the bbox, respectivelyfieldtype: the fieldtype (sometimes called category or key) of the predictionline_item_id: ID of the line item. This should be a different number for each line item, the order does not matter. Omit for KILE predictions.score[optional]: the confidence for this prediction, can be omitted (in that case predictions are taken in the order in which they are stored in the list)text[optional]: text of the prediction, evaluated in a secondary metric only (when--evaluate-also-textis used)use_only_for_ap[optional, default is False]: only use the prediction for AP metric computation, not for f1, precision and recall (useful for less confident predictions).You can useField classto work with the predictions in code, in which case you can store them in a json file in the required format by using thedocile.dataset.store_predictionsfunction. You can also use theBBox classto easily convert between relative and absolute coordinates (relative coordinates are used everywhere by default).Usedocile/tools/print_results.pyto display results of your predictions formatted in a table (choosing from table styles like github, latex or csv).Tasks and evaluation metricsThe DocILE benchmark comes with two tracks, Key Information Localization and Extraction (KILE) and Line Item Recognition (LIR), as described in thedataset and benchmark paper. In both tracks, the goal is to localize key information of pre-defined categories, i.e., for each document, detect fields with theirfieldtype, location (pageandbbox) and optionallytext. For LIR, fields have to be additionally grouped into line items. A Line Item (LI) is a tuple of fields (e.g., description, quantity, and price) describing a single object instance to be extracted, e.g., a row in a table.Evaluated metrics are Average Precision (AP), F1, precision and recall, computed over all fields across all documents and field types (micro-average). A predicted and a ground truth field can be matched if they have the samefieldtypeand if they are in the same location, which is decided by the overlap of their bounding boxes with theprovided OCR words, described more precisely in thedataset and benchmark paper:For LIR, the fields have to additionally belong to corresponding Line Items. The mapping betweenline_item_idfor predicted and ground truth fields is decided by taking the maximum matching which maximizes the total number of matched fields, when considering only fields withuse_only_for_ap=False(becauseuse_only_for_ap=Truecan be used to include also less confident predictions for AP computation which could negatively affect this matching).For both KILE and LIR, the metrics are then computed like this:For each document page, find matching between predicted and ground truth fields. If more predicted fields match the same ground truth field, the one with higherscoreis used (but primarily the one withuse_only_for_ap=False).Order all predictions by descendingscore(but again, predictions withuse_only_for_ap=Truecome last). Break ties by the original order in which predictions were provided. Seeevaluate.pyfor the precise ordering rules. Notice that the order does not influence F1, precision and recall but it influences AP.Compute F1, precision and recall by counting number of true positives and false positives/negatives and AP by iteratively adding predictions and updating precision and recall. We use the COCO style of AP where "gaps are filled", i.e., the precision-recall curve becomes non-increasing.Pre-computed OCRPre-computed OCR is provided with the dataset. The prediction was done using theDocTRlibrary. On top of that, word boxes were snapped to text (check the code indocile/dataset/document_ocr.py). These snapped word boxes are used in evaluation.To get the OCR words for a single page, calldocument.ocr.get_all_words(page), optionally passingsnapped=Trueto get the snapped bounding boxes. In some rare cases, the list of words might be empty (when the page is blank). Also notice the predicted words are grouped into blocks. For some models it might be beneficial to re-order the words primarily by lines.While it should not be needed, it is possible to (re)generate OCR from scratch (including the snapping) with the providedDockerfile.gpu. Just deleteDATASET_PATH/ocrdirectory and then access the ocr for each document and page withdocument.ocr.get_all_words(page, snapped=True).Development instructionsFor development, installpoetryand runpoetry install. Start a shell with the virtual environment activated withpoetry shell. No other dependencies are needed to run pre-commit and the tests. It's recommended to use docker (as explained above) if you need the extra (interactive or ocr) dependencies.Install pre-commit withpre-commit install(don't forget you need to prepend all commands withpoetry run ...if you did not runpoetry shellfirst).Run tests by callingpytest tests.Dataset and benchmark paperThe dataset, the benchmark tasks and the evaluation criteria are described in detail in thedataset paper. To cite the dataset, please use the following BibTeX entry:@misc{simsa2023docile,
    title={{DocILE} Benchmark for Document Information Localization and Extraction},
    author={{\v{S}}imsa, {\v{S}}t{\v{e}}p{\'a}n and {\v{S}}ulc, Milan and U{\v{r}}i{\v{c}}{\'a}{\v{r}}, Michal and Patel, Yash and Hamdi, Ahmed and Koci{\'a}n, Mat{\v{e}}j and Skalick{\`y}, Maty{\'a}{\v{s}} and Matas, Ji{\v{r}}{\'\i} and Doucet, Antoine and Coustaty, Micka{\"e}l and Karatzas, Dimosthenis},
    url = {https://arxiv.org/abs/2302.05658},
    journal={arXiv preprint arXiv:2302.05658},
    year={2023}
}