Download scripts/infer/render_third_party_scale.py from Tinghong-Ye22/code-HFD_lzk: direct link, hf CLI and curl.
- Browser
- Download file 8.99 kB
-
https://huggingface.co/Tinghong-Ye22/code-HFD_lzk/resolve/main/scripts/infer/render_third_party_scale.py
- Command line
-
hf download hf://Tinghong-Ye22/code-HFD_lzk/scripts/infer/render_third_party_scale.py
-
curl -L -o render_third_party_scale.py https://huggingface.co/Tinghong-Ye22/code-HFD_lzk/resolve/main/scripts/infer/render_third_party_scale.py
8.99 kB
| #!/usr/bin/env python3 | |
| """CPU-only final scale previews, with separate train and held-out indexes.""" | |
| from __future__ import annotations | |
| import argparse | |
| from concurrent.futures import ThreadPoolExecutor, as_completed | |
| import os | |
| from pathlib import Path | |
| import sys | |
| import time | |
| import wave | |
| PROJECT = Path(__file__).resolve().parents[2] | |
| sys.path[:0] = [str(PROJECT), str(PROJECT / "src")] | |
| from scripts.infer.render_third_party_review import ( | |
| ASSETS, LABELS, contained, digest, read, render_one, write) | |
| def selected_rows(selection, report, step): | |
| if (selection.get("scaleup") is not True or selection.get("split") not in ("train", "validation") | |
| or report.get("status") != "complete" or report.get("step") != step | |
| or report.get("evaluation_split") != selection["split"]): | |
| raise ValueError("Final prediction split/step differs from its frozen selection") | |
| by_id = {r["session_id"]: r for r in selection["sessions"]} | |
| samples = {r["session_id"]: r for r in report["samples"]} | |
| if len(samples) != report["count"] or len(samples) != len(report["samples"]): | |
| raise ValueError("Duplicate/incomplete final predictions") | |
| ids = selection["video_session_ids"] | |
| if not ids or len(ids) != len(set(ids)) or not set(ids) <= samples.keys() & by_id.keys(): | |
| raise ValueError("Missing predetermined video samples") | |
| rows = [] | |
| for index, sid in enumerate(ids, 1): | |
| row, chosen = samples[sid], by_id[sid] | |
| if (row["profile"] != chosen["session_type"] or row["split"] != selection["split"] | |
| or row["background_language"] != chosen["background_language"]): | |
| raise ValueError("Prediction labels differ from frozen selection") | |
| rows.append({**row, "number": f"{index:02d}", "background_high": chosen["background_high"]}) | |
| return rows | |
| def prepare(run, manifest): | |
| import numpy as np | |
| run = contained(run, PROJECT / "runs") | |
| result, identity = read(run / "result.json"), read(run / "scale_identity.json") | |
| if result["status"] != "complete": | |
| raise ValueError("Training/evaluation has not completed") | |
| rows, hashes = [], {} | |
| for split in ("train", "validation"): | |
| path = manifest if split == "train" else manifest.parent / "validation.json" | |
| key = "manifest_sha256" if split == "train" else "validation_sha256" | |
| if digest(path) != identity[key]: | |
| raise ValueError("Frozen video selection changed") | |
| selection = read(path) | |
| report_path = run / "evaluations/final" / split / "report.json" | |
| report = read(report_path); hashes[split] = digest(report_path) | |
| for row in selected_rows(selection, report, result["step"]): | |
| source = contained(row["prediction_directory"], report_path.parent / "samples") | |
| meta, recipe = read(source / "infer_meta.json"), read(source / "session_recipe.json") | |
| if (meta["session_id"] != row["session_id"] or meta["global_step"] != result["step"] | |
| or meta.get("evaluation_split") != split or meta["artifact_role"] != "model_prediction_not_GT" | |
| or recipe["session_id"] != row["session_id"] or recipe["split"] != split): | |
| raise ValueError("Prediction provenance mismatch") | |
| audio = contained(meta["frozen_audio_path"], Path(selection["production_root"]) / "audio" / split) | |
| if digest(audio) != meta["frozen_audio_sha256"] or digest(audio) != recipe["audio"]["sha256"]: | |
| raise ValueError("Original mixed audio changed") | |
| with wave.open(str(audio), "rb") as wav: | |
| if (wav.getframerate(), wav.getnchannels(), wav.getsampwidth()) != (16000, 1, 2): | |
| raise ValueError("Expected original mono 16kHz PCM16") | |
| seconds = wav.getnframes() / 16000 | |
| frames = round(seconds * 50) | |
| motion = np.load(source / "motion.npy", allow_pickle=False) | |
| if motion.shape != (frames, 36) or not np.isfinite(motion).all() or digest(source / "motion.npy") != row["motion_sha256"]: | |
| raise ValueError("Prediction trajectory changed or incomplete") | |
| for name, width in (("body_pos.csv", 3), ("body_quat.csv", 4), ("joint_pos.csv", 29)): | |
| values = np.loadtxt(source / name, delimiter=",", skiprows=1, ndmin=2) | |
| if values.shape != (frames, width) or not np.isfinite(values).all(): | |
| raise ValueError(f"Invalid predicted render CSV: {name}") | |
| rows.append({**row, "source_directory": str(source), "audio_path": str(audio), | |
| "audio_sha256": meta["frozen_audio_sha256"], "duration_seconds": seconds, | |
| "source_files": {name: digest(source / name) for name in ASSETS}}) | |
| return {"schema": 1, "run_dir": str(run), "step": result["step"], "count": len(rows), "samples": rows, | |
| "report_hashes": hashes, "gpu_used": False, "inference_repeated": False, | |
| "selection_policy": "predetermined one per available class/language/loudness cell, never by model score", | |
| "artifact_role": "model_prediction_not_GT", "render": {"workers": 12, "fps": 25, "size": [320, 240]}} | |
| def publish(output, plan, completed): | |
| done = {(r["split"], r["number"]) for r in completed} | |
| lines = ["# 全量训练最终预测视频(不是 GT)", "", | |
| f"新阶段 checkpoint {plan['step']};完成 {len(completed)}/{plan['count']}。", | |
| "复用完整预测;原混音不改变音量和时间。CPU 并行渲染,320×240、25fps;原预测仍为50Hz。", | |
| "样例在训练前按六类×中英文背景×普通/较高音量固定随机选定,不按效果挑选。", ""] | |
| for split, label in (("train", "训练集(拟合)"), ("validation", "独立验证集(未参与优化)")): | |
| lines += [f"## {label}", "", "| 视频序号 | 类型 | 背景语言 | 背景音量 | 控制准确率 | 背景误动率 |", | |
| "|---|---|---|---|---:|---:|"] | |
| for row in plan["samples"]: | |
| if row["split"] != split: | |
| continue | |
| number = row["number"] | |
| link = f"[{number}]({split}/{number}.mp4)" if (split, number) in done else f"{number} 待生成" | |
| metrics = row["metrics"]; bg = metrics.get("background_only_false_motion_rate") | |
| bg_text = "无评分区间" if bg is None else f"{bg:.1%}" | |
| lines.append(f"| {link} | {LABELS[row['profile']]} | {'中文' if row['background_language']=='zh' else '英文'} | " | |
| f"{'较高' if row['background_high'] else '普通'} | {metrics['control_accuracy']:.1%} | {bg_text} |") | |
| lines += ["", f"[本组原始逐条指标](../evaluations/final/{split}/report.json)", ""] | |
| lines += ["单条视频指标用于定位问题,不代替全部验证结果,也不代表机器人实际执行成功。", "", | |
| "[渲染状态](status.json) · [完整抽样溯源](plan.json)", ""] | |
| (output / "README.md").write_text("\n".join(lines)) | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument("--run-dir", type=Path, required=True) | |
| parser.add_argument("--manifest", type=Path, required=True) | |
| args = parser.parse_args() | |
| os.environ.update(CUDA_VISIBLE_DEVICES="", OMP_NUM_THREADS="1", MKL_NUM_THREADS="1", PYTHONDONTWRITEBYTECODE="1") | |
| from scripts.infer.third_party_checkpoint_probe import check_renderer | |
| plan = prepare(args.run_dir, args.manifest.resolve()) | |
| output = Path(plan["run_dir"]) / "prediction_videos" | |
| output.mkdir(exist_ok=False) | |
| write(output / "plan.json", plan) | |
| for split in ("train", "validation"): | |
| (output / split).mkdir() | |
| completed, failures = [], [] | |
| def status(phase): | |
| write(output / "status.json", {"phase": phase, "time": time.time(), "pid": os.getpid(), | |
| "completed": len(completed), "total": plan["count"], "failures": failures, | |
| "gpu_used": False, "inference_repeated": False}) | |
| publish(output, plan, completed) | |
| status("starting") | |
| try: | |
| check_renderer() | |
| with ThreadPoolExecutor(max_workers=12) as pool: | |
| jobs = {pool.submit(render_one, row, output / row["split"]): row for row in plan["samples"]} | |
| for future in as_completed(jobs): | |
| row = jobs[future] | |
| try: | |
| completed.append(future.result()) | |
| print(f"{row['split']}/{row['number']} completed", flush=True) | |
| except Exception as error: | |
| failures.append({"split": row["split"], "number": row["number"], "error": repr(error)}) | |
| status("rendering") | |
| if failures: | |
| raise RuntimeError("Some final videos failed; inspect status.json and per-sample render.log") | |
| status("completed") | |
| except Exception as error: | |
| failures.append({"error": repr(error)}); status("failed"); raise | |
| if __name__ == "__main__": | |
| main() | |