---
title: "Fine-tune an OCR recognizer offline in C#"
url: https://recursiveai.org/oneocr/cookbook/fine-tune-local-ocr-recognizer/
description: "Fine-tune the bundled OneOcr recognizer on your own transcribed line crops, then save and reload the weights without a labeling service."
version: "1.0.0"
---

Update an OCR model with line images and their correct text. With the three labeled crops in the zip, training runs on your machine without a service, uploads, or downloads once NuGet restore is done.

```shell
dotnet add package RecursiveAI.OneOcr.Training
dotnet add package RecursiveAI.OneOcr.Models
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using OneOcr;
using OneOcr.Training;

var report = new OcrTrainer(new() {
    WorkDirectory = "work", Steps = 1,
    BatchSize = 1, Restart = true
}).Train();
_ = OcrEngine.Load(report.ModelPath);
Console.WriteLine("Reloaded model.onm.");
```

Unzip [the example](https://recursiveai.org/oneocr/cookbook/fine-tune-local-ocr-recognizer/fine-tune-local-ocr-recognizer.zip), then `dotnet run`.

Writes `work/model.onm` and reloads it. `work/` in the zip contains crops for `Lot 17`, `Lot 28`, and `Lot 39`. Each `work/labels/*.json` has a `height` of `32` and a `lines` entry with a `cropWidth` and base64 `crop` (raw grayscale bytes, not a PNG). `work/reviewed.json` contains the transcriptions. One entry:

```json
{ "id": "d5723b4476293227-0", "text": "Lot 17" }
```

The part before `-0` is the label file’s `id`. The output from the snippet is:

```text
Reloaded model.onm.
```

## Full program

The full program also prints the number of lines used to update the weights and the number held back for evaluation. The three crops stand in for three separate documents.

```csharp
using OneOcr;
using OneOcr.Training;

var trainer = new OcrTrainer(new()
{
    WorkDirectory = "work",
    Steps = 1,
    BatchSize = 1,
    Restart = true
});
var report = trainer.Train();
_ = OcrEngine.Load(report.ModelPath);
Console.WriteLine($"Reloaded {Path.GetFileName(report.ModelPath)}.");
Console.WriteLine($"Training lines: {report.TrainLines}");
Console.WriteLine($"Held-out lines: {report.HeldOutLines}");
```

Output:

```text
Reloaded model.onm.
Training lines: 2
Held-out lines: 1
```

`Train()` throws `InvalidOperationException` if there’s no eligible training line outside the held-out split: it needs either text that the local reader and labeling model agreed on or text a reviewer supplied.

## Variations

To hold back two of the three example documents instead of one, add this to the options initializer:

```csharp
HeldOutFraction = 0.67,
```

Leave `Restart = true` when changing the split for this example so the run does not try to reuse the previous checkpoint.

## Related recipes

- [Measure OCR character and word error rates](https://recursiveai.org/oneocr/cookbook/evaluate-local-ocr-model/)
- [Load a custom OCR recognizer](https://recursiveai.org/oneocr/cookbook/load-custom-ocr-recognition-model/)

## The short answer

Put line crops in `work/labels/` and their verified transcriptions in `work/reviewed.json`, then call `new OcrTrainer(options).Train()` without a labeling client. With no `ModelPath` set, the trainer exports the bundled recognizer as its starting model. `Train()` saves the tuned weights in `work/model.onm` by default. Load that file with `OcrEngine.Load` instead of calling `LoadBundled()` when you want to use the tuned recognizer.

## How can I fine-tune an OCR model without a labeling service?

Start with the [work-directory format](https://recursiveai.org/oneocr/docs/review-queue/), not a folder of full-page scans. Each label file represents one document and holds its line crops. The `id` in the label file must match its filename without `.json`. For manually prepared data, store the line under `lines` with a zero-based `index`, set `state` to `Disagreed`, and put your verified text in `reviewed.json` under the ID formed from the document ID, a hyphen, and the line index. Use `format: oneocr.reviewed` and `version: 1` in the review file. `Disagreed` normally means the local reader and the labeling model returned a close match that still needs review. The hand-labeled example uses that state so the line isn’t treated as automatically agreed; the verified entry in `reviewed.json` makes it a corrected line and supplies the text.

To build the files from your own line images without a provider, use the crop and transcription fields in the [work-directory spec](https://recursiveai.org/oneocr/docs/review-queue/#work-directory); the [CLI labeling and review flow](https://github.com/recursive-ai-oss/oneocr-csharp/blob/8db430fb77c4d86d51686f15dfd9b5ccc20c99a7/README.md#L513-L526) writes those files from page images if you configure a labeling provider.

Scale each crop to the input height of the starting recognizer, keeping its aspect ratio. The file stores one byte per gray pixel in row order. The decoded byte count must be the crop width times the model’s height. A transcription that contains a character outside the model’s character set is counted and skipped for training; fine-tuning this way does not add new characters to the model. See the [model input format](https://recursiveai.org/oneocr/docs/model-container/) for how the recognizer interprets the pixel values.

## How do I tell whether fine-tuning helped my OCR model?

Keep representative documents out of training and compare the starting and tuned models against the same verified transcripts. With two or more documents, OneOcr holds out whole documents using a hash of the document ID and seed. With just one document and multiple lines, it splits by line, so its holdout can share the same page layout and scanning artifacts as training. For a meaningful comparison on new pages, use more than one source document and keep their IDs stable across runs.

The trainer reports character and word error rates before and after when there are held-out references. The three synthetic lines here show that the workflow runs and saves a usable model. One held-out line is not evidence that fine-tuning helped on other documents.
