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