Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.
Python ai4sciencedocument-analysisdocxextract-datalayout-analysisocrparserpdfpdf-converterpdf-extractor-llmpdf-extractor-pretrainpdf-extractor-ragpdf-parserpptxpythonxlsx
feat: add precision override for OCR inference and adjust device handling
myhloli90dff0408dee
diff --git a/mineru/model/ocr/pytorch_paddle.py b/mineru/model/ocr/pytorch_paddle.pyindex 089a55d3..7a415854 100644--- a/mineru/model/ocr/pytorch_paddle.py+++ b/mineru/model/ocr/pytorch_paddle.py@@ -78,6 +78,8 @@ class PytorchPaddleOCR(TextSystem):kwargs['rec_char_dict_path'] = os.path.join(root_dir, 'pytorchocr', 'utils', 'resources', 'dict', dict_file)kwargs['rec_batch_num'] = 6if self.is_seal:+ # Seal detector 的 BatchNorm 统计量会在 fp16 下溢出,初始化时固定使用 fp32。+ kwargs['det_inference_precision'] = 'fp32'kwargs['det_limit_side_len'] = 736kwargs['det_limit_type'] = 'min'kwargs['det_max_side_limit'] = 4000diff --git a/mineru/model/utils/pytorchocr/base_ocr_v20.py b/mineru/model/utils/pytorchocr/base_ocr_v20.pyindex 96af9ef6..1b2b672d 100644--- a/mineru/model/utils/pytorchocr/base_ocr_v20.py+++ b/mineru/model/utils/pytorchocr/base_ocr_v20.py@@ -22,9 +22,13 @@ class BaseOCRV20:def build_net(self, **kwargs):self.net = BaseModel(self.config, **kwargs)- def _resolve_inference_dtype(self, device):- """根据常量和设备类型解析 OCR 网络推理使用的浮点精度。"""- precision = OCR_INFERENCE_PRECISION.lower()+ def _resolve_inference_dtype(self, device, precision_override=None):+ """根据实例级覆盖、全局常量和设备类型解析 OCR 网络推理精度。"""+ precision = (+ OCR_INFERENCE_PRECISION+ if precision_override is None+ else precision_override+ ).lower()device_name = str(device).lower()is_cpu = device_name.startswith("cpu")@@ -36,10 +40,13 @@ class BaseOCRV20:return torch.float32return torch.float16- def _apply_inference_precision(self, device):- """将 OCR 网络移动到目标设备,并在非 CPU 半精度场景下切到 fp16。"""+ def _apply_inference_precision(self, device, precision_override=None):+ """将 OCR 网络移动到目标设备,并按实例级覆盖或默认规则设置精度。"""self.net.to(device)- self.ocr_inference_dtype = self._resolve_inference_dtype(device)+ self.ocr_inference_dtype = self._resolve_inference_dtype(+ device,+ precision_override=precision_override,+ )if self.ocr_inference_dtype == torch.float16:self.net.to(dtype=torch.float16)diff --git a/mineru/model/utils/tools/infer/predict_det.py b/mineru/model/utils/tools/infer/predict_det.pyindex c638b38d..8c6ab03c 100644--- a/mineru/model/utils/tools/infer/predict_det.py+++ b/mineru/model/utils/tools/infer/predict_det.py@@ -121,7 +121,10 @@ class TextDetector(BaseOCRV20):super(TextDetector, self).__init__(network_config, **kwargs)self.load_pytorch_weights(self.weights_path)self.net.eval()- self._apply_inference_precision(self.device)+ self._apply_inference_precision(+ self.device,+ precision_override=getattr(args, "det_inference_precision", None),+ )for module in self.net.modules():if hasattr(module, 'rep'):module.rep()