fix(detect): 推理尺寸与训练尺寸不一致时报警

用 640 整图训练的权重跑 160 切片推理,结果看起来完全正常——照常出框、照常有置信度,
但类别系统性地错:实测 mosquito 的 TP 归零,760 张里 599 张被判成 blackdot,
而同一权重整图推理时 mosquito 有 92% 判对。这种错误不会自己暴露。

从 checkpoint 里读训练时的 imgsz,与当前切片尺寸不符就警告。三个调用点都接上:
可视化、批量推送预测、ML backend 加载权重时。visualize 的 summary.json 也补上
tile_size / tile_overlap,免得事后分不清某次产出是怎么跑的。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Shaun Zhou
2026-09-24 15:33:41 +08:00
co-authored by Claude Opus 5
parent 6e746b7351
commit ddd77653b5
5 changed files with 106 additions and 3 deletions
+4 -1
View File
@@ -32,7 +32,7 @@ from label_studio_ml.response import ModelResponse
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from engine.tiled_inference import load_image, predict_image # noqa: E402
from engine.tiled_inference import check_size_matches, load_image, predict_image # noqa: E402
logger = logging.getLogger(__name__)
@@ -63,6 +63,9 @@ class FilmDefectDetector(LabelStudioMLBase):
weights = os.environ.get("YOLO_WEIGHTS", "yolo26n.pt")
logger.info("加载权重 %s", weights)
FilmDefectDetector._model = YOLO(weights)
check_size_matches(
FilmDefectDetector._model, int(os.environ.get("YOLO_TILE_SIZE", 160)), logger
)
return FilmDefectDetector._model
def predict(self, tasks: list[dict], context: dict | None = None, **kwargs) -> ModelResponse: