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.

Files changed (111) hide show
  1. deepdoctection/__init__.py +8 -25
  2. deepdoctection/analyzer/dd.py +84 -71
  3. deepdoctection/dataflow/common.py +9 -5
  4. deepdoctection/dataflow/custom.py +5 -5
  5. deepdoctection/dataflow/custom_serialize.py +75 -18
  6. deepdoctection/dataflow/parallel_map.py +3 -3
  7. deepdoctection/dataflow/serialize.py +4 -4
  8. deepdoctection/dataflow/stats.py +3 -3
  9. deepdoctection/datapoint/annotation.py +78 -56
  10. deepdoctection/datapoint/box.py +7 -7
  11. deepdoctection/datapoint/convert.py +6 -6
  12. deepdoctection/datapoint/image.py +157 -75
  13. deepdoctection/datapoint/view.py +175 -151
  14. deepdoctection/datasets/adapter.py +30 -24
  15. deepdoctection/datasets/base.py +10 -10
  16. deepdoctection/datasets/dataflow_builder.py +3 -3
  17. deepdoctection/datasets/info.py +23 -25
  18. deepdoctection/datasets/instances/doclaynet.py +48 -49
  19. deepdoctection/datasets/instances/fintabnet.py +44 -45
  20. deepdoctection/datasets/instances/funsd.py +23 -23
  21. deepdoctection/datasets/instances/iiitar13k.py +8 -8
  22. deepdoctection/datasets/instances/layouttest.py +2 -2
  23. deepdoctection/datasets/instances/publaynet.py +3 -3
  24. deepdoctection/datasets/instances/pubtables1m.py +18 -18
  25. deepdoctection/datasets/instances/pubtabnet.py +30 -29
  26. deepdoctection/datasets/instances/rvlcdip.py +28 -29
  27. deepdoctection/datasets/instances/xfund.py +51 -30
  28. deepdoctection/datasets/save.py +6 -6
  29. deepdoctection/eval/accmetric.py +32 -33
  30. deepdoctection/eval/base.py +8 -9
  31. deepdoctection/eval/cocometric.py +13 -12
  32. deepdoctection/eval/eval.py +32 -26
  33. deepdoctection/eval/tedsmetric.py +16 -12
  34. deepdoctection/eval/tp_eval_callback.py +7 -16
  35. deepdoctection/extern/base.py +339 -134
  36. deepdoctection/extern/d2detect.py +69 -89
  37. deepdoctection/extern/deskew.py +11 -10
  38. deepdoctection/extern/doctrocr.py +81 -64
  39. deepdoctection/extern/fastlang.py +23 -16
  40. deepdoctection/extern/hfdetr.py +53 -38
  41. deepdoctection/extern/hflayoutlm.py +216 -155
  42. deepdoctection/extern/hflm.py +35 -30
  43. deepdoctection/extern/model.py +433 -255
  44. deepdoctection/extern/pdftext.py +15 -15
  45. deepdoctection/extern/pt/ptutils.py +4 -2
  46. deepdoctection/extern/tessocr.py +39 -38
  47. deepdoctection/extern/texocr.py +14 -16
  48. deepdoctection/extern/tp/tfutils.py +16 -2
  49. deepdoctection/extern/tp/tpcompat.py +11 -7
  50. deepdoctection/extern/tp/tpfrcnn/config/config.py +4 -4
  51. deepdoctection/extern/tp/tpfrcnn/modeling/backbone.py +1 -1
  52. deepdoctection/extern/tp/tpfrcnn/modeling/model_box.py +5 -5
  53. deepdoctection/extern/tp/tpfrcnn/modeling/model_fpn.py +6 -6
  54. deepdoctection/extern/tp/tpfrcnn/modeling/model_frcnn.py +4 -4
  55. deepdoctection/extern/tp/tpfrcnn/modeling/model_mrcnn.py +5 -3
  56. deepdoctection/extern/tp/tpfrcnn/preproc.py +5 -5
  57. deepdoctection/extern/tpdetect.py +40 -45
  58. deepdoctection/mapper/cats.py +36 -40
  59. deepdoctection/mapper/cocostruct.py +16 -12
  60. deepdoctection/mapper/d2struct.py +22 -22
  61. deepdoctection/mapper/hfstruct.py +7 -7
  62. deepdoctection/mapper/laylmstruct.py +22 -24
  63. deepdoctection/mapper/maputils.py +9 -10
  64. deepdoctection/mapper/match.py +33 -2
  65. deepdoctection/mapper/misc.py +6 -7
  66. deepdoctection/mapper/pascalstruct.py +4 -4
  67. deepdoctection/mapper/prodigystruct.py +6 -6
  68. deepdoctection/mapper/pubstruct.py +84 -92
  69. deepdoctection/mapper/tpstruct.py +3 -3
  70. deepdoctection/mapper/xfundstruct.py +33 -33
  71. deepdoctection/pipe/anngen.py +39 -14
  72. deepdoctection/pipe/base.py +68 -99
  73. deepdoctection/pipe/common.py +181 -85
  74. deepdoctection/pipe/concurrency.py +14 -10
  75. deepdoctection/pipe/doctectionpipe.py +24 -21
  76. deepdoctection/pipe/language.py +20 -25
  77. deepdoctection/pipe/layout.py +18 -16
  78. deepdoctection/pipe/lm.py +49 -47
  79. deepdoctection/pipe/order.py +63 -65
  80. deepdoctection/pipe/refine.py +102 -109
  81. deepdoctection/pipe/segment.py +157 -162
  82. deepdoctection/pipe/sub_layout.py +50 -40
  83. deepdoctection/pipe/text.py +37 -36
  84. deepdoctection/pipe/transform.py +19 -16
  85. deepdoctection/train/d2_frcnn_train.py +27 -25
  86. deepdoctection/train/hf_detr_train.py +22 -18
  87. deepdoctection/train/hf_layoutlm_train.py +49 -48
  88. deepdoctection/train/tp_frcnn_train.py +10 -11
  89. deepdoctection/utils/concurrency.py +1 -1
  90. deepdoctection/utils/context.py +13 -6
  91. deepdoctection/utils/develop.py +4 -4
  92. deepdoctection/utils/env_info.py +52 -14
  93. deepdoctection/utils/file_utils.py +6 -11
  94. deepdoctection/utils/fs.py +41 -14
  95. deepdoctection/utils/identifier.py +2 -2
  96. deepdoctection/utils/logger.py +15 -15
  97. deepdoctection/utils/metacfg.py +7 -7
  98. deepdoctection/utils/pdf_utils.py +39 -14
  99. deepdoctection/utils/settings.py +188 -182
  100. deepdoctection/utils/tqdm.py +1 -1
  101. deepdoctection/utils/transform.py +14 -9
  102. deepdoctection/utils/types.py +104 -0
  103. deepdoctection/utils/utils.py +7 -7
  104. deepdoctection/utils/viz.py +70 -69
  105. {deepdoctection-0.32.dist-info → deepdoctection-0.34.dist-info}/METADATA +7 -4
  106. deepdoctection-0.34.dist-info/RECORD +146 -0
  107. {deepdoctection-0.32.dist-info → deepdoctection-0.34.dist-info}/WHEEL +1 -1
  108. deepdoctection/utils/detection_types.py +0 -68
  109. deepdoctection-0.32.dist-info/RECORD +0 -146
  110. {deepdoctection-0.32.dist-info → deepdoctection-0.34.dist-info}/LICENSE +0 -0
  111. {deepdoctection-0.32.dist-info → deepdoctection-0.34.dist-info}/top_level.txt +0 -0
@@ -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 List, Literal, Mapping, Optional, Sequence, Union
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 .base import DetectionResult, ObjectDetector
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: ImageType,
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
- ) -> List[DetectionResult]:
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[str, TypeOrStr], filter_categories: Optional[Sequence[TypeOrStr]] = None):
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 = {idx: get_type(cat) for idx, cat in categories.items()}
108
+ self.categories = ModelCategories(init_categories=categories)
108
109
  if filter_categories:
109
- filter_categories = [get_type(cat) for cat in 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: List[DetectionResult]) -> List[DetectionResult]:
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: List[DetectionResult] = []
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 = 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:
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: str) -> str:
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: str,
166
- path_weights: str,
167
- path_feature_extractor_config_json: str,
168
- categories: Mapping[str, TypeOrStr],
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: ImageType) -> List[DetectionResult]:
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: str, config: PretrainedConfig) -> TableTransformerForObjectDetection:
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: str) -> DetrFeatureExtractor:
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(pretrained_model_name_or_path=path_feature_extractor_config)
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: str) -> PretrainedConfig:
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) -> List[Requirement]:
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, self.path_weights, self.path_feature_extractor_config, self.categories, self.device
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: str, path_weights: str, device: Optional[Union[Literal["cpu", "cuda"], torch.device]] = None
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