deepdoctection 0.42.1__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.
- deepdoctection/__init__.py +2 -1
- deepdoctection/analyzer/__init__.py +2 -1
- deepdoctection/analyzer/config.py +904 -0
- deepdoctection/analyzer/dd.py +36 -62
- deepdoctection/analyzer/factory.py +311 -141
- deepdoctection/configs/conf_dd_one.yaml +100 -44
- deepdoctection/configs/profiles.jsonl +32 -0
- deepdoctection/dataflow/__init__.py +9 -6
- deepdoctection/dataflow/base.py +33 -15
- deepdoctection/dataflow/common.py +96 -75
- deepdoctection/dataflow/custom.py +36 -29
- deepdoctection/dataflow/custom_serialize.py +135 -91
- deepdoctection/dataflow/parallel_map.py +33 -31
- deepdoctection/dataflow/serialize.py +15 -10
- deepdoctection/dataflow/stats.py +41 -28
- deepdoctection/datapoint/__init__.py +4 -6
- deepdoctection/datapoint/annotation.py +104 -66
- deepdoctection/datapoint/box.py +190 -130
- deepdoctection/datapoint/convert.py +66 -39
- deepdoctection/datapoint/image.py +151 -95
- deepdoctection/datapoint/view.py +383 -236
- deepdoctection/datasets/__init__.py +2 -6
- deepdoctection/datasets/adapter.py +11 -11
- deepdoctection/datasets/base.py +118 -81
- deepdoctection/datasets/dataflow_builder.py +18 -12
- deepdoctection/datasets/info.py +76 -57
- deepdoctection/datasets/instances/__init__.py +6 -2
- deepdoctection/datasets/instances/doclaynet.py +17 -14
- deepdoctection/datasets/instances/fintabnet.py +16 -22
- deepdoctection/datasets/instances/funsd.py +11 -6
- deepdoctection/datasets/instances/iiitar13k.py +9 -9
- deepdoctection/datasets/instances/layouttest.py +9 -9
- deepdoctection/datasets/instances/publaynet.py +9 -9
- deepdoctection/datasets/instances/pubtables1m.py +13 -13
- deepdoctection/datasets/instances/pubtabnet.py +13 -15
- deepdoctection/datasets/instances/rvlcdip.py +8 -8
- deepdoctection/datasets/instances/xfund.py +11 -9
- deepdoctection/datasets/registry.py +18 -11
- deepdoctection/datasets/save.py +12 -11
- deepdoctection/eval/__init__.py +3 -2
- deepdoctection/eval/accmetric.py +72 -52
- deepdoctection/eval/base.py +29 -10
- deepdoctection/eval/cocometric.py +14 -12
- deepdoctection/eval/eval.py +56 -41
- deepdoctection/eval/registry.py +6 -3
- deepdoctection/eval/tedsmetric.py +24 -9
- deepdoctection/eval/tp_eval_callback.py +13 -12
- deepdoctection/extern/__init__.py +1 -1
- deepdoctection/extern/base.py +176 -97
- deepdoctection/extern/d2detect.py +127 -92
- deepdoctection/extern/deskew.py +19 -10
- deepdoctection/extern/doctrocr.py +157 -106
- deepdoctection/extern/fastlang.py +25 -17
- deepdoctection/extern/hfdetr.py +137 -60
- deepdoctection/extern/hflayoutlm.py +329 -248
- deepdoctection/extern/hflm.py +67 -33
- deepdoctection/extern/model.py +108 -762
- deepdoctection/extern/pdftext.py +37 -12
- deepdoctection/extern/pt/nms.py +15 -1
- deepdoctection/extern/pt/ptutils.py +13 -9
- deepdoctection/extern/tessocr.py +87 -54
- deepdoctection/extern/texocr.py +29 -14
- deepdoctection/extern/tp/tfutils.py +36 -8
- deepdoctection/extern/tp/tpcompat.py +54 -16
- deepdoctection/extern/tp/tpfrcnn/config/config.py +20 -4
- deepdoctection/extern/tpdetect.py +4 -2
- deepdoctection/mapper/__init__.py +1 -1
- deepdoctection/mapper/cats.py +117 -76
- deepdoctection/mapper/cocostruct.py +35 -17
- deepdoctection/mapper/d2struct.py +56 -29
- deepdoctection/mapper/hfstruct.py +32 -19
- deepdoctection/mapper/laylmstruct.py +221 -185
- deepdoctection/mapper/maputils.py +71 -35
- deepdoctection/mapper/match.py +76 -62
- deepdoctection/mapper/misc.py +68 -44
- deepdoctection/mapper/pascalstruct.py +13 -12
- deepdoctection/mapper/prodigystruct.py +33 -19
- deepdoctection/mapper/pubstruct.py +42 -32
- deepdoctection/mapper/tpstruct.py +39 -19
- deepdoctection/mapper/xfundstruct.py +20 -13
- deepdoctection/pipe/__init__.py +1 -2
- deepdoctection/pipe/anngen.py +104 -62
- deepdoctection/pipe/base.py +226 -107
- deepdoctection/pipe/common.py +206 -123
- deepdoctection/pipe/concurrency.py +74 -47
- deepdoctection/pipe/doctectionpipe.py +108 -47
- deepdoctection/pipe/language.py +41 -24
- deepdoctection/pipe/layout.py +45 -18
- deepdoctection/pipe/lm.py +146 -78
- deepdoctection/pipe/order.py +196 -113
- deepdoctection/pipe/refine.py +111 -63
- deepdoctection/pipe/registry.py +1 -1
- deepdoctection/pipe/segment.py +213 -142
- deepdoctection/pipe/sub_layout.py +76 -46
- deepdoctection/pipe/text.py +52 -33
- deepdoctection/pipe/transform.py +8 -6
- deepdoctection/train/d2_frcnn_train.py +87 -69
- deepdoctection/train/hf_detr_train.py +72 -40
- deepdoctection/train/hf_layoutlm_train.py +85 -46
- deepdoctection/train/tp_frcnn_train.py +56 -28
- deepdoctection/utils/concurrency.py +59 -16
- deepdoctection/utils/context.py +40 -19
- deepdoctection/utils/develop.py +25 -17
- deepdoctection/utils/env_info.py +85 -36
- deepdoctection/utils/error.py +16 -10
- deepdoctection/utils/file_utils.py +246 -62
- deepdoctection/utils/fs.py +162 -43
- deepdoctection/utils/identifier.py +29 -16
- deepdoctection/utils/logger.py +49 -32
- deepdoctection/utils/metacfg.py +83 -21
- deepdoctection/utils/pdf_utils.py +119 -62
- deepdoctection/utils/settings.py +24 -10
- deepdoctection/utils/tqdm.py +10 -5
- deepdoctection/utils/transform.py +182 -46
- deepdoctection/utils/utils.py +61 -28
- deepdoctection/utils/viz.py +150 -104
- deepdoctection-0.43.dist-info/METADATA +376 -0
- deepdoctection-0.43.dist-info/RECORD +149 -0
- deepdoctection/analyzer/_config.py +0 -146
- deepdoctection-0.42.1.dist-info/METADATA +0 -431
- deepdoctection-0.42.1.dist-info/RECORD +0 -148
- {deepdoctection-0.42.1.dist-info → deepdoctection-0.43.dist-info}/WHEEL +0 -0
- {deepdoctection-0.42.1.dist-info → deepdoctection-0.43.dist-info}/licenses/LICENSE +0 -0
- {deepdoctection-0.42.1.dist-info → deepdoctection-0.43.dist-info}/top_level.txt +0 -0
|
@@ -16,7 +16,10 @@
|
|
|
16
16
|
# limitations under the License.
|
|
17
17
|
|
|
18
18
|
"""
|
|
19
|
-
Compatibility classes and methods related to Tensorpack package
|
|
19
|
+
Compatibility classes and methods related to Tensorpack package.
|
|
20
|
+
|
|
21
|
+
Info:
|
|
22
|
+
This module provides compatibility classes and methods related to the Tensorpack package.
|
|
20
23
|
"""
|
|
21
24
|
from __future__ import annotations
|
|
22
25
|
|
|
@@ -43,23 +46,32 @@ if not import_guard.is_successful():
|
|
|
43
46
|
|
|
44
47
|
class ModelDescWithConfig(ModelDesc, ABC): # type: ignore
|
|
45
48
|
"""
|
|
46
|
-
A wrapper for Tensorpack ModelDesc for bridging the gap between Tensorpack and DD API.
|
|
47
|
-
|
|
49
|
+
A wrapper for `Tensorpack ModelDesc` for bridging the gap between Tensorpack and DD API.
|
|
50
|
+
|
|
51
|
+
Only for storing a configuration of hyperparameters and maybe training settings.
|
|
52
|
+
|
|
53
|
+
|
|
48
54
|
"""
|
|
49
55
|
|
|
50
56
|
def __init__(self, config: AttrDict) -> None:
|
|
51
57
|
"""
|
|
52
|
-
:
|
|
58
|
+
Args:
|
|
59
|
+
config: Config setting.
|
|
53
60
|
"""
|
|
54
61
|
super().__init__()
|
|
55
62
|
self.cfg = config
|
|
56
63
|
|
|
57
64
|
def get_inference_tensor_names(self) -> tuple[list[str], list[str]]:
|
|
58
65
|
"""
|
|
59
|
-
Returns lists of tensor names to be used to create an inference callable.
|
|
60
|
-
of these names when called under inference context.
|
|
66
|
+
Returns lists of tensor names to be used to create an inference callable.
|
|
61
67
|
|
|
62
|
-
|
|
68
|
+
`build_graph` must create tensors of these names when called under inference context.
|
|
69
|
+
|
|
70
|
+
Returns:
|
|
71
|
+
Tuple of list input and list output names. The names must coincide with tensor within the model.
|
|
72
|
+
|
|
73
|
+
Raises:
|
|
74
|
+
NotImplementedError: If not implemented in subclass.
|
|
63
75
|
"""
|
|
64
76
|
raise NotImplementedError()
|
|
65
77
|
|
|
@@ -82,10 +94,11 @@ class TensorpackPredictor(ABC):
|
|
|
82
94
|
|
|
83
95
|
def __init__(self, model: ModelDescWithConfig, path_weights: PathLikeOrStr, ignore_mismatch: bool) -> None:
|
|
84
96
|
"""
|
|
85
|
-
:
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
97
|
+
Args:
|
|
98
|
+
model: Model, either as `ModelDescWithConfig` or derived from that class.
|
|
99
|
+
path_weights: Model weights of the prediction config.
|
|
100
|
+
ignore_mismatch: When True will ignore mismatches between checkpoint weights and models. This is needed
|
|
101
|
+
if a pre-trained model is to be fine-tuned on a custom dataset.
|
|
89
102
|
"""
|
|
90
103
|
self._model = model
|
|
91
104
|
self.path_weights = Path(path_weights)
|
|
@@ -96,7 +109,10 @@ class TensorpackPredictor(ABC):
|
|
|
96
109
|
|
|
97
110
|
def get_predictor(self) -> OfflinePredictor:
|
|
98
111
|
"""
|
|
99
|
-
|
|
112
|
+
Returns an `OfflinePredictor`.
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
Returns an `OfflinePredictor`.
|
|
100
116
|
"""
|
|
101
117
|
return OfflinePredictor(self.predict_config)
|
|
102
118
|
|
|
@@ -117,16 +133,38 @@ class TensorpackPredictor(ABC):
|
|
|
117
133
|
path_yaml: PathLikeOrStr, categories: Mapping[int, ObjectTypes], config_overwrite: Union[list[str], None]
|
|
118
134
|
) -> ModelDescWithConfig:
|
|
119
135
|
"""
|
|
120
|
-
Implement the config generation, its modification and instantiate a version of the model.
|
|
121
|
-
|
|
136
|
+
Implement the config generation, its modification and instantiate a version of the model.
|
|
137
|
+
|
|
138
|
+
See `pipe.tpfrcnn.TPFrcnnDetector` for an example.
|
|
139
|
+
|
|
140
|
+
Raises:
|
|
141
|
+
NotImplementedError: If not implemented in subclass.
|
|
142
|
+
|
|
143
|
+
Args:
|
|
144
|
+
path_yaml: Path to the yaml file.
|
|
145
|
+
categories: Mapping of categories.
|
|
146
|
+
config_overwrite: List of config overwrites or None.
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
An instance of `ModelDescWithConfig`.
|
|
122
150
|
"""
|
|
123
151
|
raise NotImplementedError()
|
|
124
152
|
|
|
125
153
|
@abstractmethod
|
|
126
154
|
def predict(self, np_img: PixelValues) -> Any:
|
|
127
155
|
"""
|
|
128
|
-
Implement
|
|
129
|
-
|
|
156
|
+
Implement how `self.tp_predictor` is invoked and raw prediction results are generated.
|
|
157
|
+
|
|
158
|
+
Do use only raw objects and nothing, which is related to the DD API.
|
|
159
|
+
|
|
160
|
+
Args:
|
|
161
|
+
np_img: The input image as pixel values.
|
|
162
|
+
|
|
163
|
+
Returns:
|
|
164
|
+
Raw prediction results.
|
|
165
|
+
|
|
166
|
+
Raises:
|
|
167
|
+
NotImplementedError: If not implemented in subclass.
|
|
130
168
|
"""
|
|
131
169
|
raise NotImplementedError()
|
|
132
170
|
|
|
@@ -31,6 +31,7 @@ Backbone settings
|
|
|
31
31
|
|
|
32
32
|
**BACKBONE**
|
|
33
33
|
|
|
34
|
+
```python
|
|
34
35
|
.BOTTLENECK: Resnet oder resnext_32xd4
|
|
35
36
|
|
|
36
37
|
.FREEZE_AFFINE: Do not train affine parameters inside norm layers
|
|
@@ -44,23 +45,26 @@ Backbone settings
|
|
|
44
45
|
.TF_PAD_MODE: Use a base model with TF-preferred padding mode, which may pad more pixels on right or bottom
|
|
45
46
|
than top/left. See https://github.com/tensorflow/tensorflow/issues/18213. Using either one should probably give the same
|
|
46
47
|
performance.
|
|
47
|
-
|
|
48
|
+
```
|
|
48
49
|
|
|
49
50
|
**CASCADE**
|
|
50
51
|
|
|
52
|
+
```python
|
|
51
53
|
.BBOX_REG_WEIGHTS: Bounding box regression weights
|
|
52
54
|
|
|
53
55
|
.IOUS: Iou levels
|
|
54
|
-
|
|
56
|
+
```
|
|
55
57
|
|
|
56
58
|
**DATA**
|
|
57
59
|
|
|
60
|
+
```python
|
|
58
61
|
.TRAIN_NUM_WORKERS: Number of threads to use when parallelizing the pre-processing (e.g. augmenting, adding anchors,
|
|
59
62
|
RPN gt-labelling,...)
|
|
60
|
-
|
|
63
|
+
```
|
|
61
64
|
|
|
62
65
|
**FPN**
|
|
63
66
|
|
|
67
|
+
```
|
|
64
68
|
.ANCHOR_STRIDES: Strides for each FPN level. Must be the same length as ANCHOR_SIZES
|
|
65
69
|
|
|
66
70
|
.CASCADE: Use Cascade RCNN
|
|
@@ -78,9 +82,11 @@ RPN gt-labelling,...)
|
|
|
78
82
|
.NUM_CHANNEL: Number of channels
|
|
79
83
|
|
|
80
84
|
.PROPOSAL_MODE: Choices: 'Level', 'Joint'
|
|
85
|
+
```
|
|
81
86
|
|
|
82
87
|
**FRCNN**
|
|
83
88
|
|
|
89
|
+
```python
|
|
84
90
|
.BATCH_PER_IM: Number of total proposals selected. Will divide into fg and bg by given ratio
|
|
85
91
|
|
|
86
92
|
.BBOX_REG_WEIGHTS: Bounding box regression weights
|
|
@@ -90,15 +96,19 @@ RPN gt-labelling,...)
|
|
|
90
96
|
.FG_THRESH: Threshold how to divide fg and bg selection
|
|
91
97
|
|
|
92
98
|
.MODE_MASK: Whether to train mask head
|
|
99
|
+
```
|
|
93
100
|
|
|
94
101
|
**MRCNN**
|
|
95
102
|
|
|
103
|
+
```python
|
|
96
104
|
.ACCURATE_PASTE: Slightly more aligned results, but very slow on numpy
|
|
97
105
|
|
|
98
106
|
.HEAD_DIM: Head dimension
|
|
107
|
+
```
|
|
99
108
|
|
|
100
109
|
**PREPROC**
|
|
101
110
|
|
|
111
|
+
```python
|
|
102
112
|
.MAX_SIZE: Maximum edge size
|
|
103
113
|
|
|
104
114
|
.PIXEL_MEAN: Pixel mean (on the training data set)
|
|
@@ -108,10 +118,11 @@ RPN gt-labelling,...)
|
|
|
108
118
|
.SHORT_EDGE_SIZE: Size to resize the image to (inference), while not exceeding max size
|
|
109
119
|
|
|
110
120
|
.TRAIN_SHORT_EDGE_SIZE: The size to resize the image to (training), while not exceeding max size. [min, max] to sample
|
|
111
|
-
|
|
121
|
+
```
|
|
112
122
|
|
|
113
123
|
**RPN**
|
|
114
124
|
|
|
125
|
+
```python
|
|
115
126
|
.ANCHOR_RATIOS: Anchor ratios
|
|
116
127
|
|
|
117
128
|
.ANCHOR_SIZES: Anchor sizes
|
|
@@ -144,9 +155,11 @@ to a value larger than 1.0 will disable the feature. It is disabled by default b
|
|
|
144
155
|
.TRAIN_POST_NMS_TOPK: Number of proposals after carrying out nms (training)
|
|
145
156
|
|
|
146
157
|
.POST_NMS_TOPK: Number of proposals after carrying out nms (inference)
|
|
158
|
+
```
|
|
147
159
|
|
|
148
160
|
**OUTPUT**
|
|
149
161
|
|
|
162
|
+
```python
|
|
150
163
|
.FRCNN_NMS_THRESH: Nms threshold for output. nms being performed per class prediction
|
|
151
164
|
|
|
152
165
|
.RESULTS_PER_IM: Number of output detection results
|
|
@@ -154,11 +167,13 @@ to a value larger than 1.0 will disable the feature. It is disabled by default b
|
|
|
154
167
|
.RESULT_SCORE_THRESH: Threshold for detection result
|
|
155
168
|
|
|
156
169
|
.NMS_THRESH_CLASS_AGNOSTIC: Nms threshold for output. nms being performed over all class predictions
|
|
170
|
+
```
|
|
157
171
|
|
|
158
172
|
TRAINER: options: 'horovod', 'replicated'. Note that Horovod trainer is not available when TF2 is installed
|
|
159
173
|
|
|
160
174
|
**TRAIN**
|
|
161
175
|
|
|
176
|
+
```python
|
|
162
177
|
.LR_SCHEDULE: "1x" schedule in detectron. LR_SCHEDULE means equivalent steps when the total batch size is 8.
|
|
163
178
|
It can be either a string like "3x" that refers to standard convention, or a list of int.
|
|
164
179
|
LR_SCHEDULE=3x is the same as LR_SCHEDULE=[420000, 500000, 540000], which
|
|
@@ -184,6 +199,7 @@ TRAINER: options: 'horovod', 'replicated'. Note that Horovod trainer is not avai
|
|
|
184
199
|
.STARTING_EPOCH: Starting epoch. Useful when restarting training.
|
|
185
200
|
|
|
186
201
|
.LOG_DIR: Log dir
|
|
202
|
+
```
|
|
187
203
|
|
|
188
204
|
"""
|
|
189
205
|
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
# limitations under the License.
|
|
17
17
|
|
|
18
18
|
"""
|
|
19
|
-
TP Faster
|
|
19
|
+
TP Faster-RCNN model
|
|
20
20
|
"""
|
|
21
21
|
from __future__ import annotations
|
|
22
22
|
|
|
@@ -149,8 +149,10 @@ class TPFrcnnDetector(TensorpackPredictor, TPFrcnnDetectorMixin):
|
|
|
149
149
|
|
|
150
150
|
hyper_param_config = set_config_by_yaml(path_yaml)
|
|
151
151
|
|
|
152
|
-
|
|
152
|
+
hyper_param_config.freeze(freezed=False)
|
|
153
|
+
if config_overwrite:
|
|
153
154
|
hyper_param_config.update_args(config_overwrite)
|
|
155
|
+
hyper_param_config.freeze()
|
|
154
156
|
|
|
155
157
|
model_frcnn_config(config=hyper_param_config, categories=categories, print_summary=False)
|
|
156
158
|
return ResNetFPNModel(config=hyper_param_config)
|
deepdoctection/mapper/cats.py
CHANGED
|
@@ -16,8 +16,7 @@
|
|
|
16
16
|
# limitations under the License.
|
|
17
17
|
|
|
18
18
|
"""
|
|
19
|
-
|
|
20
|
-
builder method of a dataset.
|
|
19
|
+
Mapping functions for handling categories
|
|
21
20
|
"""
|
|
22
21
|
|
|
23
22
|
from collections import defaultdict
|
|
@@ -36,14 +35,36 @@ def cat_to_sub_cat(
|
|
|
36
35
|
cat_to_sub_cat_dict: Optional[dict[TypeOrStr, TypeOrStr]] = None,
|
|
37
36
|
) -> Image:
|
|
38
37
|
"""
|
|
39
|
-
Replace some
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
38
|
+
Replace some categories with sub categories.
|
|
39
|
+
|
|
40
|
+
Example:
|
|
41
|
+
```python
|
|
42
|
+
categories_dict_names_as_key = {'foo': 1}
|
|
43
|
+
cat_to_sub_cat_dict = {'foo': 'foo_1', 'bak': 'bak_1'}
|
|
44
|
+
|
|
45
|
+
dp = cat_to_sub_cat(categories_dict_names_as_key, cat_to_sub_cat_dict)(dp)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
will transform
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
ImageAnnotation(category_name='foo', category_id=1, ...)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
to
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
ImageAnnotation(category_name='foo_1', category_id=1, ...)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
Args:
|
|
62
|
+
dp: Image datapoint
|
|
63
|
+
categories_dict_names_as_key: A dict of all possible categories and their ids
|
|
64
|
+
cat_to_sub_cat_dict: e.g. `{'foo': 'sub_cat_1', 'bak': 'sub_cat_2'}`
|
|
65
|
+
|
|
66
|
+
Returns:
|
|
67
|
+
Image with updated `ImageAnnotation`s
|
|
47
68
|
"""
|
|
48
69
|
if categories_dict_names_as_key is None:
|
|
49
70
|
categories_dict_names_as_key = {}
|
|
@@ -67,29 +88,31 @@ def re_assign_cat_ids(
|
|
|
67
88
|
cat_to_sub_cat_mapping: Optional[Mapping[ObjectTypes, Any]] = None,
|
|
68
89
|
) -> Image:
|
|
69
90
|
"""
|
|
70
|
-
Re-assigning
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
:
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
`
|
|
91
|
-
|
|
92
|
-
|
|
91
|
+
Re-assigning `category_id`s is sometimes necessary to align with categories of the `DatasetCategories` .
|
|
92
|
+
|
|
93
|
+
Example:
|
|
94
|
+
Consider the situation where some categories are filtered. In order to guarantee alignment of category ids of
|
|
95
|
+
the `DatasetCategories` the ids in the annotation have to be re-assigned.
|
|
96
|
+
|
|
97
|
+
Annotations that are not in the dictionary provided will be removed.
|
|
98
|
+
|
|
99
|
+
Args:
|
|
100
|
+
dp: Image
|
|
101
|
+
categories_dict_name_as_key: e.g. `{LayoutType.word: 1}`
|
|
102
|
+
cat_to_sub_cat_mapping: e.g. `{LayoutType.word: {WordType.token_class:
|
|
103
|
+
{FundsFirstPage.REPORT_DATE: 1,
|
|
104
|
+
FundsFirstPage.REPORT_TYPE: 2,
|
|
105
|
+
FundsFirstPage.UMBRELLA: 3,
|
|
106
|
+
FundsFirstPage.FUND_NAME: 4,
|
|
107
|
+
TokenClasses.OTHER: 5},
|
|
108
|
+
WordType.TAG:{ BioTag.INSIDE: 1,
|
|
109
|
+
BioTag.OUTSIDE: 2,
|
|
110
|
+
BioTag.BEGIN: 3}}}`
|
|
111
|
+
To re-assign the `category_id`s of an image summary, use the key 'default_type' for the default category,
|
|
112
|
+
e.g. `{DefaultType.DEFAULT_TYPE: {PageType.DOCUMENT_TYPE: {DocumentType.INVOICE:1,
|
|
113
|
+
DocumentType.BANK_STATEMENT:2}}}`
|
|
114
|
+
Returns:
|
|
115
|
+
Image
|
|
93
116
|
"""
|
|
94
117
|
|
|
95
118
|
ann_ids_to_remove: list[str] = []
|
|
@@ -129,12 +152,14 @@ def filter_cat(
|
|
|
129
152
|
Filters category annotations based on the on a list of categories to be kept and a list of all possible
|
|
130
153
|
category names that might be available in dp.
|
|
131
154
|
|
|
132
|
-
:
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
155
|
+
Args:
|
|
156
|
+
dp: Image datapoint
|
|
157
|
+
categories_as_list_filtered: A list of `category_name`s with categories to keep. Using a dataset e.g.
|
|
158
|
+
`my_data.categories.get_categories(as_dict=False,filtered=True)`
|
|
159
|
+
categories_as_list_unfiltered: A list of all available `category_name`s. Using a dataset e.g.
|
|
160
|
+
`my_data.categories.get_categories(as_dict=False)`
|
|
161
|
+
Returns:
|
|
162
|
+
Image with filtered Annotations
|
|
138
163
|
"""
|
|
139
164
|
|
|
140
165
|
cats_to_remove_list = [cat for cat in categories_as_list_unfiltered if cat not in categories_as_list_filtered]
|
|
@@ -161,13 +186,15 @@ def filter_summary(
|
|
|
161
186
|
Filters datapoints with given summary conditions. If several conditions are given, it will filter out datapoints
|
|
162
187
|
that do not satisfy all conditions.
|
|
163
188
|
|
|
164
|
-
:
|
|
165
|
-
|
|
189
|
+
Args:
|
|
190
|
+
dp: Image datapoint
|
|
191
|
+
sub_cat_to_sub_cat_names_or_ids: A dict of list. The key correspond to the sub category key to look for in
|
|
166
192
|
the summary. The value correspond to a sequence of either category names
|
|
167
193
|
or category ids
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
:
|
|
194
|
+
mode: With respect to the previous argument, it will look if the `category_name`, the `value` or the
|
|
195
|
+
`category_id` corresponds to any of the given values.
|
|
196
|
+
Returns:
|
|
197
|
+
Image or `None`
|
|
171
198
|
"""
|
|
172
199
|
for key, values in sub_cat_to_sub_cat_names_or_ids.items():
|
|
173
200
|
if mode == "name":
|
|
@@ -194,46 +221,54 @@ def image_to_cat_id(
|
|
|
194
221
|
Extracts all category_ids, sub category information or summary sub category information with given names into a
|
|
195
222
|
defaultdict. This mapping is useful when running evaluation with e.g. an accuracy metric.
|
|
196
223
|
|
|
197
|
-
|
|
224
|
+
Example 1:
|
|
198
225
|
|
|
199
226
|
dp contains image annotations
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
227
|
+
```python
|
|
228
|
+
ImageAnnotation(category_name='foo',category_id='1',...),
|
|
229
|
+
ImageAnnotation(category_name='bak',category_id='2',...),
|
|
230
|
+
ImageAnnotation(category_name='baz',category_id='3',...),
|
|
231
|
+
ImageAnnotation(category_name='foo',category_id='1',...),
|
|
232
|
+
```
|
|
205
233
|
|
|
206
234
|
Then
|
|
207
235
|
|
|
208
|
-
|
|
209
|
-
|
|
236
|
+
```python
|
|
237
|
+
image_to_cat_id(category_names=['foo', 'bak', 'baz'])(dp)
|
|
238
|
+
```
|
|
210
239
|
will return
|
|
211
240
|
|
|
212
|
-
|
|
241
|
+
```python
|
|
242
|
+
({'foo':[1,1], 'bak':[2], 'baz':[3]}, image_id)
|
|
243
|
+
```
|
|
213
244
|
|
|
214
|
-
|
|
215
|
-
**Example 2:**
|
|
245
|
+
Example 2:
|
|
216
246
|
|
|
217
247
|
dp contains image annotations as given in Example 1. Moreover, the 'foo' image annotation have sub categories:
|
|
218
248
|
|
|
219
|
-
|
|
220
|
-
|
|
249
|
+
```python
|
|
250
|
+
foo_sub_1: CategoryAnnotation(category_name='sub_1', category_id='4')
|
|
251
|
+
foo_sub_1: CategoryAnnotation(category_name='sub_1', category_id='5')
|
|
221
252
|
|
|
222
|
-
|
|
253
|
+
image_to_cat_id(sub_categories={'foo':'foo_sub_1'})
|
|
254
|
+
```
|
|
223
255
|
|
|
224
256
|
will return
|
|
225
257
|
|
|
226
|
-
|
|
227
|
-
|
|
258
|
+
```python
|
|
259
|
+
({'foo_sub_1':[5,6]}, image_id)
|
|
260
|
+
```
|
|
228
261
|
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
262
|
+
Args:
|
|
263
|
+
dp: Image
|
|
264
|
+
category_names: A list of category names
|
|
265
|
+
sub_categories: A dict `{'cat':'sub_cat'}` or a list. Will dump the results with sub_cat as key
|
|
266
|
+
id_name_or_value: Only relevant for sub categories. It will extract the sub category id, the name or, if the
|
|
234
267
|
sub category is a container, it will extract a value.
|
|
235
|
-
|
|
236
|
-
|
|
268
|
+
summary_sub_category_names: A list of summary sub categories
|
|
269
|
+
|
|
270
|
+
Returns:
|
|
271
|
+
A defaultdict of lists
|
|
237
272
|
"""
|
|
238
273
|
|
|
239
274
|
cat_container = defaultdict(list)
|
|
@@ -309,13 +344,16 @@ def remove_cats(
|
|
|
309
344
|
Remove categories according to given category names or sub category names. Note that these will change the container
|
|
310
345
|
in which the objects are stored.
|
|
311
346
|
|
|
312
|
-
:
|
|
313
|
-
|
|
347
|
+
Args:
|
|
348
|
+
dp: A datapoint image
|
|
349
|
+
category_names: A single category name or a list of categories to remove. On default will remove
|
|
314
350
|
nothing.
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
351
|
+
sub_categories: A dict with category names and a list of their sub categories to be removed
|
|
352
|
+
relationships: A dict with category names and a list of relationship names to be removed
|
|
353
|
+
summary_sub_categories: A single sub category or a list of sub categories from a summary to be removed
|
|
354
|
+
|
|
355
|
+
Returns:
|
|
356
|
+
A datapoint image with removed categories
|
|
319
357
|
"""
|
|
320
358
|
|
|
321
359
|
if isinstance(category_names, str):
|
|
@@ -364,9 +402,12 @@ def add_summary(dp: Image, categories: Mapping[int, ObjectTypes]) -> Image:
|
|
|
364
402
|
"""
|
|
365
403
|
Adding a summary with the number of categories in an image.
|
|
366
404
|
|
|
367
|
-
:
|
|
368
|
-
|
|
369
|
-
|
|
405
|
+
Args:
|
|
406
|
+
dp: Image
|
|
407
|
+
categories: A dict of all categories, e.g. `{"1": "text", "2":"title", ...}`
|
|
408
|
+
|
|
409
|
+
Returns:
|
|
410
|
+
Image
|
|
370
411
|
"""
|
|
371
412
|
category_list = list(categories.values())
|
|
372
413
|
anns = dp.get_annotation(category_names=category_list)
|
|
@@ -42,19 +42,29 @@ def coco_to_image(
|
|
|
42
42
|
coarse_sub_cat_name: Optional[ObjectTypes] = None,
|
|
43
43
|
) -> Optional[Image]:
|
|
44
44
|
"""
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
:
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
45
|
+
Maps a dataset in `COCO` format that has been serialized to image format.
|
|
46
|
+
|
|
47
|
+
This serialized input requirements hold when a `COCO` style sheet is loaded via `SerializerCoco.load`.
|
|
48
|
+
|
|
49
|
+
Args:
|
|
50
|
+
dp: A datapoint in serialized COCO format.
|
|
51
|
+
categories: A dict of categories, e.g. `DatasetCategories.get_categories`.
|
|
52
|
+
load_image: If `True`, it will load image to `Image.image`.
|
|
53
|
+
filter_empty_image: Will return `None` if datapoint has no annotations.
|
|
54
|
+
fake_score: If `dp` does not contain a score, a fake score with uniform random variables in `(0,1)` will be
|
|
55
|
+
added.
|
|
56
|
+
coarse_mapping: A mapping to map categories into broader categories. Note that the coarser categories must
|
|
57
|
+
already be included in the original mapping.
|
|
58
|
+
coarse_sub_cat_name: A name to be provided as sub category key for a coarse mapping.
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
`Image` or `None`.
|
|
62
|
+
|
|
63
|
+
Raises:
|
|
64
|
+
ValueError: If `coarse_sub_cat_name` is provided but `coarse_mapping` is not.
|
|
65
|
+
|
|
66
|
+
Note:
|
|
67
|
+
A coarse mapping must be provided when `coarse_sub_cat_name` has been passed.
|
|
58
68
|
"""
|
|
59
69
|
|
|
60
70
|
if coarse_sub_cat_name and coarse_mapping is None:
|
|
@@ -111,11 +121,19 @@ def coco_to_image(
|
|
|
111
121
|
|
|
112
122
|
def image_to_coco(dp: Image) -> tuple[JsonDict, list[JsonDict]]:
|
|
113
123
|
"""
|
|
114
|
-
|
|
115
|
-
|
|
124
|
+
Converts an image back into the `COCO` format.
|
|
125
|
+
|
|
126
|
+
As images and annotations are separated, it will return a dict with the image information and one for its
|
|
127
|
+
annotations.
|
|
128
|
+
|
|
129
|
+
Args:
|
|
130
|
+
dp: An `Image`.
|
|
131
|
+
|
|
132
|
+
Returns:
|
|
133
|
+
A tuple of dicts, the first corresponding to the COCO-image object, the second to their COCO-annotations.
|
|
116
134
|
|
|
117
|
-
:
|
|
118
|
-
|
|
135
|
+
Raises:
|
|
136
|
+
TypeError: If `dp` is not of type `Image`.
|
|
119
137
|
"""
|
|
120
138
|
|
|
121
139
|
if not isinstance(dp, Image):
|