File size: 12,300 Bytes
f65dfab
8a4a94f
 
 
 
 
 
 
 
 
 
 
 
 
f65dfab
8a4a94f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
---
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

- **Contact at LightOn:** [Said Taghadouini](mailto:[email protected])

### Dataset Summary

FC-AMF-OCR dataset is a document dataset OCRed from the [AMF-PDF](https://huggingface.co/datasets/PleIAs/AMF-PDF) dataset part of the Finance Commons collection. The dataset comes with native text annotation available here [AMF-Text](https://huggingface.co/datasets/PleIAs/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](https://github.com/mindee/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.

<center>
    <img src="https://huggingface.co/datasets/lightonai/fc-amf-ocr/resolve/main/docs/sample0.png" alt="An example from the FC-AMF-OCR dataset" width="600" height="300">
    <p><em>An example page of one pdf document with existing text annotation(red) and the OCR annotation(green). </em></p>
</center>

Following most large scale OCR datasets like [IDL](https://huggingface.co/datasets/pixparse/idl-wds), this dataset is in [webdataset](https://github.com/webdataset/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.

```python
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:

```python
 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.
<center>
    <img src="https://huggingface.co/datasets/lightonai/fc-amf-ocr/resolve/main/docs/page_distribution.png" alt="." width="600" height="300">
    <p><em>The distribution of number pf pages in the FC-AMF-OCR dataset. </em></p>
</center>

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](https://github.com/Belval/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.

```bash
apt-get install poppler-utils 
```

```python
from pdf2image import convert_from_bytes

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

<center>
  <img src="https://huggingface.co/datasets/lightonai/fc-amf-ocr/resolve/main/docs/pdf_first_page.png" alt="" width="400" height="600">
</center>

Each `pdf` is paired with a `json.gz` file with the following structure. These strucure is that of docTR outputs. Learn more [here](https://mindee.github.io/doctr/using_doctr/using_models.html#what-should-i-do-with-the-output). 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.

```json
{'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](https://huggingface.co/datasets/PleIAs/AMF-PDF) license.