Model description
Model Name: cyrillic-latin-textline-classifier
Model Version: 26102026
Model Type: Convolutional neural network (image classifier)
Base Model: ConvNeXt Tiny, initialised from the torchvision ImageNet-1k weights and fine-tuned
Purpose: Binary classification of text line images by script: cyrillic / latin
License: Apache 2.0
This is a ConvNeXt Tiny model tuned for binary (cyrillic / latin) classification of text line images based on script type. The model can be used to detect script type of text lines in HTR / OCR processing pipeline so that each line can be passed on to correct text recognition model.
This model was developed in the ArchXAI project funded by the Central Baltic Programme.
Intended Use
Routing of text line images in an HTR or OCR pipeline: a line classified cyrillic goes to a Cyrillic text
recognition model, any other line to a Latin-script model.
The model classifies script, not language: latin covers every language written in Latin script in the training data
(Finnish, Swedish, Estonian, Latvian, German).
Input: One RGB image of one (horizontal) text line. The image it is resized to 96 x 768 pixels without keeping the aspect ratio.
Output: Two logits, 0 = cyrillic, 1 = latin.
Label definition
A line is considered cyrillic if it contains at least one Cyrillic letter, including a single Cyrillic word in an otherwise Latin line.
A line without any Cyrillic letter is latin. This includes lines of only digits or punctuation, which have no script of their own and are also labeled latin,
so the model's decision on such lines should not be relied on.
Labels come mosty from the transcriptions of the ground truth data (script of the letters), but also from document-level metadata and from OCR-assisted labeling with manual spot checks for part of the new data.
Training data
Handwritten and typeset text line images from historical documents from the collections of the national archives of Estonia, Finland and Latvia. Some synthetically generated typeset Latin lines were also used. The training data is not distributed with the model.
| lines | latin | cyrillic | |
|---|---|---|---|
| Training set | 210 435 | 157 069 | 53 366 |
| Validation set | 16 651 | 14 051 | 2 600 |
Training and validation lines come from different collections. All ground truth that was available at the time went into training. No independent test set was held out for this version (see Evaluation).
Evaluation
There is no held-out test set for this model version. The earlier test collections were added to the training data to train on as much ground truth as possible.
Metrics: cyrillic found = share of lines labeled cyrillic that are classified cyrillic (recall); latin correct = share of lines labeled latin that are classified latin.
Decision rule: the class with the higher probability (threshold 0.5).
| lines | result | |
|---|---|---|
| cyrillic found | 2 600 | 96.6 % |
| latin correct | 14 051 | 98.5 % |
| balanced accuracy | 97.6 % | |
| overall accuracy | 16 651 | 98.2 % |
| AUC (cyrillic against latin) | 0.996 |
The validation lines come from collections the model did not train on, but the validation set was used to choose the checkpoint (the epoch with the best validation AUC) and to schedule the learning rate, so these figures are slightly optimistic.
Used Hyperparameters
Train batch size: 32
Optimizer: Adam, learning rate 1e-4, no weight decay
Scheduler: ReduceLROnPlateau (mode="max", factor=0.5, patience=2), monitoring the validation AUC
Epochs: 15 epochs of 120 000 sampled lines
Weight averaging: an exponential moving average of the weights (decay 0.9997)
Checkpoint selection: best validation AUC
Input image size: 96 x 768 (resized without keeping the aspect ratio)
Augmentation: Small rotation (up to 2.5 degrees, p = 0.4), slant (up to 5 degrees, p = 0.3), extra margins (p = 0.3), down- and up-scaling (p = 0.15), JPEG artifacts (p = 0.15), brightness / contrast / saturation jitter (p = 0.5), blur (p = 0.15).
Seed: 42
How to Use the Model
The checkpoint holds the averaged weights in model_state_dict, plus the label mapping.
You can use the model for inference by loading the model.pt file:
import torch
import torch.nn as nn
import torchvision.models as models
from PIL import Image
from torchvision import transforms
device = "cuda" if torch.cuda.is_available() else "cpu"
# The same preprocessing as in training: resize to 96x768 (aspect ratio is not kept), ImageNet normalisation
transform = transforms.Compose([
transforms.Resize((96, 768)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
def load_model(model_path):
"""Returns (model, idx2label)."""
ckpt = torch.load(model_path, map_location="cpu", weights_only=True)
model = models.convnext_tiny(weights=None)
model.classifier[2] = nn.Linear(model.classifier[2].in_features, 2)
model.load_state_dict(ckpt["model_state_dict"])
idx2label = {int(k): v for k, v in ckpt["idx2label"].items()} # {0: "cyrillic", 1: "latin"}
return model.to(device).eval(), idx2label
@torch.inference_mode()
def predict(model, images):
"""images: list of PIL images (one text line each). Returns a list of (label_index, probability_of_cyrillic)."""
batch = torch.stack([transform(im.convert("RGB")) for im in images]).to(device)
p_cyrillic = torch.softmax(model(batch), dim=1)[:, 0] # class 0 = cyrillic
return [(0 if p >= 0.5 else 1, float(p)) for p in p_cyrillic]
model, idx2label = load_model("/path/to/model.pt")
image = Image.open("/path/to/line.png")
idx, p_cyr = predict(model, [image])[0]
print(f"{idx2label[idx]} (probability of cyrillic {p_cyr:.3f})")
Limitations and Biases
Scripts. The model only knows
cyrillicandlatin, and assigns everything into one of these classes.Lines with both scripts. Lines with both scripts are labeled
cyrillic, but they can be difficult for the model to classify correctly.Unfamiliar collections. Model quality drops on collections and places that look unlike the training data.
Confident mistakes. Most mistakes come with a high probability, so the probability is not a reliable measure of uncertainty.
Short lines. Lines that are less than about 6 times as wide as they are tall (a single word or name) are classified clearly less reliably than ordinary lines (AUC about 0.91 to 0.96 instead of 0.99).
Digits and punctuation. Lines without letters have no script and are arbitrarily classified.
Orientation. The model expects horizontal lines. Vertical or rotated crops may be classified wrongly.
This model is a fine-tuned version of ConvNeXt Tiny and thus may inherit some of its limitations and biases.
Future Work
Potential improvements for this model include:
- More and more diverse ground truth data, especially from collections whose documents contain both scripts, and from collections where the model performs poorly.
- A test set that is kept out of training, to measure the quality on unseen material.
- Better handling of mixed lines that contain only a little Cyrillic text.
Citation
If you use this model in your work, please cite it as:
@misc{cyrillic-latin-textline-classifier,
author = {Kansallisarkisto},
title = {NAF Cyrillic Latin Textline Classification Model},
year = {2026},
publisher = {Hugging Face},
howpublished = {\url{https://huggingface.co/Kansallisarkisto/cyrillic-latin-textline-classifier/}},
}
Model Card Authors
Author: Kansallisarkisto
Contact Information: mikko.lipsanen@kansallisarkisto.fi, sanna.joska@kansallisarkisto.fi