From 6994fa8458c3fb3e97706689910e7ad82a06b721 Mon Sep 17 00:00:00 2001 From: zzw <1464003642@qq.com> Date: Thu, 10 Sep 2026 10:32:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20bn=20=E7=BA=BF=E7=A8=8B=E5=85=B1?= =?UTF-8?q?=E4=BA=AB=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wanzheng.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/wanzheng.py b/wanzheng.py index efc632d..ae4d711 100644 --- a/wanzheng.py +++ b/wanzheng.py @@ -16,11 +16,15 @@ import argparse from concurrent.futures import ThreadPoolExecutor, as_completed from tqdm import tqdm import logging +import threading from typing import Dict, List, Optional, Tuple import requests from config.settings import API_HOST, API_CALLBACK +# YOLO 模型非线程安全(fuse/BN 会竞态报 AttributeError: bn),推理需串行 +_yolo_infer_lock = threading.Lock() + # ==================== 日志配置 ==================== def setup_logging(verbose: bool = False): level = logging.DEBUG if verbose else logging.INFO @@ -59,8 +63,9 @@ def cv2_imwrite_chinese(file_path: str, img: np.ndarray) -> bool: # ==================== YOLO 痤疮检测 ==================== def detect_acne(img: np.ndarray, image_path: str, model: YOLO, output_path: str) -> Tuple[bool, Dict, str]: try: - # 直接全图检测 - results = model(img, conf=0.2, verbose=False) + # 共享模型下串行推理,避免多线程 fuse/BN 竞态 + with _yolo_infer_lock: + results = model(img, conf=0.2, verbose=False) annotated_img = img.copy() class_counts = {} @@ -632,6 +637,14 @@ def main(): logger.info("加载 YOLO 痤疮模型...") try: model = YOLO(model_path) + # 主线程预 fuse,避免首批多线程推理时争抢 BN + try: + if hasattr(model, "fuse"): + model.fuse() + elif hasattr(model, "model") and hasattr(model.model, "fuse"): + model.model.fuse() + except Exception as fuse_err: + logger.warning(f"YOLO 预 fuse 跳过:{fuse_err}") logger.info("痤疮模型加载成功") except Exception as e: logger.error(f"痤疮模型加载失败:{e}")