This folder contains code related to PyTorch text recognition. There are three subtasks supported by this code:
- general alphanumeric text recognition
- two specific formula-recognition tasks:
- recognition of the handwritten polynomial equations
- recognition of the rendered and scanned printed formulas This code is based on a PyTorch realization of the code from the original repository.
Models code is designed to enable ONNX* export and inference on CPU\GPU via OpenVINO™.
- Ubuntu* 18.04
- Python* 3.7 or newer
- PyTorch* (1.5.1)
- OpenVINO™ 2021.2 with Python API
These packages are used for rendering images while evaluation and demo.
sudo apt-get update &&
sudo apt-get install -y --no-install-recommends \
texlive \
imagemagick \
ghostscriptEvaluation process uses imagemagick to convert PDF-rendered formulas into PNG images. Sometimes there could be errors:
convert-im6.q16: not authorized `/tmp/tmpgr1m4d4_.pdf' @ error/constitute.c/ReadImage/412.
convert-im6.q16: no images defined `/tmp/tmpgr1m4d4_.png' @ error/convert.c/ConvertImageCommand/3258.
The problem is missing required permissions.
To fix this open file /etc/ImageMagick-6/policy.xml:
sudo nano /etc/ImageMagick-6/policy.xml
Find <policy domain="coder" rights="none" pattern="PDF" />
and replace with:
<policy domain="coder" rights="read|write" pattern="PDF" />
Create and activate virtual environment:
bash init_venv.shSeveral dataset formats are supported:
-
Im2latex format. Dataset format is similar to im2latex-100k. Main structure of the dataset is following:
formulas.norm.lst- file with one formula per line.imaged_processed- folder containing input images.split_file- this file containsimage_name(tab symbol)formula_idxper line connecting corresponding index of the formula in the file with formulas and particular image withimage_name. Example:There should be at least two such files:11.png 11 34.png 34 ...train_filter.lstandvalidate_filter.lst
You can prepare your own dataset in the same format as above. Samples of the dataset can be found here.
NOTE: By default the following structure of the dataset is assumed:
images_processed- folder with imagesformulas.norm.lst- file with preprocessed formulas. If you want to use your own dataset, formulas should be preprocessed. For details, refer to this script.validate_filter.lstandtrain_filter.lst- corresponding splits of the data. -
ICDAR13 recognition dataset. See details here
-
Synth90k dataset (MJSynth) See details here
-
IIIT5k See details here
Every dataset class has its own constructor with specific parameters. You can see costructors here. Examples of use of different datasets can be seen in the config files:
When you prepare your own dataset with formulas.norm.lst file, you will have to create a vocabulary file for this dataset.
Vocabulary file is a special file which is used to cast token ids to human readable tokens and vice versa.
Like letters and digits in the natural language, tokens here are atomic units of the latex language (e.g. \\sin, 1, \\sqrt, etc).
You can find an example in the vocabs folder of this project.
Use this script to create vocab file from your own formulas file.
The script will read the formulas and create the vocabulary from the formulas used in train split of the dataset.
If you use one of the general text recognition datasets (such as ICDAR13 or synth90k), vocab file is already prepared. You can find it here
To train text recognition model run:
python tools/train.py --config <path to config> --work_dir <path to work dir>Work dir is used to store information about learning: saved model checkpoints, logs.
The config file is divided into 4 sections: train, eval, export, demo. Common parameters (like path to the model) are stored on the same level as train and other sections. Unique parameters (like learning rate) are stored in specific sections. Unique parameters and common parameters are mutually exclusive.
Note: All values in the config file which have 'path' in their name will be treated as paths and the script which reads configuration will try to resolve all relative paths. By default all relative paths are resolved relatively to the folder where this README.md file is placed. Keep this in mind or use full paths.
backbone_config: *arch: type of the architecture. For more details, please, refer to ResnetLikeBackBone *disable_layer_3anddisable_layer_4- disables layer 3 and 4 in resnet-like backbone. ResNet backbone from the torchvision module consists of 4 block of layers, each of them increase the number of channels and decrease the spatial dimensionality. These parameters allow to switch off the 3rd and the 4th of such layers, respectively. *enable_last_conv- enables additional convolution layer to adjust number of output channels to the number of input channels in the LSTM. Optional. Default: false. *output_channels- number of output channels channels. Ifenable_last_convistrue, this parameter should be equal tohead.encoder_input_size, otherwise it should be equal to actual number of output channels of the backbone.head- configuration of the text recognition head.- Now two text recognition heads are supported: Attention-based text reconition head and CTC-based LSTM-encoder-decoder head
positional_encodings- if true, use positional encodings like in transformer
encoder_hidden_size- number of channels in encoderencoder_input_size- number of channels in the lstm input, should be equal tobackbone_config.output_channels
- Attention-based head specific parameters:
trainable_initial_hidden- if true, inital states of the LSTM cells will be trainable, else it will be zero tensor.
beam_width- width used in beam search. 0 - do not use beam search, 1 and more - use beam search with corresponding number of possible tracks.emb_size- dimension of the embeddingmax_len- maximum possible length of the predicted formulan_layer- number of layers in the trainable initial hidden state for each row
- CTC head specific parameters:
cnn_encoder_height- height dimension size after backbone. Default is 1. Used for dimension reductiomnreduction- the way of reducing dimensionality. LSTM takes as input 3-dimensional tensor, and backbone produces 4-dimensional. Height dimension is reduced. Options:mean- apply Average Pooling,flatten- Flatten tensor,weighted- apply convolution with kernelcnn_encoder_height x 1.
model_path- path to the pretrained model checkpoint (you can find the links to the checkpoints below in this document).vocab_path- path where vocabulary file is stored.val_transforms_list- here you can describe set of desirable transformations for validation datasets respectively. An example is given in the config file, for other options, please, refer to constructor of transforms (sectioncreate_list_of_transforms)device- device for training, used in PyTorch .to() method. Possible options: 'cuda', 'cpu'.cpuis used by default.
In addition to common parameters you can specify the following arguments:
batch_size- batch size used for traininglearning_rate- learining ratelog_path- path to store training logsoptimizer- any possible type of the optimizer supported by pytorch (like Adam or SGD)scheduler- any possible type of the scheduler supported by pytorch (like ReduceLROnPlateau)scheduler_params- dict describing scheduler params in accordance with pytorch documentationloss_type- type of loss:NLLfor Attention-based head andCTCfor CTC-based headsave_dir- dir to save checkpointsdatasets- list of datasets which will be used in training. Common parameters are:type- name of the dataset, for details see here. Dataset names are described in thestr_to_classsectionsubset- how to use this subset. Options:trainorvalidateAny other parameters are dataset specific and should be set in correspondance with its constructor.
train_transforms_list- similar toval_transforms_listepochs- number of epochs to train
One can use some pretrained models. Right now three models are available:
- medium model:
- checkpoint link
- digits, letters, some greek letters, fractions, trigonometric operations are supported; for more details, please, look at corresponding vocab file.
- to use this model, just set the correct value to the
model_pathfield in the bcorresponding config file:
model_path: <path to the model>
The model can be used for recognizing both rendered and scanned formulas (e.g. from a scanner or from a phone camera)
- handwritten polynomials model:
- checkpoint
- digits, letters, upper indices are supported
- to use this model, please, change model path in the corresponding config file:
model_path: <path to the model>
The model can be used for recognizing handwritten polynomial equations.
- alphanumeric model
- checkpoint
- number from 0 to 9 and latin letters in lower case are supported
- to use this model, please, change model path in the corresponding config file:
model_path: <path to the model>
All the above models can be used for aftertuning or as ready for inference models. To provide maximum quality at recognizing text, it is highly recommended to preprocess image - simply binarize it:
val_transform_list:
- name: TransformBin
threshold: 100
You can find other prepocessing at this file. Some of sample images in the data section of this repo are already preprocessed, you can look at the examples.
dataset- the same as intrainsection, but here it is just one dataset, so it does not havesubsetsection.render- render images to compare them or just compare predicted and ground-truth text. By default istrue. Used only for formula recognition. See Evaluation section for details.
transforms_list- list of image transformations (optional)
These parameters are used for model export to ONNX & OpenVINO™ IR:
-
In case model is divided into encoder and decoder (for models with attention head, like formula recognition models):
res_encoder_name- filename to save the converted encoder model (with.onnxpostfix)res_decoder_name- filename to save the converted decoder model (with.onnxpostfix)
-
Else if model is monolithic (for models with CTC-head, like alphanumeric recognition):
res_model_name- filename to save the converted model (with.onnxpostfix)
-
export_ir- Set this flag totrueto export model to the OpenVINO IR. For details refer to convert to IR section -
verbose_export- Set this flag totrueto perform verbose export (i.e. print model optimizer commands to terminal) -
input_shape_decoderfor composite (encoder-decoder) orinput_shapefor monolithic model - list of dimensions describing input shape for encoder for OpenVINO IR conversion. -
model_input_namesandmodel_output_names- comma-separated names of input and output tensors respectively. Used for export of monolithic model. Optional -
encoder_input_names,decoder_input_names,encoder_output_names,decoder_output_names- the same as above for composite models. Optional. Change it only if default values for these parameters are note acceptable, for details see here
tools/test.py script is designed for quality evaluation of formula-recognition models.
For example, one can run evaluation process using config for medium model.
python tools/test.py --config configs/medium_config.ymlEvaluation process is the following:
- Run the model and get predictions
- (optionally) Render predictions from the first step into images of the formulas
- Compare images if
renderflag is true, else just compare predicted and GT text.
The third step is important for LaTeX models because in LaTeX language one can write different formulas that are looking the same. Example:
s^{12}_{i}ands_{i}^{12}looking the same: both of them are rendered asThat is why we cannot just compare text predictions one-by-one, we have to render images and compare them.
In order to see how trained model works using OpenVINO™ please refer to Formula recognition Python* Demo and Text detection C++* demo. Before running the demo you have to export trained model to IR. Please, see below how to do that.
If you want to see how trained PyTorch model is working, you can run tools/demo.py script with correct config file. Fill in the input_images variable with the paths to desired images. For every image in this list, model will predict the formula and print it into the terminal.
To run the model via OpenVINO™ one has to export PyTorch model to ONNX first and then convert to OpenVINO™ Intermediate Representation (IR) using Model Optimizer.
Model will be split into two parts if it has Attention head:
- Encoder (CNN-backbone and part of the text recognition head)
- Text recognition decoder (LSTM + attention-based head)
Else the model will be exported as one file.
The tools/export.py script exports a given model to ONNX representation.
python tools/export.py --config configs/medium_config.ymlConversion from ONNX model representation to OpenVINO™ IR is straightforward and handled by OpenVINO™ Model Optimizer.
To convert model to IR one has to set flag export_ir in config file:
...
export_ir: true
...
If this flag is set, full pipeline (PyTorch -> ONNX -> Openvino™ IR) is running, else model is exported to ONNX only.
There are 3 group of tests for every supported configuration:
- Train test
- Evaluation test
- Export test
To run tests:
# cd to the dir where this README.md is placed (text_recognition)
# if you are in the root of the training_extensions repo:
cd misc/pytorch_toolkit_text_recognition
# activate venv and run tests:
python tests/test_train.py
python tests/test_eval.py
python tests/test_export.py