Skip to content
No description, website, or topics provided.
Python Shell Makefile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Typegroups classifier for OCR

Quick setup

If needed, create a virtual environment for Python 3 (it was tested successfully with Python 3.7), activate it, and install ocrd.

virtualenv -p python3 ocrd-venv3
source ocrd-venv3/bin/activate
pip3 install ocrd

Enter in the folder containing the tool: cd ocrd_typegroups_classifier/

Install the module and its dependencies

make install
make deps

Finally, run the test:

sh test/


The model densenet121.tgc is based on a DenseNet with 121 layers, and is trained on the following 12 classes:

  • Antiqua

  • Bastarda

  • Fraktur

  • Gotico-Antiqua

  • Greek

  • Hebrew

  • Italic

  • Rotunda

  • Schwabacher

  • Textura

  • other_font

  • not_a_font

Updating PyTorch

If you update PyTorch, it is possible that the model cannot be loaded anymore. To solve this issue, proceed as follows.

  1. Downgrade to a version of PyTorch which can load the model,

  2. Run the following code:

import torch
from ocrd_typegroups_classifier.typegroups_classifier import TypegroupsClassifier
tgc = TypegroupsClassifier.load('ocrd_typegroups_classifier/models/densenet121.tgc'), '')
  1. Upgrade to the desired version of PyTorch

  2. Run the following code:

import torch
from import densenet121
from ocrd_typegroups_classifier.typegroups_classifier import TypegroupsClassifier
print('Creating the network')
net = densenet121(num_classes=12)
print('Creating the classifier')
tgc = TypegroupsClassifier(
  1. delete model.mdl

If PyTorch cannot load model.mdl, then you will have to train a new model from scratch.

Training a classifier

The data used for training the classifier provided in this repository is freely available at the following address:

The script in tool/ can be used to extract a suitable amount of crops to train the network, with data balancing.

The script in tools/ continues the training of any existing densenet121.tgc in the models/ folder. If there is none present, then a new one is created and trained from scratch.

Note that you might have to adapt the paths in these scripts so that they correspond to where data is in your system.

Generating activation heatmaps

For investigation purpose, it is possible to produce heatmaps showing where and how much the network gets activated for specific classes.

You need first to install an additional dependency which is not required by the OCR-D tool with:

pip install tqdm

Then, you can run

python3 --layer 9 --image_path sample2.jpg

You can specify which layer of the network you are interested in, between 0 and 11. Best results are to be expected with larger values. If no layer is specified, then the 11th is used by default.

You can’t perform that action at this time.