API Reference

This reference is automatically generated from the docstrings in the kraken source code.

High-Level API

These modules provide the main entry points for using kraken programmatically.

kraken.containers

kraken.containers

Container classes replacing the old dictionaries returned by kraken’s functional blocks.

class kraken.containers.BBoxLine

Bounding box-type line record.

A container class for a single line in axis-aligned bounding box format, optionally containing a transcription, tags, or associated regions.

bbox

tuple in form (xmin, ymin, xmax, ymax) defining the bounding box.

text_direction

Sets the principal orientation (of the line) and reading direction (of the document).

bbox: tuple[int, int, int, int] | None = None
text_direction: Literal['horizontal-lr', 'horizontal-rl', 'vertical-lr', 'vertical-rl'] = 'horizontal-lr'
to_baseline(topline=False)

Casts this bounding box line record into a BaselineLine.

The boundary of the returned line is the four corners of bbox as a closed 5-point polygon. The synthesized baseline is a two-point segment along the reading axis, inset 25% of the bbox extent along the perpendicular axis so it sits inside the selected quadrant rather than on a bbox edge. Endpoint order matches the reading direction encoded in self.text_direction.

Parameters:

topline (Optional[bool]) –

Baseline placement convention:

  • False (default, standard baseline): lower quadrant; bottom for horizontal, right for vertical-rl, left for vertical-lr.

  • True (hanging baseline): upper quadrant; top for horizontal, left for vertical-rl, right for vertical-lr.

  • None (centerline): centered along the perpendicular axis.

Returns:

A new BaselineLine. This instance is not modified.

Return type:

BaselineLine

type: str = 'bbox'
class kraken.containers.BBoxOCRRecord(prediction, cuts, confidences, line, base_dir=None, display_order=True, logits=None, image=None)

A record object containing the recognition result of a single line in bbox format.

Parameters:
  • prediction (str)

  • cuts (list[tuple[tuple[int, int], tuple[int, int], tuple[int, int], tuple[int, int]]])

  • confidences (list[float])

  • line (Union[BBoxLine, dict[str, Any]])

  • base_dir (Optional[Literal['L', 'R']])

  • display_order (bool)

  • logits (Optional[torch.FloatTensor])

  • image (Optional[PIL.Image.Image])

type

‘bbox’ to indicate a bounding box record

prediction

The text predicted by the network as one continuous string.

Return type:

str

cuts

The absolute bounding polygons for each code point in prediction as a list of 4-tuples ((x0, y0), (x1, y0), (x1, y1), (x0, y1)).

Return type:

list

confidences

A list of floats indicating the confidence value of each code point.

Return type:

list[float]

base_dir

An optional string defining the base direction (also called paragraph direction) for the BiDi algorithm. Valid values are ‘L’ or ‘R’. If None is given the default auto-resolution will be used.

display_order

Flag indicating the order of the code points in the prediction. In display order (True) the n-th code point in the string corresponds to the n-th leftmost code point, in logical order (False) the n-th code point corresponds to the n-th read code point. See [UAX #9](https://unicode.org/reports/tr9) for more details.

Parameters:

base_dir (Optional[Literal['L', 'R']])

Return type:

BBoxOCRRecord

logits

The logits for the prediction.

image

The line image used to produce the prediction.

Notes

When slicing the record the behavior of the cuts is changed from earlier versions of kraken. Instead of returning per-character bounding polygons a single polygons section of the line bounding polygon starting at the first and extending to the last code point emitted by the network is returned. This aids numerical stability when computing aggregated bounding polygons such as for words. Individual code point bounding polygons are still accessible through the cuts attribute or by iterating over the record code point by code point.

base_dir = None
display_order(base_dir=None)

Returns the OCR record in Unicode display order, i.e. ordered from left to right inside the line.

Parameters:

base_dir (Optional[Literal['L', 'R']]) – An optional string defining the base direction (also called paragraph direction) for the BiDi algorithm. Valid values are ‘L’ or ‘R’. If None is given the default auto-resolution will be used.

Return type:

BBoxOCRRecord

logical_order(base_dir=None)

Returns the OCR record in Unicode logical order, i.e. in the order the characters in the line would be read by a human.

Parameters:

base_dir (Optional[Literal['L', 'R']]) – An optional string defining the base direction (also called paragraph direction) for the BiDi algorithm. Valid values are ‘L’ or ‘R’. If None is given the default auto-resolution will be used.

Return type:

BBoxOCRRecord

type = 'bbox'
class kraken.containers.BaselineLine

Baseline-type line record.

A container class for a single line in baseline + bounding polygon format, optionally containing a transcription, tags, or associated regions.

baseline

list of tuples (x_n, y_n) defining the baseline.

boundary

list of tuples (x_n, y_n) defining the bounding polygon of the line. The first and last points should be identical.

baseline: list[tuple[int, int]] | None = None
boundary: list[tuple[int, int]] | None = None
to_bbox(text_direction='horizontal-lr')

Casts this baseline line record into an axis-aligned BBoxLine.

The bounding box is the min/max extent of the bounding polygon (boundary). If boundary is not set, the baseline polyline is used instead. All shared ocr_line fields are carried over.

Parameters:

text_direction (Literal['horizontal-lr', 'horizontal-rl', 'vertical-lr', 'vertical-rl']) – Principal orientation / reading direction to assign to the returned BBoxLine. BaselineLine does not carry an orientation itself, so the caller must supply one (typically the enclosing Segmentation’s text_direction).

Returns:

A new BBoxLine. This instance is not modified.

Return type:

BBoxLine

type: str = 'baselines'
class kraken.containers.BaselineOCRRecord(prediction, cuts, confidences, line, base_dir=None, display_order=True, logits=None, image=None)

A record object containing the recognition result of a single line in baseline format.

Parameters:
  • prediction (str)

  • cuts (list[tuple[int, int]])

  • confidences (list[float])

  • line (Union[BaselineLine, dict[str, Any]])

  • base_dir (Optional[Literal['L', 'R']])

  • display_order (bool)

  • logits (Optional[torch.FloatTensor])

  • image (Optional[PIL.Image.Image])

type

‘baselines’ to indicate a baseline record

prediction

The text predicted by the network as one continuous string.

Return type:

str

cuts

The absolute bounding polygons for each code point in prediction as a list of tuples [(x0, y0), (x1, y2), …].

Return type:

tuple

confidences

A list of floats indicating the confidence value of each code point.

Return type:

list[float]

base_dir

An optional string defining the base direction (also called paragraph direction) for the BiDi algorithm. Valid values are ‘L’ or ‘R’. If None is given the default auto-resolution will be used.

display_order

Flag indicating the order of the code points in the prediction. In display order (True) the n-th code point in the string corresponds to the n-th leftmost code point, in logical order (False) the n-th code point corresponds to the n-th read code point. See [UAX #9](https://unicode.org/reports/tr9) for more details.

Parameters:

base_dir (Optional[Literal['L', 'R']])

Return type:

BaselineOCRRecord

logits

The logits for the prediction.

image

The line image used to produce the prediction.

Notes

When slicing the record the behavior of the cuts is changed from earlier versions of kraken. Instead of returning per-character bounding polygons a single polygons section of the line bounding polygon starting at the first and extending to the last code point emitted by the network is returned. This aids numerical stability when computing aggregated bounding polygons such as for words. Individual code point bounding polygons are still accessible through the cuts attribute or by iterating over the record code point by code point.

base_dir = None
property cuts: tuple
Return type:

tuple

display_order(base_dir=None)

Returns the OCR record in Unicode display order, i.e. ordered from left to right inside the line.

Parameters:

base_dir (Optional[Literal['L', 'R']]) – An optional string defining the base direction (also called paragraph direction) for the BiDi algorithm. Valid values are ‘L’ or ‘R’. If None is given the default auto-resolution will be used.

Return type:

BaselineOCRRecord

logical_order(base_dir=None)

Returns the OCR record in Unicode logical order, i.e. in the order the characters in the line would be read by a human.

Parameters:

base_dir (Optional[Literal['L', 'R']]) – An optional string defining the base direction (also called paragraph direction) for the BiDi algorithm. Valid values are ‘L’ or ‘R’. If None is given the default auto-resolution will be used.

Return type:

BaselineOCRRecord

type = 'baselines'
class kraken.containers.ProcessingStep

A processing step in the recognition pipeline.

id

Unique identifier

category

Category of processing step that has been performed.

description

Natural-language description of the process.

settings

dict describing the parameters of the processing step.

category: Literal['preprocessing', 'processing', 'postprocessing']
description: str
id: str
settings: dict[str, dict | str | float | int | bool]
class kraken.containers.Region

Container class of a single polygonal region.

id

Unique identifier

boundary

list of tuples (x_n, y_n) defining the bounding polygon of the region. The first and last points should be identical.

imagename

Path to the image associated with the region.

tags

A dict mapping types to values.

boundary: list[tuple[int, int]]
id: str
imagename: str | os.PathLike | None = None
language: list[str] | None = None
tags: dict[str, list[dict[str, str]]] | None = None
class kraken.containers.Segmentation

A container class for segmentation or recognition results.

In order to allow easy JSON de-/serialization, nested classes for lines (BaselineLine/BBoxLine) and regions (Region) are reinstantiated from their dictionaries.

type

Field indicating if baselines (kraken.containers.BaselineLine) or bbox (kraken.containers.BBoxLine) line records are in the segmentation.

imagename

Path to the image associated with the segmentation.

text_direction

Sets the principal orientation (of the line), i.e. horizontal/vertical, and reading direction (of the document), i.e. lr/rl.

script_detection

Flag indicating if the line records have tags.

lines

list of line records. Records are expected to be in a valid reading order.

regions

dict mapping types to lists of regions.

line_orders

list of alternative reading orders for the segmentation. Each reading order is a list of line indices.

imagename: str | os.PathLike
language: list[str] | None = None
line_orders: list[list[int]] | None = None
lines: list[BaselineLine | BBoxLine] | None = None
regions: dict[str, list[Region]] | None = None
script_detection: bool
text_direction: Literal['horizontal-lr', 'horizontal-rl', 'vertical-lr', 'vertical-rl']
to_baselines(topline=False)

Returns a new Segmentation of type 'baselines'.

If this segmentation is already of type 'baselines' a deep copy is returned so the caller always gets an independent instance. Otherwise each contained BBoxLine is cast via BBoxLine.to_baseline(). regions, line_orders, and all other fields pass through unchanged (deep-copied).

Parameters:

topline (Optional[bool]) – Baseline placement convention passed through to BBoxLine.to_baseline() for each contained line.

Return type:

Segmentation

to_bbox()

Returns a new Segmentation of type 'bbox'.

If this segmentation is already of type 'bbox' a deep copy is returned so the caller always gets an independent instance. Otherwise each contained BaselineLine is cast via BaselineLine.to_bbox(), passing this segmentation’s text_direction through. regions, line_orders, and all other fields pass through unchanged (deep-copied).

Return type:

Segmentation

type: Literal['baselines', 'bbox']
kraken.containers.compute_polygon_section(baseline, boundary, dist1, dist2)

Given a baseline, polygonal boundary, and two points on the baseline return the rectangle formed by the orthogonal cuts on that baseline segment. The resulting polygon is not garantueed to have a non-zero area.

The distance can be larger than the actual length of the baseline if the baseline endpoints are inside the bounding polygon. In that case the baseline will be extrapolated to the polygon edge.

Parameters:
  • baseline (collections.abc.Sequence[tuple[int, int]]) – A polyline ((x1, y1), …, (xn, yn))

  • boundary (collections.abc.Sequence[tuple[int, int]]) – A bounding polygon around the baseline (same format as baseline). Last and first point are automatically connected.

  • dist1 (int) – Absolute distance along the baseline of the first point.

  • dist2 (int) – Absolute distance along the baseline of the second point.

Returns:

A sequence of polygon points.

Return type:

tuple[tuple[int, int]]

kraken.containers.get_display_map(text, base_dir=None)

Reorders a string from logical order into display order according to the Unicode 17.0.0 bidirectional algorithm. The text is treated as a single paragraph.

Parameters:
  • text (str) – Text in logical order.

  • base_dir (Optional[Literal['L', 'R']]) – Base (paragraph) direction, ‘L’ or ‘R’. When None the direction is resolved from the first strong character (rules P2/P3).

Returns:

A tuple of the reordered string and the index of each of its characters in text. Mirrored characters (e.g. parentheses in right-to-left runs) are replaced by their mirror image; explicit directional formatting characters (LRE, RLE, LRO, RLO, PDF, LRI, RLI, FSI, PDI) and characters with bidirectional class BN are removed from the output.

Return type:

tuple[str, list[int]]

class kraken.containers.ocr_line

A line record.

id

Unique identifier

text

Transcription of this line.

base_dir

An optional string defining the base direction (also called paragraph direction) for the BiDi algorithm. Valid values are ‘L’ or ‘R’. If None is given the default auto-resolution will be used.

imagename

Path to the image associated with the line.

tags

A dict mapping types to values.

split

Defines whether this line is in the train, validation, or test set during training.

regions

A list of identifiers of regions the line is associated with.

languages

A list of identifiers of regions the line is associated with.

base_dir: Literal['L', 'R'] | None = None
id: str
imagename: str | os.PathLike | None = None
language: list[str] | None = None
regions: list[str] | None = None
split: Literal['train', 'validation', 'test'] | None = None
tags: dict[str, list[dict[str, str]]] | None = None
text: str | None = None
class kraken.containers.ocr_record(prediction, cuts, confidences, display_order=True, logits=None, image=None)

A record object containing the recognition result of a single line

Parameters:
  • prediction (str)

  • cuts (list[Union[tuple[int, int], tuple[tuple[int, int], tuple[int, int], tuple[int, int], tuple[int, int]]]])

  • confidences (list[float])

  • display_order (bool)

  • logits (Optional[torch.FloatTensor])

  • image (Optional[PIL.Image.Image])

base_dir = None
property confidences: list[float]
Return type:

list[float]

property cuts: list
Return type:

list

abstractmethod display_order(base_dir)
Return type:

ocr_record

image = None
abstractmethod logical_order(base_dir)
Return type:

ocr_record

logits = None
property prediction: str
Return type:

str

abstract property type
kraken.containers.precompute_polygon_sections(baseline, boundary, cut_pairs)

Batch-precompute polygon sections for all characters, amortizing the expensive baseline extension and cumulative distance computation.

Parameters:
  • baseline (collections.abc.Sequence[tuple[int, int]]) – A polyline ((x1, y1), …, (xn, yn))

  • boundary (collections.abc.Sequence[tuple[int, int]]) – A bounding polygon around the baseline.

  • cut_pairs (list[tuple[int, int]]) – List of (dist1, dist2) tuples for each character.

Returns:

  • char_polygons is a list of polygon tuples, one per character

  • intersection_cache maps clamped distance values to raw _test_intersect result arrays (or None on failure)

  • bl_length is the total baseline length after extension

Return type:

A tuple of (char_polygons, intersection_cache, bl_length) where

kraken.lib.xml

ALTO/Page data loaders for segmentation training

class kraken.lib.xml.Segmentation

A container class for segmentation or recognition results.

In order to allow easy JSON de-/serialization, nested classes for lines (BaselineLine/BBoxLine) and regions (Region) are reinstantiated from their dictionaries.

type

Field indicating if baselines (kraken.containers.BaselineLine) or bbox (kraken.containers.BBoxLine) line records are in the segmentation.

imagename

Path to the image associated with the segmentation.

text_direction

Sets the principal orientation (of the line), i.e. horizontal/vertical, and reading direction (of the document), i.e. lr/rl.

script_detection

Flag indicating if the line records have tags.

lines

list of line records. Records are expected to be in a valid reading order.

regions

dict mapping types to lists of regions.

line_orders

list of alternative reading orders for the segmentation. Each reading order is a list of line indices.

imagename: str | os.PathLike
language: list[str] | None = None
line_orders: list[list[int]] | None = None
lines: list[BaselineLine | BBoxLine] | None = None
regions: dict[str, list[Region]] | None = None
script_detection: bool
text_direction: Literal['horizontal-lr', 'horizontal-rl', 'vertical-lr', 'vertical-rl']
to_baselines(topline=False)

Returns a new Segmentation of type 'baselines'.

If this segmentation is already of type 'baselines' a deep copy is returned so the caller always gets an independent instance. Otherwise each contained BBoxLine is cast via BBoxLine.to_baseline(). regions, line_orders, and all other fields pass through unchanged (deep-copied).

Parameters:

topline (Optional[bool]) – Baseline placement convention passed through to BBoxLine.to_baseline() for each contained line.

Return type:

Segmentation

to_bbox()

Returns a new Segmentation of type 'bbox'.

If this segmentation is already of type 'bbox' a deep copy is returned so the caller always gets an independent instance. Otherwise each contained BaselineLine is cast via BaselineLine.to_bbox(), passing this segmentation’s text_direction through. regions, line_orders, and all other fields pass through unchanged (deep-copied).

Return type:

Segmentation

type: Literal['baselines', 'bbox']
class kraken.lib.xml.XMLPage(filename, filetype='xml', linetype='baselines')

Parses XML facsimiles in ALTO or PageXML format.

The parser is able to deal with most (but not all) features supported by those standards. In particular, any data below the line level is discarded.

Parameters:
  • filename (Union[str, os.PathLike]) – Path to the XML file

  • filetype (Literal['xml', 'alto', 'page']) – Selector for explicit subparser choice.

  • linetype (Literal['baselines', 'bbox']) – Parse line data as baselines or bounding box type.

type

Either ‘baselines’ or ‘bbox’.

imagename

Path to the image to the XML file.

image_size

Size of the image as a (width, height) tuple

has_tags

Indicates if the source document contains tag information

has_splits

Indicates if the source document contains explicit training splits

base_dir: Literal['L', 'R'] | None = None
filename
filetype = 'xml'
get_lines_by_split(split)
Parameters:

split (Literal['train', 'validation', 'test'])

get_lines_by_tag(key, value)
get_sorted_lines(ro='line_implicit')

Returns ordered baselines from particular reading order.

get_sorted_lines_by_region(region, ro='line_implicit')

Returns ordered lines in region.

get_sorted_regions(ro='region_implicit')

Returns ordered regions from particular reading order.

has_splits: bool = False
has_tags: bool = False
image_size: tuple[int, int] = None
imagename: os.PathLike = None
property lines
property reading_orders
property regions
property splits
property tags
to_container()

Returns a Segmentation object.

Return type:

kraken.containers.Segmentation

type: Literal['baselines', 'bbox'] = 'baselines'
kraken.lib.xml.alto_regions
kraken.lib.xml.flatten_order_to_lines(raw_order, lines_dict, region_ids, line_implicit_order, string_to_line_map=None, missing_region_ids=None)

Flatten a raw reading order (list of IDs) to line-level.

For each ID: - Line ID: append directly - Region ID: expand to contained lines using implicit order - String ID (ALTO only): map to parent TextLine, deduplicate consecutive - Unknown ID: log warning, skip

Parameters:
  • raw_order (list[str])

  • lines_dict (dict)

  • region_ids (set[str])

  • line_implicit_order (list[str])

  • string_to_line_map (Optional[dict[str, str]])

  • missing_region_ids (Optional[set[str]])

Return type:

list[str]

kraken.lib.xml.flatten_order_to_regions(raw_order, lines_dict, region_ids, string_to_line_map=None, missing_region_ids=None)

Flatten a raw reading order (list of IDs) to region-level.

For each ID: - Region ID: append directly - Line ID: resolve to parent region, deduplicate consecutive - String ID (ALTO only): resolve to parent line then region, deduplicate - Unknown ID: log warning, skip

Parameters:
  • raw_order (list[str])

  • lines_dict (dict)

  • region_ids (set[str])

  • string_to_line_map (Optional[dict[str, str]])

  • missing_region_ids (Optional[set[str]])

Return type:

list[str]

kraken.lib.xml.logger
kraken.lib.xml.page_regions
kraken.lib.xml.parse_alto(doc, filename, linetype)

Parse an ALTO XML document.

Parameters:
  • doc – Parsed lxml document.

  • filename – Path to the XML file (for error messages and resolving image paths).

  • linetype – ‘baselines’ or ‘bbox’.

Returns:

imagename, image_size, regions, lines, orders, tag_set,

raw_orders, string_to_line_map

Return type:

dict with keys

kraken.lib.xml.parse_page(doc, filename, linetype)

Parse a PageXML document.

Parameters:
  • doc – Parsed lxml document.

  • filename – Path to the XML file (for error messages and resolving image paths).

  • linetype – ‘baselines’ or ‘bbox’.

Returns:

imagename, image_size, regions, lines, orders, tag_set,

raw_orders

Return type:

Dict with keys

kraken.lib.xml.validate_and_clean_order(flat_order, valid_ids)

Validate a flattened order.

Checks: - All IDs exist in valid_ids - No duplicate IDs (indicates circular reference)

Returns:

(cleaned_order, is_valid)

Parameters:
  • flat_order (list[str])

  • valid_ids (set[str])

Return type:

tuple[list[str], bool]

kraken.tasks

class kraken.tasks.ForcedAlignmentTaskModel(models)

A wrapper for forced alignment of CTC output.

Using a text recognition model the existing transcription of a page will be aligned to the character positions of the network output.

Raises:

ValueError – Is raised when the model type is not a sequence recognizer.

Parameters:

models (list[torch.nn.Module])

classmethod load_model(path)
Parameters:

path (Union[str, os.PathLike])

net
one_channel_mode
predict(im, segmentation, config)

Aligns the transcription of an image with the output of the text recognition model, producing approximate character locations.

When the character sets of transcription and recognition model differ, the affected code points in the furnished transcription will silently be ignored. In case inference fails on a line, a record without cuts/confidences is returned.

Parameters:
Returns:

A single segmentation that contains the aligned ocr_record objects.

Return type:

kraken.containers.Segmentation

Example

>>> from PIL import Image
>>> from kraken.tasks import ForcedAlignmentTaskModel
>>> from kraken.containers import Segmentation, BaselineLine
>>> from kraken.configs import RecognitionInferenceConfig
>>> # Assume `model.mlmodel` is a recognition model
>>> model = ForcedAlignmentTaskModel.load_model('model.mlmodel')
>>> im = Image.open('image.png')
>>> # Create a dummy segmentation with a line and a transcription
>>> line = BaselineLine(baseline=[(0,0), (100,0)], boundary=[(0,-10), (100,-10), (100,10), (0,10)], text='Hello World')
>>> segmentation = Segmentation(lines=[line])
>>> config = RecognitionInferenceConfig()
>>> aligned_segmentation = model.predict(im, segmentation, config)
>>> record = aligned_segmentation.lines[0]:
>>> print(record.prediction)
>>> print(record.cuts)
resolve_config(config)

Rebuilds a configuration into the class declared by the wrapped model (inference_config_cls), applying the values in the configuration’s extra_options on top.

Raises:
  • ValueError – When an extra option is not a field of the model’s

  • configuration class. –

Parameters:

config (kraken.configs.RecognitionInferenceConfig)

Return type:

kraken.configs.RecognitionInferenceConfig

seg_type
validate_extra_options(config_or_options)

Checks that all extra options are accepted by the wrapped model’s configuration class.

Parameters:

config_or_options – A configuration object or a plain option dict.

Raises:
  • ValueError – When an option is not a field of the model’s

  • configuration class. –

Return type:

None

class kraken.tasks.RecognitionTaskModel(models)

A wrapper for a model performing a recognition task.

A recognition task is the process of transcribing a line of text from an image. This class provides a high-level interface for running a recognition model on an image, given a segmentation.

Raises:

ValueError – Is raised when the model type is not a sequence recognizer.

Parameters:

models (list[torch.nn.Module])

classmethod load_model(path)
Parameters:

path (Union[str, os.PathLike])

net
one_channel_mode
predict(im, segmentation, config)

Inference using a recognition model.

Parameters:
Yields:

One ocr_record for each line.

Return type:

collections.abc.Generator[kraken.containers.ocr_record, None, None]

Example

>>> from PIL import Image
>>> from kraken.tasks import RecognitionTaskModel
>>> from kraken.containers import Segmentation
>>> from kraken.configs import RecognitionInferenceConfig
>>> model = RecognitionTaskModel.load_model('model.mlmodel')
>>> im = Image.open('image.png')
>>> segmentation = Segmentation(...)
>>> config = RecognitionInferenceConfig()
>>> for record in model.predict(im, segmentation, config):
...     print(record.prediction)
resolve_config(config)

Rebuilds a configuration into the class declared by the wrapped model (inference_config_cls), applying the values in the configuration’s extra_options on top.

Raises:
  • ValueError – When an extra option is not a field of the model’s

  • configuration class. –

Parameters:

config (kraken.configs.RecognitionInferenceConfig)

Return type:

kraken.configs.RecognitionInferenceConfig

seg_type
validate_extra_options(config_or_options)

Checks that all extra options are accepted by the wrapped model’s configuration class.

Parameters:

config_or_options – A configuration object or a plain option dict.

Raises:
  • ValueError – When an option is not a field of the model’s

  • configuration class. –

Return type:

None

class kraken.tasks.SegmentationTaskModel(models)

A wrapper class collecting one or more models that perform segmentation.

A segmentation task is the process of identifying the regions and lines of text in an image. This class provides a high-level interface for running segmentation models on an image.

It deals with the following tasks:
  • region segmentation

  • line detection

  • line reading order

If no neural reading order model is part of the model collection handed to the task, a simple heuristic will be used.

Parameters:

models (list[torch.nn.Module]) – A collection of models performing segmentation tasks.

Raises:

ValueError – Is raised when no segmentation models are in the model list.

classmethod load_model(path=None)

Loads a collection from layout analysis models from the given file path.

If no path is provided, the default BLLA segmentation model will be loaded.

Parameters:

path (Optional[Union[str, os.PathLike]]) – Path to model weights file.

Return type:

SegmentationTaskModel

predict(im, config)

Runs all models associated with the task to produce a segmentation for the input page.

Parameters:
  • im (PIL.Image.Image) – Input image with an arbitrary color mode and size

  • config (kraken.configs.SegmentationInferenceConfig) – A configuration object for the segmentation task, such as the batch size and the precision.

Returns:

A single Segmentation object that contains the merged output of all associated segmentation models.

Return type:

kraken.containers.Segmentation

Example

>>> from PIL import Image
>>> from kraken.tasks import SegmentationTaskModel
>>> from kraken.configs import SegmentationInferenceConfig
>>> model = SegmentationTaskModel.load_model()
>>> im = Image.open('image.png')
>>> config = SegmentationInferenceConfig()
>>> segmentation = model.predict(im, config)
ro_models
seg_models
validate_extra_options(config_or_options)

Checks that all extra options are accepted by the configuration class of at least one wrapped model.

Options known to only some of the wrapped models are applied to those models only.

Parameters:

config_or_options – A configuration object or a plain option dict.

Raises:
  • ValueError – When an option is not a field of any wrapped model’s

  • configuration class. –

Return type:

None

kraken.train

class kraken.train.BLLASegmentationDataModule(data_config)

A DataModule standardizes the training, val, test splits, data preparation and transforms. The main advantage is consistent data splits, data preparation and transforms across models.

Example:

import lightning as L
import torch.utils.data as data
from lightning.pytorch.demos.boring_classes import RandomDataset

class MyDataModule(L.LightningDataModule):
    def prepare_data(self):
        # download, IO, etc. Useful with shared filesystems
        # only called on 1 GPU/TPU in distributed
        ...

    def setup(self, stage):
        # make assignments here (val/train/test split)
        # called on every process in DDP
        dataset = RandomDataset(1, 100)
        self.train, self.val, self.test = data.random_split(
            dataset, [80, 10, 10], generator=torch.Generator().manual_seed(42)
        )

    def train_dataloader(self):
        return data.DataLoader(self.train)

    def val_dataloader(self):
        return data.DataLoader(self.val)

    def test_dataloader(self):
        return data.DataLoader(self.test)

    def on_exception(self, exception):
        # clean up state after the trainer faced an exception
        ...

    def teardown(self):
        # clean up state after the trainer stops, delete files...
        # called on every process in DDP
        ...
Parameters:

data_config (kraken.configs.BLLASegmentationTrainingDataConfig)

setup(stage=None)

Called at the beginning of fit (train + validate), validate, test, or predict. This is a good hook when you need to build models dynamically or adjust something about them. This hook is called on every process when using DDP.

Parameters:

stage (str) – either 'fit', 'validate', 'test', or 'predict'

Example:

class LitModel(...):
    def __init__(self):
        self.l1 = None

    def prepare_data(self):
        download_data()
        tokenize()

        # don't do this
        self.something = else

    def setup(self, stage):
        data = load_data(...)
        self.l1 = nn.Linear(28, data.num_classes)
test_dataloader()

An iterable or collection of iterables specifying test samples.

For more information about multiple dataloaders, see this section.

For data processing use the following pattern:

  • download in prepare_data()

  • process and split in setup()

However, the above are only necessary for distributed processing.

Warning

do not assign state in prepare_data

Note

Lightning tries to add the correct sampler for distributed and arbitrary hardware. There is no need to set it yourself.

Note

If you don’t need a test dataset and a test_step(), you don’t need to implement this method.

train_dataloader()

An iterable or collection of iterables specifying training samples.

For more information about multiple dataloaders, see this section.

The dataloader you return will not be reloaded unless you set :paramref:`~lightning.pytorch.trainer.trainer.Trainer.reload_dataloaders_every_n_epochs` to a positive integer.

For data processing use the following pattern:

  • download in prepare_data()

  • process and split in setup()

However, the above are only necessary for distributed processing.

Warning

do not assign state in prepare_data

Note

Lightning tries to add the correct sampler for distributed and arbitrary hardware. There is no need to set it yourself.

val_dataloader()

An iterable or collection of iterables specifying validation samples.

For more information about multiple dataloaders, see this section.

The dataloader you return will not be reloaded unless you set :paramref:`~lightning.pytorch.trainer.trainer.Trainer.reload_dataloaders_every_n_epochs` to a positive integer.

It’s recommended that all data downloads and preparation happen in prepare_data().

  • fit()

  • validate()

  • prepare_data()

  • setup()

Note

Lightning tries to add the correct sampler for distributed and arbitrary hardware There is no need to set it yourself.

Note

If you don’t need a validation dataset and a validation_step(), you don’t need to implement this method.

class kraken.train.BLLASegmentationModel(config, model=None)

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call to(), etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:

training (bool) – Boolean represents whether this module is in training or evaluation mode.

Parameters:
  • config (kraken.configs.BLLASegmentationTrainingConfig)

  • model (Optional[kraken.models.BaseModel])

configure_optimizers()

Choose what optimizers and learning-rate schedulers to use in your optimization. Normally you’d need one. But in the case of GANs or similar you might have multiple. Optimization with multiple optimizers only works in the manual optimization mode.

Returns:

Any of these 6 options.

  • Single optimizer.

  • List or Tuple of optimizers.

  • Two lists - The first list has multiple optimizers, and the second has multiple LR schedulers (or multiple lr_scheduler_config).

  • Dictionary, with an "optimizer" key, and (optionally) a "lr_scheduler" key whose value is a single LR scheduler or lr_scheduler_config.

  • None - Fit will run without any optimizer.

The lr_scheduler_config is a dictionary which contains the scheduler and its associated configuration. The default configuration is shown below.

lr_scheduler_config = {
    # REQUIRED: The scheduler instance
    "scheduler": lr_scheduler,
    # The unit of the scheduler's step size, could also be 'step'.
    # 'epoch' updates the scheduler on epoch end whereas 'step'
    # updates it after a optimizer update.
    "interval": "epoch",
    # How many epochs/steps should pass between calls to
    # `scheduler.step()`. 1 corresponds to updating the learning
    # rate after every epoch/step.
    "frequency": 1,
    # Metric to monitor for schedulers like `ReduceLROnPlateau`
    "monitor": "val_loss",
    # If set to `True`, will enforce that the value specified 'monitor'
    # is available when the scheduler is updated, thus stopping
    # training if not found. If set to `False`, it will only produce a warning
    "strict": True,
    # If using the `LearningRateMonitor` callback to monitor the
    # learning rate progress, this keyword can be used to specify
    # a custom logged name
    "name": None,
}

When there are schedulers in which the .step() method is conditioned on a value, such as the torch.optim.lr_scheduler.ReduceLROnPlateau scheduler, Lightning requires that the lr_scheduler_config contains the keyword "monitor" set to the metric name that the scheduler should be conditioned on.

Metrics can be made available to monitor by simply logging it using self.log('metric_to_track', metric_val) in your LightningModule.

Note

Some things to know:

  • Lightning calls .backward() and .step() automatically in case of automatic optimization.

  • If a learning rate scheduler is specified in configure_optimizers() with key "interval" (default “epoch”) in the scheduler configuration, Lightning will call the scheduler’s .step() method automatically in case of automatic optimization.

  • If you use 16-bit precision (precision=16), Lightning will automatically handle the optimizer.

  • If you use torch.optim.LBFGS, Lightning handles the closure function automatically for you.

  • If you use multiple optimizers, you will have to switch to ‘manual optimization’ mode and step them yourself.

  • If you need to control how often the optimizer steps, override the optimizer_step() hook.

criterion
dice_weight
example_input_array

The example input array is a specification of what the module can consume in the forward() method. The return type is interpreted as follows:

  • Single tensor: It is assumed the model takes a single argument, i.e., model.forward(model.example_input_array)

  • Tuple: The input array should be interpreted as a sequence of positional arguments, i.e., model.forward(*model.example_input_array)

  • Dict: The input array represents named keyword arguments, i.e., model.forward(**model.example_input_array)

forward(x)

Same as torch.nn.Module.forward().

Parameters:
  • *args – Whatever you decide to pass into the forward method.

  • **kwargs – Keyword arguments are also possible.

  • x (torch.Tensor)

Returns:

Your model’s output

Return type:

tuple[torch.Tensor, torch.Tensor]

on_test_epoch_end()

Called in the test loop at the very end of the epoch.

on_test_epoch_start()

Called in the test loop at the very beginning of the epoch.

on_validation_epoch_end()

Called in the validation loop at the very end of the epoch.

setup(stage=None)

Called at the beginning of fit (train + validate), validate, test, or predict. This is a good hook when you need to build models dynamically or adjust something about them. This hook is called on every process when using DDP.

Parameters:

stage (Optional[str]) – either 'fit', 'validate', 'test', or 'predict'

Example:

class LitModel(...):
    def __init__(self):
        self.l1 = None

    def prepare_data(self):
        download_data()
        tokenize()

        # don't do this
        self.something = else

    def setup(self, stage):
        data = load_data(...)
        self.l1 = nn.Linear(28, data.num_classes)
test_step(batch, batch_idx, test_dataloader=0)

Operates on a single batch of data from the test set. In this step you’d normally generate examples or calculate anything of interest such as accuracy.

Parameters:
  • batch – The output of your data iterable, normally a DataLoader.

  • batch_idx – The index of this batch.

  • dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)

Returns:

  • Tensor - The loss tensor

  • dict - A dictionary. Can include any keys, but must include the key 'loss'.

  • None - Skip to the next batch.

# if you have one test dataloader:
def test_step(self, batch, batch_idx): ...


# if you have multiple test dataloaders:
def test_step(self, batch, batch_idx, dataloader_idx=0): ...

Examples:

# CASE 1: A single test dataset
def test_step(self, batch, batch_idx):
    x, y = batch

    # implement your own
    out = self(x)
    loss = self.loss(out, y)

    # log 6 example images
    # or generated text... or whatever
    sample_imgs = x[:6]
    grid = torchvision.utils.make_grid(sample_imgs)
    self.logger.experiment.add_image('example_images', grid, 0)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    test_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs!
    self.log_dict({'test_loss': loss, 'test_acc': test_acc})

If you pass in multiple test dataloaders, test_step() will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.

# CASE 2: multiple test dataloaders
def test_step(self, batch, batch_idx, dataloader_idx=0):
    # dataloader_idx tells you which dataset this is.
    x, y = batch

    # implement your own
    out = self(x)

    if dataloader_idx == 0:
        loss = self.loss0(out, y)
    else:
        loss = self.loss1(out, y)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs separately for each dataloader
    self.log_dict({f"test_loss_{dataloader_idx}": loss, f"test_acc_{dataloader_idx}": acc})

Note

If you don’t need to test you don’t need to implement this method.

Note

When the test_step() is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of the test epoch, the model goes back to training mode and gradients are enabled.

training_step(batch, batch_idx)

Here you compute and return the training loss and some additional metrics for e.g. the progress bar or logger.

Parameters:
  • batch – The output of your data iterable, normally a DataLoader.

  • batch_idx – The index of this batch.

  • dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)

Returns:

  • Tensor - The loss tensor

  • dict - A dictionary which can include any keys, but must include the key 'loss' in the case of automatic optimization.

  • None - In automatic optimization, this will skip to the next batch (but is not supported for multi-GPU, TPU, or DeepSpeed). For manual optimization, this has no special meaning, as returning the loss is not required.

In this step you’d normally do the forward pass and calculate the loss for a batch. You can also do fancier things like multiple forward passes or something model specific.

Example:

def training_step(self, batch, batch_idx):
    x, y, z = batch
    out = self.encoder(x)
    loss = self.loss(out, x)
    return loss

To use multiple optimizers, you can switch to ‘manual optimization’ and control their stepping:

def __init__(self):
    super().__init__()
    self.automatic_optimization = False


# Multiple optimizers (e.g.: GANs)
def training_step(self, batch, batch_idx):
    opt1, opt2 = self.optimizers()

    # do training_step with encoder
    ...
    opt1.step()
    # do training_step with decoder
    ...
    opt2.step()

Note

When accumulate_grad_batches > 1, the loss returned here will be automatically normalized by accumulate_grad_batches internally.

validation_step(batch, batch_idx)

Operates on a single batch of data from the validation set. In this step you’d might generate examples or calculate anything of interest like accuracy.

Parameters:
  • batch – The output of your data iterable, normally a DataLoader.

  • batch_idx – The index of this batch.

  • dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)

Returns:

  • Tensor - The loss tensor

  • dict - A dictionary. Can include any keys, but must include the key 'loss'.

  • None - Skip to the next batch.

# if you have one val dataloader:
def validation_step(self, batch, batch_idx): ...


# if you have multiple val dataloaders:
def validation_step(self, batch, batch_idx, dataloader_idx=0): ...

Examples:

# CASE 1: A single validation dataset
def validation_step(self, batch, batch_idx):
    x, y = batch

    # implement your own
    out = self(x)
    loss = self.loss(out, y)

    # log 6 example images
    # or generated text... or whatever
    sample_imgs = x[:6]
    grid = torchvision.utils.make_grid(sample_imgs)
    self.logger.experiment.add_image('example_images', grid, 0)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs!
    self.log_dict({'val_loss': loss, 'val_acc': val_acc})

If you pass in multiple val dataloaders, validation_step() will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.

# CASE 2: multiple validation dataloaders
def validation_step(self, batch, batch_idx, dataloader_idx=0):
    # dataloader_idx tells you which dataset this is.
    x, y = batch

    # implement your own
    out = self(x)

    if dataloader_idx == 0:
        loss = self.loss0(out, y)
    else:
        loss = self.loss1(out, y)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs separately for each dataloader
    self.log_dict({f"val_loss_{dataloader_idx}": loss, f"val_acc_{dataloader_idx}": acc})

Note

If you don’t need to validate you don’t need to implement this method.

Note

When the validation_step() is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of validation, the model goes back to training mode and gradients are enabled.

class kraken.train.KrakenTrainer(enable_progress_bar=True, enable_summary=True, min_epochs=5, max_epochs=100, freeze_backbone=-1, pl_logger=None, log_dir=None, wandb_project='kraken', *args, **kwargs)
Parameters:
  • enable_progress_bar (bool)

  • enable_summary (bool)

  • min_epochs (int)

  • max_epochs (int)

  • pl_logger (Union[lightning.pytorch.loggers.logger.Logger, str, None])

  • log_dir (Optional[os.PathLike])

  • wandb_project (str)

fit(*args, **kwargs)

Runs the full optimization routine.

Parameters:
  • model – Model to fit.

  • train_dataloaders – An iterable or collection of iterables specifying training samples. Alternatively, a LightningDataModule that defines the train_dataloader hook.

  • val_dataloaders – An iterable or collection of iterables specifying validation samples.

  • datamodule – A LightningDataModule that defines the train_dataloader hook.

  • ckpt_path –

    Path/URL of the checkpoint from which training is resumed. Could also be one of three special keywords "last", "hpc" and "registry". Otherwise, if there is no checkpoint file at the path, an exception is raised.

    • best: the best model checkpoint from the previous trainer.fit call will be loaded

    • last: the last model checkpoint from the previous trainer.fit call will be loaded

    • registry: the model will be downloaded from the Lightning Model Registry with following notations:

      • 'registry': uses the latest/default version of default model set with Trainer(..., model_registry="my-model")

      • 'registry:model-name': uses the latest/default version of this model model-name

      • 'registry:model-name:version:v2': uses the specific version ‘v2’ of the model model-name

      • 'registry:version:v2': uses the default model set with Trainer(..., model_registry="my-model") and version ‘v2’

  • weights_only – Defaults to None. If True, restricts loading to state_dicts of plain torch.Tensor and other primitive types. If loading a checkpoint from a trusted source that contains an nn.Module, use weights_only=False. If loading checkpoint from an untrusted source, we recommend using weights_only=True. For more information, please refer to the PyTorch Developer Notes on Serialization Semantics.

For more information about multiple dataloaders, see this section.

Return type:

None

Raises:

TypeError – If model is not LightningModule for torch version less than 2.0.0 and if model is not LightningModule or torch._dynamo.OptimizedModule for torch versions greater than or equal to 2.0.0 .

test(*args, **kwargs)

Perform one evaluation epoch over the test set. It’s separated from fit to make sure you never run on your test set until you want to.

Parameters:
  • model – The model to test.

  • dataloaders – An iterable or collection of iterables specifying test samples. Alternatively, a LightningDataModule that defines the test_dataloader hook.

  • ckpt_path – Either "best", "last", "hpc", "registry" or path to the checkpoint you wish to test. If None and the model instance was passed, use the current weights. Otherwise, the best model checkpoint from the previous trainer.fit call will be loaded if a checkpoint callback is configured.

  • verbose – If True, prints the test results.

  • datamodule – A LightningDataModule that defines the test_dataloader hook.

  • weights_only –

    Defaults to None. If True, restricts loading to state_dicts of plain torch.Tensor and other primitive types. If loading a checkpoint from a trusted source that contains an nn.Module, use weights_only=False. If loading checkpoint from an untrusted source, we recommend using weights_only=True. For more information, please refer to the PyTorch Developer Notes on Serialization Semantics.

Return type:

TestMetrics

For more information about multiple dataloaders, see this section.

Returns:

List of dictionaries with metrics logged during the test phase, e.g., in model- or callback hooks like test_step() etc. The length of the list corresponds to the number of test dataloaders used.

Raises:
  • TypeError – If no model is passed and there was no LightningModule passed in the previous run. If model passed is not LightningModule or torch._dynamo.OptimizedModule.

  • MisconfigurationException – If both dataloaders and datamodule are passed. Pass only one of these.

  • RuntimeError – If a compiled model is passed and the strategy is not supported.

Return type:

TestMetrics

class kraken.train.PPOCRv6RecognitionDataModule(data_config)

Recognition datamodule for PP-OCRv6 models.

Parameters:

data_config (kraken.configs.PPOCRv6RecognitionTrainingDataConfig)

test_dataloader()

An iterable or collection of iterables specifying test samples.

For more information about multiple dataloaders, see this section.

For data processing use the following pattern:

  • download in prepare_data()

  • process and split in setup()

However, the above are only necessary for distributed processing.

Warning

do not assign state in prepare_data

  • test()

  • prepare_data()

  • setup()

Note

Lightning tries to add the correct sampler for distributed and arbitrary hardware. There is no need to set it yourself.

Note

If you don’t need a test dataset and a test_step(), you don’t need to implement this method.

train_dataloader()

An iterable or collection of iterables specifying training samples.

For more information about multiple dataloaders, see this section.

The dataloader you return will not be reloaded unless you set :paramref:`~lightning.pytorch.trainer.trainer.Trainer.reload_dataloaders_every_n_epochs` to a positive integer.

For data processing use the following pattern:

  • download in prepare_data()

  • process and split in setup()

However, the above are only necessary for distributed processing.

Warning

do not assign state in prepare_data

  • fit()

  • prepare_data()

  • setup()

Note

Lightning tries to add the correct sampler for distributed and arbitrary hardware. There is no need to set it yourself.

val_dataloader()

An iterable or collection of iterables specifying validation samples.

For more information about multiple dataloaders, see this section.

The dataloader you return will not be reloaded unless you set :paramref:`~lightning.pytorch.trainer.trainer.Trainer.reload_dataloaders_every_n_epochs` to a positive integer.

It’s recommended that all data downloads and preparation happen in prepare_data().

  • fit()

  • validate()

  • prepare_data()

  • setup()

Note

Lightning tries to add the correct sampler for distributed and arbitrary hardware There is no need to set it yourself.

Note

If you don’t need a validation dataset and a validation_step(), you don’t need to implement this method.

class kraken.train.PPOCRv6RecognitionModel(config, model=None)

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call to(), etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:

training (bool) – Boolean represents whether this module is in training or evaluation mode.

Parameters:
  • config (kraken.configs.PPOCRv6RecognitionTrainingConfig)

  • model (Optional[kraken.models.BaseModel])

codec: kraken.lib.codec.PytorchCodec | None = None
configure_optimizers()

Choose what optimizers and learning-rate schedulers to use in your optimization. Normally you’d need one. But in the case of GANs or similar you might have multiple. Optimization with multiple optimizers only works in the manual optimization mode.

Returns:

Any of these 6 options.

  • Single optimizer.

  • List or Tuple of optimizers.

  • Two lists - The first list has multiple optimizers, and the second has multiple LR schedulers (or multiple lr_scheduler_config).

  • Dictionary, with an "optimizer" key, and (optionally) a "lr_scheduler" key whose value is a single LR scheduler or lr_scheduler_config.

  • None - Fit will run without any optimizer.

The lr_scheduler_config is a dictionary which contains the scheduler and its associated configuration. The default configuration is shown below.

lr_scheduler_config = {
    # REQUIRED: The scheduler instance
    "scheduler": lr_scheduler,
    # The unit of the scheduler's step size, could also be 'step'.
    # 'epoch' updates the scheduler on epoch end whereas 'step'
    # updates it after a optimizer update.
    "interval": "epoch",
    # How many epochs/steps should pass between calls to
    # `scheduler.step()`. 1 corresponds to updating the learning
    # rate after every epoch/step.
    "frequency": 1,
    # Metric to monitor for schedulers like `ReduceLROnPlateau`
    "monitor": "val_loss",
    # If set to `True`, will enforce that the value specified 'monitor'
    # is available when the scheduler is updated, thus stopping
    # training if not found. If set to `False`, it will only produce a warning
    "strict": True,
    # If using the `LearningRateMonitor` callback to monitor the
    # learning rate progress, this keyword can be used to specify
    # a custom logged name
    "name": None,
}

When there are schedulers in which the .step() method is conditioned on a value, such as the torch.optim.lr_scheduler.ReduceLROnPlateau scheduler, Lightning requires that the lr_scheduler_config contains the keyword "monitor" set to the metric name that the scheduler should be conditioned on.

Metrics can be made available to monitor by simply logging it using self.log('metric_to_track', metric_val) in your LightningModule.

Note

Some things to know:

  • Lightning calls .backward() and .step() automatically in case of automatic optimization.

  • If a learning rate scheduler is specified in configure_optimizers() with key "interval" (default “epoch”) in the scheduler configuration, Lightning will call the scheduler’s .step() method automatically in case of automatic optimization.

  • If you use 16-bit precision (precision=16), Lightning will automatically handle the optimizer.

  • If you use torch.optim.LBFGS, Lightning handles the closure function automatically for you.

  • If you use multiple optimizers, you will have to switch to ‘manual optimization’ mode and step them yourself.

  • If you need to control how often the optimizer steps, override the optimizer_step() hook.

criterion
forward(x, seq_lens=None)

Same as torch.nn.Module.forward().

Parameters:
  • *args – Whatever you decide to pass into the forward method.

  • **kwargs – Keyword arguments are also possible.

Returns:

Your model’s output

lr_scheduler_step(scheduler, metric)

Override this method to adjust the default way the Trainer calls each scheduler. By default, Lightning calls step() and as shown in the example for each scheduler based on its interval.

Parameters:
  • scheduler – Learning rate scheduler.

  • metric – Value of the monitor used for schedulers like ReduceLROnPlateau.

Examples:

# DEFAULT
def lr_scheduler_step(self, scheduler, metric):
    if metric is None:
        scheduler.step()
    else:
        scheduler.step(metric)

# Alternative way to update schedulers if it requires an epoch value
def lr_scheduler_step(self, scheduler, metric):
    scheduler.step(epoch=self.current_epoch)
nrtr_criterion
nrtr_head: torch.nn.Module | None = None
on_fit_start()

Called at the very beginning of fit.

If on DDP it is called on every process

on_test_epoch_end()

Called in the test loop at the very end of the epoch.

on_test_epoch_start()

Called in the test loop at the very beginning of the epoch.

on_validation_epoch_end()

Called in the validation loop at the very end of the epoch.

optimizer_step(epoch, batch_idx, optimizer, optimizer_closure)

Override this method to adjust the default way the Trainer calls the optimizer.

By default, Lightning calls step() and zero_grad() as shown in the example. This method (and zero_grad()) won’t be called during the accumulation phase when Trainer(accumulate_grad_batches != 1). Overriding this hook has no benefit with manual optimization.

Parameters:
  • epoch – Current epoch

  • batch_idx – Index of current batch

  • optimizer – A PyTorch optimizer

  • optimizer_closure – The optimizer closure. This closure must be executed as it includes the calls to training_step(), optimizer.zero_grad(), and backward().

Examples:

def optimizer_step(self, epoch, batch_idx, optimizer, optimizer_closure):
    # Add your custom logic to run directly before `optimizer.step()`

    optimizer.step(closure=optimizer_closure)

    # Add your custom logic to run directly after `optimizer.step()`
setup(stage=None)

Called at the beginning of fit (train + validate), validate, test, or predict. This is a good hook when you need to build models dynamically or adjust something about them. This hook is called on every process when using DDP.

Parameters:

stage (Optional[str]) – either 'fit', 'validate', 'test', or 'predict'

Example:

class LitModel(...):
    def __init__(self):
        self.l1 = None

    def prepare_data(self):
        download_data()
        tokenize()

        # don't do this
        self.something = else

    def setup(self, stage):
        data = load_data(...)
        self.l1 = nn.Linear(28, data.num_classes)
test_step(batch, batch_idx, test_dataloader=0)

Operates on a single batch of data from the test set. In this step you’d normally generate examples or calculate anything of interest such as accuracy.

Parameters:
  • batch – The output of your data iterable, normally a DataLoader.

  • batch_idx – The index of this batch.

  • dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)

Returns:

  • Tensor - The loss tensor

  • dict - A dictionary. Can include any keys, but must include the key 'loss'.

  • None - Skip to the next batch.

# if you have one test dataloader:
def test_step(self, batch, batch_idx): ...


# if you have multiple test dataloaders:
def test_step(self, batch, batch_idx, dataloader_idx=0): ...

Examples:

# CASE 1: A single test dataset
def test_step(self, batch, batch_idx):
    x, y = batch

    # implement your own
    out = self(x)
    loss = self.loss(out, y)

    # log 6 example images
    # or generated text... or whatever
    sample_imgs = x[:6]
    grid = torchvision.utils.make_grid(sample_imgs)
    self.logger.experiment.add_image('example_images', grid, 0)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    test_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs!
    self.log_dict({'test_loss': loss, 'test_acc': test_acc})

If you pass in multiple test dataloaders, test_step() will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.

# CASE 2: multiple test dataloaders
def test_step(self, batch, batch_idx, dataloader_idx=0):
    # dataloader_idx tells you which dataset this is.
    x, y = batch

    # implement your own
    out = self(x)

    if dataloader_idx == 0:
        loss = self.loss0(out, y)
    else:
        loss = self.loss1(out, y)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs separately for each dataloader
    self.log_dict({f"test_loss_{dataloader_idx}": loss, f"test_acc_{dataloader_idx}": acc})

Note

If you don’t need to test you don’t need to implement this method.

Note

When the test_step() is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of the test epoch, the model goes back to training mode and gradients are enabled.

training_step(batch, batch_idx)

Here you compute and return the training loss and some additional metrics for e.g. the progress bar or logger.

Parameters:
  • batch – The output of your data iterable, normally a DataLoader.

  • batch_idx – The index of this batch.

  • dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)

Returns:

  • Tensor - The loss tensor

  • dict - A dictionary which can include any keys, but must include the key 'loss' in the case of automatic optimization.

  • None - In automatic optimization, this will skip to the next batch (but is not supported for multi-GPU, TPU, or DeepSpeed). For manual optimization, this has no special meaning, as returning the loss is not required.

In this step you’d normally do the forward pass and calculate the loss for a batch. You can also do fancier things like multiple forward passes or something model specific.

Example:

def training_step(self, batch, batch_idx):
    x, y, z = batch
    out = self.encoder(x)
    loss = self.loss(out, x)
    return loss

To use multiple optimizers, you can switch to ‘manual optimization’ and control their stepping:

def __init__(self):
    super().__init__()
    self.automatic_optimization = False


# Multiple optimizers (e.g.: GANs)
def training_step(self, batch, batch_idx):
    opt1, opt2 = self.optimizers()

    # do training_step with encoder
    ...
    opt1.step()
    # do training_step with decoder
    ...
    opt2.step()

Note

When accumulate_grad_batches > 1, the loss returned here will be automatically normalized by accumulate_grad_batches internally.

validation_step(batch, batch_idx)

Operates on a single batch of data from the validation set. In this step you’d might generate examples or calculate anything of interest like accuracy.

Parameters:
  • batch – The output of your data iterable, normally a DataLoader.

  • batch_idx – The index of this batch.

  • dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)

Returns:

  • Tensor - The loss tensor

  • dict - A dictionary. Can include any keys, but must include the key 'loss'.

  • None - Skip to the next batch.

# if you have one val dataloader:
def validation_step(self, batch, batch_idx): ...


# if you have multiple val dataloaders:
def validation_step(self, batch, batch_idx, dataloader_idx=0): ...

Examples:

# CASE 1: A single validation dataset
def validation_step(self, batch, batch_idx):
    x, y = batch

    # implement your own
    out = self(x)
    loss = self.loss(out, y)

    # log 6 example images
    # or generated text... or whatever
    sample_imgs = x[:6]
    grid = torchvision.utils.make_grid(sample_imgs)
    self.logger.experiment.add_image('example_images', grid, 0)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs!
    self.log_dict({'val_loss': loss, 'val_acc': val_acc})

If you pass in multiple val dataloaders, validation_step() will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.

# CASE 2: multiple validation dataloaders
def validation_step(self, batch, batch_idx, dataloader_idx=0):
    # dataloader_idx tells you which dataset this is.
    x, y = batch

    # implement your own
    out = self(x)

    if dataloader_idx == 0:
        loss = self.loss0(out, y)
    else:
        loss = self.loss1(out, y)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs separately for each dataloader
    self.log_dict({f"val_loss_{dataloader_idx}": loss, f"val_acc_{dataloader_idx}": acc})

Note

If you don’t need to validate you don’t need to implement this method.

Note

When the validation_step() is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of validation, the model goes back to training mode and gradients are enabled.

class kraken.train.VGSLRecognitionDataModule(data_config)

A DataModule standardizes the training, val, test splits, data preparation and transforms. The main advantage is consistent data splits, data preparation and transforms across models.

Example:

import lightning as L
import torch.utils.data as data
from lightning.pytorch.demos.boring_classes import RandomDataset

class MyDataModule(L.LightningDataModule):
    def prepare_data(self):
        # download, IO, etc. Useful with shared filesystems
        # only called on 1 GPU/TPU in distributed
        ...

    def setup(self, stage):
        # make assignments here (val/train/test split)
        # called on every process in DDP
        dataset = RandomDataset(1, 100)
        self.train, self.val, self.test = data.random_split(
            dataset, [80, 10, 10], generator=torch.Generator().manual_seed(42)
        )

    def train_dataloader(self):
        return data.DataLoader(self.train)

    def val_dataloader(self):
        return data.DataLoader(self.val)

    def test_dataloader(self):
        return data.DataLoader(self.test)

    def on_exception(self, exception):
        # clean up state after the trainer faced an exception
        ...

    def teardown(self):
        # clean up state after the trainer stops, delete files...
        # called on every process in DDP
        ...
Parameters:

data_config (kraken.configs.VGSLRecognitionTrainingDataConfig)

setup(stage=None)

Called at the beginning of fit (train + validate), validate, test, or predict. This is a good hook when you need to build models dynamically or adjust something about them. This hook is called on every process when using DDP.

Parameters:

stage (str) – either 'fit', 'validate', 'test', or 'predict'

Example:

class LitModel(...):
    def __init__(self):
        self.l1 = None

    def prepare_data(self):
        download_data()
        tokenize()

        # don't do this
        self.something = else

    def setup(self, stage):
        data = load_data(...)
        self.l1 = nn.Linear(28, data.num_classes)
test_dataloader()

An iterable or collection of iterables specifying test samples.

For more information about multiple dataloaders, see this section.

For data processing use the following pattern:

  • download in prepare_data()

  • process and split in setup()

However, the above are only necessary for distributed processing.

Warning

do not assign state in prepare_data

Note

Lightning tries to add the correct sampler for distributed and arbitrary hardware. There is no need to set it yourself.

Note

If you don’t need a test dataset and a test_step(), you don’t need to implement this method.

train_dataloader()

An iterable or collection of iterables specifying training samples.

For more information about multiple dataloaders, see this section.

The dataloader you return will not be reloaded unless you set :paramref:`~lightning.pytorch.trainer.trainer.Trainer.reload_dataloaders_every_n_epochs` to a positive integer.

For data processing use the following pattern:

  • download in prepare_data()

  • process and split in setup()

However, the above are only necessary for distributed processing.

Warning

do not assign state in prepare_data

Note

Lightning tries to add the correct sampler for distributed and arbitrary hardware. There is no need to set it yourself.

val_dataloader()

An iterable or collection of iterables specifying validation samples.

For more information about multiple dataloaders, see this section.

The dataloader you return will not be reloaded unless you set :paramref:`~lightning.pytorch.trainer.trainer.Trainer.reload_dataloaders_every_n_epochs` to a positive integer.

It’s recommended that all data downloads and preparation happen in prepare_data().

  • fit()

  • validate()

  • prepare_data()

  • setup()

Note

Lightning tries to add the correct sampler for distributed and arbitrary hardware There is no need to set it yourself.

Note

If you don’t need a validation dataset and a validation_step(), you don’t need to implement this method.

class kraken.train.VGSLRecognitionModel(config, model=None)

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call to(), etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:

training (bool) – Boolean represents whether this module is in training or evaluation mode.

Parameters:
  • config (kraken.configs.VGSLRecognitionTrainingConfig)

  • model (Optional[kraken.models.BaseModel])

configure_callbacks()

Configure model-specific callbacks. When the model gets attached, e.g., when .fit() or .test() gets called, the list or a callback returned here will be merged with the list of callbacks passed to the Trainer’s callbacks argument. If a callback returned here has the same type as one or several callbacks already present in the Trainer’s callbacks list, it will take priority and replace them. In addition, Lightning will make sure ModelCheckpoint callbacks run last.

Returns:

A callback or a list of callbacks which will extend the list of callbacks in the Trainer.

Example:

def configure_callbacks(self):
    early_stop = EarlyStopping(monitor="val_acc", mode="max")
    checkpoint = ModelCheckpoint(monitor="val_loss")
    return [early_stop, checkpoint]
configure_optimizers()

Choose what optimizers and learning-rate schedulers to use in your optimization. Normally you’d need one. But in the case of GANs or similar you might have multiple. Optimization with multiple optimizers only works in the manual optimization mode.

Returns:

Any of these 6 options.

  • Single optimizer.

  • List or Tuple of optimizers.

  • Two lists - The first list has multiple optimizers, and the second has multiple LR schedulers (or multiple lr_scheduler_config).

  • Dictionary, with an "optimizer" key, and (optionally) a "lr_scheduler" key whose value is a single LR scheduler or lr_scheduler_config.

  • None - Fit will run without any optimizer.

The lr_scheduler_config is a dictionary which contains the scheduler and its associated configuration. The default configuration is shown below.

lr_scheduler_config = {
    # REQUIRED: The scheduler instance
    "scheduler": lr_scheduler,
    # The unit of the scheduler's step size, could also be 'step'.
    # 'epoch' updates the scheduler on epoch end whereas 'step'
    # updates it after a optimizer update.
    "interval": "epoch",
    # How many epochs/steps should pass between calls to
    # `scheduler.step()`. 1 corresponds to updating the learning
    # rate after every epoch/step.
    "frequency": 1,
    # Metric to monitor for schedulers like `ReduceLROnPlateau`
    "monitor": "val_loss",
    # If set to `True`, will enforce that the value specified 'monitor'
    # is available when the scheduler is updated, thus stopping
    # training if not found. If set to `False`, it will only produce a warning
    "strict": True,
    # If using the `LearningRateMonitor` callback to monitor the
    # learning rate progress, this keyword can be used to specify
    # a custom logged name
    "name": None,
}

When there are schedulers in which the .step() method is conditioned on a value, such as the torch.optim.lr_scheduler.ReduceLROnPlateau scheduler, Lightning requires that the lr_scheduler_config contains the keyword "monitor" set to the metric name that the scheduler should be conditioned on.

Metrics can be made available to monitor by simply logging it using self.log('metric_to_track', metric_val) in your LightningModule.

Note

Some things to know:

  • Lightning calls .backward() and .step() automatically in case of automatic optimization.

  • If a learning rate scheduler is specified in configure_optimizers() with key "interval" (default “epoch”) in the scheduler configuration, Lightning will call the scheduler’s .step() method automatically in case of automatic optimization.

  • If you use 16-bit precision (precision=16), Lightning will automatically handle the optimizer.

  • If you use torch.optim.LBFGS, Lightning handles the closure function automatically for you.

  • If you use multiple optimizers, you will have to switch to ‘manual optimization’ mode and step them yourself.

  • If you need to control how often the optimizer steps, override the optimizer_step() hook.

example_input_array

The example input array is a specification of what the module can consume in the forward() method. The return type is interpreted as follows:

  • Single tensor: It is assumed the model takes a single argument, i.e., model.forward(model.example_input_array)

  • Tuple: The input array should be interpreted as a sequence of positional arguments, i.e., model.forward(*model.example_input_array)

  • Dict: The input array represents named keyword arguments, i.e., model.forward(**model.example_input_array)

forward(x, seq_lens=None)

Same as torch.nn.Module.forward().

Parameters:
  • *args – Whatever you decide to pass into the forward method.

  • **kwargs – Keyword arguments are also possible.

Returns:

Your model’s output

on_test_epoch_end()

Called in the test loop at the very end of the epoch.

on_test_epoch_start()

Called in the test loop at the very beginning of the epoch.

on_validation_epoch_end()

Called in the validation loop at the very end of the epoch.

setup(stage=None)

Called at the beginning of fit (train + validate), validate, test, or predict. This is a good hook when you need to build models dynamically or adjust something about them. This hook is called on every process when using DDP.

Parameters:

stage (Optional[str]) – either 'fit', 'validate', 'test', or 'predict'

Example:

class LitModel(...):
    def __init__(self):
        self.l1 = None

    def prepare_data(self):
        download_data()
        tokenize()

        # don't do this
        self.something = else

    def setup(self, stage):
        data = load_data(...)
        self.l1 = nn.Linear(28, data.num_classes)
test_step(batch, batch_idx, test_dataloader=0)

Operates on a single batch of data from the test set. In this step you’d normally generate examples or calculate anything of interest such as accuracy.

Parameters:
  • batch – The output of your data iterable, normally a DataLoader.

  • batch_idx – The index of this batch.

  • dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)

Returns:

  • Tensor - The loss tensor

  • dict - A dictionary. Can include any keys, but must include the key 'loss'.

  • None - Skip to the next batch.

# if you have one test dataloader:
def test_step(self, batch, batch_idx): ...


# if you have multiple test dataloaders:
def test_step(self, batch, batch_idx, dataloader_idx=0): ...

Examples:

# CASE 1: A single test dataset
def test_step(self, batch, batch_idx):
    x, y = batch

    # implement your own
    out = self(x)
    loss = self.loss(out, y)

    # log 6 example images
    # or generated text... or whatever
    sample_imgs = x[:6]
    grid = torchvision.utils.make_grid(sample_imgs)
    self.logger.experiment.add_image('example_images', grid, 0)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    test_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs!
    self.log_dict({'test_loss': loss, 'test_acc': test_acc})

If you pass in multiple test dataloaders, test_step() will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.

# CASE 2: multiple test dataloaders
def test_step(self, batch, batch_idx, dataloader_idx=0):
    # dataloader_idx tells you which dataset this is.
    x, y = batch

    # implement your own
    out = self(x)

    if dataloader_idx == 0:
        loss = self.loss0(out, y)
    else:
        loss = self.loss1(out, y)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs separately for each dataloader
    self.log_dict({f"test_loss_{dataloader_idx}": loss, f"test_acc_{dataloader_idx}": acc})

Note

If you don’t need to test you don’t need to implement this method.

Note

When the test_step() is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of the test epoch, the model goes back to training mode and gradients are enabled.

training_step(batch, batch_idx)

Here you compute and return the training loss and some additional metrics for e.g. the progress bar or logger.

Parameters:
  • batch – The output of your data iterable, normally a DataLoader.

  • batch_idx – The index of this batch.

  • dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)

Returns:

  • Tensor - The loss tensor

  • dict - A dictionary which can include any keys, but must include the key 'loss' in the case of automatic optimization.

  • None - In automatic optimization, this will skip to the next batch (but is not supported for multi-GPU, TPU, or DeepSpeed). For manual optimization, this has no special meaning, as returning the loss is not required.

In this step you’d normally do the forward pass and calculate the loss for a batch. You can also do fancier things like multiple forward passes or something model specific.

Example:

def training_step(self, batch, batch_idx):
    x, y, z = batch
    out = self.encoder(x)
    loss = self.loss(out, x)
    return loss

To use multiple optimizers, you can switch to ‘manual optimization’ and control their stepping:

def __init__(self):
    super().__init__()
    self.automatic_optimization = False


# Multiple optimizers (e.g.: GANs)
def training_step(self, batch, batch_idx):
    opt1, opt2 = self.optimizers()

    # do training_step with encoder
    ...
    opt1.step()
    # do training_step with decoder
    ...
    opt2.step()

Note

When accumulate_grad_batches > 1, the loss returned here will be automatically normalized by accumulate_grad_batches internally.

validation_step(batch, batch_idx)

Operates on a single batch of data from the validation set. In this step you’d might generate examples or calculate anything of interest like accuracy.

Parameters:
  • batch – The output of your data iterable, normally a DataLoader.

  • batch_idx – The index of this batch.

  • dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)

Returns:

  • Tensor - The loss tensor

  • dict - A dictionary. Can include any keys, but must include the key 'loss'.

  • None - Skip to the next batch.

# if you have one val dataloader:
def validation_step(self, batch, batch_idx): ...


# if you have multiple val dataloaders:
def validation_step(self, batch, batch_idx, dataloader_idx=0): ...

Examples:

# CASE 1: A single validation dataset
def validation_step(self, batch, batch_idx):
    x, y = batch

    # implement your own
    out = self(x)
    loss = self.loss(out, y)

    # log 6 example images
    # or generated text... or whatever
    sample_imgs = x[:6]
    grid = torchvision.utils.make_grid(sample_imgs)
    self.logger.experiment.add_image('example_images', grid, 0)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    val_acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs!
    self.log_dict({'val_loss': loss, 'val_acc': val_acc})

If you pass in multiple val dataloaders, validation_step() will have an additional argument. We recommend setting the default value of 0 so that you can quickly switch between single and multiple dataloaders.

# CASE 2: multiple validation dataloaders
def validation_step(self, batch, batch_idx, dataloader_idx=0):
    # dataloader_idx tells you which dataset this is.
    x, y = batch

    # implement your own
    out = self(x)

    if dataloader_idx == 0:
        loss = self.loss0(out, y)
    else:
        loss = self.loss1(out, y)

    # calculate acc
    labels_hat = torch.argmax(out, dim=1)
    acc = torch.sum(y == labels_hat).item() / (len(y) * 1.0)

    # log the outputs separately for each dataloader
    self.log_dict({f"val_loss_{dataloader_idx}": loss, f"val_acc_{dataloader_idx}": acc})

Note

If you don’t need to validate you don’t need to implement this method.

Note

When the validation_step() is called, the model has been put in eval mode and PyTorch gradients have been disabled. At the end of validation, the model goes back to training mode and gradients are enabled.

Plugin API

kraken.models.base

kraken.models.base

Base metaclass for models

class kraken.models.base.BaseModel

Base model metaclass that all models inherit from.

inference_config_cls: ClassVar[type[kraken.configs.Config] | None] = None

The configuration class this model’s prepare_for_inference takes as an argument. Task models rebuild the configuration they receive into this class, applying any values from its extra_options. None means the base inference configuration of the task the model is run under (e.g. SegmentationInferenceConfig for segmentation). Models serving multiple tasks with differing option sets should keep it at None.

The class has to be a subclass of the base inference configuration of the task the model is run under; a class that is not causes a ValueError when the model is run. To have the options show up in kraken options without a model file, register it under the kraken.configs entry point group.

abstractmethod load_state_dict()
abstract property model_type: _T_tasks

An attribute containing the tasks the model can perform.

Return type:

_T_tasks

property one_channel_mode: None | Literal['1', 'L']

An auxiliary attribute of VGSL models to distinguish between b/w and grayscale input image modes.

Return type:

Union[None, Literal[‘1’, ‘L’]]

abstractmethod prepare_for_inference(config)

Prepares the model for inference.

Parameters:

config (kraken.configs.Config)

property seg_type: None | Literal['bbox', 'baseline']

An auxiliary attribute of text recognition models to determine if they have been trained on bounding box or baseline data.

Return type:

Union[None, Literal[‘bbox’, ‘baseline’]]

abstractmethod state_dict()
property use_legacy_polygons: None | bool

An auxiliary attribute of text recognition models to determine if they have been trained with data produced using the slow legacy polygon extraction implementation. Informational only as the legacy extractor has been removed.

Return type:

Union[None, bool]

property user_metadata: dict

A dictionary containing all hyperparameters, statistics, etc. of the model.

Return type:

dict

class kraken.models.base.RecognitionBaseModel

Base model metaclass for text recognition models.

abstractmethod predict(im, segmentation)
Parameters:
Return type:

collections.abc.Generator[kraken.containers.ocr_record, None, None]

class kraken.models.base.SegmentationBaseModel

Base model metaclass for layout analysis models.

abstractmethod predict(im)

Computes a segmentation on an input image.

Parameters:

im (PIL.Image.Image.Image)

Return type:

kraken.containers.Segmentation

kraken.models

kraken.models.write_safetensors(objs, path)

Writes a set of models as a safetensors.

Parameters:
Return type:

os.PathLike

kraken.configs

class kraken.configs.RecognitionInferenceConfig(**kwargs)

Configuration for recognition inference.

Arg:

> Universal parameters

temperature (float, defaults to 1.0):

The value used to smoothen the softmax distribution of the output cofidences.

return_line_image (bool defaults to False):

Switch to add the extracted line image to the output records.

return_logits (bool, default to False):

Switch to add raw logits to output records.

> Line recognizer parameters

padding (int, defaults to 16):

Extra blank padding to the left and right of text line

num_line_workers (int, defaults to 2):

Number of worker processes to extract lines from images.

no_legacy_polygons (bool, defaults to False):

Deprecated and ignored. The legacy polygon extractor has been removed.

linetype (Optional[Literal[‘baselines’, ‘bbox’]], defaults to None):

Line type used when parsing XML input files. If None the type is derived from the recognition model’s seg_type.

text_direction (Literal[‘horizontal-tb’, ‘vertical-lr’, ‘vertical-rl’], defaults to ‘horizontal-tb’):

Sets the orientation of bounding box segmentation data

> CTC model parameters

decoder (Callable, defaults to kraken.lib.ctc_decoder.greedy_decoder):

CTC decoder

bidi_reordering (bool, defaults to True):

Reorder classes in the ocr_record according to the Unicode bidirectional algorithm for correct display. Set to L|R to override default text direction.

bidi_reordering
decoder
linetype
no_legacy_polygons
num_line_workers
padding
return_line_image
return_logits
temperature
text_direction
class kraken.configs.RecognitionTrainingDataConfig(**kwargs)

Configuration for recognition training data.

Arg:

> Text recognition parameters

binary_dataset_split (bool, defaults to False):

Deprecated and ignored.

xml_split (bool, defaults to False):

Use explicit train/validation/test line splits defined in XML training, evaluation, or test files.

format_type (Literal[‘alto’, ‘page’, ‘xml’, ‘path’, ‘binary’], defaults to ‘xml’):

Format of the training data.

linetype (Optional[Literal[‘baselines’, ‘bbox’]], defaults to None):

Line type of the training data. If None the type is selected automatically: ‘baselines’ for XML data, ‘bbox’ for path data, and the type recorded in the metadata for binary datasets.

codec: (Union[dict[str, Sequence[int]], Sequence[str], str], defaults to None):

Codec mapping one or more Unicode code points to one or more integers.

binary_dataset_split
codec
format_type
linetype
xml_split
class kraken.configs.SegmentationInferenceConfig(**kwargs)

Configuration for segmentation inference.

Arg:

> Universal parameters

text_direction (Literal[‘horizontal-lr’, ‘horizontal-rl’, ‘vertical-lr’, ‘vertical-rl’], defaults to ‘horizontal-lr’):

Sets the principal text direction and line orientation of the input page.

input_padding (int or tuple[int, int] or tuple[int, int, int, int], defaults to 0):

Padding to add around the input image.

> Parameters for the legacy bounding box segmenter

legacy_scale: Optional[float] = None, legacy_maxcolseps: float = 2, legacy_black_colseps: bool = False, legacy_no_hlines: bool = True,

> Parameters for bounding box segmenters (currently only the legacy segmenter)

bbox_line_padding (Union[int, tuple[int, int]], defaults to 0):

Padding to be added around left/right side of bounding boxes in bbox line segmenter.

> Parameters for reading order determination

bbox_ro_fn (Callable, defaults t kraken.lib.segmentation.reading_order):

Function to compute the basic reading order of a set of lines in bbox format.

baseline_ro_fn (Callable, defaults to kraken.lib.segmentation.polygonal_reading_order):

Function to compute the basic reading order of a set of lines in baseline format.

baseline_ro_fn
bbox_line_padding
bbox_ro_fn
input_padding
legacy_black_colseps
legacy_maxcolseps
legacy_no_hlines
legacy_scale
text_direction
class kraken.configs.SegmentationTrainingDataConfig(**kwargs)

Configuration for segmentation training data.

Arg:

> Universal parameters

format_type (Literal[‘alto’, ‘page’, ‘xml’] defaults to ‘xml’):

Format of the training data.

line_class_mapping (dict[str, int], defaults to defaultdict):

Mapping between line class identifiers and integer labels.

region_class_mapping (dict[str, int], defaults to None):

Mapping between region class identifiers and integer labels. Same semantics as line_class_mapping.

topline (Union[bool, None], defaults to False):

Indicator of baseline position in dataset. False = baseline, True = topline, None = centerline.

Notes

line_class_mapping and region_class_mapping share a single output label space with the auxiliary classes. Indices 0 and 1 are reserved for aux classes (_start_separator, _end_separator); user-defined baseline and region indices must start at 2 or above. Baselines and regions must occupy disjoint index ranges — overlapping indices would cause lines and regions to be merged into the same output channel.

Merging within a category is supported by mapping multiple types to the same index:

` line_class_mapping = OrderedDict([('line_type_1', 2), ('line_type_2', 2)]) `

will merge ‘line_type_1’ and ‘line_type_2’ in the detector. The OrderedDict is necessary to make the reverse lookup of labels to identifiers deterministic (the first occurence of the type will be used as the identifier in the output).

Regions or baselines can be suppressed to train only on one or the other. To do so, set line_class_mapping or region_class_mapping to an empty dictionary.

The values for both mappings default to a modified defaultdict that assigns a unique label to each line/region class in the training data.

format_type
line_class_mapping
region_class_mapping
topline
class kraken.configs.TrainingConfig(**kwargs)

Generic configuration for model training of all tasks supported by kraken.

Arg:

> General parameters

epochs (int, optional with early stopping):

Number of epochs to train for.

completed_epochs (int):

How many epochs of the schedule have already been completed.

freq (float, defaults to 1.0):

Evaluation and checkpoint saving frequency

checkpoint_path (PathLike, defaults to model):

Path prefix to save checkpoints during training.

weights_format (Literal[safetensors, coreml], defaults to ‘safetensors’):

Weight format to convert checkpoint at end of training to.

> Optimizer configuration

optimizer (str, defaults to AdamW):

Optimizer to use.

lrate (float, defaults to 1e-5):

Learning rate

momentum (float, defaults to 0.9):

Momentum parameter. Ignored if optimizer doesn’t use it.

weight_decay (float, defaults to 0.0):

Weight decay. Ignored if optimizer doesn’t support it.

gradient_clip_val (float, defaults to 1.0):

Threshold for gradient clipping.

accumulate_grad_batches (int, defaults to 1):

Number of batches to aggregate before backpropagation.

> Learning rate scheduling parameters

schedule (str, defaults to constant):

Type of learning rate schedule.

warmup (int, defaults to 0):

Number of iterations to warmup learning rate.

step_size (int, defaults to 10):

Learning rate decay in stepped schedule.

gamma (float, defaults to 0.1):

Learning rate decay in exponential schedule.

rop_factor (float, defaults to 0.1):

Learning rate decay in reduce on plateau schedule.

rop_patience (int, defaults to 5):

Number of epochs to wait before reducing learning rate.

cos_t_max (int, defaults to 10):

Epoch at which cosine schedule reaches final learning rate.

cos_min_lr (float, defaults to 1e-6):

Final learning rate with cosine schedule.

> Early stopping parameters

quit (str, early or fixed, defaults to fixed):

Training stopping condition.

min_epochs (int, defaults to 0):

Minimum number of epochs to train without considering validation scores.

lag (int, defaults to 10):

Number of epochs to wait for improvement in validation scores before aborting.

min_delta (float, defaults to 0.0):

Minimum delta of validation scores.

accumulate_grad_batches
checkpoint_path
completed_epochs
cos_min_lr
cos_t_max
epochs
freq
gamma
gradient_clip_val
lag
lrate
min_delta
min_epochs
momentum
optimizer
quit
rop_factor
rop_patience
schedule
step_size
warmup
weight_decay
weights_format
class kraken.configs.TrainingDataConfig(**kwargs)

Generic configuration for datasets for all tasks.

Arg:

> Universal parameters

training_data (list of paths):

A list of training data files.

evaluation_data (list of paths, optional):

A list of evaluation data files.

test_data (list of paths, optional):

A list of evaluation data files.

partition (float, defaults to 0.9):

Automatic partition of training data files if no evaluation data is defined.

num_workers (int, defaults to 1):

Number of dataloader workers.

augment (bool, defaults to False):

Switch to enable augmentation.

batch_size (int, defaults to 1):

Number of items to pack into a single sample.

augment
batch_size
evaluation_data
num_workers
partition
test_data
training_data

kraken.train.base

Common base class for kraken LightningModule trainers.

class kraken.train.base.KrakenTrainerModule(*args, **kwargs)

Base class for all neural network modules.

Your models should also subclass this class.

Modules can also contain other Modules, allowing them to be nested in a tree structure. You can assign the submodules as regular attributes:

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
        x = F.relu(self.conv1(x))
        return F.relu(self.conv2(x))

Submodules assigned in this way will be registered, and will also have their parameters converted when you call to(), etc.

Note

As per the example above, an __init__() call to the parent class must be made before assignment on the child.

Variables:

training (bool) – Boolean represents whether this module is in training or evaluation mode.

Parameters:
  • args (Any)

  • kwargs (Any)

configure_callbacks()

Configure model-specific callbacks. When the model gets attached, e.g., when .fit() or .test() gets called, the list or a callback returned here will be merged with the list of callbacks passed to the Trainer’s callbacks argument. If a callback returned here has the same type as one or several callbacks already present in the Trainer’s callbacks list, it will take priority and replace them. In addition, Lightning will make sure ModelCheckpoint callbacks run last.

Returns:

A callback or a list of callbacks which will extend the list of callbacks in the Trainer.

Example:

def configure_callbacks(self):
    early_stop = EarlyStopping(monitor="val_acc", mode="max")
    checkpoint = ModelCheckpoint(monitor="val_loss")
    return [early_stop, checkpoint]
classmethod load_from_weights(path, config)

Initializes the module from a model weights file by selecting the first model in the file whose type and class matches the trainer’s task.

Parameters:
  • path (Union[str, os.PathLike])

  • config (Any)

Return type:

KrakenTrainerModule

lr_scheduler_step(scheduler, metric)

Override this method to adjust the default way the Trainer calls each scheduler. By default, Lightning calls step() and as shown in the example for each scheduler based on its interval.

Parameters:
  • scheduler – Learning rate scheduler.

  • metric – Value of the monitor used for schedulers like ReduceLROnPlateau.

Examples:

# DEFAULT
def lr_scheduler_step(self, scheduler, metric):
    if metric is None:
        scheduler.step()
    else:
        scheduler.step(metric)

# Alternative way to update schedulers if it requires an epoch value
def lr_scheduler_step(self, scheduler, metric):
    scheduler.step(epoch=self.current_epoch)
on_load_checkpoint(checkpoint)

Called by Lightning to restore your model. If you saved something with on_save_checkpoint() this is your chance to restore this.

Parameters:

checkpoint (dict) – Loaded checkpoint

Return type:

None

Example:

def on_load_checkpoint(self, checkpoint):
    # 99% of the time you don't need to implement this method
    self.something_cool_i_want_to_save = checkpoint['something_cool_i_want_to_save']

Note

Lightning auto-restores global step, epoch, and train state including amp scaling. There is no need for you to restore anything regarding training.

on_save_checkpoint(checkpoint)

Called by Lightning when saving a checkpoint to give you a chance to store anything else you might want to save.

Parameters:

checkpoint (dict) – The full checkpoint dictionary before it gets dumped to a file. Implementations of this hook can insert additional data into this dictionary.

Return type:

None

Example:

def on_save_checkpoint(self, checkpoint):
    # 99% of use cases you don't need to implement this method
    checkpoint['something_cool_i_want_to_save'] = my_cool_pickable_object

Note

Lightning saves all aspects of training (epoch, global step, etc…) including amp scaling. There is no need for you to store anything about training.

optimizer_step(epoch, batch_idx, optimizer, optimizer_closure)

Override this method to adjust the default way the Trainer calls the optimizer.

By default, Lightning calls step() and zero_grad() as shown in the example. This method (and zero_grad()) won’t be called during the accumulation phase when Trainer(accumulate_grad_batches != 1). Overriding this hook has no benefit with manual optimization.

Parameters:
  • epoch – Current epoch

  • batch_idx – Index of current batch

  • optimizer – A PyTorch optimizer

  • optimizer_closure – The optimizer closure. This closure must be executed as it includes the calls to training_step(), optimizer.zero_grad(), and backward().

Examples:

def optimizer_step(self, epoch, batch_idx, optimizer, optimizer_closure):
    # Add your custom logic to run directly before `optimizer.step()`

    optimizer.step(closure=optimizer_closure)

    # Add your custom logic to run directly after `optimizer.step()`

kraken.train.utils

Training loop interception helpers

class kraken.train.utils.RecognitionTestMetrics

A container class of text recognition test metrics for a collection of pages.

case_insensitive_cer: float
cer: float
character_counts: collections.Counter
confusions: collections.Counter
deletes: collections.Counter
insertions: int
num_errors: int
scripts: collections.Counter
substitutions: collections.Counter
wer: float

Low-Level API

These modules provide lower-level access to the core components of kraken. In most cases, it is recommended to use the high-level API instead.

kraken.ketos.pretrain

Command line driver for unsupervised recognition pretraining

Top-level module containing datasets for recognition and segmentation training.

Implementation of the Unicode Bidirectional Algorithm (UAX #9) as of Unicode 17.0.0. This module is a replacement for the pre-6.3 UBA implementation from python-bidi. This is an unoptimized, literal python port of the spec. Rules P1 and L3 are not implemented.

Pytorch compatible codec with many-to-many mapping between labels and graphemes.

kraken.lib.exceptions

All custom exceptions raised by kraken’s modules and packages. Packages should always define their exceptions here.

Processing for baseline segmenter output

Decoders for softmax outputs of CTC trained networks.

Decoders extract label sequences out of the raw output matrix of the line recognition network. There are multiple different approaches implemented here, from a simple greedy decoder, to the legacy ocropy thresholding decoder, and a more complex beam search decoder.

Extracted label sequences are converted into the code point domain using kraken.lib.codec.PytorchCodec.

Legacy Modules

These modules are retained for compatibility reasons or highly specialized use cases. Their use is not recommended.

kraken.binarization

An adaptive binarization algorithm. This code is legacy and only remains for historical reasons. Binarization is no longer necessary for most workflows.

kraken.pageseg

The legacy bounding box segmentation method using conventional image processing techniques.

kraken.rpred

Legacy line text recognition API. New code should use the RecognitionTaskModel from kraken.tasks which is more versatile and offers higher performance.

kraken.lib.models

Wrapper around TorchVGSLModel including a variety of forward pass helpers for sequence classification.