deepdoctection 0.42.0__py3-none-any.whl → 0.43__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 (124) hide show
  1. deepdoctection/__init__.py +2 -1
  2. deepdoctection/analyzer/__init__.py +2 -1
  3. deepdoctection/analyzer/config.py +904 -0
  4. deepdoctection/analyzer/dd.py +36 -62
  5. deepdoctection/analyzer/factory.py +311 -141
  6. deepdoctection/configs/conf_dd_one.yaml +100 -44
  7. deepdoctection/configs/profiles.jsonl +32 -0
  8. deepdoctection/dataflow/__init__.py +9 -6
  9. deepdoctection/dataflow/base.py +33 -15
  10. deepdoctection/dataflow/common.py +96 -75
  11. deepdoctection/dataflow/custom.py +36 -29
  12. deepdoctection/dataflow/custom_serialize.py +135 -91
  13. deepdoctection/dataflow/parallel_map.py +33 -31
  14. deepdoctection/dataflow/serialize.py +15 -10
  15. deepdoctection/dataflow/stats.py +41 -28
  16. deepdoctection/datapoint/__init__.py +4 -6
  17. deepdoctection/datapoint/annotation.py +104 -66
  18. deepdoctection/datapoint/box.py +190 -130
  19. deepdoctection/datapoint/convert.py +66 -39
  20. deepdoctection/datapoint/image.py +151 -95
  21. deepdoctection/datapoint/view.py +383 -236
  22. deepdoctection/datasets/__init__.py +2 -6
  23. deepdoctection/datasets/adapter.py +11 -11
  24. deepdoctection/datasets/base.py +118 -81
  25. deepdoctection/datasets/dataflow_builder.py +18 -12
  26. deepdoctection/datasets/info.py +76 -57
  27. deepdoctection/datasets/instances/__init__.py +6 -2
  28. deepdoctection/datasets/instances/doclaynet.py +17 -14
  29. deepdoctection/datasets/instances/fintabnet.py +16 -22
  30. deepdoctection/datasets/instances/funsd.py +11 -6
  31. deepdoctection/datasets/instances/iiitar13k.py +9 -9
  32. deepdoctection/datasets/instances/layouttest.py +9 -9
  33. deepdoctection/datasets/instances/publaynet.py +9 -9
  34. deepdoctection/datasets/instances/pubtables1m.py +13 -13
  35. deepdoctection/datasets/instances/pubtabnet.py +13 -15
  36. deepdoctection/datasets/instances/rvlcdip.py +8 -8
  37. deepdoctection/datasets/instances/xfund.py +11 -9
  38. deepdoctection/datasets/registry.py +18 -11
  39. deepdoctection/datasets/save.py +12 -11
  40. deepdoctection/eval/__init__.py +3 -2
  41. deepdoctection/eval/accmetric.py +72 -52
  42. deepdoctection/eval/base.py +29 -10
  43. deepdoctection/eval/cocometric.py +14 -12
  44. deepdoctection/eval/eval.py +56 -41
  45. deepdoctection/eval/registry.py +6 -3
  46. deepdoctection/eval/tedsmetric.py +24 -9
  47. deepdoctection/eval/tp_eval_callback.py +13 -12
  48. deepdoctection/extern/__init__.py +1 -1
  49. deepdoctection/extern/base.py +176 -97
  50. deepdoctection/extern/d2detect.py +127 -92
  51. deepdoctection/extern/deskew.py +19 -10
  52. deepdoctection/extern/doctrocr.py +157 -106
  53. deepdoctection/extern/fastlang.py +25 -17
  54. deepdoctection/extern/hfdetr.py +137 -60
  55. deepdoctection/extern/hflayoutlm.py +329 -248
  56. deepdoctection/extern/hflm.py +67 -33
  57. deepdoctection/extern/model.py +108 -762
  58. deepdoctection/extern/pdftext.py +37 -12
  59. deepdoctection/extern/pt/nms.py +15 -1
  60. deepdoctection/extern/pt/ptutils.py +13 -9
  61. deepdoctection/extern/tessocr.py +87 -54
  62. deepdoctection/extern/texocr.py +29 -14
  63. deepdoctection/extern/tp/tfutils.py +36 -8
  64. deepdoctection/extern/tp/tpcompat.py +54 -16
  65. deepdoctection/extern/tp/tpfrcnn/config/config.py +20 -4
  66. deepdoctection/extern/tpdetect.py +4 -2
  67. deepdoctection/mapper/__init__.py +1 -1
  68. deepdoctection/mapper/cats.py +117 -76
  69. deepdoctection/mapper/cocostruct.py +35 -17
  70. deepdoctection/mapper/d2struct.py +56 -29
  71. deepdoctection/mapper/hfstruct.py +32 -19
  72. deepdoctection/mapper/laylmstruct.py +221 -185
  73. deepdoctection/mapper/maputils.py +71 -35
  74. deepdoctection/mapper/match.py +76 -62
  75. deepdoctection/mapper/misc.py +68 -44
  76. deepdoctection/mapper/pascalstruct.py +13 -12
  77. deepdoctection/mapper/prodigystruct.py +33 -19
  78. deepdoctection/mapper/pubstruct.py +42 -32
  79. deepdoctection/mapper/tpstruct.py +39 -19
  80. deepdoctection/mapper/xfundstruct.py +20 -13
  81. deepdoctection/pipe/__init__.py +1 -2
  82. deepdoctection/pipe/anngen.py +104 -62
  83. deepdoctection/pipe/base.py +226 -107
  84. deepdoctection/pipe/common.py +206 -123
  85. deepdoctection/pipe/concurrency.py +74 -47
  86. deepdoctection/pipe/doctectionpipe.py +108 -47
  87. deepdoctection/pipe/language.py +41 -24
  88. deepdoctection/pipe/layout.py +45 -18
  89. deepdoctection/pipe/lm.py +146 -78
  90. deepdoctection/pipe/order.py +196 -113
  91. deepdoctection/pipe/refine.py +111 -63
  92. deepdoctection/pipe/registry.py +1 -1
  93. deepdoctection/pipe/segment.py +213 -142
  94. deepdoctection/pipe/sub_layout.py +76 -46
  95. deepdoctection/pipe/text.py +52 -33
  96. deepdoctection/pipe/transform.py +8 -6
  97. deepdoctection/train/d2_frcnn_train.py +87 -69
  98. deepdoctection/train/hf_detr_train.py +72 -40
  99. deepdoctection/train/hf_layoutlm_train.py +85 -46
  100. deepdoctection/train/tp_frcnn_train.py +56 -28
  101. deepdoctection/utils/concurrency.py +59 -16
  102. deepdoctection/utils/context.py +40 -19
  103. deepdoctection/utils/develop.py +25 -17
  104. deepdoctection/utils/env_info.py +85 -36
  105. deepdoctection/utils/error.py +16 -10
  106. deepdoctection/utils/file_utils.py +246 -62
  107. deepdoctection/utils/fs.py +162 -43
  108. deepdoctection/utils/identifier.py +29 -16
  109. deepdoctection/utils/logger.py +49 -32
  110. deepdoctection/utils/metacfg.py +83 -21
  111. deepdoctection/utils/pdf_utils.py +119 -62
  112. deepdoctection/utils/settings.py +24 -10
  113. deepdoctection/utils/tqdm.py +10 -5
  114. deepdoctection/utils/transform.py +182 -46
  115. deepdoctection/utils/utils.py +61 -28
  116. deepdoctection/utils/viz.py +150 -104
  117. deepdoctection-0.43.dist-info/METADATA +376 -0
  118. deepdoctection-0.43.dist-info/RECORD +149 -0
  119. {deepdoctection-0.42.0.dist-info → deepdoctection-0.43.dist-info}/WHEEL +1 -1
  120. deepdoctection/analyzer/_config.py +0 -146
  121. deepdoctection-0.42.0.dist-info/METADATA +0 -431
  122. deepdoctection-0.42.0.dist-info/RECORD +0 -148
  123. {deepdoctection-0.42.0.dist-info → deepdoctection-0.43.dist-info}/licenses/LICENSE +0 -0
  124. {deepdoctection-0.42.0.dist-info → deepdoctection-0.43.dist-info}/top_level.txt +0 -0
@@ -16,14 +16,15 @@
16
16
  # limitations under the License.
17
17
 
18
18
  """
19
- HF Detr model for object detection.
19
+ HF Detr and DeformableDetr models.
20
20
  """
21
+
21
22
  from __future__ import annotations
22
23
 
23
24
  import os
24
25
  from abc import ABC
25
26
  from pathlib import Path
26
- from typing import Literal, Mapping, Optional, Sequence, Union
27
+ from typing import TYPE_CHECKING, Literal, Mapping, Optional, Sequence, Union
27
28
 
28
29
  from lazy_imports import try_import
29
30
 
@@ -39,13 +40,17 @@ with try_import() as pt_import_guard:
39
40
 
40
41
  with try_import() as tr_import_guard:
41
42
  from transformers import ( # pylint: disable=W0611
42
- AutoFeatureExtractor,
43
- DetrFeatureExtractor,
44
- DetrImageProcessor,
43
+ DeformableDetrForObjectDetection,
44
+ DeformableDetrImageProcessorFast,
45
+ DetrImageProcessorFast,
45
46
  PretrainedConfig,
46
47
  TableTransformerForObjectDetection,
47
48
  )
48
49
 
50
+ if TYPE_CHECKING:
51
+ EligibleDetrModel = Union[TableTransformerForObjectDetection, DeformableDetrForObjectDetection]
52
+ DetrImageProcessor = Union[DetrImageProcessorFast, DeformableDetrImageProcessorFast]
53
+
49
54
 
50
55
  def _detr_post_processing(
51
56
  boxes: torch.Tensor, scores: torch.Tensor, labels: torch.Tensor, nms_thresh: float
@@ -55,24 +60,27 @@ def _detr_post_processing(
55
60
 
56
61
  def detr_predict_image(
57
62
  np_img: PixelValues,
58
- predictor: TableTransformerForObjectDetection,
63
+ predictor: EligibleDetrModel,
59
64
  feature_extractor: DetrImageProcessor,
60
65
  device: torch.device,
61
66
  threshold: float,
62
67
  nms_threshold: float,
63
68
  ) -> list[DetectionResult]:
64
69
  """
65
- Calling predictor. Before doing that, tensors must be transferred to the device where the model is loaded. After
66
- running prediction it will present prediction in DetectionResult format-
67
-
68
- :param np_img: image as numpy array
69
- :param predictor: TableTransformerForObjectDetection
70
- :param feature_extractor: feature extractor
71
- :param device: device where the model is loaded
72
- :param threshold: Will filter all predictions with confidence score less threshold
73
- :param nms_threshold: Threshold to perform NMS on prediction outputs. (Note, that NMS does not belong to canonical
74
- Detr inference processing)
75
-
70
+ Calling predictor. Before, tensors must be transferred to the device where the model is loaded.
71
+
72
+ Args:
73
+ np_img: Image as `np.array`.
74
+ predictor: `TableTransformerForObjectDetection` instance.
75
+ feature_extractor: Feature extractor instance.
76
+ device: Device where the model is loaded.
77
+ threshold: Will filter all predictions with confidence score less threshold.
78
+ nms_threshold: Threshold to perform NMS on prediction outputs.
79
+ Note:
80
+ NMS does not belong to canonical Detr inference processing.
81
+
82
+ Returns:
83
+ List of `DetectionResult` after running prediction.
76
84
  """
77
85
  target_sizes = [np_img.shape[:2]]
78
86
  inputs = feature_extractor(images=np_img, return_tensors="pt")
@@ -101,10 +109,10 @@ class HFDetrDerivedDetectorMixin(ObjectDetector, ABC):
101
109
 
102
110
  def __init__(self, categories: Mapping[int, TypeOrStr], filter_categories: Optional[Sequence[TypeOrStr]] = None):
103
111
  """
104
-
105
- :param categories: A dict with key (indices) and values (category names).
106
- :param filter_categories: The model might return objects that are not supposed to be predicted and that should
107
- be filtered. Pass a list of category names that must not be returned
112
+ Args:
113
+ categories: A dict with key (indices) and values (category names).
114
+ filter_categories: The model might return objects that are not supposed to be predicted and that should
115
+ be filtered. Pass a list of category names that must not be returned.
108
116
  """
109
117
  self.categories = ModelCategories(init_categories=categories)
110
118
  if filter_categories:
@@ -112,10 +120,13 @@ class HFDetrDerivedDetectorMixin(ObjectDetector, ABC):
112
120
 
113
121
  def _map_category_names(self, detection_results: list[DetectionResult]) -> list[DetectionResult]:
114
122
  """
115
- Populating category names to detection results. Will also filter categories
123
+ Populating category names to `DetectionResult`. Will also filter categories.
124
+
125
+ Args:
126
+ detection_results: List of `DetectionResult`s.
116
127
 
117
- :param detection_results: list of detection results
118
- :return: List of detection results with attribute class_name populated
128
+ Returns:
129
+ List of `DetectionResult`s with `class_name`.
119
130
  """
120
131
  filtered_detection_result: list[DetectionResult] = []
121
132
  shifted_categories = self.categories.shift_category_ids(shift_by=-1)
@@ -132,24 +143,39 @@ class HFDetrDerivedDetectorMixin(ObjectDetector, ABC):
132
143
 
133
144
  @staticmethod
134
145
  def get_name(path_weights: PathLikeOrStr) -> str:
135
- """Returns the name of the model"""
146
+ """
147
+ Returns the name of the model.
148
+
149
+ Args:
150
+ path_weights: Path to the model weights.
151
+
152
+ Returns:
153
+ Model name string.
154
+ """
136
155
  return "Transformers_Tatr_" + "_".join(Path(path_weights).parts[-2:])
137
156
 
138
157
  def get_category_names(self) -> tuple[ObjectTypes, ...]:
158
+ """
159
+ Returns:
160
+ Tuple of `category_name`s.
161
+ """
139
162
  return self.categories.get_categories(as_dict=False)
140
163
 
141
164
 
142
165
  class HFDetrDerivedDetector(HFDetrDerivedDetectorMixin):
143
166
  """
144
- Model wrapper for TableTransformerForObjectDetection that again is based on
145
-
146
- https://github.com/microsoft/table-transformer .
167
+ Model wrapper for `TableTransformerForObjectDetection` that again is based on
168
+ <https://github.com/microsoft/table-transformer>.
147
169
 
148
170
  The wrapper can be used to load pre-trained models for table detection and table structure recognition. Running Detr
149
- models trained from scratch on custom datasets is possible as well. Note, that this wrapper will load
150
- `TableTransformerForObjectDetection` that is slightly different compared to `DetrForObjectDetection` that can be
151
- found in the transformer library as well.
171
+ models trained from scratch on custom datasets is possible as well.
172
+
173
+ Note:
174
+ This wrapper will load `TableTransformerForObjectDetection` that is slightly different compared to
175
+ `DetrForObjectDetection` that can be found in the transformer library as well.
152
176
 
177
+ Example:
178
+ ```python
153
179
  config_path = ModelCatalog.
154
180
  get_full_path_configs("microsoft/table-transformer-structure-recognition/pytorch_model.bin")
155
181
  weights_path = ModelDownloadManager.
@@ -162,6 +188,7 @@ class HFDetrDerivedDetector(HFDetrDerivedDetectorMixin):
162
188
  detr_predictor = HFDetrDerivedDetector(config_path,weights_path,feature_extractor_config_path,categories)
163
189
 
164
190
  detection_result = detr_predictor.predict(bgr_image_np_array)
191
+ ```
165
192
  """
166
193
 
167
194
  def __init__(
@@ -175,13 +202,15 @@ class HFDetrDerivedDetector(HFDetrDerivedDetectorMixin):
175
202
  ):
176
203
  """
177
204
  Set up the predictor.
178
- :param path_config_json: The path to the json config.
179
- :param path_weights: The path to the model checkpoint.
180
- :param path_feature_extractor_config_json: The path to the feature extractor config.
181
- :param categories: A dict with key (indices) and values (category names).
182
- :param device: "cpu" or "cuda". If not specified will auto select depending on what is available
183
- :param filter_categories: The model might return objects that are not supposed to be predicted and that should
184
- be filtered. Pass a list of category names that must not be returned
205
+
206
+ Args:
207
+ path_config_json: The path to the json config.
208
+ path_weights: The path to the model checkpoint.
209
+ path_feature_extractor_config_json: The path to the feature extractor config.
210
+ categories: A dict with key (indices) and values (category names).
211
+ device: "cpu" or "cuda". If not specified will auto select depending on what is available.
212
+ filter_categories: The model might return objects that are not supposed to be predicted and that should
213
+ be filtered. Pass a list of category names that must not be returned.
185
214
  """
186
215
  super().__init__(categories, filter_categories)
187
216
 
@@ -195,12 +224,21 @@ class HFDetrDerivedDetector(HFDetrDerivedDetectorMixin):
195
224
  self.config = self.get_config(path_config_json)
196
225
 
197
226
  self.hf_detr_predictor = self.get_model(self.path_weights, self.config)
198
- self.feature_extractor = self.get_pre_processor(self.path_feature_extractor_config)
227
+ self.feature_extractor = self.get_pre_processor(self.path_feature_extractor_config, self.config)
199
228
 
200
229
  self.device = get_torch_device(device)
201
230
  self.hf_detr_predictor.to(self.device)
202
231
 
203
232
  def predict(self, np_img: PixelValues) -> list[DetectionResult]:
233
+ """
234
+ Predicts objects in an image.
235
+
236
+ Args:
237
+ np_img: Image as `np.array`.
238
+
239
+ Returns:
240
+ List of `DetectionResult`.
241
+ """
204
242
  results = detr_predict_image(
205
243
  np_img,
206
244
  self.hf_detr_predictor,
@@ -212,36 +250,71 @@ class HFDetrDerivedDetector(HFDetrDerivedDetectorMixin):
212
250
  return self._map_category_names(results)
213
251
 
214
252
  @staticmethod
215
- def get_model(path_weights: PathLikeOrStr, config: PretrainedConfig) -> TableTransformerForObjectDetection:
253
+ def get_model(path_weights: PathLikeOrStr, config: PretrainedConfig) -> EligibleDetrModel:
216
254
  """
217
- Builds the Detr model
255
+ Builds the Detr model.
218
256
 
219
- :param path_weights: The path to the model checkpoint.
220
- :param config: `PretrainedConfig`
221
- :return: TableTransformerForObjectDetection instance
257
+ Args:
258
+ path_weights: The path to the model checkpoint.
259
+ config: `PretrainedConfig` instance.
260
+
261
+ Returns:
262
+ `TableTransformerForObjectDetection` instance.
263
+
264
+ Raises:
265
+ ValueError: If model architecture is not eligible.
222
266
  """
223
- return TableTransformerForObjectDetection.from_pretrained(
224
- pretrained_model_name_or_path=os.fspath(path_weights), config=config
267
+ if "TableTransformerForObjectDetection" in config.architectures:
268
+ return TableTransformerForObjectDetection.from_pretrained(
269
+ pretrained_model_name_or_path=os.fspath(path_weights), config=config
270
+ )
271
+ if "DeformableDetrForObjectDetection" in config.architectures:
272
+ return DeformableDetrForObjectDetection.from_pretrained(
273
+ pretrained_model_name_or_path=os.fspath(path_weights), config=config
274
+ )
275
+ raise ValueError(
276
+ f"Model architecture {config.architectures} not eligible. Please use either "
277
+ "TableTransformerForObjectDetection or DeformableDetrForObjectDetection."
225
278
  )
226
279
 
227
280
  @staticmethod
228
- def get_pre_processor(path_feature_extractor_config: PathLikeOrStr) -> DetrImageProcessor:
281
+ def get_pre_processor(path_feature_extractor_config: PathLikeOrStr, config: PretrainedConfig) -> DetrImageProcessor:
229
282
  """
230
- Builds the feature extractor
283
+ Builds the feature extractor.
284
+
285
+ Args:
286
+ path_feature_extractor_config: Path to feature extractor config.
287
+ config: Model configuration.
231
288
 
232
- :return: DetrFeatureExtractor
289
+ Returns:
290
+ `DetrImageProcessor` instance.
291
+
292
+ Raises:
293
+ ValueError: If model architecture is not eligible.
233
294
  """
234
- return DetrImageProcessor.from_pretrained(
235
- pretrained_model_name_or_path=os.fspath(path_feature_extractor_config)
295
+ if "TableTransformerForObjectDetection" in config.architectures:
296
+ return DetrImageProcessorFast.from_pretrained(
297
+ pretrained_model_name_or_path=os.fspath(path_feature_extractor_config),
298
+ )
299
+ if "DeformableDetrForObjectDetection" in config.architectures:
300
+ return DeformableDetrImageProcessorFast.from_pretrained(
301
+ pretrained_model_name_or_path=os.fspath(path_feature_extractor_config),
302
+ )
303
+ raise ValueError(
304
+ f"Model architecture {config.architectures} not eligible. Please use either "
305
+ "TableTransformerForObjectDetection or DeformableDetrForObjectDetection."
236
306
  )
237
307
 
238
308
  @staticmethod
239
309
  def get_config(path_config: PathLikeOrStr) -> PretrainedConfig:
240
310
  """
241
- Builds the config
311
+ Builds the config.
312
+
313
+ Args:
314
+ path_config: The path to the config.
242
315
 
243
- :param path_config: The path to the json config.
244
- :return: PretrainedConfig instance
316
+ Returns:
317
+ `PretrainedConfig` instance.
245
318
  """
246
319
  config = PretrainedConfig.from_pretrained(pretrained_model_name_or_path=os.fspath(path_config))
247
320
  config.use_timm_backbone = True
@@ -270,17 +343,21 @@ class HFDetrDerivedDetector(HFDetrDerivedDetectorMixin):
270
343
  device: Optional[Union[Literal["cpu", "cuda"], torch.device]] = None,
271
344
  ) -> TableTransformerForObjectDetection:
272
345
  """
273
- Get the wrapped model
346
+ Get the wrapped model.
347
+
348
+ Args:
349
+ path_config_json: The path to the json config.
350
+ path_weights: The path to the model checkpoint.
351
+ device: "cpu" or "cuda". If not specified will auto select depending on what is available.
274
352
 
275
- :param path_config_json: The path to the json config.
276
- :param path_weights: The path to the model checkpoint.
277
- :param device: "cpu" or "cuda". If not specified will auto select depending on what is available
278
- :return: TableTransformerForObjectDetection instance
353
+ Returns:
354
+ `TableTransformerForObjectDetection` instance.
279
355
  """
280
356
  config = HFDetrDerivedDetector.get_config(path_config_json)
281
357
  hf_detr_predictor = HFDetrDerivedDetector.get_model(path_weights, config)
282
358
  device = get_torch_device(device)
283
- return hf_detr_predictor.to(device)
359
+ hf_detr_predictor.to(device)
360
+ return hf_detr_predictor
284
361
 
285
362
  def clear_model(self) -> None:
286
363
  self.hf_detr_predictor = None