Netout

The pylaia-htr-netout command can be used to dump the features extracted by PyLaia for a set of text-lines. To know more about the options of this command, use pylaia-htr-netout --help.

This command was initially designed to combine PyLaia and Kaldi. Since December 2022, combining PyLaia with language models can be achieved more easily by building a language model with KenLM and predicting with pylaia-htr-decode-ctc.

Purpose

This command outputs the feature matrix and lattice computed by PyLaia in Kaldi format for a given dataset.

It requires:

The files generated by this command are designed to combine PyLaia and Kaldi, but could also be used to predict with a custom decoder.

Parameters

The full list of parameters is detailed in this section.

General parameters

Parameter Description Type Default

img_list

Positional argument. File containing the names of the images to decode (one image per line).

str

img_dirs

Directories containing line images.

str

config

Path to a JSON configuration file

json

Common parameters

Name Description Type Default

common.train_path

Directory where the model will be saved

str

.

common.model_filename

Filename of the model.

str

model

common.experiment_dirname

Directory name of the experiment.

experiment

74565

common.checkpoint

Checkpoint to load. Must be a filepath, a filename, a glob pattern or None (in this case, the best checkpoint will be loaded). Note that the checkpoint will be searched in common.experiment_dirname, unless you provide an absolute filepath.

int

None

Data arguments

Name Description Type Default

data.batch_size

Batch size.

int

8

data.color_mode

Color mode. Must be either L, RGB or RGBA.

ColorMode

ColorMode.L

Netout arguments

Name Description Type Default

netout.output_transform

Transformation to apply at the end of the model. Should be softmax or log_softmax.

str

None

netout.matrix

Path to the output file containing a list of keys (image ids) and values (output matrix where rows represents timesteps and columns CTC labels). This file can be directly used with Kaldi.

Optional[str]

None

netout.lattice

Path to the output file containing containing a list of keys (image ids) and values (lattices representing the CTC output). This file can be directly used with Kaldi.

Optional[str]

None

netout.digits

Number of digits to be used for formatting

int

10

Logging arguments

Name Description Type Default

logging.fmt

Logging format.

str

%(asctime)s %(levelname)s %(name)s] %(message)s

logging.level

Logging level. Should be in

  • NOTSET

  • DEBUG

  • INFO

  • WARNING

  • ERROR

  • CRITICAL

Level

INFO

logging.filepath

Filepath for the logs file. Can be a filepath or a filename to be created in train_path/experiment_dirname

Optional[str]

logging.overwrite

Whether to overwrite the logfile or to append.

bool

False

logging.to_stderr_level

If filename is set, use this to log also to stderr at the given level.

Level

ERROR

Trainer arguments

Pytorch Lightning Trainer flags can also be set using the --trainer argument. See the documentation.

This flag is mostly useful to define whether to predict on CPU or GPU.

  • --trainer.gpus 0 to run on CPU,

  • --trainer.gpus n to run on n GPUs (use with --training.auto_select True for auto-selection),

  • --trainer.gpus -1 to run on all GPUs.

Examples

Dumping PyLaia’s features can be done using command-line arguments or a YAML configuration file. Note that CLI arguments override the values from the configuration file.

Dumping features from a model from Hugging Face

First, clone a trained model from Hugging Face:

git clone https://huggingface.co/Teklia/pylaia-huginmunin

List image names in img_list.txt:

docs/assets/219007024-f45433e7-99fd-43b0-bce6-93f63fa72a8f
docs/assets/219008758-c0097bb4-c55a-4652-ad2e-bba350bee0e4

Dump PyLaia’s output with:

pylaia-htr-netout --common.experiment_dirname pylaia-huginmunin/ \
                  --common.model_filename pylaia-huginmunin/model \
                  --netout.matrix matrix.txt \
                  --netout.lattice lattice.txt \
                  --img_dir [docs/assets] \
                  img_list.txt

Output files will be written in --common.experiment_dirname:

├── pylaia-huginmunin/
    ├── matrix.txt
    └── lattice.txt

Dumping features using a YAML configuration file

Run the following command to dump PyLaia’s output:

pylaia-htr-decode-ctc --config config_netout.yaml

With the following configuration file:

common:
  experiment_dirname: pylaia-huginmunin
  model_filename: pylaia-huginmunin/model
img_list: img_list.txt
img_dirs:
  - docs/assets/
netout:
  matrix: matrix.txt
  lattice: lattice.txt

Output files will be written in --common.experiment_dirname:

├── pylaia-huginmunin/
    ├── matrix.txt
    └── lattice.txt