deepdoctection 0.31__py3-none-any.whl → 0.33__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.
Potentially problematic release.
This version of deepdoctection might be problematic. Click here for more details.
- deepdoctection/__init__.py +16 -29
- deepdoctection/analyzer/dd.py +70 -59
- deepdoctection/configs/conf_dd_one.yaml +34 -31
- deepdoctection/dataflow/common.py +9 -5
- deepdoctection/dataflow/custom.py +5 -5
- deepdoctection/dataflow/custom_serialize.py +75 -18
- deepdoctection/dataflow/parallel_map.py +3 -3
- deepdoctection/dataflow/serialize.py +4 -4
- deepdoctection/dataflow/stats.py +3 -3
- deepdoctection/datapoint/annotation.py +41 -56
- deepdoctection/datapoint/box.py +9 -8
- deepdoctection/datapoint/convert.py +6 -6
- deepdoctection/datapoint/image.py +56 -44
- deepdoctection/datapoint/view.py +245 -150
- deepdoctection/datasets/__init__.py +1 -4
- deepdoctection/datasets/adapter.py +35 -26
- deepdoctection/datasets/base.py +14 -12
- deepdoctection/datasets/dataflow_builder.py +3 -3
- deepdoctection/datasets/info.py +24 -26
- deepdoctection/datasets/instances/doclaynet.py +51 -51
- deepdoctection/datasets/instances/fintabnet.py +46 -46
- deepdoctection/datasets/instances/funsd.py +25 -24
- deepdoctection/datasets/instances/iiitar13k.py +13 -10
- deepdoctection/datasets/instances/layouttest.py +4 -3
- deepdoctection/datasets/instances/publaynet.py +5 -5
- deepdoctection/datasets/instances/pubtables1m.py +24 -21
- deepdoctection/datasets/instances/pubtabnet.py +32 -30
- deepdoctection/datasets/instances/rvlcdip.py +30 -30
- deepdoctection/datasets/instances/xfund.py +26 -26
- deepdoctection/datasets/save.py +6 -6
- deepdoctection/eval/__init__.py +1 -4
- deepdoctection/eval/accmetric.py +32 -33
- deepdoctection/eval/base.py +8 -9
- deepdoctection/eval/cocometric.py +15 -13
- deepdoctection/eval/eval.py +41 -37
- deepdoctection/eval/tedsmetric.py +30 -23
- deepdoctection/eval/tp_eval_callback.py +16 -19
- deepdoctection/extern/__init__.py +2 -7
- deepdoctection/extern/base.py +339 -134
- deepdoctection/extern/d2detect.py +85 -113
- deepdoctection/extern/deskew.py +14 -11
- deepdoctection/extern/doctrocr.py +141 -130
- deepdoctection/extern/fastlang.py +27 -18
- deepdoctection/extern/hfdetr.py +71 -62
- deepdoctection/extern/hflayoutlm.py +504 -211
- deepdoctection/extern/hflm.py +230 -0
- deepdoctection/extern/model.py +488 -302
- deepdoctection/extern/pdftext.py +23 -19
- deepdoctection/extern/pt/__init__.py +1 -3
- deepdoctection/extern/pt/nms.py +6 -2
- deepdoctection/extern/pt/ptutils.py +29 -19
- deepdoctection/extern/tessocr.py +39 -38
- deepdoctection/extern/texocr.py +18 -18
- deepdoctection/extern/tp/tfutils.py +57 -9
- deepdoctection/extern/tp/tpcompat.py +21 -14
- deepdoctection/extern/tp/tpfrcnn/__init__.py +20 -0
- deepdoctection/extern/tp/tpfrcnn/common.py +7 -3
- deepdoctection/extern/tp/tpfrcnn/config/__init__.py +20 -0
- deepdoctection/extern/tp/tpfrcnn/config/config.py +13 -10
- deepdoctection/extern/tp/tpfrcnn/modeling/__init__.py +20 -0
- deepdoctection/extern/tp/tpfrcnn/modeling/backbone.py +18 -8
- deepdoctection/extern/tp/tpfrcnn/modeling/generalized_rcnn.py +12 -6
- deepdoctection/extern/tp/tpfrcnn/modeling/model_box.py +14 -9
- deepdoctection/extern/tp/tpfrcnn/modeling/model_cascade.py +8 -5
- deepdoctection/extern/tp/tpfrcnn/modeling/model_fpn.py +22 -17
- deepdoctection/extern/tp/tpfrcnn/modeling/model_frcnn.py +21 -14
- deepdoctection/extern/tp/tpfrcnn/modeling/model_mrcnn.py +19 -11
- deepdoctection/extern/tp/tpfrcnn/modeling/model_rpn.py +15 -10
- deepdoctection/extern/tp/tpfrcnn/predict.py +9 -4
- deepdoctection/extern/tp/tpfrcnn/preproc.py +12 -8
- deepdoctection/extern/tp/tpfrcnn/utils/__init__.py +20 -0
- deepdoctection/extern/tp/tpfrcnn/utils/box_ops.py +10 -2
- deepdoctection/extern/tpdetect.py +45 -53
- deepdoctection/mapper/__init__.py +3 -8
- deepdoctection/mapper/cats.py +27 -29
- deepdoctection/mapper/cocostruct.py +10 -10
- deepdoctection/mapper/d2struct.py +27 -26
- deepdoctection/mapper/hfstruct.py +13 -8
- deepdoctection/mapper/laylmstruct.py +178 -37
- deepdoctection/mapper/maputils.py +12 -11
- deepdoctection/mapper/match.py +2 -2
- deepdoctection/mapper/misc.py +11 -9
- deepdoctection/mapper/pascalstruct.py +4 -4
- deepdoctection/mapper/prodigystruct.py +5 -5
- deepdoctection/mapper/pubstruct.py +84 -92
- deepdoctection/mapper/tpstruct.py +5 -5
- deepdoctection/mapper/xfundstruct.py +33 -33
- deepdoctection/pipe/__init__.py +1 -1
- deepdoctection/pipe/anngen.py +12 -14
- deepdoctection/pipe/base.py +52 -106
- deepdoctection/pipe/common.py +72 -59
- deepdoctection/pipe/concurrency.py +16 -11
- deepdoctection/pipe/doctectionpipe.py +24 -21
- deepdoctection/pipe/language.py +20 -25
- deepdoctection/pipe/layout.py +20 -16
- deepdoctection/pipe/lm.py +75 -105
- deepdoctection/pipe/order.py +194 -89
- deepdoctection/pipe/refine.py +111 -124
- deepdoctection/pipe/segment.py +156 -161
- deepdoctection/pipe/{cell.py → sub_layout.py} +50 -40
- deepdoctection/pipe/text.py +37 -36
- deepdoctection/pipe/transform.py +19 -16
- deepdoctection/train/__init__.py +6 -12
- deepdoctection/train/d2_frcnn_train.py +48 -41
- deepdoctection/train/hf_detr_train.py +41 -30
- deepdoctection/train/hf_layoutlm_train.py +153 -135
- deepdoctection/train/tp_frcnn_train.py +32 -31
- deepdoctection/utils/concurrency.py +1 -1
- deepdoctection/utils/context.py +13 -6
- deepdoctection/utils/develop.py +4 -4
- deepdoctection/utils/env_info.py +87 -125
- deepdoctection/utils/file_utils.py +6 -11
- deepdoctection/utils/fs.py +22 -18
- deepdoctection/utils/identifier.py +2 -2
- deepdoctection/utils/logger.py +16 -15
- deepdoctection/utils/metacfg.py +7 -7
- deepdoctection/utils/mocks.py +93 -0
- deepdoctection/utils/pdf_utils.py +11 -11
- deepdoctection/utils/settings.py +185 -181
- deepdoctection/utils/tqdm.py +1 -1
- deepdoctection/utils/transform.py +14 -9
- deepdoctection/utils/types.py +104 -0
- deepdoctection/utils/utils.py +7 -7
- deepdoctection/utils/viz.py +74 -72
- {deepdoctection-0.31.dist-info → deepdoctection-0.33.dist-info}/METADATA +30 -21
- deepdoctection-0.33.dist-info/RECORD +146 -0
- {deepdoctection-0.31.dist-info → deepdoctection-0.33.dist-info}/WHEEL +1 -1
- deepdoctection/utils/detection_types.py +0 -68
- deepdoctection-0.31.dist-info/RECORD +0 -144
- {deepdoctection-0.31.dist-info → deepdoctection-0.33.dist-info}/LICENSE +0 -0
- {deepdoctection-0.31.dist-info → deepdoctection-0.33.dist-info}/top_level.txt +0 -0
deepdoctection/extern/hfdetr.py
CHANGED
|
@@ -18,27 +18,26 @@
|
|
|
18
18
|
"""
|
|
19
19
|
HF Detr model for object detection.
|
|
20
20
|
"""
|
|
21
|
+
from __future__ import annotations
|
|
21
22
|
|
|
23
|
+
import os
|
|
22
24
|
from abc import ABC
|
|
23
25
|
from pathlib import Path
|
|
24
|
-
from typing import
|
|
25
|
-
|
|
26
|
-
from
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
from .pt.ptutils import set_torch_auto_device
|
|
36
|
-
|
|
37
|
-
if pytorch_available():
|
|
26
|
+
from typing import Literal, Mapping, Optional, Sequence, Union
|
|
27
|
+
|
|
28
|
+
from lazy_imports import try_import
|
|
29
|
+
|
|
30
|
+
from ..utils.file_utils import get_pytorch_requirement, get_transformers_requirement
|
|
31
|
+
from ..utils.settings import DefaultType, ObjectTypes, TypeOrStr, get_type
|
|
32
|
+
from ..utils.types import PathLikeOrStr, PixelValues, Requirement
|
|
33
|
+
from .base import DetectionResult, ModelCategories, ObjectDetector
|
|
34
|
+
from .pt.ptutils import get_torch_device
|
|
35
|
+
|
|
36
|
+
with try_import() as pt_import_guard:
|
|
38
37
|
import torch # pylint: disable=W0611
|
|
39
38
|
from torchvision.ops import boxes as box_ops # type: ignore
|
|
40
39
|
|
|
41
|
-
|
|
40
|
+
with try_import() as tr_import_guard:
|
|
42
41
|
from transformers import ( # pylint: disable=W0611
|
|
43
42
|
AutoFeatureExtractor,
|
|
44
43
|
DetrFeatureExtractor,
|
|
@@ -48,19 +47,19 @@ if transformers_available():
|
|
|
48
47
|
|
|
49
48
|
|
|
50
49
|
def _detr_post_processing(
|
|
51
|
-
boxes:
|
|
52
|
-
) ->
|
|
50
|
+
boxes: torch.Tensor, scores: torch.Tensor, labels: torch.Tensor, nms_thresh: float
|
|
51
|
+
) -> torch.Tensor:
|
|
53
52
|
return box_ops.batched_nms(boxes.float(), scores, labels, nms_thresh)
|
|
54
53
|
|
|
55
54
|
|
|
56
55
|
def detr_predict_image(
|
|
57
|
-
np_img:
|
|
58
|
-
predictor:
|
|
59
|
-
feature_extractor:
|
|
60
|
-
device:
|
|
56
|
+
np_img: PixelValues,
|
|
57
|
+
predictor: TableTransformerForObjectDetection,
|
|
58
|
+
feature_extractor: DetrFeatureExtractor,
|
|
59
|
+
device: torch.device,
|
|
61
60
|
threshold: float,
|
|
62
61
|
nms_threshold: float,
|
|
63
|
-
) ->
|
|
62
|
+
) -> list[DetectionResult]:
|
|
64
63
|
"""
|
|
65
64
|
Calling predictor. Before doing that, tensors must be transferred to the device where the model is loaded. After
|
|
66
65
|
running prediction it will present prediction in DetectionResult format-
|
|
@@ -99,43 +98,45 @@ def detr_predict_image(
|
|
|
99
98
|
class HFDetrDerivedDetectorMixin(ObjectDetector, ABC):
|
|
100
99
|
"""Base class for Detr object detector. This class only implements the basic wrapper functions"""
|
|
101
100
|
|
|
102
|
-
def __init__(self, categories: Mapping[
|
|
101
|
+
def __init__(self, categories: Mapping[int, TypeOrStr], filter_categories: Optional[Sequence[TypeOrStr]] = None):
|
|
103
102
|
"""
|
|
104
103
|
|
|
105
104
|
:param categories: A dict with key (indices) and values (category names).
|
|
106
105
|
:param filter_categories: The model might return objects that are not supposed to be predicted and that should
|
|
107
106
|
be filtered. Pass a list of category names that must not be returned
|
|
108
107
|
"""
|
|
109
|
-
self.categories =
|
|
108
|
+
self.categories = ModelCategories(init_categories=categories)
|
|
110
109
|
if filter_categories:
|
|
111
|
-
filter_categories =
|
|
112
|
-
self.filter_categories = filter_categories
|
|
110
|
+
self.categories.filter_categories = tuple(get_type(cat) for cat in filter_categories)
|
|
113
111
|
|
|
114
|
-
def _map_category_names(self, detection_results:
|
|
112
|
+
def _map_category_names(self, detection_results: list[DetectionResult]) -> list[DetectionResult]:
|
|
115
113
|
"""
|
|
116
114
|
Populating category names to detection results. Will also filter categories
|
|
117
115
|
|
|
118
116
|
:param detection_results: list of detection results
|
|
119
117
|
:return: List of detection results with attribute class_name populated
|
|
120
118
|
"""
|
|
121
|
-
filtered_detection_result:
|
|
119
|
+
filtered_detection_result: list[DetectionResult] = []
|
|
120
|
+
shifted_categories = self.categories.shift_category_ids(shift_by=-1)
|
|
122
121
|
for result in detection_results:
|
|
123
|
-
result.class_name =
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
if
|
|
127
|
-
if result.
|
|
122
|
+
result.class_name = shifted_categories.get(
|
|
123
|
+
result.class_id if result.class_id is not None else -1, DefaultType.DEFAULT_TYPE
|
|
124
|
+
)
|
|
125
|
+
if result.class_name != DefaultType.DEFAULT_TYPE:
|
|
126
|
+
if result.class_id is not None:
|
|
127
|
+
result.class_id += 1
|
|
128
128
|
filtered_detection_result.append(result)
|
|
129
|
-
else:
|
|
130
|
-
filtered_detection_result.append(result)
|
|
131
129
|
|
|
132
130
|
return filtered_detection_result
|
|
133
131
|
|
|
134
132
|
@staticmethod
|
|
135
|
-
def get_name(path_weights:
|
|
133
|
+
def get_name(path_weights: PathLikeOrStr) -> str:
|
|
136
134
|
"""Returns the name of the model"""
|
|
137
135
|
return "Transformers_Tatr_" + "_".join(Path(path_weights).parts[-2:])
|
|
138
136
|
|
|
137
|
+
def get_category_names(self) -> tuple[ObjectTypes, ...]:
|
|
138
|
+
return self.categories.get_categories(as_dict=False)
|
|
139
|
+
|
|
139
140
|
|
|
140
141
|
class HFDetrDerivedDetector(HFDetrDerivedDetectorMixin):
|
|
141
142
|
"""
|
|
@@ -164,11 +165,11 @@ class HFDetrDerivedDetector(HFDetrDerivedDetectorMixin):
|
|
|
164
165
|
|
|
165
166
|
def __init__(
|
|
166
167
|
self,
|
|
167
|
-
path_config_json:
|
|
168
|
-
path_weights:
|
|
169
|
-
path_feature_extractor_config_json:
|
|
170
|
-
categories: Mapping[
|
|
171
|
-
device: Optional[Literal["cpu", "cuda"]] = None,
|
|
168
|
+
path_config_json: PathLikeOrStr,
|
|
169
|
+
path_weights: PathLikeOrStr,
|
|
170
|
+
path_feature_extractor_config_json: PathLikeOrStr,
|
|
171
|
+
categories: Mapping[int, TypeOrStr],
|
|
172
|
+
device: Optional[Union[Literal["cpu", "cuda"], torch.device]] = None,
|
|
172
173
|
filter_categories: Optional[Sequence[TypeOrStr]] = None,
|
|
173
174
|
):
|
|
174
175
|
"""
|
|
@@ -183,9 +184,9 @@ class HFDetrDerivedDetector(HFDetrDerivedDetectorMixin):
|
|
|
183
184
|
"""
|
|
184
185
|
super().__init__(categories, filter_categories)
|
|
185
186
|
|
|
186
|
-
self.path_config = path_config_json
|
|
187
|
-
self.path_weights = path_weights
|
|
188
|
-
self.path_feature_extractor_config = path_feature_extractor_config_json
|
|
187
|
+
self.path_config = Path(path_config_json)
|
|
188
|
+
self.path_weights = Path(path_weights)
|
|
189
|
+
self.path_feature_extractor_config = Path(path_feature_extractor_config_json)
|
|
189
190
|
|
|
190
191
|
self.name = self.get_name(self.path_weights)
|
|
191
192
|
self.model_id = self.get_model_id()
|
|
@@ -195,13 +196,10 @@ class HFDetrDerivedDetector(HFDetrDerivedDetectorMixin):
|
|
|
195
196
|
self.hf_detr_predictor = self.get_model(self.path_weights, self.config)
|
|
196
197
|
self.feature_extractor = self.get_pre_processor(self.path_feature_extractor_config)
|
|
197
198
|
|
|
198
|
-
|
|
199
|
-
self.device = device
|
|
200
|
-
else:
|
|
201
|
-
self.device = set_torch_auto_device()
|
|
199
|
+
self.device = get_torch_device(device)
|
|
202
200
|
self.hf_detr_predictor.to(self.device)
|
|
203
201
|
|
|
204
|
-
def predict(self, np_img:
|
|
202
|
+
def predict(self, np_img: PixelValues) -> list[DetectionResult]:
|
|
205
203
|
results = detr_predict_image(
|
|
206
204
|
np_img,
|
|
207
205
|
self.hf_detr_predictor,
|
|
@@ -213,7 +211,7 @@ class HFDetrDerivedDetector(HFDetrDerivedDetectorMixin):
|
|
|
213
211
|
return self._map_category_names(results)
|
|
214
212
|
|
|
215
213
|
@staticmethod
|
|
216
|
-
def get_model(path_weights:
|
|
214
|
+
def get_model(path_weights: PathLikeOrStr, config: PretrainedConfig) -> TableTransformerForObjectDetection:
|
|
217
215
|
"""
|
|
218
216
|
Builds the Detr model
|
|
219
217
|
|
|
@@ -222,45 +220,54 @@ class HFDetrDerivedDetector(HFDetrDerivedDetectorMixin):
|
|
|
222
220
|
:return: TableTransformerForObjectDetection instance
|
|
223
221
|
"""
|
|
224
222
|
return TableTransformerForObjectDetection.from_pretrained(
|
|
225
|
-
pretrained_model_name_or_path=path_weights, config=config
|
|
223
|
+
pretrained_model_name_or_path=os.fspath(path_weights), config=config
|
|
226
224
|
)
|
|
227
225
|
|
|
228
226
|
@staticmethod
|
|
229
|
-
def get_pre_processor(path_feature_extractor_config:
|
|
227
|
+
def get_pre_processor(path_feature_extractor_config: PathLikeOrStr) -> DetrFeatureExtractor:
|
|
230
228
|
"""
|
|
231
229
|
Builds the feature extractor
|
|
232
230
|
|
|
233
231
|
:return: DetrFeatureExtractor
|
|
234
232
|
"""
|
|
235
|
-
return AutoFeatureExtractor.from_pretrained(
|
|
233
|
+
return AutoFeatureExtractor.from_pretrained(
|
|
234
|
+
pretrained_model_name_or_path=os.fspath(path_feature_extractor_config)
|
|
235
|
+
)
|
|
236
236
|
|
|
237
237
|
@staticmethod
|
|
238
|
-
def get_config(path_config:
|
|
238
|
+
def get_config(path_config: PathLikeOrStr) -> PretrainedConfig:
|
|
239
239
|
"""
|
|
240
240
|
Builds the config
|
|
241
241
|
|
|
242
242
|
:param path_config: The path to the json config.
|
|
243
243
|
:return: PretrainedConfig instance
|
|
244
244
|
"""
|
|
245
|
-
config = PretrainedConfig.from_pretrained(pretrained_model_name_or_path=path_config)
|
|
245
|
+
config = PretrainedConfig.from_pretrained(pretrained_model_name_or_path=os.fspath(path_config))
|
|
246
246
|
config.use_timm_backbone = True
|
|
247
247
|
config.threshold = 0.1
|
|
248
248
|
config.nms_threshold = 0.05
|
|
249
249
|
return config
|
|
250
250
|
|
|
251
251
|
@classmethod
|
|
252
|
-
def get_requirements(cls) ->
|
|
252
|
+
def get_requirements(cls) -> list[Requirement]:
|
|
253
253
|
return [get_pytorch_requirement(), get_transformers_requirement()]
|
|
254
254
|
|
|
255
|
-
def clone(self) ->
|
|
255
|
+
def clone(self) -> HFDetrDerivedDetector:
|
|
256
256
|
return self.__class__(
|
|
257
|
-
self.path_config,
|
|
257
|
+
self.path_config,
|
|
258
|
+
self.path_weights,
|
|
259
|
+
self.path_feature_extractor_config,
|
|
260
|
+
self.categories.get_categories(),
|
|
261
|
+
self.device,
|
|
262
|
+
self.categories.filter_categories,
|
|
258
263
|
)
|
|
259
264
|
|
|
260
265
|
@staticmethod
|
|
261
266
|
def get_wrapped_model(
|
|
262
|
-
path_config_json:
|
|
263
|
-
|
|
267
|
+
path_config_json: PathLikeOrStr,
|
|
268
|
+
path_weights: PathLikeOrStr,
|
|
269
|
+
device: Optional[Union[Literal["cpu", "cuda"], torch.device]] = None,
|
|
270
|
+
) -> TableTransformerForObjectDetection:
|
|
264
271
|
"""
|
|
265
272
|
Get the wrapped model
|
|
266
273
|
|
|
@@ -271,6 +278,8 @@ class HFDetrDerivedDetector(HFDetrDerivedDetectorMixin):
|
|
|
271
278
|
"""
|
|
272
279
|
config = HFDetrDerivedDetector.get_config(path_config_json)
|
|
273
280
|
hf_detr_predictor = HFDetrDerivedDetector.get_model(path_weights, config)
|
|
274
|
-
|
|
275
|
-
device = set_torch_auto_device()
|
|
281
|
+
device = get_torch_device(device)
|
|
276
282
|
return hf_detr_predictor.to(device)
|
|
283
|
+
|
|
284
|
+
def clear_model(self) -> None:
|
|
285
|
+
self.hf_detr_predictor = None
|