Skip to main content
Developer guide

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.

    bash
    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=eng

    You 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.

    ApproachUse whenData neededTime
    Fine-tune (FINETUNE_TYPE=1)Same script, new font or domain vocabulary1,000–5,000 linesHours
    Cut top layer (FINETUNE_TYPE=2)Same script, meaningfully different glyph shapes5,000–20,000 linesHours–days
    Train from scratchNew script or very different alphabet50,000+ linesDays

    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.

    2:47 · English narration with on-screen captions

    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-ocr package (check with dpkg -L tesseract-ocr | grep lstmtraining). Elsewhere, build Tesseract from source with libleptonica-dev, pkg-config, automake and a C++ toolchain.
    • git, make and Python 3 with tesstrain's requirements.txt (Pillow and friends).
    • The tesstrain repository, plus langdata_lstm and a tessdata_best starting model (make training downloads it into TESSDATA if it is missing).
    bash
    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 unicharsets

    Prepare 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.

    bash
    # 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 unicharset so 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.

    bash
    make training \
      MODEL_NAME=myfont \
      START_MODEL=eng \
      TESSDATA=../tessdata_best \
      MAX_ITERATIONS=10000
    VariableMeaningTypical value
    START_MODELModel to continue from (fine-tuning)eng, jpn…
    TESSDATADirectory holding the starting model../tessdata_best
    MAX_ITERATIONSUpper bound on training steps10000–100000
    LEARNING_RATEStep size; lower is safer for fine-tuning0.0001–0.001
    FINETUNE_TYPE0 scratch, 1 fine-tune, 2 cut top layer1
    NET_SPECNetwork architecture specleave default
    RATIO_TRAINTrain/validation split0.9
    TARGET_ERROR_RATEEarly-stop CER target0.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).

    make training
    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.036
    • At 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 train and BWER train: character and word error rates, in percent, averaged over recent training lines. These are the numbers to watch while training runs.
    • mean rms and delta: 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 in data/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 into data/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_training file 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_int in the training tools.
    • best models are the most accurate; fast models are smaller and quicker. Always fine-tune from tessdata_best: the integer tessdata_fast models cannot be trained further.

    Evaluation: character error rate with lstmeval

    Judge the model on data it never trained on.

    bash
    lstmeval --model data/myfont/checkpoints/myfont_checkpoint \
      --traineddata data/myfont/myfont.traineddata \
      --eval_listfile data/myfont/list.eval

    Track 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.

    bash
    # 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 myfont

    You 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

    SymptomCauseFix
    Bad boxes / misaligned ground truthIncorrect font metrics or oversized line imagesRegenerate with the right --font and --ptsize; verify a few images by eye.
    Missing characters in outputCharacter absent from the unicharsetAdd the glyphs to your ground truth and rebuild the unicharset.
    Loss diverges / NaNLearning rate too highLower LEARNING_RATE and resume from an earlier checkpoint.
    Out of memoryBatch too large for RAM/GPUReduce BATCH_SIZE/network size, or use a machine with more memory.
    Validation CER rises while training loss fallsOverfittingStop 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.