python-doctr 1.0.1__py3-none-any.whl → 1.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- doctr/cli/__init__.py +0 -0
- doctr/cli/main.py +669 -0
- doctr/datasets/__init__.py +2 -0
- doctr/datasets/coco_text.py +4 -2
- doctr/datasets/cord.py +2 -1
- doctr/datasets/datasets/base.py +12 -24
- doctr/datasets/datasets/pytorch.py +14 -4
- doctr/datasets/detection.py +14 -3
- doctr/datasets/doc_artefacts.py +2 -1
- doctr/datasets/funsd.py +2 -1
- doctr/datasets/generator/base.py +121 -37
- doctr/datasets/generator/pytorch.py +5 -6
- doctr/datasets/ic03.py +9 -7
- doctr/datasets/ic13.py +4 -2
- doctr/datasets/iiit5k.py +2 -1
- doctr/datasets/iiithws.py +4 -2
- doctr/datasets/imgur5k.py +4 -2
- doctr/datasets/layout.py +129 -0
- doctr/datasets/mjsynth.py +4 -2
- doctr/datasets/ocr.py +2 -1
- doctr/datasets/orientation.py +2 -1
- doctr/datasets/recognition.py +5 -4
- doctr/datasets/sroie.py +2 -1
- doctr/datasets/svhn.py +2 -1
- doctr/datasets/svt.py +10 -8
- doctr/datasets/synthtext.py +2 -1
- doctr/datasets/table_structure.py +97 -0
- doctr/datasets/utils.py +104 -21
- doctr/datasets/vocabs.py +320 -148
- doctr/datasets/wildreceipt.py +4 -2
- doctr/file_utils.py +3 -1
- doctr/io/__init__.py +3 -0
- doctr/io/elements.py +381 -271
- doctr/io/exporters.py +1915 -0
- doctr/io/figures.py +313 -0
- doctr/io/image/base.py +54 -7
- doctr/io/image/pytorch.py +13 -5
- doctr/io/pdf.py +6 -5
- doctr/io/reader.py +268 -18
- doctr/io/sources.py +636 -0
- doctr/models/__init__.py +3 -0
- doctr/models/_utils.py +206 -13
- doctr/models/builder.py +590 -137
- doctr/models/classification/__init__.py +3 -0
- doctr/models/classification/hgnetv2/__init__.py +1 -0
- doctr/models/classification/hgnetv2/pytorch.py +352 -0
- doctr/models/classification/magc_resnet/pytorch.py +1 -1
- doctr/models/classification/mobilenet/pytorch.py +6 -6
- doctr/models/classification/predictor/pytorch.py +34 -2
- doctr/models/classification/resnet/pytorch.py +5 -5
- doctr/models/classification/starnet/__init__.py +1 -0
- doctr/models/classification/starnet/pytorch.py +202 -0
- doctr/models/classification/textnet/pytorch.py +6 -5
- doctr/models/classification/vgg/pytorch.py +3 -2
- doctr/models/classification/vip/layers/pytorch.py +4 -4
- doctr/models/classification/vip/pytorch.py +4 -4
- doctr/models/classification/vit/pytorch.py +3 -3
- doctr/models/classification/vit_det/__init__.py +1 -0
- doctr/models/classification/vit_det/layers/__init__.py +1 -0
- doctr/models/classification/vit_det/layers/pytorch.py +215 -0
- doctr/models/classification/vit_det/pytorch.py +346 -0
- doctr/models/classification/zoo.py +14 -2
- doctr/models/detection/_utils/base.py +23 -7
- doctr/models/detection/core.py +32 -18
- doctr/models/detection/differentiable_binarization/base.py +23 -9
- doctr/models/detection/differentiable_binarization/pytorch.py +10 -4
- doctr/models/detection/fast/base.py +22 -6
- doctr/models/detection/fast/pytorch.py +24 -11
- doctr/models/detection/linknet/base.py +22 -6
- doctr/models/detection/linknet/pytorch.py +10 -3
- doctr/models/detection/predictor/pytorch.py +43 -2
- doctr/models/detection/zoo.py +6 -2
- doctr/models/factory/hub.py +59 -41
- doctr/models/formula/__init__.py +2 -0
- doctr/models/formula/formulanet/__init__.py +2 -0
- doctr/models/formula/formulanet/base.py +70 -0
- doctr/models/formula/formulanet/pytorch.py +587 -0
- doctr/models/formula/predictor/__init__.py +1 -0
- doctr/models/formula/predictor/pytorch.py +132 -0
- doctr/models/formula/zoo.py +102 -0
- doctr/models/kie_predictor/base.py +23 -1
- doctr/models/kie_predictor/pytorch.py +87 -15
- doctr/models/layout/__init__.py +2 -0
- doctr/models/layout/lw_detr/__init__.py +1 -0
- doctr/models/layout/lw_detr/base.py +309 -0
- doctr/models/layout/lw_detr/layers/__init__.py +1 -0
- doctr/models/layout/lw_detr/layers/pytorch.py +785 -0
- doctr/models/layout/lw_detr/pytorch.py +895 -0
- doctr/models/layout/predictor/__init__.py +1 -0
- doctr/models/layout/predictor/pytorch.py +114 -0
- doctr/models/layout/zoo.py +94 -0
- doctr/models/modules/layers/pytorch.py +139 -3
- doctr/models/modules/transformer/pytorch.py +151 -31
- doctr/models/predictor/base.py +415 -17
- doctr/models/predictor/pytorch.py +109 -12
- doctr/models/preprocessor/pytorch.py +82 -25
- doctr/models/reading_order/__init__.py +1 -0
- doctr/models/reading_order/base.py +1127 -0
- doctr/models/recognition/core.py +14 -1
- doctr/models/recognition/crnn/pytorch.py +40 -16
- doctr/models/recognition/master/base.py +4 -1
- doctr/models/recognition/master/pytorch.py +84 -22
- doctr/models/recognition/parseq/base.py +4 -1
- doctr/models/recognition/parseq/pytorch.py +126 -25
- doctr/models/recognition/predictor/_utils.py +131 -14
- doctr/models/recognition/predictor/pytorch.py +68 -12
- doctr/models/recognition/sar/pytorch.py +21 -7
- doctr/models/recognition/utils.py +80 -50
- doctr/models/recognition/viptr/pytorch.py +38 -14
- doctr/models/recognition/vitstr/base.py +4 -1
- doctr/models/recognition/vitstr/pytorch.py +11 -7
- doctr/models/recognition/zoo.py +36 -9
- doctr/models/table_structure/__init__.py +2 -0
- doctr/models/table_structure/grid.py +430 -0
- doctr/models/table_structure/predictor/__init__.py +1 -0
- doctr/models/table_structure/predictor/pytorch.py +231 -0
- doctr/models/table_structure/tablecenternet/__init__.py +2 -0
- doctr/models/table_structure/tablecenternet/base.py +675 -0
- doctr/models/table_structure/tablecenternet/pytorch.py +525 -0
- doctr/models/table_structure/zoo.py +87 -0
- doctr/models/utils/pytorch.py +381 -25
- doctr/models/zoo.py +141 -4
- doctr/transforms/functional/base.py +6 -0
- doctr/transforms/functional/pytorch.py +134 -6
- doctr/transforms/modules/base.py +261 -82
- doctr/transforms/modules/pytorch.py +396 -76
- doctr/utils/common_types.py +21 -1
- doctr/utils/data.py +24 -11
- doctr/utils/fonts.py +64 -17
- doctr/utils/geometry.py +167 -28
- doctr/utils/metrics.py +481 -19
- doctr/utils/multithreading.py +127 -4
- doctr/utils/reconstitution.py +696 -132
- doctr/utils/visualization.py +73 -10
- doctr/version.py +1 -1
- {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/METADATA +119 -249
- python_doctr-1.2.0.dist-info/RECORD +191 -0
- {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/WHEEL +1 -1
- python_doctr-1.2.0.dist-info/entry_points.txt +2 -0
- python_doctr-1.0.1.dist-info/RECORD +0 -149
- {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/licenses/LICENSE +0 -0
- {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/top_level.txt +0 -0
- {python_doctr-1.0.1.dist-info → python_doctr-1.2.0.dist-info}/zip-safe +0 -0
doctr/utils/multithreading.py
CHANGED
|
@@ -6,13 +6,18 @@
|
|
|
6
6
|
|
|
7
7
|
import multiprocessing as mp
|
|
8
8
|
import os
|
|
9
|
-
|
|
9
|
+
import threading
|
|
10
|
+
from collections.abc import Callable, Generator, Iterable, Iterator
|
|
11
|
+
from concurrent.futures import ThreadPoolExecutor
|
|
12
|
+
from itertools import islice
|
|
10
13
|
from multiprocessing.pool import ThreadPool
|
|
11
14
|
from typing import Any
|
|
12
15
|
|
|
16
|
+
import numpy as np
|
|
17
|
+
|
|
13
18
|
from doctr.file_utils import ENV_VARS_TRUE_VALUES
|
|
14
19
|
|
|
15
|
-
__all__ = ["multithread_exec"]
|
|
20
|
+
__all__ = ["multithread_exec", "chunked", "prefetch", "prefetch_chunks"]
|
|
16
21
|
|
|
17
22
|
|
|
18
23
|
def multithread_exec(func: Callable[[Any], Any], seq: Iterable[Any], threads: int | None = None) -> Iterator[Any]:
|
|
@@ -36,13 +41,131 @@ def multithread_exec(func: Callable[[Any], Any], seq: Iterable[Any], threads: in
|
|
|
36
41
|
you might want to disable multiprocessing. To achieve that, set 'DOCTR_MULTIPROCESSING_DISABLE' to 'TRUE'.
|
|
37
42
|
"""
|
|
38
43
|
threads = threads if isinstance(threads, int) else min(16, mp.cpu_count())
|
|
44
|
+
items = seq if isinstance(seq, (list, tuple)) else list(seq)
|
|
45
|
+
# Never spawn more workers than items - single-item calls skip pool startup entirely
|
|
46
|
+
threads = min(threads, len(items))
|
|
39
47
|
# Single-thread
|
|
40
48
|
if threads < 2 or os.environ.get("DOCTR_MULTIPROCESSING_DISABLE", "").upper() in ENV_VARS_TRUE_VALUES:
|
|
41
|
-
results = map(func,
|
|
49
|
+
results = map(func, items)
|
|
42
50
|
# Multi-threading
|
|
43
51
|
else:
|
|
44
52
|
with ThreadPool(threads) as tp:
|
|
45
53
|
# ThreadPool's map function returns a list, but seq could be of a different type
|
|
46
54
|
# That's why wrapping result in map to return iterator
|
|
47
|
-
results = map(lambda x: x, tp.map(func,
|
|
55
|
+
results = map(lambda x: x, tp.map(func, items)) # noqa: C417
|
|
48
56
|
return results
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def chunked(seq: Iterable[Any], size: int) -> Iterator[list[Any]]:
|
|
60
|
+
"""Split an iterable into consecutive lists of at most `size` elements, consuming it lazily
|
|
61
|
+
|
|
62
|
+
>>> from doctr.utils.multithreading import chunked
|
|
63
|
+
>>> list(chunked(range(5), 2))
|
|
64
|
+
[[0, 1], [2, 3], [4]]
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
seq: iterable
|
|
68
|
+
size: the maximum number of elements per chunk
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
iterator of chunks
|
|
72
|
+
"""
|
|
73
|
+
if size < 1:
|
|
74
|
+
raise ValueError("the chunk size must be a positive integer")
|
|
75
|
+
iterator = iter(seq)
|
|
76
|
+
while chunk := list(islice(iterator, size)):
|
|
77
|
+
yield chunk
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
_EXHAUSTED = object()
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def prefetch(seq: Iterable[Any], enabled: bool = True) -> Generator[Any, None, None]:
|
|
84
|
+
"""Iterate over an iterable while producing its next element in a background thread
|
|
85
|
+
|
|
86
|
+
This overlaps the production of the next element (e.g. decoding the next pages of a document) with the
|
|
87
|
+
processing of the current one (e.g. running the models). At most one element is produced ahead, and the
|
|
88
|
+
iterable is only ever advanced by one thread at a time.
|
|
89
|
+
|
|
90
|
+
>>> from doctr.utils.multithreading import prefetch
|
|
91
|
+
>>> for item in prefetch(expensive_generator()):
|
|
92
|
+
... process(item)
|
|
93
|
+
|
|
94
|
+
Args:
|
|
95
|
+
seq: iterable
|
|
96
|
+
enabled: whether the elements are produced in a background thread
|
|
97
|
+
|
|
98
|
+
Returns:
|
|
99
|
+
generator over the elements of the iterable: closing it waits for the element being produced, if any
|
|
100
|
+
|
|
101
|
+
Notes:
|
|
102
|
+
Prefetching is disabled when 'DOCTR_MULTIPROCESSING_DISABLE' is set to 'TRUE'.
|
|
103
|
+
"""
|
|
104
|
+
if not enabled or os.environ.get("DOCTR_MULTIPROCESSING_DISABLE", "").upper() in ENV_VARS_TRUE_VALUES:
|
|
105
|
+
yield from seq
|
|
106
|
+
return
|
|
107
|
+
|
|
108
|
+
iterator = iter(seq)
|
|
109
|
+
try:
|
|
110
|
+
# On early exit, leaving the context waits for the element being produced, so that the iterable is
|
|
111
|
+
# never closed while it is advanced in the background thread
|
|
112
|
+
with ThreadPoolExecutor(max_workers=1, thread_name_prefix="doctr-prefetch") as executor:
|
|
113
|
+
future = executor.submit(next, iterator, _EXHAUSTED)
|
|
114
|
+
while (item := future.result()) is not _EXHAUSTED:
|
|
115
|
+
future = executor.submit(next, iterator, _EXHAUSTED)
|
|
116
|
+
yield item
|
|
117
|
+
finally:
|
|
118
|
+
close = getattr(iterator, "close", None)
|
|
119
|
+
if callable(close):
|
|
120
|
+
close()
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def prefetch_chunks(seq: Iterable[Any], size: int, enabled: bool = True) -> Generator[list[Any], None, None]:
|
|
124
|
+
"""Split an iterable into consecutive lists of at most `size` elements, producing the next chunk in a background
|
|
125
|
+
thread while the current one is processed
|
|
126
|
+
|
|
127
|
+
This is how the predictors consume their inputs: a lazy page source (or any iterator) has its next chunk decoded
|
|
128
|
+
ahead, while in-memory inputs (lists, tuples, arrays) have nothing to decode and are only sliced, without a
|
|
129
|
+
background thread.
|
|
130
|
+
|
|
131
|
+
>>> from doctr.utils.multithreading import prefetch_chunks
|
|
132
|
+
>>> for chunk in prefetch_chunks(lazy_source, 32):
|
|
133
|
+
... process(chunk)
|
|
134
|
+
|
|
135
|
+
Args:
|
|
136
|
+
seq: iterable
|
|
137
|
+
size: the maximum number of elements per chunk
|
|
138
|
+
enabled: whether the next chunk is produced in a background thread (ignored for in-memory inputs)
|
|
139
|
+
|
|
140
|
+
Returns:
|
|
141
|
+
generator of chunks. Closing it (e.g. when the consumer stops early, or on error) stops producing ahead after
|
|
142
|
+
the element in progress rather than completing a whole chunk, and releases the resources held by the
|
|
143
|
+
iteration over `seq` (e.g. an open PDF).
|
|
144
|
+
|
|
145
|
+
Notes:
|
|
146
|
+
Prefetching is disabled when 'DOCTR_MULTIPROCESSING_DISABLE' is set to 'TRUE'.
|
|
147
|
+
"""
|
|
148
|
+
in_memory = isinstance(seq, (list, tuple, np.ndarray))
|
|
149
|
+
stop = threading.Event()
|
|
150
|
+
|
|
151
|
+
def _until_stopped() -> Iterator[Any]:
|
|
152
|
+
iterator = iter(seq)
|
|
153
|
+
try:
|
|
154
|
+
for item in iterator:
|
|
155
|
+
yield item
|
|
156
|
+
if stop.is_set():
|
|
157
|
+
return
|
|
158
|
+
finally:
|
|
159
|
+
# Release the resources held by the iteration (e.g. an open PDF) as soon as it is over
|
|
160
|
+
close = getattr(iterator, "close", None)
|
|
161
|
+
if callable(close):
|
|
162
|
+
close()
|
|
163
|
+
|
|
164
|
+
chunks = prefetch(chunked(_until_stopped(), size), enabled=enabled and not in_memory)
|
|
165
|
+
try:
|
|
166
|
+
# Not `yield from`: on early exit, it would close `chunks` (waiting for a whole chunk) before `stop` is set
|
|
167
|
+
for chunk in chunks:
|
|
168
|
+
yield chunk
|
|
169
|
+
finally:
|
|
170
|
+
stop.set()
|
|
171
|
+
chunks.close()
|