Skip to content

Commit e764bc5

Browse files
authored
feat: round numbers to reduce undeterministic behavior (#3740)
This PR rounds the floating point number associated with coordinates in `pdfminer_processing.py`. This helps to eliminate machine precision caused randomness in bounding box overlap detection. Currently the rounding is set to the nearest machine precision for `np.float32` using `np.finfo(float)`, which yields resolution = `1e-15`. ## future work We should reduce the rounding to only 6 digits after floating point since the data type `float32` has a resolution of only `1e-6`. However it would break tests. A followup is required to tune the threshold values in `pdfminer_processing.py` so that it works with `1e-6` resolution.
1 parent 3240e3d commit e764bc5

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

Diff for: CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
## 0.16.1-dev4
1+
## 0.16.1-dev5
22

33
### Enhancements
44

5+
* **Round coordinates** Round coordinates when computing bounding box overlaps in `pdfminer_processing.py` to nearest machine precision. This can help reduce underterministic behavior from machine precision that affects which bounding boxes to combine.
6+
57
### Features
68

79
### Fixes

Diff for: unstructured/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.16.1-dev4" # pragma: no cover
1+
__version__ = "0.16.1-dev5" # pragma: no cover

Diff for: unstructured/partition/pdf_image/pdfminer_processing.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222

2323
EPSILON_AREA = 0.01
24+
# rounding floating point to nearest machine precision
25+
DEFAULT_ROUND = 15
2426

2527

2628
def process_file_with_pdfminer(
@@ -115,19 +117,19 @@ def _create_text_region(x1, y1, x2, y2, coef, text, source, region_class):
115117
)
116118

117119

118-
def get_coords_from_bboxes(bboxes) -> np.ndarray:
120+
def get_coords_from_bboxes(bboxes, round_to: int = DEFAULT_ROUND) -> np.ndarray:
119121
"""convert a list of boxes's coords into np array"""
120122
# preallocate memory
121123
coords = np.zeros((len(bboxes), 4), dtype=np.float32)
122124

123125
for i, bbox in enumerate(bboxes):
124126
coords[i, :] = [bbox.x1, bbox.y1, bbox.x2, bbox.y2]
125127

126-
return coords
128+
return coords.round(round_to)
127129

128130

129131
def areas_of_boxes_and_intersection_area(
130-
coords1: np.ndarray, coords2: np.ndarray, threshold: float = 0.5
132+
coords1: np.ndarray, coords2: np.ndarray, round_to: int = DEFAULT_ROUND
131133
):
132134
"""compute intersection area and own areas for two groups of bounding boxes"""
133135
x11, y11, x12, y12 = np.split(coords1, 4, axis=1)
@@ -139,26 +141,33 @@ def areas_of_boxes_and_intersection_area(
139141
boxa_area = (x12 - x11 + 1) * (y12 - y11 + 1)
140142
boxb_area = (x22 - x21 + 1) * (y22 - y21 + 1)
141143

142-
return inter_area, boxa_area, boxb_area
144+
return inter_area.round(round_to), boxa_area.round(round_to), boxb_area.round(round_to)
143145

144146

145-
def bboxes1_is_almost_subregion_of_bboxes2(bboxes1, bboxes2, threshold: float = 0.5) -> np.ndarray:
147+
def bboxes1_is_almost_subregion_of_bboxes2(
148+
bboxes1, bboxes2, threshold: float = 0.5, round_to: int = DEFAULT_ROUND
149+
) -> np.ndarray:
146150
"""compute if each element from bboxes1 is almost a subregion of one or more elements in
147151
bboxes2"""
148-
coords1, coords2 = get_coords_from_bboxes(bboxes1), get_coords_from_bboxes(bboxes2)
152+
coords1 = get_coords_from_bboxes(bboxes1, round_to=round_to)
153+
coords2 = get_coords_from_bboxes(bboxes2, round_to=round_to)
149154

150-
inter_area, boxa_area, boxb_area = areas_of_boxes_and_intersection_area(coords1, coords2)
155+
inter_area, boxa_area, boxb_area = areas_of_boxes_and_intersection_area(
156+
coords1, coords2, round_to=round_to
157+
)
151158

152159
return (inter_area / np.maximum(boxa_area, EPSILON_AREA) > threshold) & (
153160
boxa_area <= boxb_area.T
154161
)
155162

156163

157-
def boxes_self_iou(bboxes, threshold: float = 0.5) -> np.ndarray:
164+
def boxes_self_iou(bboxes, threshold: float = 0.5, round_to: int = DEFAULT_ROUND) -> np.ndarray:
158165
"""compute iou for a group of elements"""
159-
coords = get_coords_from_bboxes(bboxes)
166+
coords = get_coords_from_bboxes(bboxes, round_to=round_to)
160167

161-
inter_area, boxa_area, boxb_area = areas_of_boxes_and_intersection_area(coords, coords)
168+
inter_area, boxa_area, boxb_area = areas_of_boxes_and_intersection_area(
169+
coords, coords, round_to=round_to
170+
)
162171

163172
return (inter_area / np.maximum(EPSILON_AREA, boxa_area + boxb_area.T - inter_area)) > threshold
164173

0 commit comments

Comments
 (0)