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.

Files changed (120) hide show
  1. deepdoctection/__init__.py +38 -29
  2. deepdoctection/analyzer/dd.py +36 -29
  3. deepdoctection/configs/conf_dd_one.yaml +34 -31
  4. deepdoctection/dataflow/base.py +0 -19
  5. deepdoctection/dataflow/custom.py +4 -3
  6. deepdoctection/dataflow/custom_serialize.py +14 -5
  7. deepdoctection/dataflow/parallel_map.py +12 -11
  8. deepdoctection/dataflow/serialize.py +5 -4
  9. deepdoctection/datapoint/annotation.py +35 -13
  10. deepdoctection/datapoint/box.py +3 -5
  11. deepdoctection/datapoint/convert.py +3 -1
  12. deepdoctection/datapoint/image.py +79 -36
  13. deepdoctection/datapoint/view.py +152 -49
  14. deepdoctection/datasets/__init__.py +1 -4
  15. deepdoctection/datasets/adapter.py +6 -3
  16. deepdoctection/datasets/base.py +86 -11
  17. deepdoctection/datasets/dataflow_builder.py +1 -1
  18. deepdoctection/datasets/info.py +4 -4
  19. deepdoctection/datasets/instances/doclaynet.py +3 -2
  20. deepdoctection/datasets/instances/fintabnet.py +2 -1
  21. deepdoctection/datasets/instances/funsd.py +2 -1
  22. deepdoctection/datasets/instances/iiitar13k.py +5 -2
  23. deepdoctection/datasets/instances/layouttest.py +4 -8
  24. deepdoctection/datasets/instances/publaynet.py +2 -2
  25. deepdoctection/datasets/instances/pubtables1m.py +6 -3
  26. deepdoctection/datasets/instances/pubtabnet.py +2 -1
  27. deepdoctection/datasets/instances/rvlcdip.py +2 -1
  28. deepdoctection/datasets/instances/xfund.py +2 -1
  29. deepdoctection/eval/__init__.py +1 -4
  30. deepdoctection/eval/accmetric.py +1 -1
  31. deepdoctection/eval/base.py +5 -4
  32. deepdoctection/eval/cocometric.py +2 -1
  33. deepdoctection/eval/eval.py +19 -15
  34. deepdoctection/eval/tedsmetric.py +14 -11
  35. deepdoctection/eval/tp_eval_callback.py +14 -7
  36. deepdoctection/extern/__init__.py +2 -7
  37. deepdoctection/extern/base.py +39 -13
  38. deepdoctection/extern/d2detect.py +182 -90
  39. deepdoctection/extern/deskew.py +36 -9
  40. deepdoctection/extern/doctrocr.py +265 -83
  41. deepdoctection/extern/fastlang.py +49 -9
  42. deepdoctection/extern/hfdetr.py +106 -55
  43. deepdoctection/extern/hflayoutlm.py +441 -122
  44. deepdoctection/extern/hflm.py +225 -0
  45. deepdoctection/extern/model.py +56 -47
  46. deepdoctection/extern/pdftext.py +10 -5
  47. deepdoctection/extern/pt/__init__.py +1 -3
  48. deepdoctection/extern/pt/nms.py +6 -2
  49. deepdoctection/extern/pt/ptutils.py +27 -18
  50. deepdoctection/extern/tessocr.py +134 -22
  51. deepdoctection/extern/texocr.py +6 -2
  52. deepdoctection/extern/tp/tfutils.py +43 -9
  53. deepdoctection/extern/tp/tpcompat.py +14 -11
  54. deepdoctection/extern/tp/tpfrcnn/__init__.py +20 -0
  55. deepdoctection/extern/tp/tpfrcnn/common.py +7 -3
  56. deepdoctection/extern/tp/tpfrcnn/config/__init__.py +20 -0
  57. deepdoctection/extern/tp/tpfrcnn/config/config.py +9 -6
  58. deepdoctection/extern/tp/tpfrcnn/modeling/__init__.py +20 -0
  59. deepdoctection/extern/tp/tpfrcnn/modeling/backbone.py +17 -7
  60. deepdoctection/extern/tp/tpfrcnn/modeling/generalized_rcnn.py +12 -6
  61. deepdoctection/extern/tp/tpfrcnn/modeling/model_box.py +9 -4
  62. deepdoctection/extern/tp/tpfrcnn/modeling/model_cascade.py +8 -5
  63. deepdoctection/extern/tp/tpfrcnn/modeling/model_fpn.py +16 -11
  64. deepdoctection/extern/tp/tpfrcnn/modeling/model_frcnn.py +17 -10
  65. deepdoctection/extern/tp/tpfrcnn/modeling/model_mrcnn.py +14 -8
  66. deepdoctection/extern/tp/tpfrcnn/modeling/model_rpn.py +15 -10
  67. deepdoctection/extern/tp/tpfrcnn/predict.py +9 -4
  68. deepdoctection/extern/tp/tpfrcnn/preproc.py +8 -9
  69. deepdoctection/extern/tp/tpfrcnn/utils/__init__.py +20 -0
  70. deepdoctection/extern/tp/tpfrcnn/utils/box_ops.py +10 -2
  71. deepdoctection/extern/tpdetect.py +54 -30
  72. deepdoctection/mapper/__init__.py +3 -8
  73. deepdoctection/mapper/d2struct.py +9 -7
  74. deepdoctection/mapper/hfstruct.py +7 -2
  75. deepdoctection/mapper/laylmstruct.py +164 -21
  76. deepdoctection/mapper/maputils.py +16 -3
  77. deepdoctection/mapper/misc.py +6 -3
  78. deepdoctection/mapper/prodigystruct.py +1 -1
  79. deepdoctection/mapper/pubstruct.py +10 -10
  80. deepdoctection/mapper/tpstruct.py +3 -3
  81. deepdoctection/pipe/__init__.py +1 -1
  82. deepdoctection/pipe/anngen.py +35 -8
  83. deepdoctection/pipe/base.py +53 -19
  84. deepdoctection/pipe/common.py +23 -13
  85. deepdoctection/pipe/concurrency.py +2 -1
  86. deepdoctection/pipe/doctectionpipe.py +2 -2
  87. deepdoctection/pipe/language.py +3 -2
  88. deepdoctection/pipe/layout.py +6 -3
  89. deepdoctection/pipe/lm.py +34 -66
  90. deepdoctection/pipe/order.py +142 -35
  91. deepdoctection/pipe/refine.py +26 -24
  92. deepdoctection/pipe/segment.py +21 -16
  93. deepdoctection/pipe/{cell.py → sub_layout.py} +30 -9
  94. deepdoctection/pipe/text.py +14 -8
  95. deepdoctection/pipe/transform.py +16 -9
  96. deepdoctection/train/__init__.py +6 -12
  97. deepdoctection/train/d2_frcnn_train.py +36 -28
  98. deepdoctection/train/hf_detr_train.py +26 -17
  99. deepdoctection/train/hf_layoutlm_train.py +133 -111
  100. deepdoctection/train/tp_frcnn_train.py +21 -19
  101. deepdoctection/utils/__init__.py +3 -0
  102. deepdoctection/utils/concurrency.py +1 -1
  103. deepdoctection/utils/context.py +2 -2
  104. deepdoctection/utils/env_info.py +41 -84
  105. deepdoctection/utils/error.py +84 -0
  106. deepdoctection/utils/file_utils.py +4 -15
  107. deepdoctection/utils/fs.py +7 -7
  108. deepdoctection/utils/logger.py +1 -0
  109. deepdoctection/utils/mocks.py +93 -0
  110. deepdoctection/utils/pdf_utils.py +5 -4
  111. deepdoctection/utils/settings.py +6 -1
  112. deepdoctection/utils/transform.py +1 -1
  113. deepdoctection/utils/utils.py +0 -6
  114. deepdoctection/utils/viz.py +48 -5
  115. {deepdoctection-0.30.dist-info → deepdoctection-0.32.dist-info}/METADATA +57 -73
  116. deepdoctection-0.32.dist-info/RECORD +146 -0
  117. {deepdoctection-0.30.dist-info → deepdoctection-0.32.dist-info}/WHEEL +1 -1
  118. deepdoctection-0.30.dist-info/RECORD +0 -143
  119. {deepdoctection-0.30.dist-info → deepdoctection-0.32.dist-info}/LICENSE +0 -0
  120. {deepdoctection-0.30.dist-info → deepdoctection-0.32.dist-info}/top_level.txt +0 -0
@@ -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 typing import List, Literal, Mapping, Optional, Sequence
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 set_torch_auto_device
33
+ from .pt.ptutils import get_torch_device
34
34
 
35
- if pytorch_available():
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
- if transformers_available():
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: "torch.Tensor", scores: "torch.Tensor", labels: "torch.Tensor", nms_thresh: float
50
- ) -> "torch.Tensor":
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: "TableTransformerForObjectDetection",
57
- feature_extractor: "DetrFeatureExtractor",
58
- device: Literal["cpu", "cuda"],
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 HFDetrDerivedDetector(ObjectDetector):
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
- self.name = "Detr"
142
- self.categories = {idx: get_type(cat) for idx, cat in categories.items()}
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
- self.config = PretrainedConfig.from_pretrained(pretrained_model_name_or_path=self.path_config)
147
- self.config.use_timm_backbone = True
148
- self.config.threshold = 0.1
149
- self.config.nms_threshold = 0.05
150
- self.hf_detr_predictor = self.set_model(path_weights)
151
- self.feature_extractor = self.set_pre_processor()
152
-
153
- if device is not None:
154
- self.device = device
155
- else:
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
- def set_model(self, path_weights: str) -> "TableTransformerForObjectDetection":
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: 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=self.config
220
+ pretrained_model_name_or_path=path_weights, config=config
182
221
  )
183
222
 
184
- def set_pre_processor(self) -> "DetrFeatureExtractor":
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=self.path_feature_extractor_config)
230
+ return AutoFeatureExtractor.from_pretrained(pretrained_model_name_or_path=path_feature_extractor_config)
191
231
 
192
- def _map_category_names(self, detection_results: List[DetectionResult]) -> List[DetectionResult]:
232
+ @staticmethod
233
+ def get_config(path_config: str) -> PretrainedConfig:
193
234
  """
194
- Populating category names to detection results. Will also filter categories
235
+ Builds the config
195
236
 
196
- :param detection_results: list of detection results
197
- :return: List of detection results with attribute class_name populated
237
+ :param path_config: The path to the json config.
238
+ :return: PretrainedConfig instance
198
239
  """
199
- filtered_detection_result: List[DetectionResult] = []
200
- for result in detection_results:
201
- result.class_name = self.categories[str(result.class_id + 1)] # type: ignore
202
- if isinstance(result.class_id, int):
203
- result.class_id += 1
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) -> "HFDetrDerivedDetector":
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)