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