build: CPU/GPU 双环境、产物命名与打包修复
交付:
· 产物按变体命名:CPU -> dist/ocr-server/,GPU -> dist/ocr-server-gpu/。
变体由构建环境实际装的 paddle 自动判断,不需要人工声明
· GPU 包额外加 --collect-binaries nvidia。漏收的后果实测是最难查的那种:
引擎能正常加载、设备也识别成 GPU,但第一次推理才报
cudnn64_9.dll error 126
· --copy-metadata 的发行包名随变体变化(paddlepaddle / paddlepaddle-gpu),
改由 scripts/paddle_env.py 动态取;写死任何一个在另一种环境下都打不出包
· 新增 scripts/prepare_release_config.py 按变体覆盖发布配置(用 Python 改写
以保持 UTF-8,PowerShell 的 Set-Content 会把中文注释写坏)
环境:
· paddle 从 pyproject 依赖里移除,改用 scripts/install_paddle.py 安装。
它没法写死:CPU/GPU 互斥,GPU 还得按显卡选 CUDA 通道(RTX 50 系必须 cu129)。
钉死 CPU 版时,uv sync / uv run / PyCharm 自动同步都会把装好的
paddlepaddle-gpu 静默挤掉、服务悄悄退回 CPU(实测踩过三次)
· 新增 scripts/switch_env.py:.venv(CPU)与 .venv-gpu(GPU)两套环境共存,
切换零成本;所有 uv run 补 --no-sync,打包改用 uv sync --inexact
· 预置模型扩到 8 套(v4/v5 × 服务端/移动端 × 检测/识别,约 398MB)
其它:
· scripts/start.* 先找发布包再退回源码环境;入口脚本名固定是 ocr-server
· benchmark_ocr.py 输出 segments 与 chars,换设备/模型时可直接比对召回
· .gitignore 覆盖 .venv-gpu/ 与 dist*/
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
+50
-32
@@ -1,5 +1,18 @@
|
||||
"""Benchmark the configured OCR pipeline without modifying model files."""
|
||||
"""Benchmark the configured OCR pipeline without modifying model files.
|
||||
|
||||
用法:
|
||||
uv run --no-sync python scripts/benchmark_ocr.py # 默认样例图
|
||||
uv run --no-sync python scripts/benchmark_ocr.py 你的图.png # 指定图片
|
||||
uv run --no-sync python scripts/benchmark_ocr.py 你的图.png 3 # 跑 3 次,报最快那次
|
||||
|
||||
⚠️ `--no-sync` 不能省:uv run 默认会按 pyproject.toml 同步环境,那里声明的是 CPU 版
|
||||
paddlepaddle,会把装好的 paddlepaddle-gpu 静默冲掉、退回 CPU——测速结果会失真。
|
||||
|
||||
改完 config.yaml 重新执行即可对比。调模型或参数时**重点看 chars**:
|
||||
速度变快但字符数掉得多,就是丢字了。
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from time import perf_counter
|
||||
|
||||
@@ -7,40 +20,45 @@ from ocr_server.config import Settings
|
||||
from ocr_server.ocr import OCRService
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
image_path = Path("http/fixtures/general_ocr_002.png")
|
||||
settings = Settings()
|
||||
service = 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,
|
||||
)
|
||||
service.prepare()
|
||||
|
||||
stages: list[str] = []
|
||||
def run_once(service: OCRService, image_path: Path) -> tuple[float, int, int, float, float]:
|
||||
marks: list[tuple[int, float]] = []
|
||||
started = perf_counter()
|
||||
|
||||
def on_progress(percent: int, message: str) -> None:
|
||||
stages.append(f"{percent}% {message}")
|
||||
marks.append((percent, perf_counter() - started))
|
||||
|
||||
started = perf_counter()
|
||||
lines = service.recognize(image_path, on_progress=on_progress)
|
||||
elapsed_ms = (perf_counter() - started) * 1000
|
||||
elapsed = perf_counter() - started
|
||||
texts = [item["text"] for item in lines if item.get("text")]
|
||||
detection = next((t for p, t in marks if p == 50), 0.0)
|
||||
recognition = next((t for p, t in marks if p == 80), 0.0)
|
||||
return elapsed, len(texts), sum(len(text) for text in texts), detection, recognition
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
image_path = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(
|
||||
"http/fixtures/general_ocr_002.png"
|
||||
)
|
||||
repeats = int(sys.argv[2]) if len(sys.argv) > 2 else 1
|
||||
|
||||
settings = Settings()
|
||||
service = OCRService.from_settings(settings)
|
||||
service.prepare()
|
||||
|
||||
print(f"image={image_path}")
|
||||
print(f"cpu_threads={settings.cpu_threads_resolved} (config={settings.cpu_threads})")
|
||||
print(f"enable_mkldnn={settings.enable_mkldnn}")
|
||||
print(f"det={settings.det_model_name} rec={settings.rec_model_name}")
|
||||
print(f"elapsed_ms={elapsed_ms:.2f}")
|
||||
print(f"segments={len(lines)}")
|
||||
for stage in stages:
|
||||
print(f"stage={stage}")
|
||||
print(f"device={service._effective_device} (config={settings.device})")
|
||||
print(f"enable_mkldnn={settings.enable_mkldnn} cpu_threads={settings.cpu_threads_resolved}")
|
||||
print(f"text_det_limit_side_len={settings.text_det_limit_side_len} "
|
||||
f"text_recognition_batch_size={settings.text_recognition_batch_size}")
|
||||
|
||||
results = [run_once(service, image_path) for _ in range(repeats)]
|
||||
# 至少跑一次预热,避免把首次推理的算子编译时间算进去。
|
||||
if repeats == 1:
|
||||
print("(只跑一次,数值可能含首次推理的预热开销;建议传次数参数多跑几次)")
|
||||
elapsed, segments, chars, detection, recognition = min(results)
|
||||
print(f"elapsed_ms={elapsed * 1000:.2f}")
|
||||
print(f" detect_ms={detection * 1000:.2f}")
|
||||
print(f" recognize_ms={(recognition - detection) * 1000:.2f}")
|
||||
print(f"segments={segments}")
|
||||
print(f"chars={chars}")
|
||||
|
||||
Reference in New Issue
Block a user