This commit is contained in:
zimoyin
2026-10-12 11:03:59 +08:00
parent 5ddda545f5
commit cebe783839
7 changed files with 53 additions and 2 deletions
+28
View File
@@ -152,6 +152,33 @@ batch 8 + cpu_threads 4 : 约 114 秒
50% → 80% 文字识别 约 50 秒 ← 主要瓶颈
```
注意:同一配置在不同时间实测波动较大(同一图片分别测到 66 秒和 128 秒),比较参数时请在相同时间段多测几次,不要只看单次结果。
### 是否使用多核
不是单核推理。PaddleX 默认 `cpu_threads = 10`(可用 `cpu_threads` 配置项覆盖),20 逻辑核机器上的实测 CPU 占用:
```text
检测阶段 CPU 约 94%~105% (约 1 个核)
识别阶段 CPU 约 534%~686% (约 5.3~6.9 个核)
平均约 553%,即约 5.5 个核,20 核未跑满
```
结论:
- 识别阶段是多线程的,约占 5~7 个核;
- 检测阶段接近单核,这是关闭 oneDNN 后检测耗时约 13 秒的主要原因;
- 20 核没有跑满,说明并行效率不高,瓶颈在「未启用 oneDNN 的 CPU 算子」,而不是核心数不够。
线程数对耗时的影响(同图、`batch 8`):
```text
cpu_threads=4 约 114 秒
cpu_threads=10 约 60 秒
```
可调整 `cpu_threads` 实测,建议不超过物理核心数,并用 `benchmark_ocr.py` 对比。
关闭 oneDNN 是必须的:`enable_mkldnn=True` 会触发 PaddlePaddle 3.3.1 的缺陷并直接失败:
```text
@@ -509,6 +536,7 @@ log_level: INFO
| `text_det_unclip_ratio` | `1.5` | 检测框扩张比例 |
| `text_rec_score_thresh` | `0.0` | 识别结果阈值 |
| `text_recognition_batch_size` | `1` | 识别批量大小;内存足够时可提高 |
| `cpu_threads` | `10` | CPU 推理线程数(Paddle `cpu_math_library_num_threads`) |
| `log_level` | `INFO` | 日志级别:`DEBUG`、`INFO`、`WARNING`、`ERROR` |
接口文档:启动后访问 <http://127.0.0.1:8000/docs>。
+5
View File
@@ -34,6 +34,11 @@ text_rec_score_thresh: 0.0
# 识别批量大小
text_recognition_batch_size: 1
# CPU 推理线程数(Paddle 的 cpu_math_library_num_threads)。
# PaddleX 默认 10;不是单核推理,但关闭 oneDNN 后多线程加速有限。
# 建议不要超过物理核心数;调大不一定更快,请用 benchmark_ocr.py 实测。
cpu_threads: 10
# 日志级别:DEBUG / INFO / WARNING / ERROR
log_level: INFO
+12 -1
View File
@@ -23,11 +23,22 @@ if __name__ == "__main__":
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,
)
service.prepare()
stages: list[str] = []
def on_progress(percent: int, message: str) -> None:
stages.append(f"{percent}% {message}")
started = perf_counter()
lines = service.recognize(image_path)
lines = service.recognize(image_path, on_progress=on_progress)
elapsed_ms = (perf_counter() - started) * 1000
print(f"image={image_path}")
print(f"cpu_threads={settings.cpu_threads}")
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}")
+1
View File
@@ -19,5 +19,6 @@ if __name__ == "__main__":
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,
).prepare()
print(f"Models are ready in: {settings.model_dir.resolve()}")
+1
View File
@@ -28,6 +28,7 @@ def create_app(settings: Settings | None = None) -> FastAPI:
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,
)
tasks = OCRTaskQueue(create_ocr, worker_count=settings.ocr_workers)
+1
View File
@@ -37,6 +37,7 @@ class Settings(BaseModel):
text_det_unclip_ratio: float = Field(default=1.5, ge=0.1, le=5.0)
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 = Field(default=10, ge=1, le=64)
log_level: str = "INFO"
def __init__(self, **overrides: Any) -> None:
+5 -1
View File
@@ -41,6 +41,7 @@ class OCRService:
text_det_unclip_ratio: float = 1.5,
text_rec_score_thresh: float = 0.0,
text_recognition_batch_size: int = 1,
cpu_threads: int = 10,
) -> None:
self.lang = lang
self.device = device
@@ -54,6 +55,7 @@ class OCRService:
self._text_det_unclip_ratio = text_det_unclip_ratio
self._text_rec_score_thresh = text_rec_score_thresh
self._text_recognition_batch_size = text_recognition_batch_size
self._cpu_threads = cpu_threads
self._engine: Any | None = None
self._lock = threading.Lock()
self._inference_lock = threading.Lock()
@@ -130,10 +132,11 @@ class OCRService:
) from exc
with self._process_model_lock:
logger.info(
"加载 OCR 引擎: det=%s rec=%s device=%s model_dir=%s",
"加载 OCR 引擎: det=%s rec=%s device=%s cpu_threads=%d model_dir=%s",
self._det_model_name,
self._rec_model_name,
self.device,
self._cpu_threads,
self.model_dir,
)
self._engine = PaddleOCR(
@@ -150,6 +153,7 @@ class OCRService:
text_det_unclip_ratio=self._text_det_unclip_ratio,
text_rec_score_thresh=self._text_rec_score_thresh,
text_recognition_batch_size=self._text_recognition_batch_size,
cpu_threads=self._cpu_threads,
)
self._install_progress_hooks(self._engine)
logger.info("OCR 引擎加载完成")