fc-amf-ocr / README.md
staghado's picture
Upload README.md with huggingface_hub
8a4a94f verified
|
raw
history blame
12.3 kB

<--- task_categories:

  • image-to-text size_categories:
  • 1M<n<10M language:
  • en
  • fr

splits:

  • name: train num_examples: 9357567

Dataset Card for Finance Commons AMF OCR dataset (FC-AMF-OCR)

Dataset Description

Dataset Summary

FC-AMF-OCR dataset is a document dataset OCRed from the AMF-PDF dataset part of the Finance Commons collection. The dataset comes with native text annotation available here AMF-Text but these annotations are imperfect and contain many errors(missing spaces, OCR artifacts, etc.). The goal of this dataset is to provide a more accurate and complete OCR annotation for the Finance Commons AMF dataset with full bounding box information and confidence scores for each word, line and block.

Most existing large scale OCR datasets like the Industry Documents Library (IDL) or the PDF Association dataset (PDFA) suffer from a number of issues:

  • Time Coverage: These datasets consist primarily of older documents or PDFs from specific periods, which might not reflect current trends or developments.
  • OCR Engines: They use outdated or inconsistent OCR technologies, affecting the accuracy and reliability of text extraction.
  • Further, some of these annotations are limited to what can be extracted and is readily available - text drawn in images and only present as a bitmap rendition might be missed entirely.

FC-AMF-OCR aims to address these issues by providing comprehensive OCR annotations of a recent collection of text-heavy documents and use the excellent docTR open source OCR engine. Using an open source OCR engine makes it less subject to API changes and users know what to expect and apply specific filtering logic if need be.

An example from the FC-AMF-OCR dataset

An example page of one pdf document with existing text annotation(red) and the OCR annotation(green).

Following most large scale OCR datasets like IDL, this dataset is in webdataset .tar format and can be used with derived forms of the webdataset library.

Usage with datasets

This dataset can be used with webdataset library or Hugging Face datasets. Here is an example of how to stream the dataset directly from Hugging Face so you don't have to download the dataset locally.

Note: We do recommend downloading the dataset to save bandwidth.

from datasets import load_dataset

dataset = load_dataset('lightonai/fc-amf-ocr', streaming=True)
print(next(iter(dataset['train'])).keys())
>> dict_keys(['__key__', '__url__', 'pdf', 'json.gz'])

You can download the dataset using the following command:

 import os
 from huggingface_hub import HfApi
 
  
 os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "1"
 api = HfApi()
 api.snapshot_download("lightonai/fc-amf-ocr", repo_type="dataset", local_dir_use_symlinks=False)
 

Approach

Filtering process

We start from the original dataset, which is a collection of 633,244 PDF files and apply some simple filters to remove files that are not relevant for training. The main goal is to have a dataset that is ready to use for large-scale training. We use the following filters:

  • Corrupted files: we remove files that fail to be decoded correctly or that take too long to load.
  • Page count: we remove files that have more than 500 pages.
  • Keep original quality: we apply no compression or rendering that would degrade the quality of the original PDF.

Here is a histogram of the number of pages in the dataset.

.

The distribution of number pf pages in the FC-AMF-OCR dataset.

At the end, each document exists as a pair of a pdf and a json.gz file containing extensive OCR annotation. The packaging in webdataset format makes it easy to use in image-to-text tasks at scale.

How to visualize a page from the dataset?

PDF files are sourced from a variety of origins and are typically stored in RGB format. These files can consist of multiple pages, each of which can be rendered using different tools or engines according to your needs. One recommended option is pdf2image, a tool that converts PDF pages into images. To use pdf2image, you need to install the poppler-utils package, which provides the necessary support for rendering and processing PDF files efficiently. This approach allows for flexible handling of PDFs, making it easier to extract and manipulate content from multi-page documents.

apt-get install poppler-utils 
from pdf2image import convert_from_bytes

pdf_first_page = convert_from_bytes(sample['pdf'], dpi=300, first_page=1, last_page=1)[0]

Each pdf is paired with a json.gz file with the following structure. These strucure is that of docTR outputs. Learn more here. We explicitly avoid applying any OCR post-processing to get an approximate reading order. Users can use their own heuristics to extract the reading order from the boinding boxes.

{'pages': [{'page_idx': 0,
   'dimensions': [1684, 1191],
   'blocks': [{'geometry': [[0.11751876049538201, 0.0478515625],
      [0.2390290459697733, 0.126953125]],
     'lines': [{'geometry': [[0.14513473446683461, 0.0478515625],
        [0.15618112405541562, 0.0556640625]],
       'words': [{'value': '*',
         'confidence': 0.9999570846557617,
         'geometry': [[0.14513473446683461, 0.0478515625],
          [0.15618112405541562, 0.0556640625]]}]},
      {'geometry': [[0.1258035526868178, 0.060546875],
        [0.13823074097397148, 0.0703125]],
       'words': [{'value': '*',
         'confidence': 0.9995156526565552,
         'geometry': [[0.1258035526868178, 0.060546875],
          [0.13823074097397148, 0.0703125]]}]},
      {'geometry': [[0.11751876049538201, 0.0751953125],
        [0.2390290459697733, 0.09375]],
       'words': [{'value': '*',
         'confidence': 0.9917723536491394,
         'geometry': [[0.11751876049538201, 0.078125],
          [0.13270754617968095, 0.08984375]]},
        {'value': 'esma',
         'confidence': 0.9842223525047302,
         'geometry': [[0.1506579292611251, 0.0751953125],
          [0.2390290459697733, 0.09375]]}]},
      {'geometry': [[0.12442275398824515, 0.09765625],
        [0.13823074097397148, 0.107421875]],
       'words': [{'value': '*',
         'confidence': 0.9726816415786743,
         'geometry': [[0.12442275398824515, 0.09765625],
          [0.13823074097397148, 0.107421875]]}]},
      {'geometry': [[0.21693626679261124, 0.0986328125],
        [0.23074425377833752, 0.107421875]],
       'words': [{'value': '*',
         'confidence': 0.9999707937240601,
         'geometry': [[0.21693626679261124, 0.0986328125],
          [0.23074425377833752, 0.107421875]]}]},
      {'geometry': [[0.14375393576826195, 0.1123046875],
        [0.15756192275398823, 0.12109375]],
       'words': [{'value': '*',
         'confidence': 0.9999815225601196,
         'geometry': [[0.14375393576826195, 0.1123046875],
          [0.15756192275398823, 0.12109375]]}]},
      {'geometry': [[0.1989858837111671, 0.1123046875],
        [0.21279387069689337, 0.1220703125]],
       'words': [{'value': '*',
         'confidence': 0.9999063014984131,
         'geometry': [[0.1989858837111671, 0.1123046875],
          [0.21279387069689337, 0.1220703125]]}]},
      {'geometry': [[0.17275070843828716, 0.119140625],
        [0.18379709802686817, 0.126953125]],
       'words': [{'value': '*',
         'confidence': 0.9989649057388306,
         'geometry': [[0.17275070843828716, 0.119140625],
          [0.18379709802686817, 0.126953125]]}]}],
     'artefacts': []},
    {'geometry': [[0.251456234256927, 0.0712890625],
      [0.41439048068849704, 0.0986328125]],
     'lines': [{'geometry': [[0.251456234256927, 0.0712890625],
        [0.41439048068849704, 0.0849609375]],
       'words': [{'value': 'European',
         'confidence': 0.9998014569282532,
         'geometry': [[0.251456234256927, 0.0732421875],
          [0.3149729743912678, 0.0849609375]]},
        {'value': 'Securities',
         'confidence': 0.9985648989677429,
         'geometry': [[0.3163537730898405, 0.072265625],
          [0.38401290931989923, 0.0830078125]]},
        {'value': 'and',
         'confidence': 0.9997856020927429,
         'geometry': [[0.3853937080184719, 0.0712890625],
          [0.41439048068849704, 0.083984375]]}]},
      {'geometry': [[0.251456234256927, 0.083984375],
        [0.3729665197313182, 0.0986328125]],
       'words': [{'value': 'Markets',
         'confidence': 0.9976556301116943,
         'geometry': [[0.251456234256927, 0.0849609375],
          [0.3053073835012594, 0.0966796875]]},
        {'value': 'Authority',
         'confidence': 0.8129342794418335,
         'geometry': [[0.30668818219983207, 0.083984375],
          [0.3729665197313182, 0.0986328125]]}]}],
     'artefacts': []},
     ...
  {'page_idx': ...},
}

Document Structure

The structural organization of the documents, including words, lines, blocks, pages, and the overall document.

Element Description
Word A Word is an uninterrupted sequence of characters.
Line A collection of Words aligned spatially and meant to be read together.
Block A collection of Lines.
Page A collection of Blocks that were on the same physical page.

Artefacts are not used here.

The top-level key, pages, is a list containing each page in the document. In this example, only one page is shown.

  • Page:

    • page_idx: The index of the page in the document (starts at 0).
    • dimensions: The dimensions of the page in pixels, formatted as [height, width].
  • Blocks:

    • A page consists of several blocks, each containing lines.
    • geometry: Defines the bounding box of the block using normalized coordinates relative to the page size.
  • Lines:

    • Each block contains a list of lines, where a line is a sequence of words grouped together.
    • geometry: Bounding box of the line in normalized coordinates relative to the page size.
  • Words:

    • Each line is composed of individual words (continuous sequences of characters).
    • value: The text content of the word.
    • confidence: The confidence score of the OCR engine for the word.
    • geometry: Bounding box of the word in normalized coordinates relative to the page size.

For each page, the structure includes:

  • Blocks: Grouped lines within a page.
  • Lines: Sequences of words within a block.
  • Words: Individual characters or words detected within each line, along with their confidence scores and positions.

Data Splits

There is only a single train split for this dataset.

Train

  • fc-amf-train-{0000..0838}.tar
  • 838 shards (each shard is 500 MB for ease of use)
  • 605,438 samples
  • 9.3M pages

Additional Information

Note

This dataset is intended as an OCR-heavy pre-training basis for vision-language models or specialized OCR models. The current version contains multilingual data with English and French as the most represented languages. The OCR annotation might not work well for other languages. Filtering based on word confidence scores can be used as a heuristic to subsample the dataset.

In a future release, we will add language information per pdf.

Licensing Information

Data has been OCRed from the original dataset. As a consequence it has the same AMF-PDF license.