Commit graph

1612 commits

Author SHA1 Message Date
kba
b2f3a8f2d8 Merge branch 'fix-0.8-modelzoo-and-predictor-kba0709' into integrating_trocr_and_torch_ensembling_and_updating_characters_list-refactor
# Conflicts:
#	train/requirements.txt
2026-07-09 17:32:23 +02:00
kba
b1f2f43051 upgrade tf2onnx fork dep, remove spurious tf-data dep 2026-07-09 17:30:48 +02:00
kba
492fcbacb7 switch to fork of tf2onnx 2026-07-09 15:17:11 +02:00
kba
b9ba43b444 training: require tf2onnx and pin ml_dtypes >= 0.5 2026-07-09 14:37:51 +02:00
Robert Sachunsky
c9c14ed83d Docker: update to ONNX base image (in Makefile, too) 2026-07-08 14:04:19 +02:00
Robert Sachunsky
98e28ca9f4 Docker: fixup cuDNN installation after OCR extra 2026-07-08 03:05:07 +02:00
Robert Sachunsky
5354583913 update readme 2026-07-08 02:56:11 +02:00
Robert Sachunsky
8cc8c28471 ModelZoo for ONNX backend: configure TRT cache path from XDG env 2026-07-08 02:55:25 +02:00
Robert Sachunsky
32568a590f Docker: update to ONNX base image 2026-07-08 02:54:34 +02:00
Robert Sachunsky
171a8a3161 move TF+Keras dependencies to training extra, replace by ONNX+TRT 2026-07-07 22:56:36 +02:00
Robert Sachunsky
1567df1379 remove numba dependency (previously used to free CUDA memory) 2026-07-07 22:09:46 +02:00
Robert Sachunsky
16943f70b4 models (ViT backbone) iterate extract_patches over batch dim…
`Patches.call`: use `tf.map_fn` instead of running
entire batch through `tf.image.extract_patches`
(faster, less VRAM, allows ONNX conversion to work)
2026-07-02 20:58:22 +02:00
Robert Sachunsky
1b27c7390f training.convert/ONNX: run strict shape inference and check model 2026-07-02 20:56:55 +02:00
Robert Sachunsky
5e531ab006 get_textlines_of_textregion_sorted: fix be61875 2026-07-01 18:27:46 +02:00
Robert Sachunsky
01e69a0e22 predictor for OCR models: work around ONNX bug …
(ONNX converted models already return `np.dtype=object`
 arrays of `np.str_` instead of `np.bytes_`; so undo this)
2026-06-26 02:42:51 +02:00
Robert Sachunsky
948d841a7d training.models for cnn-rnn-ocr: make ONNX convertible…
- `training.models.CTCDecoder`: switch back
  from `tf.nn.ctc_beam_search_decoder()`
  to `tf.nn.ctc_greedy_decoder()`
  (because ONNX only implements `CTCGreedyDecoder`)
- `training.models.cnn_rnn_ocr_model(inference=True)` and
  `training.models.cnn_rnn_ocr_model4inference`:
  drop layer `tf.io.decode_raw()`
  (because ONNX does not implement `DecodePaddedRaw`)
- `Eynollah_ocr.run_cnn()`: expect bytes arrays from predictor
  instead of uint8
- `predictor`: to prevent segfaults when sending `tf.string` results
  via `shared_memory`, convert `np.object` to `np.bytes_` directly
2026-06-26 02:21:47 +02:00
Robert Sachunsky
45168178dc ModelZoo ONNX backend: log configured top provider (backend) 2026-06-26 02:13:39 +02:00
Robert Sachunsky
ef47f0ef09 training convert: only add characters_org.txt if it exists 2026-06-26 02:11:59 +02:00
Robert Sachunsky
42a3751e63 ModelZoo ONNX: avoid verbose logging 2026-06-19 22:01:39 +02:00
Robert Sachunsky
0bfbbfdc80 training.metrics: allow module init without TFA 2026-06-19 22:01:02 +02:00
Robert Sachunsky
eb4cae9dee training.models for cnn-rnn-ocr: avoid Conv1D(..channels_first..) 2026-06-16 17:28:10 +02:00
Robert Sachunsky
dfa651ef8a predictor: show full stacktrace before passing the exception over 2026-06-12 22:21:06 +02:00
vahidrezanezhad
89ce9de6fa musicregion is added to pagexml to label 2026-06-12 18:00:03 +02:00
vahidrezanezhad
303bdfe0e7 Amiri font which works for both arabic and latin 2026-06-12 18:00:03 +02:00
vahidrezanezhad
d2123a2746 FIXME: get label for decoration without type attribute 2026-06-12 18:00:03 +02:00
vahidrezanezhad
499e3d0715 trocr inference is integrated - works on CPU cause seg fault on GPU 2026-06-12 18:00:03 +02:00
vahidrezanezhad
a11c833fc1 bug fix: layout visualization 2026-06-12 18:00:03 +02:00
vahidrezanezhad
d0b3bb419f extracting ocr textline images and text: vertical lines threshold has changed to 1.4 2026-06-12 17:58:09 +02:00
vahidrezanezhad
4776ea9fc4 torch model ensembling is integrated 2026-06-12 17:58:09 +02:00
vahidrezanezhad
aba0138216 generate or update list of characters in the case of cnn-rnn ocr training 2026-06-12 17:58:09 +02:00
vahidrezanezhad
7f86a55ccb integrating transformer ocr 2026-06-12 17:58:07 +02:00
Robert Sachunsky
e9839a8b54 makefile to reload models: avoid ONNX conversion for cnn-rnn-ocr too 2026-06-12 15:00:36 +02:00
Robert Sachunsky
19504cb932 makefile to reload models: add target for SavedModel Keras format 2026-06-12 14:59:38 +02:00
Robert Sachunsky
60c9f4786c ModelZoo device selection: warn if model category still unmatched…
(and try GPU)
2026-06-12 14:58:32 +02:00
Robert Sachunsky
94082bc64a ModelZoo TF-Serving backend: deal with buggy .inputs signature…
work around TF bug that adds captured/unknown inputs to function signature
2026-06-12 14:56:44 +02:00
Robert Sachunsky
45c92eada2 models w/ multiple inputs yield a tuple for .input_shape 2026-06-12 14:55:46 +02:00
Robert Sachunsky
08946067ac ModelZoo ONNX backend: handle multiple inputs, too 2026-06-12 14:54:51 +02:00
Robert Sachunsky
9d2412080f training.models for cnn-rnn-ocr: fix config names for height/width…
- rename `image_height` → `input_height`
- rename `image_width` → `input_width`
2026-06-12 14:52:23 +02:00
Robert Sachunsky
4181e03bc9 training convert --rebuild for cnn-rnn-ocr: override charset file…
when rebuilding the inference model for cnn-rnn-ocr,
- open the old `characters_org.txt` file for the charset
- use it to pass the actual `n_classes` (overriding the config)
- use its path to pass the `characters_txt_file` (overriding the config)
2026-06-12 14:48:47 +02:00
Robert Sachunsky
348ac95ad3 Eynollah_ocr: drop fixed input sizes…
- tr-ocr: no need to resize images in advance (done by model, anyway)
- cnn-rnn-ocr: get model size from model's input shape
2026-06-03 20:59:00 +02:00
Robert Sachunsky
24c7d4c277 update trocr smoke test, add cnnrnn ocr smoke test 2026-06-03 20:58:05 +02:00
Robert Sachunsky
27ca9733db ModelZoo ONNX backend for inference: support multi-input or -output 2026-06-03 20:57:02 +02:00
Robert Sachunsky
38fe4d33ad Predictor for multi-input models: present as list instead of tuple…
(because TF-Serving expects that and cannot cast)
2026-06-03 20:56:00 +02:00
Robert Sachunsky
4e7e1c06b9 trocr viarant for Predictor runtime: no model size for input_shape…
Because transformers v4 and v5 API for image preprocessor differs,
and the model-internal image input sizes are actually irrelevant,
because the preprocessor will resize them anyway, and there is no
batch dimension (because the input images will have different shapes),
do not advertise this information in `.input_shape`.
2026-06-03 20:51:56 +02:00
Robert Sachunsky
f447a9f248 trocr: move preprocessor and decoder into model object, too…
- ModelZoo: drop `trocr_processor` model type
- `ModelZoo.load_models()`: use Predictor for `ocr_tr` models, too
- `ModelZoo.load_model()`: for `ocr_tr`, load processor and model,
  then define a function object as stand-in for the common model
  interface based on Keras (w/ `.predict_on_batch()`)
- Predictor: allow multi-input without actual batch dimension
  for `ocr_tr` models (because the model takes a list of original
  image arrays and resizes them to model shape internally)
- Eynollah_ocr: adapt (replacing preprocessing, prediction and
  decoding steps by a single `.predict()` call)
2026-06-03 03:41:44 +02:00
Robert Sachunsky
d2f2a1e06b Eynollah_ocr: correctly handle min_conf, improve writer…
- `min_conf_value_of_textline_text`: apply by skipping
  lines below threshold (instead of writing empty text),
  and delete their TextEquiv (if existing)
- `write_ocr()`: simplify, and ensure consistency between
  line and region level text correctly
2026-06-03 00:43:46 +02:00
Robert Sachunsky
8ffc4ed8d3 Eynollah_ocr: adapt to inference model, improve and simplify…
- drop `end_character` mechanics and `characters` model type
  for decoding output probability (not needed)
- drop `decode_batch_predictions()` and `num_to_char` model type
  (part of inference model)
- drop roughshot confidence estimation calculation
  (returned precisely by inference model)
- adapt model prediction to inference model: just omit zeros,
  map to bytes, filter OOV tokens and decode UTF-8 to str
- if no binarization input was provided, then compute it on the fly
  using `binarization` model
- also apply `min_conf_value_of_textline_text` (as for TrOCR)
- batching over entire page instead of region-wise
  (which underfilled batches)
- simplify and avoid copied redundant code
- rename `extracted_conf_value_merged` → `extracted_confs_merged`
- move `batched()` from `utils.utils_ocr` to `utils`
- drop `utils_ocr.distortion_free_resize()` (not needed)
- simplify `utils_ocr.break_curved_line_into_small_pieces_and_then_merge()`
- drop `utils_ocr.return_textline_contour_with_added_box_coordinate()`
  and `utils_ocr.return_rnn_cnn_ocr_of_given_textlines()` (not needed)
2026-06-02 21:20:06 +02:00
Robert Sachunsky
a391ee24e6 Predictor: handle multi-input and/or multi-output cases 2026-06-02 21:18:22 +02:00
Robert Sachunsky
c79b73dcc8 cnn-rnn-ocr: move CTC decoder and string decoder to inference model…
- ModelZoo: drop `num_to_char` and `characters` model types,
  also drop `_load_characters()` and `_load_num_to_char()` loaders
- `ModelZoo.load_models()`: use Predictor for `ocr` models, too
- `ModelZoo.load_model()`: delegate runtime/inference conversion of
  OCR models to `eynollah.training.models.cnn_rnn_ocr_model4inference`
- `training.models`: add (purely functional) Keras layer `CTCDecoder`
  for inference on top of softmax output, but using TF backend
  function instead of (broken) `Keras.backend.ctc_decode()`, while
  switching to beam search (instead of greedy) and also returning
  decoded path probability
- `training.models.cnn_rnn_ocr_model()` w/ `inference=True`:
  * add kwarg `characters_txt_file` for file path of character set
  * configure secondary tensor path on OCR graph for binarized input
    (additional input `image_bin`, averaging softmax outputs)
  * use new `CTCDecoder` layer and inverse `StringLookup` layer to
    decode from softmax output to tf.string; so inference models
    now have 2 inputs (RGB, binarized) and 2 outputs (text, prob)
  * since `np.dtype=object` cannot be handled by SharedMemory (as
    needed by Predictor queues), also replace tf.string by tf.uint8
    arrays
  * use this for `training convert` for OCR models w/ `--rebuild`
- `training.models.cnn_rnn_ocr_model4inference`:
  * new function which does the same but loads an existing OCR model
    in training configuration (i.e. without prior `inference=True`)
  * use this for `training convert` for OCR models w/o `--rebuild`
2026-06-02 20:26:42 +02:00
Robert Sachunsky
13f2f81c45 ModelZoo: support inference with ONNX/TensorRT…
- comment out ad-hoc conversion/loading of autosized models
- refactor predictor backends for model types into separate functions
- only attempt inference conversion of cnn-rnn-ocr model
  if applicable (`ctc_loss` layer still present)
- apply VRAM limits across model types
  (Keras, TF-Serving, ONNX)
- apply TF device selection across model types
  (Keras, TF-Serving)
- implement predictor backend for ONNX models:
  - using onnxruntime
  - covering CUDA and TensorRT providers
  - trying to support manual device selection
  - hiding session management details
  - converting float32 to float16
2026-05-28 18:08:08 +02:00