Skip to content

Commit 8c6d669

Browse files
authored
feat: make table transformer parameters configurable (#224)
- refactor `tables.py` so that the structure element confidence threshold values are loaded from `inference_config` - refactor intersection over box area threshold in `objects_to_structure` to config intead of using hardwired value of 0.5 (default is still 0.5)
1 parent eaa8d65 commit 8c6d669

File tree

4 files changed

+63
-12
lines changed

4 files changed

+63
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.6.2
2+
3+
* move the confidence threshold for table transformer to config
4+
15
## 0.6.1
26

37
* YoloX_quantized is now the default model. This models detects most diverse types and detect tables better than previous model.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.6.1" # pragma: no cover
1+
__version__ = "0.6.2" # pragma: no cover

unstructured_inference/config.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,42 @@ def TABLE_IMAGE_BACKGROUND_PAD(self) -> int:
4646
"""
4747
return self._get_int("TABLE_IMAGE_BACKGROUND_PAD", 0)
4848

49+
@property
50+
def TT_TABLE_CONF(self) -> float:
51+
"""confidence threshold for table identified by table transformer"""
52+
return self._get_float("TT_TABLE_CONF", 0.5)
53+
54+
@property
55+
def TABLE_COLUMN_CONF(self) -> float:
56+
"""confidence threshold for column identified by table transformer"""
57+
return self._get_float("TABLE_COLUMN_CONF", 0.5)
58+
59+
@property
60+
def TABLE_ROW_CONF(self) -> float:
61+
"""confidence threshold for column identified by table transformer"""
62+
return self._get_float("TABLE_ROW_CONF", 0.5)
63+
64+
@property
65+
def TABLE_COLUMN_HEADER_CONF(self) -> float:
66+
"""confidence threshold for column header identified by table transformer"""
67+
return self._get_float("TABLE_COLUMN_HEADER_CONF", 0.5)
68+
69+
@property
70+
def TABLE_PROJECTED_ROW_HEADER_CONF(self) -> float:
71+
"""confidence threshold for projected row header identified by table transformer"""
72+
return self._get_float("TABLE_PROJECTED_ROW_HEADER_CONF", 0.5)
73+
74+
@property
75+
def TABLE_SPANNING_CELL_CONF(self) -> float:
76+
"""confidence threshold for table spanning cells identified by table transformer"""
77+
return self._get_float("TABLE_SPANNING_CELL_CONF", 0.5)
78+
79+
@property
80+
def TABLE_IOB_THRESHOLD(self) -> float:
81+
"""minimum intersection over box area ratio for a box to be considered part of a larger box
82+
it intersects"""
83+
return self._get_float("TABLE_IOB_THRESHOLD", 0.5)
84+
4985
@property
5086
def LAYOUT_SAME_REGION_THRESHOLD(self) -> float:
5187
"""threshold for two layouts' bounding boxes to be considered as the same region

unstructured_inference/models/tables.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,13 @@ def get_class_map(data_type: str):
177177

178178

179179
structure_class_thresholds = {
180-
"table": 0.5,
181-
"table column": 0.5,
182-
"table row": 0.5,
183-
"table column header": 0.5,
184-
"table projected row header": 0.5,
185-
"table spanning cell": 0.5,
180+
"table": inference_config.TT_TABLE_CONF,
181+
"table column": inference_config.TABLE_COLUMN_CONF,
182+
"table row": inference_config.TABLE_ROW_CONF,
183+
"table column header": inference_config.TABLE_COLUMN_HEADER_CONF,
184+
"table projected row header": inference_config.TABLE_PROJECTED_ROW_HEADER_CONF,
185+
"table spanning cell": inference_config.TABLE_SPANNING_CELL_CONF,
186+
# FIXME (yao) this parameter doesn't seem to be used at all in inference? Can we remove it
186187
"no object": 10,
187188
}
188189

@@ -282,8 +283,16 @@ def objects_to_structures(objects, tokens, class_thresholds):
282283
table_structures = []
283284

284285
for table in tables:
285-
table_objects = [obj for obj in objects if iob(obj["bbox"], table["bbox"]) >= 0.5]
286-
table_tokens = [token for token in tokens if iob(token["bbox"], table["bbox"]) >= 0.5]
286+
table_objects = [
287+
obj
288+
for obj in objects
289+
if iob(obj["bbox"], table["bbox"]) >= inference_config.TABLE_IOB_THRESHOLD
290+
]
291+
table_tokens = [
292+
token
293+
for token in tokens
294+
if iob(token["bbox"], table["bbox"]) >= inference_config.TABLE_IOB_THRESHOLD
295+
]
287296

288297
structure = {}
289298

@@ -302,7 +311,7 @@ def objects_to_structures(objects, tokens, class_thresholds):
302311
for obj in rows:
303312
obj["column header"] = False
304313
for header_obj in column_headers:
305-
if iob(obj["bbox"], header_obj["bbox"]) >= 0.5:
314+
if iob(obj["bbox"], header_obj["bbox"]) >= inference_config.TABLE_IOB_THRESHOLD:
306315
obj["column header"] = True
307316

308317
# Refine table structures
@@ -478,7 +487,7 @@ def structure_to_cells(table_structure, tokens):
478487
spanning_cell_rect = Rect(list(spanning_cell["bbox"]))
479488
if (
480489
spanning_cell_rect.intersect(cell_rect).get_area() / cell_rect.get_area()
481-
) > 0.5:
490+
) > inference_config.TABLE_IOB_THRESHOLD:
482491
cell["subcell"] = True
483492
break
484493

@@ -499,7 +508,9 @@ def structure_to_cells(table_structure, tokens):
499508
for subcell in subcells:
500509
subcell_rect = Rect(list(subcell["bbox"]))
501510
subcell_rect_area = subcell_rect.get_area()
502-
if (subcell_rect.intersect(spanning_cell_rect).get_area() / subcell_rect_area) > 0.5:
511+
if (
512+
subcell_rect.intersect(spanning_cell_rect).get_area() / subcell_rect_area
513+
) > inference_config.TABLE_IOB_THRESHOLD:
503514
if cell_rect is None:
504515
cell_rect = Rect(list(subcell["bbox"]))
505516
else:

0 commit comments

Comments
 (0)