deepdoctection 0.30__py3-none-any.whl → 0.32__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 +38 -29
- deepdoctection/analyzer/dd.py +36 -29
- deepdoctection/configs/conf_dd_one.yaml +34 -31
- deepdoctection/dataflow/base.py +0 -19
- deepdoctection/dataflow/custom.py +4 -3
- deepdoctection/dataflow/custom_serialize.py +14 -5
- deepdoctection/dataflow/parallel_map.py +12 -11
- deepdoctection/dataflow/serialize.py +5 -4
- deepdoctection/datapoint/annotation.py +35 -13
- deepdoctection/datapoint/box.py +3 -5
- deepdoctection/datapoint/convert.py +3 -1
- deepdoctection/datapoint/image.py +79 -36
- deepdoctection/datapoint/view.py +152 -49
- deepdoctection/datasets/__init__.py +1 -4
- deepdoctection/datasets/adapter.py +6 -3
- deepdoctection/datasets/base.py +86 -11
- deepdoctection/datasets/dataflow_builder.py +1 -1
- deepdoctection/datasets/info.py +4 -4
- deepdoctection/datasets/instances/doclaynet.py +3 -2
- deepdoctection/datasets/instances/fintabnet.py +2 -1
- deepdoctection/datasets/instances/funsd.py +2 -1
- deepdoctection/datasets/instances/iiitar13k.py +5 -2
- deepdoctection/datasets/instances/layouttest.py +4 -8
- deepdoctection/datasets/instances/publaynet.py +2 -2
- deepdoctection/datasets/instances/pubtables1m.py +6 -3
- deepdoctection/datasets/instances/pubtabnet.py +2 -1
- deepdoctection/datasets/instances/rvlcdip.py +2 -1
- deepdoctection/datasets/instances/xfund.py +2 -1
- deepdoctection/eval/__init__.py +1 -4
- deepdoctection/eval/accmetric.py +1 -1
- deepdoctection/eval/base.py +5 -4
- deepdoctection/eval/cocometric.py +2 -1
- deepdoctection/eval/eval.py +19 -15
- deepdoctection/eval/tedsmetric.py +14 -11
- deepdoctection/eval/tp_eval_callback.py +14 -7
- deepdoctection/extern/__init__.py +2 -7
- deepdoctection/extern/base.py +39 -13
- deepdoctection/extern/d2detect.py +182 -90
- deepdoctection/extern/deskew.py +36 -9
- deepdoctection/extern/doctrocr.py +265 -83
- deepdoctection/extern/fastlang.py +49 -9
- deepdoctection/extern/hfdetr.py +106 -55
- deepdoctection/extern/hflayoutlm.py +441 -122
- deepdoctection/extern/hflm.py +225 -0
- deepdoctection/extern/model.py +56 -47
- deepdoctection/extern/pdftext.py +10 -5
- deepdoctection/extern/pt/__init__.py +1 -3
- deepdoctection/extern/pt/nms.py +6 -2
- deepdoctection/extern/pt/ptutils.py +27 -18
- deepdoctection/extern/tessocr.py +134 -22
- deepdoctection/extern/texocr.py +6 -2
- deepdoctection/extern/tp/tfutils.py +43 -9
- deepdoctection/extern/tp/tpcompat.py +14 -11
- 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 +9 -6
- deepdoctection/extern/tp/tpfrcnn/modeling/__init__.py +20 -0
- deepdoctection/extern/tp/tpfrcnn/modeling/backbone.py +17 -7
- deepdoctection/extern/tp/tpfrcnn/modeling/generalized_rcnn.py +12 -6
- deepdoctection/extern/tp/tpfrcnn/modeling/model_box.py +9 -4
- deepdoctection/extern/tp/tpfrcnn/modeling/model_cascade.py +8 -5
- deepdoctection/extern/tp/tpfrcnn/modeling/model_fpn.py +16 -11
- deepdoctection/extern/tp/tpfrcnn/modeling/model_frcnn.py +17 -10
- deepdoctection/extern/tp/tpfrcnn/modeling/model_mrcnn.py +14 -8
- deepdoctection/extern/tp/tpfrcnn/modeling/model_rpn.py +15 -10
- deepdoctection/extern/tp/tpfrcnn/predict.py +9 -4
- deepdoctection/extern/tp/tpfrcnn/preproc.py +8 -9
- deepdoctection/extern/tp/tpfrcnn/utils/__init__.py +20 -0
- deepdoctection/extern/tp/tpfrcnn/utils/box_ops.py +10 -2
- deepdoctection/extern/tpdetect.py +54 -30
- deepdoctection/mapper/__init__.py +3 -8
- deepdoctection/mapper/d2struct.py +9 -7
- deepdoctection/mapper/hfstruct.py +7 -2
- deepdoctection/mapper/laylmstruct.py +164 -21
- deepdoctection/mapper/maputils.py +16 -3
- deepdoctection/mapper/misc.py +6 -3
- deepdoctection/mapper/prodigystruct.py +1 -1
- deepdoctection/mapper/pubstruct.py +10 -10
- deepdoctection/mapper/tpstruct.py +3 -3
- deepdoctection/pipe/__init__.py +1 -1
- deepdoctection/pipe/anngen.py +35 -8
- deepdoctection/pipe/base.py +53 -19
- deepdoctection/pipe/common.py +23 -13
- deepdoctection/pipe/concurrency.py +2 -1
- deepdoctection/pipe/doctectionpipe.py +2 -2
- deepdoctection/pipe/language.py +3 -2
- deepdoctection/pipe/layout.py +6 -3
- deepdoctection/pipe/lm.py +34 -66
- deepdoctection/pipe/order.py +142 -35
- deepdoctection/pipe/refine.py +26 -24
- deepdoctection/pipe/segment.py +21 -16
- deepdoctection/pipe/{cell.py → sub_layout.py} +30 -9
- deepdoctection/pipe/text.py +14 -8
- deepdoctection/pipe/transform.py +16 -9
- deepdoctection/train/__init__.py +6 -12
- deepdoctection/train/d2_frcnn_train.py +36 -28
- deepdoctection/train/hf_detr_train.py +26 -17
- deepdoctection/train/hf_layoutlm_train.py +133 -111
- deepdoctection/train/tp_frcnn_train.py +21 -19
- deepdoctection/utils/__init__.py +3 -0
- deepdoctection/utils/concurrency.py +1 -1
- deepdoctection/utils/context.py +2 -2
- deepdoctection/utils/env_info.py +41 -84
- deepdoctection/utils/error.py +84 -0
- deepdoctection/utils/file_utils.py +4 -15
- deepdoctection/utils/fs.py +7 -7
- deepdoctection/utils/logger.py +1 -0
- deepdoctection/utils/mocks.py +93 -0
- deepdoctection/utils/pdf_utils.py +5 -4
- deepdoctection/utils/settings.py +6 -1
- deepdoctection/utils/transform.py +1 -1
- deepdoctection/utils/utils.py +0 -6
- deepdoctection/utils/viz.py +48 -5
- {deepdoctection-0.30.dist-info → deepdoctection-0.32.dist-info}/METADATA +57 -73
- deepdoctection-0.32.dist-info/RECORD +146 -0
- {deepdoctection-0.30.dist-info → deepdoctection-0.32.dist-info}/WHEEL +1 -1
- deepdoctection-0.30.dist-info/RECORD +0 -143
- {deepdoctection-0.30.dist-info → deepdoctection-0.32.dist-info}/LICENSE +0 -0
- {deepdoctection-0.30.dist-info → deepdoctection-0.32.dist-info}/top_level.txt +0 -0
deepdoctection/extern/hfdetr.py
CHANGED
|
@@ -18,25 +18,25 @@
|
|
|
18
18
|
"""
|
|
19
19
|
HF Detr model for object detection.
|
|
20
20
|
"""
|
|
21
|
+
from __future__ import annotations
|
|
21
22
|
|
|
22
|
-
from
|
|
23
|
+
from abc import ABC
|
|
24
|
+
from pathlib import Path
|
|
25
|
+
from typing import List, Literal, Mapping, Optional, Sequence, Union
|
|
26
|
+
|
|
27
|
+
from lazy_imports import try_import
|
|
23
28
|
|
|
24
29
|
from ..utils.detection_types import ImageType, Requirement
|
|
25
|
-
from ..utils.file_utils import
|
|
26
|
-
get_pytorch_requirement,
|
|
27
|
-
get_transformers_requirement,
|
|
28
|
-
pytorch_available,
|
|
29
|
-
transformers_available,
|
|
30
|
-
)
|
|
30
|
+
from ..utils.file_utils import get_pytorch_requirement, get_transformers_requirement
|
|
31
31
|
from ..utils.settings import TypeOrStr, get_type
|
|
32
32
|
from .base import DetectionResult, ObjectDetector
|
|
33
|
-
from .pt.ptutils import
|
|
33
|
+
from .pt.ptutils import get_torch_device
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
with try_import() as pt_import_guard:
|
|
36
36
|
import torch # pylint: disable=W0611
|
|
37
37
|
from torchvision.ops import boxes as box_ops # type: ignore
|
|
38
38
|
|
|
39
|
-
|
|
39
|
+
with try_import() as tr_import_guard:
|
|
40
40
|
from transformers import ( # pylint: disable=W0611
|
|
41
41
|
AutoFeatureExtractor,
|
|
42
42
|
DetrFeatureExtractor,
|
|
@@ -46,16 +46,16 @@ if transformers_available():
|
|
|
46
46
|
|
|
47
47
|
|
|
48
48
|
def _detr_post_processing(
|
|
49
|
-
boxes:
|
|
50
|
-
) ->
|
|
49
|
+
boxes: torch.Tensor, scores: torch.Tensor, labels: torch.Tensor, nms_thresh: float
|
|
50
|
+
) -> torch.Tensor:
|
|
51
51
|
return box_ops.batched_nms(boxes.float(), scores, labels, nms_thresh)
|
|
52
52
|
|
|
53
53
|
|
|
54
54
|
def detr_predict_image(
|
|
55
55
|
np_img: ImageType,
|
|
56
|
-
predictor:
|
|
57
|
-
feature_extractor:
|
|
58
|
-
device:
|
|
56
|
+
predictor: TableTransformerForObjectDetection,
|
|
57
|
+
feature_extractor: DetrFeatureExtractor,
|
|
58
|
+
device: torch.device,
|
|
59
59
|
threshold: float,
|
|
60
60
|
nms_threshold: float,
|
|
61
61
|
) -> List[DetectionResult]:
|
|
@@ -94,7 +94,48 @@ def detr_predict_image(
|
|
|
94
94
|
]
|
|
95
95
|
|
|
96
96
|
|
|
97
|
-
class
|
|
97
|
+
class HFDetrDerivedDetectorMixin(ObjectDetector, ABC):
|
|
98
|
+
"""Base class for Detr object detector. This class only implements the basic wrapper functions"""
|
|
99
|
+
|
|
100
|
+
def __init__(self, categories: Mapping[str, TypeOrStr], filter_categories: Optional[Sequence[TypeOrStr]] = None):
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
:param categories: A dict with key (indices) and values (category names).
|
|
104
|
+
:param filter_categories: The model might return objects that are not supposed to be predicted and that should
|
|
105
|
+
be filtered. Pass a list of category names that must not be returned
|
|
106
|
+
"""
|
|
107
|
+
self.categories = {idx: get_type(cat) for idx, cat in categories.items()}
|
|
108
|
+
if filter_categories:
|
|
109
|
+
filter_categories = [get_type(cat) for cat in filter_categories]
|
|
110
|
+
self.filter_categories = filter_categories
|
|
111
|
+
|
|
112
|
+
def _map_category_names(self, detection_results: List[DetectionResult]) -> List[DetectionResult]:
|
|
113
|
+
"""
|
|
114
|
+
Populating category names to detection results. Will also filter categories
|
|
115
|
+
|
|
116
|
+
:param detection_results: list of detection results
|
|
117
|
+
:return: List of detection results with attribute class_name populated
|
|
118
|
+
"""
|
|
119
|
+
filtered_detection_result: List[DetectionResult] = []
|
|
120
|
+
for result in detection_results:
|
|
121
|
+
result.class_name = self.categories[str(result.class_id + 1)] # type: ignore
|
|
122
|
+
if isinstance(result.class_id, int):
|
|
123
|
+
result.class_id += 1
|
|
124
|
+
if self.filter_categories:
|
|
125
|
+
if result.class_name not in self.filter_categories:
|
|
126
|
+
filtered_detection_result.append(result)
|
|
127
|
+
else:
|
|
128
|
+
filtered_detection_result.append(result)
|
|
129
|
+
|
|
130
|
+
return filtered_detection_result
|
|
131
|
+
|
|
132
|
+
@staticmethod
|
|
133
|
+
def get_name(path_weights: str) -> str:
|
|
134
|
+
"""Returns the name of the model"""
|
|
135
|
+
return "Transformers_Tatr_" + "_".join(Path(path_weights).parts[-2:])
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class HFDetrDerivedDetector(HFDetrDerivedDetectorMixin):
|
|
98
139
|
"""
|
|
99
140
|
Model wrapper for TableTransformerForObjectDetection that again is based on
|
|
100
141
|
|
|
@@ -125,7 +166,7 @@ class HFDetrDerivedDetector(ObjectDetector):
|
|
|
125
166
|
path_weights: str,
|
|
126
167
|
path_feature_extractor_config_json: str,
|
|
127
168
|
categories: Mapping[str, TypeOrStr],
|
|
128
|
-
device: Optional[Literal["cpu", "cuda"]] = None,
|
|
169
|
+
device: Optional[Union[Literal["cpu", "cuda"], torch.device]] = None,
|
|
129
170
|
filter_categories: Optional[Sequence[TypeOrStr]] = None,
|
|
130
171
|
):
|
|
131
172
|
"""
|
|
@@ -138,26 +179,22 @@ class HFDetrDerivedDetector(ObjectDetector):
|
|
|
138
179
|
:param filter_categories: The model might return objects that are not supposed to be predicted and that should
|
|
139
180
|
be filtered. Pass a list of category names that must not be returned
|
|
140
181
|
"""
|
|
141
|
-
|
|
142
|
-
|
|
182
|
+
super().__init__(categories, filter_categories)
|
|
183
|
+
|
|
143
184
|
self.path_config = path_config_json
|
|
144
185
|
self.path_weights = path_weights
|
|
145
186
|
self.path_feature_extractor_config = path_feature_extractor_config_json
|
|
146
|
-
|
|
147
|
-
self.
|
|
148
|
-
self.
|
|
149
|
-
|
|
150
|
-
self.
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
self.device = set_torch_auto_device()
|
|
187
|
+
|
|
188
|
+
self.name = self.get_name(self.path_weights)
|
|
189
|
+
self.model_id = self.get_model_id()
|
|
190
|
+
|
|
191
|
+
self.config = self.get_config(path_config_json)
|
|
192
|
+
|
|
193
|
+
self.hf_detr_predictor = self.get_model(self.path_weights, self.config)
|
|
194
|
+
self.feature_extractor = self.get_pre_processor(self.path_feature_extractor_config)
|
|
195
|
+
|
|
196
|
+
self.device = get_torch_device(device)
|
|
157
197
|
self.hf_detr_predictor.to(self.device)
|
|
158
|
-
if filter_categories:
|
|
159
|
-
filter_categories = [get_type(cat) for cat in filter_categories]
|
|
160
|
-
self.filter_categories = filter_categories
|
|
161
198
|
|
|
162
199
|
def predict(self, np_img: ImageType) -> List[DetectionResult]:
|
|
163
200
|
results = detr_predict_image(
|
|
@@ -170,50 +207,64 @@ class HFDetrDerivedDetector(ObjectDetector):
|
|
|
170
207
|
)
|
|
171
208
|
return self._map_category_names(results)
|
|
172
209
|
|
|
173
|
-
|
|
210
|
+
@staticmethod
|
|
211
|
+
def get_model(path_weights: str, config: PretrainedConfig) -> TableTransformerForObjectDetection:
|
|
174
212
|
"""
|
|
175
213
|
Builds the Detr model
|
|
176
214
|
|
|
177
|
-
:param path_weights:
|
|
215
|
+
:param path_weights: The path to the model checkpoint.
|
|
216
|
+
:param config: `PretrainedConfig`
|
|
178
217
|
:return: TableTransformerForObjectDetection instance
|
|
179
218
|
"""
|
|
180
219
|
return TableTransformerForObjectDetection.from_pretrained(
|
|
181
|
-
pretrained_model_name_or_path=path_weights, config=
|
|
220
|
+
pretrained_model_name_or_path=path_weights, config=config
|
|
182
221
|
)
|
|
183
222
|
|
|
184
|
-
|
|
223
|
+
@staticmethod
|
|
224
|
+
def get_pre_processor(path_feature_extractor_config: str) -> DetrFeatureExtractor:
|
|
185
225
|
"""
|
|
186
226
|
Builds the feature extractor
|
|
187
227
|
|
|
188
228
|
:return: DetrFeatureExtractor
|
|
189
229
|
"""
|
|
190
|
-
return AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path=
|
|
230
|
+
return AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path=path_feature_extractor_config)
|
|
191
231
|
|
|
192
|
-
|
|
232
|
+
@staticmethod
|
|
233
|
+
def get_config(path_config: str) -> PretrainedConfig:
|
|
193
234
|
"""
|
|
194
|
-
|
|
235
|
+
Builds the config
|
|
195
236
|
|
|
196
|
-
:param
|
|
197
|
-
:return:
|
|
237
|
+
:param path_config: The path to the json config.
|
|
238
|
+
:return: PretrainedConfig instance
|
|
198
239
|
"""
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
if self.filter_categories:
|
|
205
|
-
if result.class_name not in self.filter_categories:
|
|
206
|
-
filtered_detection_result.append(result)
|
|
207
|
-
else:
|
|
208
|
-
filtered_detection_result.append(result)
|
|
209
|
-
|
|
210
|
-
return filtered_detection_result
|
|
240
|
+
config = PretrainedConfig.from_pretrained(pretrained_model_name_or_path=path_config)
|
|
241
|
+
config.use_timm_backbone = True
|
|
242
|
+
config.threshold = 0.1
|
|
243
|
+
config.nms_threshold = 0.05
|
|
244
|
+
return config
|
|
211
245
|
|
|
212
246
|
@classmethod
|
|
213
247
|
def get_requirements(cls) -> List[Requirement]:
|
|
214
248
|
return [get_pytorch_requirement(), get_transformers_requirement()]
|
|
215
249
|
|
|
216
|
-
def clone(self) ->
|
|
250
|
+
def clone(self) -> HFDetrDerivedDetector:
|
|
217
251
|
return self.__class__(
|
|
218
252
|
self.path_config, self.path_weights, self.path_feature_extractor_config, self.categories, self.device
|
|
219
253
|
)
|
|
254
|
+
|
|
255
|
+
@staticmethod
|
|
256
|
+
def get_wrapped_model(
|
|
257
|
+
path_config_json: str, path_weights: str, device: Optional[Union[Literal["cpu", "cuda"], torch.device]] = None
|
|
258
|
+
) -> TableTransformerForObjectDetection:
|
|
259
|
+
"""
|
|
260
|
+
Get the wrapped model
|
|
261
|
+
|
|
262
|
+
:param path_config_json: The path to the json config.
|
|
263
|
+
:param path_weights: The path to the model checkpoint.
|
|
264
|
+
:param device: "cpu" or "cuda". If not specified will auto select depending on what is available
|
|
265
|
+
:return: TableTransformerForObjectDetection instance
|
|
266
|
+
"""
|
|
267
|
+
config = HFDetrDerivedDetector.get_config(path_config_json)
|
|
268
|
+
hf_detr_predictor = HFDetrDerivedDetector.get_model(path_weights, config)
|
|
269
|
+
device = get_torch_device()
|
|
270
|
+
return hf_detr_predictor.to(device)
|