feat: 外露推理调优参数并支持 device: auto
把此前只能改代码的参数摊到 config.yaml,交付后按客户机器调整: use_tensorrt / precision / enable_cinn / mkldnn_cache_capacity text_det_input_shape / text_rec_input_shape device 新增 auto:启动时探测可用的 CUDA 设备,有卡用 GPU、没卡用 CPU, 同一份配置在不同机器之间搬运不用改。探测不到卡是预期路径,报 INFO; 显式写 gpu 却用不了是配置问题,报 WARNING——便于区分这两种情况。 设备名校验加了两层(配置层拦拼写错误 + 运行时最后一道防线)。起因是实测 踩到:冻结包里的代码比 config.yaml 旧时,字符串 auto 被原样传给 PaddleX, 抛出的 AssertionError 完全看不出是配置写错了。 text_recognition_batch_size 默认值 8 -> 32:实测 1.png 同分辨率 23.87s -> 19.03s,框数不变、字符数 2240 -> 2241(差异仅英文分词空格与个别低置信字符)。 顺带把参数装配收敛到 OCRService.from_settings():启动、下载脚本、压测脚本 原本各自手工罗列参数,压测脚本已经漏掉了新配置项。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
+1
-16
@@ -22,22 +22,7 @@ def create_app(settings: Settings | None = None) -> FastAPI:
|
||||
field_separators = tuple(settings.extract_separators or (":", ":"))
|
||||
|
||||
def create_ocr() -> OCRService:
|
||||
return OCRService(
|
||||
lang=settings.lang,
|
||||
device=settings.device,
|
||||
model_dir=settings.model_dir,
|
||||
det_model_name=settings.det_model_name,
|
||||
rec_model_name=settings.rec_model_name,
|
||||
text_det_limit_side_len=settings.text_det_limit_side_len,
|
||||
text_det_limit_type=settings.text_det_limit_type,
|
||||
text_det_thresh=settings.text_det_thresh,
|
||||
text_det_box_thresh=settings.text_det_box_thresh,
|
||||
text_det_unclip_ratio=settings.text_det_unclip_ratio,
|
||||
text_rec_score_thresh=settings.text_rec_score_thresh,
|
||||
text_recognition_batch_size=settings.text_recognition_batch_size,
|
||||
cpu_threads=settings.cpu_threads_resolved,
|
||||
enable_mkldnn=settings.enable_mkldnn,
|
||||
)
|
||||
return OCRService.from_settings(settings)
|
||||
|
||||
tasks = OCRTaskQueue(create_ocr, worker_count=settings.ocr_workers)
|
||||
app = FastAPI(title="OCR Server", version="0.1.0")
|
||||
|
||||
@@ -12,6 +12,27 @@ PROJECT_ROOT = Path(__file__).resolve().parents[2]
|
||||
|
||||
CPU_THREADS_MAX = "max"
|
||||
|
||||
# 设备名形如 cpu / gpu / gpu:0 / npu。取值的合法集合来自 PaddleX 的
|
||||
# paddlex/utils/device.py:SUPPORTED_DEVICE_TYPE,外加本项目自有的 auto。
|
||||
# 在配置层就挡住拼错的值:否则它会一路传到 PaddleX 内部,
|
||||
# 抛出一个没有任何上下文的 AssertionError(实测踩过)。
|
||||
DEVICE_PATTERN = re.compile(r"^[a-z_]+(:\d+)?$")
|
||||
SUPPORTED_DEVICE_TYPES = (
|
||||
"cpu",
|
||||
"gpu",
|
||||
"xpu",
|
||||
"npu",
|
||||
"mlu",
|
||||
"gcu",
|
||||
"dcu",
|
||||
"iluvatar_gpu",
|
||||
"metax_gpu",
|
||||
)
|
||||
AUTO_DEVICE = "auto"
|
||||
|
||||
# 与 paddleocr._constants.SUPPORTED_PRECISION_LIST 保持一致。
|
||||
PRECISION_CHOICES = ("fp32", "fp16")
|
||||
|
||||
|
||||
def _runtime_root() -> Path:
|
||||
if getattr(sys, "frozen", False):
|
||||
@@ -88,22 +109,37 @@ class Settings(BaseModel):
|
||||
port: int = Field(default=8000, ge=1, le=65535)
|
||||
upload_dir: Path = _runtime_root() / "data" / "uploads"
|
||||
model_dir: Path = _runtime_root() / "models"
|
||||
# 下面这些类的默认值与 config.yaml 里的推荐配置保持一致,作为「配置文件缺失」时的兜底。
|
||||
det_model_name: str = "PP-OCRv5_server_det"
|
||||
rec_model_name: str = "PP-OCRv5_server_rec"
|
||||
rec_model_name: str = "PP-OCRv4_mobile_rec"
|
||||
ocr_workers: int = Field(default=1, ge=1, le=32)
|
||||
max_upload_mb: int = Field(default=10, ge=1, le=100)
|
||||
max_image_pixels: int = Field(default=20_000_000, ge=1)
|
||||
# 推理设备。cpu / gpu / gpu:0 / npu 等,具体可用性取决于 PaddlePaddle 编译版本。
|
||||
# `auto` 表示启动时探测可用的 CUDA 设备自行选择,适合同一份配置在有无显卡的
|
||||
# 机器之间搬运。注意:PyPI 上的 paddlepaddle 是纯 CPU 版,只有
|
||||
# paddlepaddle-gpu 才能跑 GPU。
|
||||
device: str = "cpu"
|
||||
lang: str = "ch"
|
||||
text_det_limit_side_len: int = Field(default=960, ge=32, le=4096)
|
||||
text_det_limit_side_len: int = Field(default=1024, ge=32, le=4096)
|
||||
text_det_limit_type: Literal["max", "min"] = "max"
|
||||
text_det_thresh: float = Field(default=0.3, ge=0.0, le=1.0)
|
||||
text_det_box_thresh: float = Field(default=0.6, ge=0.0, le=1.0)
|
||||
text_det_unclip_ratio: float = Field(default=1.5, ge=0.1, le=5.0)
|
||||
# 固定检测输入尺寸,形如 [3, 960, 960]。默认 null 表示用 text_det_limit_side_len 动态缩放。
|
||||
text_det_input_shape: list[int] | None = None
|
||||
text_rec_score_thresh: float = Field(default=0.0, ge=0.0, le=1.0)
|
||||
text_recognition_batch_size: int = Field(default=1, ge=1, le=64)
|
||||
cpu_threads: int | str = 10
|
||||
enable_mkldnn: bool = False
|
||||
# 固定识别输入尺寸,形如 [3, 48, 320]。默认 null 表示按文本框宽高比动态填充。
|
||||
text_rec_input_shape: list[int] | None = None
|
||||
text_recognition_batch_size: int = Field(default=32, ge=1, le=64)
|
||||
cpu_threads: int | str = 1
|
||||
enable_mkldnn: bool = True
|
||||
# oneDNN 算子缓存容量,仅在 enable_mkldnn 打开时生效。
|
||||
mkldnn_cache_capacity: int = Field(default=10, ge=1)
|
||||
# 以下三项只在 GPU 上生效;device 为 cpu 时会被忽略。
|
||||
use_tensorrt: bool = False
|
||||
precision: Literal["fp32", "fp16"] = "fp32"
|
||||
enable_cinn: bool = False
|
||||
extract_fields: list[FieldPreset] = Field(default_factory=list)
|
||||
extract_separators: list[str] = Field(default_factory=lambda: [":", ":"])
|
||||
extract_include_empty: bool = False
|
||||
@@ -130,6 +166,36 @@ class Settings(BaseModel):
|
||||
raise ValueError("cpu_threads 取值范围为 1~256")
|
||||
return value
|
||||
|
||||
@field_validator("device")
|
||||
@classmethod
|
||||
def _validate_device(cls, value: Any) -> str:
|
||||
name = str(value).strip().lower()
|
||||
if not DEVICE_PATTERN.match(name):
|
||||
raise ValueError(
|
||||
f"device 格式无效 {value!r},应为 cpu / gpu / gpu:0 / auto / npu 这类写法"
|
||||
)
|
||||
device_type = name.split(":", 1)[0]
|
||||
if device_type not in (*SUPPORTED_DEVICE_TYPES, AUTO_DEVICE):
|
||||
allowed = "、".join((*SUPPORTED_DEVICE_TYPES, AUTO_DEVICE))
|
||||
raise ValueError(
|
||||
f"device 取值无效 {value!r}。可用值:{allowed};"
|
||||
"也可以带编号,如 gpu:0 表示第 0 块卡。"
|
||||
)
|
||||
return name
|
||||
|
||||
@field_validator("text_det_input_shape", "text_rec_input_shape")
|
||||
@classmethod
|
||||
def _validate_input_shape(cls, value: Any) -> list[int] | None:
|
||||
if value is None:
|
||||
return None
|
||||
shape = list(value)
|
||||
if len(shape) != 3 or any(
|
||||
isinstance(item, bool) or not isinstance(item, int) or item < 1
|
||||
for item in shape
|
||||
):
|
||||
raise ValueError(f"输入尺寸必须是 3 个正整数,例如 [3, 48, 320],收到 {value!r}")
|
||||
return shape
|
||||
|
||||
@property
|
||||
def cpu_threads_resolved(self) -> int:
|
||||
"""实际传给 Paddle 的线程数;`max` 在这里展开成核心数。"""
|
||||
|
||||
+176
-3
@@ -1,4 +1,5 @@
|
||||
import importlib
|
||||
import importlib.util
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
@@ -7,7 +8,11 @@ import threading
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable
|
||||
|
||||
from ocr_server.config import resolve_cpu_threads
|
||||
from ocr_server.config import (
|
||||
AUTO_DEVICE,
|
||||
SUPPORTED_DEVICE_TYPES,
|
||||
resolve_cpu_threads,
|
||||
)
|
||||
|
||||
logger = logging.getLogger("ocr_server.ocr")
|
||||
|
||||
@@ -17,6 +22,24 @@ ProgressCallback = Callable[[int, str], None]
|
||||
# NotImplementedError(ConvertPirAttribute2RuntimeAttribute not support)。
|
||||
KNOWN_MKLDNN_BROKEN_PREFIXES = ("3.3.",)
|
||||
|
||||
# 交付包预置的模型。scripts/download_models.py 会把这几套全部下载到 model_dir,
|
||||
# 之后在客户机器上只改 config.yaml 的模型名即可离线切换,无需联网。
|
||||
# 覆盖 v4/v5 两个代际的服务端与移动端,共 4 检测 + 4 识别。
|
||||
# ⚠️ PP-OCRv4_mobile_det 在密集文本上会大面积漏检(1.png 只检出 46 框,v5 为 339 框),
|
||||
# 预置它是为了完整性,不要用在表格/文档类图片上。
|
||||
STAGED_DET_MODELS = (
|
||||
"PP-OCRv5_server_det",
|
||||
"PP-OCRv5_mobile_det",
|
||||
"PP-OCRv4_server_det",
|
||||
"PP-OCRv4_mobile_det",
|
||||
)
|
||||
STAGED_REC_MODELS = (
|
||||
"PP-OCRv5_server_rec",
|
||||
"PP-OCRv5_mobile_rec",
|
||||
"PP-OCRv4_server_rec",
|
||||
"PP-OCRv4_mobile_rec",
|
||||
)
|
||||
|
||||
|
||||
class OCRNotAvailable(RuntimeError):
|
||||
pass
|
||||
@@ -45,10 +68,16 @@ class OCRService:
|
||||
text_det_thresh: float = 0.3,
|
||||
text_det_box_thresh: float = 0.6,
|
||||
text_det_unclip_ratio: float = 1.5,
|
||||
text_det_input_shape: list[int] | None = None,
|
||||
text_rec_score_thresh: float = 0.0,
|
||||
text_rec_input_shape: list[int] | None = None,
|
||||
text_recognition_batch_size: int = 1,
|
||||
cpu_threads: int | str = 10,
|
||||
enable_mkldnn: bool = False,
|
||||
mkldnn_cache_capacity: int = 10,
|
||||
use_tensorrt: bool = False,
|
||||
precision: str = "fp32",
|
||||
enable_cinn: bool = False,
|
||||
) -> None:
|
||||
self.lang = lang
|
||||
self.device = device
|
||||
@@ -60,16 +89,55 @@ class OCRService:
|
||||
self._text_det_thresh = text_det_thresh
|
||||
self._text_det_box_thresh = text_det_box_thresh
|
||||
self._text_det_unclip_ratio = text_det_unclip_ratio
|
||||
self._text_det_input_shape = text_det_input_shape
|
||||
self._text_rec_score_thresh = text_rec_score_thresh
|
||||
self._text_rec_input_shape = text_rec_input_shape
|
||||
self._text_recognition_batch_size = text_recognition_batch_size
|
||||
self._cpu_threads = resolve_cpu_threads(cpu_threads)
|
||||
self._enable_mkldnn = bool(enable_mkldnn)
|
||||
self._mkldnn_cache_capacity = int(mkldnn_cache_capacity)
|
||||
self._use_tensorrt = bool(use_tensorrt)
|
||||
self._precision = precision
|
||||
self._enable_cinn = bool(enable_cinn)
|
||||
self._effective_device = self.device
|
||||
self._engine: Any | None = None
|
||||
self._lock = threading.Lock()
|
||||
self._inference_lock = threading.Lock()
|
||||
self._progress_callback: ProgressCallback | None = None
|
||||
self._progress_reached = 0
|
||||
|
||||
@classmethod
|
||||
def from_settings(cls, settings: Any, **overrides: Any) -> "OCRService":
|
||||
"""按 Settings 构造实例。
|
||||
|
||||
集中在这里,避免启动、下载脚本、压测脚本各自手工罗列参数时漏掉新配置项。
|
||||
`settings` 只做鸭子类型使用,不引入对 config 模块的硬依赖。
|
||||
"""
|
||||
arguments: dict[str, Any] = {
|
||||
"lang": settings.lang,
|
||||
"device": settings.device,
|
||||
"model_dir": settings.model_dir,
|
||||
"det_model_name": settings.det_model_name,
|
||||
"rec_model_name": settings.rec_model_name,
|
||||
"text_det_limit_side_len": settings.text_det_limit_side_len,
|
||||
"text_det_limit_type": settings.text_det_limit_type,
|
||||
"text_det_thresh": settings.text_det_thresh,
|
||||
"text_det_box_thresh": settings.text_det_box_thresh,
|
||||
"text_det_unclip_ratio": settings.text_det_unclip_ratio,
|
||||
"text_det_input_shape": settings.text_det_input_shape,
|
||||
"text_rec_score_thresh": settings.text_rec_score_thresh,
|
||||
"text_rec_input_shape": settings.text_rec_input_shape,
|
||||
"text_recognition_batch_size": settings.text_recognition_batch_size,
|
||||
"cpu_threads": settings.cpu_threads_resolved,
|
||||
"enable_mkldnn": settings.enable_mkldnn,
|
||||
"mkldnn_cache_capacity": settings.mkldnn_cache_capacity,
|
||||
"use_tensorrt": settings.use_tensorrt,
|
||||
"precision": settings.precision,
|
||||
"enable_cinn": settings.enable_cinn,
|
||||
}
|
||||
arguments.update(overrides)
|
||||
return cls(**arguments)
|
||||
|
||||
def _get_engine(self) -> Any:
|
||||
if self._engine is None:
|
||||
with self._lock:
|
||||
@@ -135,16 +203,26 @@ class OCRService:
|
||||
"DependencyError", (Exception,), {}
|
||||
)
|
||||
except ImportError as exc:
|
||||
# paddle 不在 pyproject 的依赖里(CPU/GPU 互斥、CUDA 通道要按机器选),
|
||||
# 所以「PaddleOCR 未安装」和「paddle 没装」要分开提示,
|
||||
# 否则 `uv sync` 之后报「请先安装项目依赖」会让人找不着北。
|
||||
if importlib.util.find_spec("paddle") is None:
|
||||
raise OCRNotAvailable(
|
||||
"未安装 paddlepaddle。它不在项目依赖里,需要按机器单独安装:"
|
||||
"python scripts/install_paddle.py(自动识别显卡)"
|
||||
",或指定 --cpu / --gpu。详见 README「CPU / GPU 切换」。"
|
||||
) from exc
|
||||
raise OCRNotAvailable(
|
||||
"PaddleOCR 未安装,请先安装项目依赖"
|
||||
) from exc
|
||||
with self._process_model_lock:
|
||||
self._resolve_device()
|
||||
logger.info(
|
||||
"加载 OCR 引擎: det=%s rec=%s device=%s cpu_threads=%d "
|
||||
"enable_mkldnn=%s model_dir=%s",
|
||||
self._det_model_name,
|
||||
self._rec_model_name,
|
||||
self.device,
|
||||
self._effective_device,
|
||||
self._cpu_threads,
|
||||
self._enable_mkldnn,
|
||||
self.model_dir,
|
||||
@@ -152,11 +230,12 @@ class OCRService:
|
||||
self._warn_if_mkldnn_unsupported()
|
||||
self._engine = PaddleOCR(
|
||||
**self._model_arguments(),
|
||||
device=self.device,
|
||||
**self._device_arguments(),
|
||||
use_doc_orientation_classify=False,
|
||||
use_doc_unwarping=False,
|
||||
use_textline_orientation=False,
|
||||
enable_mkldnn=self._enable_mkldnn,
|
||||
mkldnn_cache_capacity=self._mkldnn_cache_capacity,
|
||||
text_det_limit_side_len=self._text_det_limit_side_len,
|
||||
text_det_limit_type=self._text_det_limit_type,
|
||||
text_det_thresh=self._text_det_thresh,
|
||||
@@ -206,6 +285,100 @@ class OCRService:
|
||||
arguments[argument_name] = str(local_path)
|
||||
return arguments
|
||||
|
||||
def _device_arguments(self) -> dict[str, Any]:
|
||||
"""组装设备相关参数。需先调用 _resolve_device()。"""
|
||||
arguments: dict[str, Any] = {
|
||||
"device": self._effective_device,
|
||||
"use_tensorrt": self._use_tensorrt,
|
||||
"precision": self._precision,
|
||||
"enable_cinn": self._enable_cinn,
|
||||
}
|
||||
# None 就是这两项的默认值,不传更稳妥:PaddleOCR 只在非空时才下发到 pipeline。
|
||||
if self._text_det_input_shape is not None:
|
||||
arguments["text_det_input_shape"] = list(self._text_det_input_shape)
|
||||
if self._text_rec_input_shape is not None:
|
||||
arguments["text_rec_input_shape"] = list(self._text_rec_input_shape)
|
||||
return arguments
|
||||
|
||||
@staticmethod
|
||||
def _gpu_unavailable_reason() -> str | None:
|
||||
"""GPU 不可用时返回原因描述,可用时返回 None。
|
||||
|
||||
只查 is_compiled_with_cuda() 不够:那是编译期属性,装了 paddlepaddle-gpu
|
||||
但机器没有 NVIDIA 驱动时会返回 True,直到构造引擎才报错。所以还要探一次
|
||||
真实可见的设备数。
|
||||
"""
|
||||
try:
|
||||
import paddle
|
||||
except ImportError:
|
||||
return "paddlepaddle 未安装"
|
||||
version = getattr(paddle, "__version__", "unknown")
|
||||
if not paddle.device.is_compiled_with_cuda():
|
||||
return f"当前 PaddlePaddle {version} 是纯 CPU 版本"
|
||||
try:
|
||||
count = int(paddle.device.cuda.device_count())
|
||||
except Exception as exc: # CUDA 初始化失败的原因很杂,统一兜住
|
||||
return f"CUDA 初始化失败({exc})"
|
||||
if count < 1:
|
||||
return "PaddlePaddle 支持 CUDA,但系统里看不到可用的 NVIDIA 设备或驱动"
|
||||
return None
|
||||
|
||||
def _resolve_device(self) -> None:
|
||||
"""确定真正可用的设备,避免配置了 GPU 但环境不支持时服务直接起不来。
|
||||
|
||||
`device: auto` 会探测可用的 CUDA 设备自行选择,适合同一份配置在
|
||||
「有卡」和「没卡」的机器之间来回搬的场景。探测失败时的日志是 INFO,
|
||||
因为那是预期行为;而显式写了 `device: gpu` 却用不了是配置问题,报 WARNING。
|
||||
"""
|
||||
device = self.device
|
||||
if device == AUTO_DEVICE:
|
||||
reason = self._gpu_unavailable_reason()
|
||||
if reason is None:
|
||||
device = "gpu"
|
||||
logger.info("device=auto:检测到可用的 CUDA 设备,使用 GPU")
|
||||
else:
|
||||
device = "cpu"
|
||||
logger.info("device=auto:没有可用的 GPU(%s),使用 CPU", reason)
|
||||
elif device.startswith("gpu"):
|
||||
reason = self._gpu_unavailable_reason()
|
||||
if reason:
|
||||
logger.warning(
|
||||
"配置 device=%s,但 GPU 无法启用:%s。"
|
||||
"需安装与本机 CUDA 匹配的 paddlepaddle-gpu(版本同样要固定 3.2.x,"
|
||||
"详见 README「CPU / GPU 切换」)。本次自动回落 CPU 运行。"
|
||||
"如果这台机器本来就不一定有卡,可以改用 device: auto。",
|
||||
device,
|
||||
reason,
|
||||
)
|
||||
device = "cpu"
|
||||
self._effective_device = self._finalize_device(device)
|
||||
if self._use_tensorrt and not self._effective_device.startswith("gpu"):
|
||||
logger.warning(
|
||||
"use_tensorrt 已开启,但生效设备是 %s;"
|
||||
"TensorRT 只在 GPU 上生效,本次忽略该项。",
|
||||
self._effective_device,
|
||||
)
|
||||
self._use_tensorrt = False
|
||||
|
||||
@staticmethod
|
||||
def _finalize_device(device: str) -> str:
|
||||
"""最后一道防线:绝不把 PaddleX 不认识的值传下去。
|
||||
|
||||
配置层已经校验过一次,但**冻结包里的代码可能比 config.yaml 旧**——实测踩过:
|
||||
旧 exe 配上新配置里的 `device: auto`,字符串原样传给 PaddleX,最后在
|
||||
`paddlex/utils/device.py` 里抛 AssertionError,日志里看不出是配置写错了。
|
||||
这里兜住并给出可读的报错。
|
||||
"""
|
||||
if device.split(":", 1)[0] in SUPPORTED_DEVICE_TYPES:
|
||||
return device
|
||||
logger.error(
|
||||
"device=%s 不是 PaddleX 支持的设备类型(可用:%s),本次回落 CPU 运行。"
|
||||
"请检查 config.yaml 的 device 取值。",
|
||||
device,
|
||||
"、".join(SUPPORTED_DEVICE_TYPES),
|
||||
)
|
||||
return "cpu"
|
||||
|
||||
@classmethod
|
||||
def _is_complete_model(cls, model_path: Path) -> bool:
|
||||
return model_path.is_dir() and all(
|
||||
|
||||
Reference in New Issue
Block a user