Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

fix(layout): raise inline formula confidence threshold to 0.45

myhloli09b37cc7dfa8
diff --git a/mineru/model/layout/pp_doclayout_v2_base.py b/mineru/model/layout/pp_doclayout_v2_base.py
index 88d58a31..f72fa834 100644
--- a/mineru/model/layout/pp_doclayout_v2_base.py
+++ b/mineru/model/layout/pp_doclayout_v2_base.py
@@ -52,7 +52,7 @@ PP_DOCLAYOUT_V2_LABELS = [
PP_DOCLAYOUT_V2_LABEL_TO_ID = {label: index for index, label in enumerate(PP_DOCLAYOUT_V2_LABELS)}
-# Per-class confidence threshold used before reading-order decoding.
+# 模型分类阈值,仅将行内公式提高到 0.45,减少低置信度公式误检。
DEFAULT_CLASS_THRESHOLDS = [
0.5, # 0 abstract
0.5, # 1 algorithm
@@ -69,7 +69,7 @@ DEFAULT_CLASS_THRESHOLDS = [
0.5, # 12 header
0.5, # 13 header_image
0.5, # 14 image
- 0.4, # 15 inline_formula
+ 0.45, # 15 inline_formula
0.5, # 16 number
0.4, # 17 paragraph_title
0.5, # 18 reference
diff --git a/mineru/model/layout/pp_doclayout_v2_onnx.py b/mineru/model/layout/pp_doclayout_v2_onnx.py
index 17fc6f9f..b60d73e7 100644
--- a/mineru/model/layout/pp_doclayout_v2_onnx.py
+++ b/mineru/model/layout/pp_doclayout_v2_onnx.py
@@ -52,7 +52,7 @@ class PPDocLayoutV2LayoutModelONNX(PPDocLayoutV2PostProcessor):
intra_op_num_threads: int = 0,
config_path: str | None = None,
) -> None:
- """加载 CPU 检测图及官方预处理配置,检测筛选使用官方分类阈值。"""
+ """加载 CPU 检测图及官方预处理配置,仅将行内公式分类阈值提高到 0.45。"""
self.device = "cpu"
self.use_paddlex_filter_boxes = use_paddlex_filter_boxes
self.model_path = str(weight)
@@ -102,7 +102,7 @@ class PPDocLayoutV2LayoutModelONNX(PPDocLayoutV2PostProcessor):
@staticmethod
def _filter_by_class_thresholds(sample: np.ndarray) -> np.ndarray:
- """按最高分类分数确定候选有效性,再对其各个标签应用官方分类阈值。"""
+ """按最高分类分数确定候选有效性,再应用仅提高行内公式门槛的分类阈值。"""
if len(sample) == 0:
return sample
diff --git a/mineru/model/layout/pp_doclayoutv2.py b/mineru/model/layout/pp_doclayoutv2.py
index aa04d202..1dbb9140 100644
--- a/mineru/model/layout/pp_doclayoutv2.py
+++ b/mineru/model/layout/pp_doclayoutv2.py
@@ -72,6 +72,9 @@ class PPDocLayoutV2LayoutModel(PPDocLayoutV2PostProcessor):
)
self.rescale_factor = float(self.preprocess_config.get("rescale_factor", DEFAULT_RESCALE_FACTOR))
self.config = PPDocLayoutV2Config.from_pretrained(self.model_dir)
+ # 仅提高行内公式阈值,让模型阅读顺序与最终检测筛选使用同一门槛。
+ inline_formula_id = PP_DOCLAYOUT_V2_LABEL_TO_ID["inline_formula"]
+ self.config.class_thresholds[inline_formula_id] = DEFAULT_CLASS_THRESHOLDS[inline_formula_id]
# 保留原有 FP32 推理精度,直接加载到目标设备以减少 CPU 权重中转。
self.model = PPDocLayoutV2ForObjectDetection.from_pretrained(
self.model_dir,
diff --git a/tests/unittest/test_pp_doclayoutv2_thresholds.py b/tests/unittest/test_pp_doclayoutv2_thresholds.py
index 2bab049a..07a304fa 100644
--- a/tests/unittest/test_pp_doclayoutv2_thresholds.py
+++ b/tests/unittest/test_pp_doclayoutv2_thresholds.py
@@ -75,6 +75,21 @@ def test_class_threshold_boundaries(backend: str, cls_id: int) -> None:
np.testing.assert_allclose(result["scores"], [threshold, threshold + 0.001], rtol=0, atol=1e-7)
[email protected]("backend", ["torch", "onnx"])
+def test_inline_formula_uses_045_without_changing_other_class_thresholds(backend: str) -> None:
+ """行内公式低于 0.45 时拒绝,行间公式和正文仍保留 0.4 以上的候选。"""
+ probabilities = np.full((7, len(PP_DOCLAYOUT_V2_LABELS)), 0.001, dtype=np.float32)
+ for query, (cls_id, score) in enumerate(
+ [(15, 0.425), (15, 0.449), (15, 0.45), (15, 0.451), (5, 0.425), (22, 0.425), (0, 0.475)]
+ ):
+ probabilities[query, cls_id] = score
+
+ result = _predict(backend, probabilities)
+
+ assert result["labels"].tolist() == [15, 15, 5, 22]
+ np.testing.assert_allclose(result["scores"], [0.45, 0.451, 0.425, 0.425], rtol=0, atol=1e-7)
+
+
@pytest.mark.parametrize("backend", ["torch", "onnx"])
def test_invalid_primary_class_cannot_be_reintroduced_by_secondary_label(backend: str) -> None:
"""摘要主类别未达标时,即使次高正文类别达标也不能重新进入输出。"""
diff --git a/tests/unittest/test_transformers5_models.py b/tests/unittest/test_transformers5_models.py
index e743eca2..0d8e6e85 100644
--- a/tests/unittest/test_transformers5_models.py
+++ b/tests/unittest/test_transformers5_models.py
@@ -234,7 +234,7 @@ def test_layout_position_embedding_preserves_native_math(device: str) -> None:
@pytest.mark.parametrize("device", ["cpu", "cuda", "cuda:1", "mps", "xpu", "xpu:1"])
def test_layout_loader_adapts_position_embeddings_for_mps_and_xpu(monkeypatch: pytest.MonkeyPatch, device: str) -> None:
- """覆盖真实加载入口的设备分支,验证 XPU 编号设备也采用 CPU 正弦编码。"""
+ """覆盖设备分支,并验证加载时仅提高行内公式阈值,保留其他模型配置。"""
from types import SimpleNamespace
from unittest.mock import Mock
@@ -246,6 +246,10 @@ def test_layout_loader_adapts_position_embeddings_for_mps_and_xpu(monkeypatch: p
model = Mock()
model.model.encoder.aifi = [layer]
config = SimpleNamespace(class_thresholds=list(layout.DEFAULT_CLASS_THRESHOLDS))
+ config.class_thresholds[15] = 0.4
+ config.class_thresholds[22] = 0.7
+ expected_thresholds = list(config.class_thresholds)
+ expected_thresholds[15] = 0.45
load = Mock(return_value=model)
monkeypatch.setattr(layout, "load_preprocess_config", lambda _path: {})
monkeypatch.setattr(layout.PPDocLayoutV2Config, "from_pretrained", Mock(return_value=config))
@@ -253,7 +257,8 @@ def test_layout_loader_adapts_position_embeddings_for_mps_and_xpu(monkeypatch: p
loaded = layout.PPDocLayoutV2LayoutModel("local-checkpoint", device=device)
- assert loaded.config.class_thresholds == layout.DEFAULT_CLASS_THRESHOLDS
+ assert loaded.config.class_thresholds == expected_thresholds
+ assert load.call_args.kwargs["config"] is loaded.config
assert not hasattr(loaded, "conf")
assert load.call_args.kwargs["device_map"] == {"": device}
model.to.assert_called_once_with(device)