修复:归档未训练项目的失效引用并保留类别编号

This commit is contained in:
tfarcraw 2026-11-09 22:09:59 +08:00
parent 8896cacec7
commit 7db5b5c0a5
2 changed files with 57 additions and 1 deletions

View File

@ -14,6 +14,39 @@ spec.loader.exec_module(worker)
class RecipeTrainingTests(unittest.TestCase):
def test_missing_untrained_project_archived_and_restored(self):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary).resolve()
current = root / "parts" / "current" / "project.json"
missing = root / "parts" / "missing" / "project.json"
current.parent.mkdir(parents=True)
data = {"name": "已训练", "recipeDirectory": str(root), "width": 100, "height": 100,
"frames": [{"index": 0, "image": "frame.jpg"}]}
worker.save_json(current, data)
registry = root / "recipe-training.json"
state = {"projects": [str(current), str(missing)], "classes": ["已训练", "未训练"],
"labels": {str(current): "已训练", str(missing): "未训练"}}
worker.save_json(registry, state)
# Without evidence from a previous model, missing data stays an error.
with self.assertRaisesRegex(FileNotFoundError, "请恢复"):
worker.recipe_context({"project": str(current)})
self.assertEqual(worker.read_json(registry), state)
worker.save_json(root / "latest-model.json", {"classes": ["已训练"]})
with patch.object(worker, "emit") as emit:
_, projects, classes = worker.recipe_context({"project": str(current)})
self.assertEqual([p for p, _ in projects], [current])
self.assertEqual(classes, state["classes"])
self.assertEqual(worker.read_json(registry)["missingProjects"], [str(missing)])
self.assertIn("警告", emit.call_args.args[0])
# Persisted retirement allows subsequent exports without renumbering.
worker.recipe_context({"project": str(current)})
missing.parent.mkdir(parents=True)
worker.save_json(missing, {**data, "name": "未训练"})
_, projects, classes = worker.recipe_context({"project": str(missing)})
self.assertEqual(len(projects), 2)
self.assertEqual(classes, state["classes"])
self.assertEqual(worker.read_json(registry)["missingProjects"], [])
def test_incremental_recipe(self):
with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary) / "装配配方"

View File

@ -262,8 +262,21 @@ def recipe_context(request):
current = str(path)
if current not in state["projects"]:
state["projects"].append(current)
projects = [load_project(item) for item in state["projects"]]
labels = state.setdefault("labels", {})
latest = root / "latest-model.json"
trained_classes = read_json(latest).get("classes", []) if latest.exists() else None
projects = []
missing = []
for item in state["projects"]:
if not Path(item).exists():
label = labels.get(item)
# Only retire a known, never-trained entry. Unknown or trained data
# must be restored, otherwise incremental training could forget it.
if trained_classes is None or not label or label in trained_classes:
raise FileNotFoundError(f"配方中的标注项目不存在:{item}。请恢复该目录及 project.json 后重试;为避免遗漏旧类别,训练已停止。")
missing.append(item)
continue
projects.append(load_project(item))
for source, project in projects:
previous = labels.get(str(source))
# A corrected name keeps its class ID when it is the sole video for that part.
@ -276,6 +289,16 @@ def recipe_context(request):
raise ValueError(f"零件项目的配方归属不一致:{source}")
if project["name"] not in state["classes"]:
state["classes"].append(project["name"])
if missing:
archived = state.setdefault("missingProjects", [])
for item in missing:
if item not in archived:
archived.append(item)
emit(f"警告:未进入已训练模型的标注项目已丢失,已移入缺失清单,本次不参与训练:{item}。类别编号保留;如需该视频,请恢复标注或重新导入。")
# Reopening a restored project reactivates it through the current request.
if "missingProjects" in state:
state["missingProjects"] = [item for item in state["missingProjects"] if item not in {str(p) for p, _ in projects}]
state["projects"] = [str(p) for p, _ in projects]
save_json(registry, state)
return root, projects, state["classes"]