Package: ocrd-typegroups-classifier
ocrd_typegroups_classifierTypegroups classifier for OCRInstallationFrom PyPIpip3installocrd_typegroup_classifierFrom sourceIf needed, create a virtual environment for Python 3 (it was tested
successfully with Python 3.7), activate it, and install ocrd.virtualenv-ppython3ocrd-venv3sourceocrd-venv3/bin/activate
pip3installocrdEnter in the folder containing the tool:cd ocrd_typegroups_classifier/Install the module and its dependenciesmake installFinally, run the test:sh test/test.shModelsDensenetThe model densenet121.tgc is based on a DenseNet with 121 layers, and
is trained to classify pages or regions, on the following 12 classes:AntiquaBastardaFrakturGotico-AntiquaGreekHebrewItalicRotundaSchwabacherTexturaother_fontnot_a_fontThe confusion matrix obtained with a DenseNet-121 on the pages with a single font from the dataset (see "Training a classifier" below) is:AntiquaBastardaFrakturGot.-Ant.GreekHebrewItalicRotundaSchwabacherTexturaOther fontNot a fontRecallAntiqua15311052598.6%Bastarda286610194.4Fraktur1933151299.5%Gotico-Antiqua269199.6Greek581196.7%Hebrew132699.7%Italic118799.5%Rotunda91495511198.3%Schwabacher164245295.4%Textura2371199.2%Other font2881594.1%Not a font42215174233198.9%Precision99.7%94.7%99.1%95.4%96.7%99.4%94.9%99.1%94.2%96.4%98.3%99.0%Colwise classifierThe model colwise_classifier.tgc is based on a pixel column
classifier composed of an RNN and a CNN backbone, it is
trained to classify text lines on the following 12 classes :AntiquaBastardaFrakturTexturaSchwabacherGreekItalicHebrewGotico-antiquaManuscriptRotundano class/ignoreUpdating PyTorchIf you update PyTorch, it is possible that the model cannot be loaded
anymore. To solve this issue, proceed as follows.Downgrade to a version of PyTorch which can load the model,Run the following code:importtorchfromocrd_typegroups_classifier.typegroups_classifierimportTypegroupsClassifiertgc=TypegroupsClassifier.load('ocrd_typegroups_classifier/models/densenet121.tgc')torch.save(tgc.model.state_dict(),'model.pt')Upgrade to the desired version of PyTorchRun the following code:importtorchfromocrd_typegroups_classifier.network.densenetimportdensenet121fromocrd_typegroups_classifier.typegroups_classifierimportTypegroupsClassifierprint('Creating the network')net=densenet121(num_classes=12)net.load_state_dict(torch.load('model.pt'))print('Creating the classifier')tgc=TypegroupsClassifier({'antiqua':0,'bastarda':1,'fraktur':2,'gotico_antiqua':3,'greek':4,'hebrew':5,'italic':6,'rotunda':7,'schwabacher':8,'textura':9,'other_font':10,'not_a_font':11},net)tgc.save('ocrd_typegroups_classifier/models/densenet121.tgc')delete model.mdlIf PyTorch cannot load model.mdl, then you will have to train a new
model from scratch.Training a classifierThe data used for training the densenet classifier provided in this
repository is freely available at the following address:https://doi.org/10.1145/3352631.3352640The script in tool/create_training_patches.py can be used to extract
a suitable amount of crops to train the network, with data balancing.The script in tools/train_densenet121.py 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 heatmapsFor investigation purpose, it is possible to produce heatmaps showing
where and how much the densenet 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 tqdmThen, you can run heatmap.py:python3 heatmap.py --layer 9 --image_path sample2.jpgYou 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.