Train Tesseract on custom fonts
and data (tesstrain, v5).
A tested, decision-driven guide to fine-tuning and training Tesseract 5 models with tesstrain: building from source, preparing ground truth, running make training, tuning the important variables, measuring CER with lstmeval, and deploying your traineddata.
On this page
TL;DR — fine-tune an existing model
If your script is already supported, fine-tuning is the fastest path to a better model.
git clone https://github.com/tesseract-ocr/tesstrain.git
cd tesstrain && make tesseract-langdata
# put line images + .gt.txt in data/myfont-ground-truth/
make training MODEL_NAME=myfont START_MODEL=engYou need Linux or WSL2. On Ubuntu and Debian the training tools (lstmtraining, text2image) come with the regular tesseract-ocr package; elsewhere, build Tesseract from source (see installation).
Decision tree: fine-tune vs cut top layer vs from scratch
Choosing the right approach saves days of wasted training.
| Approach | Use when | Data needed | Time |
|---|---|---|---|
Fine-tune (FINETUNE_TYPE=1) | Same script, new font or domain vocabulary | 1,000–5,000 lines | Hours |
Cut top layer (FINETUNE_TYPE=2) | Same script, meaningfully different glyph shapes | 5,000–20,000 lines | Hours–days |
| Train from scratch | New script or very different alphabet | 50,000+ lines | Days |
Start with the lightest option that could work, evaluate, and only escalate if CER is unacceptable. Most custom-font problems are solved by fine-tuning.
Video: a real fine-tuning run, start to finish
Dot-matrix print goes from 14% to 0.24% character errors in a 5-minute tesstrain run on a 4-core CPU. Watch it in under 3 minutes.
Prerequisites
On Ubuntu and Debian, the packaged Tesseract already includes the training tools.
- A Linux environment — native or WSL2 on Windows. macOS is not supported for training.
- The training tools: on Ubuntu and Debian they ship with the
tesseract-ocrpackage (check withdpkg -L tesseract-ocr | grep lstmtraining). Elsewhere, build Tesseract from source withlibleptonica-dev,pkg-config,automakeand a C++ toolchain. git,makeand Python 3 with tesstrain'srequirements.txt(Pillow and friends).- The
tesstrainrepository, pluslangdata_lstmand atessdata_beststarting model (make trainingdownloads it intoTESSDATAif it is missing).
sudo apt update
sudo apt install -y git make python3-pip tesseract-ocr
dpkg -L tesseract-ocr | grep -E "bin/(lstmtraining|text2image)" # training tools present
git clone https://github.com/tesseract-ocr/tesstrain.git
cd tesstrain
pip install -r requirements.txt
make tesseract-langdata # fetches the langdata_lstm unicharsetsPrepare ground truth
Ground truth is line images paired with their exact text.
tesstrain expects one image per text line in data/<MODEL_NAME>-ground-truth/, each with a matching .gt.txt file containing the exact transcription. Generate images from a text file and a font with text2image, then split long text into lines with split_training_text.py from langdata_lstm.
# Generate line images from a text corpus + a font
text2image --text=corpus.txt \
--outputbase=data/myfont-ground-truth/myfont \
--font='My Custom Font' --fonts_dir=/usr/share/fonts \
--ptsize=14
# text2image also emits .box files; convert them to .gt.txt
# (tesstrain/training/ has helper scripts, or rename after verification)How much data: 100–500 lines to validate the pipeline, 1,000–5,000 for production. Accuracy comes from variety — different fonts, sizes, spacing and noise — more than raw volume.
Fonts and unicharset selection
The character set must cover everything the model will see.
- Install the training fonts in the environment; pass the exact family name to
--font. - When fine-tuning, keep the starting model's
unicharsetso the output layer matches. - For a new script, derive the unicharset from your ground truth so every glyph is represented.
- Include punctuation, digits and mixed case if the real documents contain them — otherwise the model will drop them.
Run training
One command drives the whole Makefile pipeline.
make training \
MODEL_NAME=myfont \
START_MODEL=eng \
TESSDATA=../tessdata_best \
MAX_ITERATIONS=10000| Variable | Meaning | Typical value |
|---|---|---|
START_MODEL | Model to continue from (fine-tuning) | eng, jpn… |
TESSDATA | Directory holding the starting model | ../tessdata_best |
MAX_ITERATIONS | Upper bound on training steps | 10000–100000 |
LEARNING_RATE | Step size; lower is safer for fine-tuning | 0.0001–0.001 |
FINETUNE_TYPE | 0 scratch, 1 fine-tune, 2 cut top layer | 1 |
NET_SPEC | Network architecture spec | leave default |
RATIO_TRAIN | Train/validation split | 0.9 |
TARGET_ERROR_RATE | Early-stop CER target | 0.01 |
Reading the training log
make training prints a progress line every 100 iterations. This excerpt is from a short real run: fine-tuning eng on 300 lines of one font with MAX_ITERATIONS=1200 (Tesseract 5.3.4).
At iteration 21/100/100, mean rms=0.524%, delta=0.608%, BCER train=0.767%, BWER train=3.079%, skip ratio=0.000%, New best BCER = 0.767 Transitioned to stage 1 wrote best model:data/myfont/checkpoints/myfont_0.767_21_100.checkpoint wrote checkpoint.
…
At iteration 25/600/600, mean rms=0.309%, delta=0.129%, BCER train=0.181%, BWER train=0.772%, skip ratio=0.000%, New best BCER = 0.181 wrote best model:data/myfont/checkpoints/myfont_0.181_25_600.checkpoint wrote checkpoint.
…
At iteration 29/1200/1200, mean rms=0.228%, delta=0.009%, BCER train=0.036%, BWER train=0.149%, skip ratio=0.000%, New best BCER = 0.036 wrote best model:data/myfont/checkpoints/myfont_0.036_29_1200.checkpoint wrote checkpoint.
Finished! Selected model with minimal training error rate (BCER) = 0.036At iteration 29/1200/1200: learning / training / sample iterations. The last two count the lines processed; the first only grows when the model still makes mistakes on a line, so it stays low when you fine-tune on text the model already reads well.BCER trainandBWER train: character and word error rates, in percent, averaged over recent training lines. These are the numbers to watch while training runs.mean rmsanddelta: raw network error measures. They help spot a run that has stalled, but BCER is easier to interpret.skip ratio: the share of lines that could not be used, usually because the ground-truth text contains characters the model cannot encode or the line is too long. Anything above 0% is worth checking.New best BCER … wrote best model: a copy of the model with the lowest training error so far is saved indata/myfont/checkpoints/, with the BCER in its file name. When the run ends,Finished!reports the best BCER reached, and tesstrain converts the final checkpoint intodata/myfont.traineddata.
BCER train is measured on the lines the model is learning from, so it always looks better than real-world accuracy. Judge the model with lstmeval on the held-out lines, as shown below.
Checkpoints, stop_training and best vs fast
- tesstrain writes periodic checkpoints (for example
myfont_checkpoint) you can evaluate or resume from. - Create a
stop_trainingfile in the output directory to end the run cleanly at the next iteration. - Convert a checkpoint to a usable model with
combine_tessdata -e/convert_to_intin the training tools. - best models are the most accurate; fast models are smaller and quicker. Always fine-tune from
tessdata_best: the integertessdata_fastmodels cannot be trained further.
Evaluation: character error rate with lstmeval
Judge the model on data it never trained on.
lstmeval --model data/myfont/checkpoints/myfont_checkpoint \
--traineddata data/myfont/myfont.traineddata \
--eval_listfile data/myfont/list.evalTrack CER on a held-out set. When it stops falling — or starts climbing while training loss still drops — you are overfitting; stop and use the earlier checkpoint.
Deploy the trained model
Copy the traineddata, point the engine at it, and test.
# Copy into a tessdata directory
cp data/myfont.traineddata /usr/share/tesseract-ocr/5/tessdata/
# or set a custom prefix
export TESSDATA_PREFIX=/path/to/tessdata
tesseract sample.png stdout -l myfontYou can combine your model with a base language, e.g. -l myfont+eng. In Python, pass the same value to pytesseract — see the Python guide.
Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
| Bad boxes / misaligned ground truth | Incorrect font metrics or oversized line images | Regenerate with the right --font and --ptsize; verify a few images by eye. |
| Missing characters in output | Character absent from the unicharset | Add the glyphs to your ground truth and rebuild the unicharset. |
| Loss diverges / NaN | Learning rate too high | Lower LEARNING_RATE and resume from an earlier checkpoint. |
| Out of memory | Batch too large for RAM/GPU | Reduce BATCH_SIZE/network size, or use a machine with more memory. |
| Validation CER rises while training loss falls | Overfitting | Stop earlier, add more varied ground truth, or use a lower learning rate. |
Training FAQ
Can I train Tesseract without building it from source?
On Ubuntu and Debian, yes: the tesseract-ocr package already includes the training tools (lstmtraining, text2image, lstmeval). Add the tesstrain repository and you can fine-tune. On Windows, use WSL2 (tesstrain needs a Linux shell); on other systems, check that lstmtraining is installed, or build Tesseract from source.
How many samples do I need?
About 100–500 lines to validate the pipeline, and 1,000–5,000 for production. Fine-tuning needs less than training from scratch.
Fine-tune or train from scratch?
Fine-tune for a new font or vocabulary in an existing script; train from scratch only for a genuinely new script.
When should I stop training?
When held-out CER stops improving — not when training loss flattens. That gap is overfitting.
Where does the model go?
Copy <MODEL_NAME>.traineddata into a tessdata directory (or set TESSDATA_PREFIX) and run tesseract image.png stdout -l myfont.