File size: 4,500 Bytes
566d53d
 
 
 
 
 
 
 
 
 
 
 
 
 
e45dd30
566d53d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
license: apache-2.0
language:
- en
library_name: pytorch
tags:
- ocr
- ocr-post-correction
- text-cleaning
- looped-transformer
- adaptive-computation
- mixture-of-experts
datasets:
- HuggingFaceFW/fineweb-edu
pipeline_tag: other
---

# OCR cleaner v1

A tiny model that fixes OCR errors in English text: confused characters (`rn` for `m`, `1` for `l`, `0` for `O`), dropped and stray characters, merged and split words, stray spaces, case flips, and hyphenated line breaks. It runs on a CPU.

Dual-licensed under **MIT or Apache-2.0**, at your option (`LICENSE-MIT`, `LICENSE-APACHE`). The metadata field above lists only one because the Hub allows one.

**Experimental.** It was trained on synthetic OCR-style damage, not on real scanner output; see Limitations.

| | |
|---|---|
| Parameters | 0.65M (2.6 MB, safetensors, fp32) |
| Speed | about 28,000 characters/s on one CPU thread |
| Character error rate, normal damage (4.3% of characters) | 4.28% β†’ 0.79–0.98% |
| Character error rate, heavy damage (16.4% of characters) | 16.4% β†’ 4.4% |
| Damaged characters fixed | 84% |

The range 0.79–0.98% is between evaluating every character for the full step budget and the fast mode `ocr_clean.py` uses, where finished characters stop computing.

## Usage

Needs `torch` and `safetensors`.

```bash
pip install torch safetensors
python ocr_clean.py < scan.txt > clean.txt
```

```python
from ocr_clean import OCRCleaner
model = OCRCleaner("model.safetensors", "config.json")
text, steps = model.clean("Tlie quick brovvn fox jurnps over tbe lazy dog. It was the be-\nginning of a new era.")
print(text)  # The quick brown fox jumps over the lazy dog. It was the beginning of a new era.
```

Single line breaks are read as wrapped lines and joined into running text; blank lines separate paragraphs and are kept. Input is printable ASCII; other characters are read as `?`.

## How it works

A looped transformer with an adaptive number of steps per character:

- Each input character is a token. Every step, each character attends to the others, then a router picks **one of 8 learned operations** (small MLPs, shared across all steps) for that character.
- Each character **decides when it is done** (a learned halting probability). Clean characters stop after about 3 steps, damaged ones after about 5, without being told which are damaged. Finished characters stop costing compute; their attention keys and values are cached.
- Each input character emits **0–2 output characters**, so one encoder pass can delete, insert and split characters (`rn` β†’ `m`, `cl` β†’ `d`, dropped letters restored).

In experiments, this design beat a plain looped block of the same per-step compute, matched an 8Γ— wider block at about 1/12 of its compute, and beat standard mixture-of-experts transformers 4–15Γ— its size on this task.

## Training

- Text: one shard (`sample/10BT/000_00000.parquet`) of [FineWeb-Edu](https://huggingface.co/datasets/HuggingFaceFW/fineweb-edu) (ODC-By 1.0), ASCII-normalised; about 490M characters, sampled as 2M windows of ~100 characters.
- Damage, synthetic: character confusions, `rn`/`m`-style splits and merges, deletions, insertions of spaces and specks (`.,'~^-|:`), case flips, stray hyphens, wrapped lines and hyphenated wraps. About 4% of characters damaged on average (log-uniform rate from 1% to 15%; 15% of windows clean).
- 40k steps, batch 128, AdamW, bf16. Loss: cross-entropy on the two output slots per character at every step, weighted by the halting distribution (PonderNet), plus a cost per expected step and a KL term toward a geometric halting prior.

## Limitations

- **Synthetic damage.** Real OCR engines make errors with other statistics (layout, fonts, scan quality). Expect lower gains on real scans than in the table above.
- English, printable ASCII only; trained on web text, not on old print, tables or code.
- Misses or mangles some errors, and the result depends on the surrounding text. For example, on its own, "The cornmittee met on Tuesday to cliscuss the budget" comes out as "The committee met on Tuesday to daiscuss the budget"; inside a longer passage the same "cliscuss" was fixed to "discuss".
- Sometimes merges a real space (`era in` β†’ `erain`) or leaves a stray one.
- Text is cut into windows of up to about 110 characters with no overlap, so damage that spans a window edge is not repaired.
- It can change correct but unusual words into common ones. Do not use it where exact wording matters without review.