diff --git a/actpred/dataset.py b/actpred/dataset.py index db3f25b..1911729 100644 --- a/actpred/dataset.py +++ b/actpred/dataset.py @@ -76,7 +76,9 @@ class ActionDataset(Dataset): raise ValueError( f"数据集为空(0 样本),无法计算归一化统计量。" f"请检查 data_dir={data_dir} 中的 episode 数据。") - all_actions = np.stack([s[2] for s in self.samples]) + all_actions = np.stack( + [np.concatenate([s[1], s[2][None]]) for s in self.samples] + ).reshape(-1, self.action_dim) self._action_mean = all_actions.mean(axis=0) self._action_std = all_actions.std(axis=0) + 1e-8 diff --git a/actpred/pretrain_mt.py b/actpred/pretrain_mt.py index 08446a0..eebce47 100644 --- a/actpred/pretrain_mt.py +++ b/actpred/pretrain_mt.py @@ -217,13 +217,23 @@ def main(): f"{cfg.data.action_stats_path}") stats = np.load(cfg.data.action_stats_path) else: - all_actions = np.stack([s[1] for s in train_ds.samples]) + # 统计量必须覆盖 history 与 target 并集:只按 targets 算时, + # targets 中恒定的列 std 落到 1e-8 地板值,而 history 窗口 + # 可能含该列的其他取值(如 .mt 脚本前几行),z-score 会爆炸 + # (实测 (90-45)/1e-8=4.5e9),后续 act_tokens ±1e9 直接 + # 打爆 DFC 量化(NegativeSlopeExponentNonFixable) + all_actions = np.stack( + [np.concatenate([s[0], s[1][None]]) for s in train_ds.samples] + ).reshape(-1, cfg.data.action_dim) stats = np.stack([all_actions.mean(axis=0), all_actions.std(axis=0) + 1e-8]) stats_path = os.path.join(cfg.train.checkpoint_dir, "action_stats.npy") np.save(stats_path, stats) - cfg.data.action_stats_path = stats_path + # 只存裸文件名:推理端按 checkpoint 目录解析相对路径 + # (predict.py/export_deploy.py/gen_calibration.py 同语义), + # 存 "./checkpoints/..." 会被二次拼接成 checkpoints/./checkpoints + cfg.data.action_stats_path = "action_stats.npy" print(f"action_stats 已保存: {stats_path}(训练集 targets 统计)") if stats.shape != (2, cfg.data.action_dim): raise ValueError( diff --git a/actpred/test_actpred.py b/actpred/test_actpred.py index 00e2664..994570b 100644 --- a/actpred/test_actpred.py +++ b/actpred/test_actpred.py @@ -261,6 +261,30 @@ class TestActionDataset(unittest.TestCase): self.assertIn("WARNING", buf.getvalue()) self.assertEqual(len(ds), 8 - 3) # 按较少者(8 帧图像)截断 + def test_stats_cover_history_values(self): + """归一化统计量必须覆盖 history 独有取值,否则 z-score 爆炸 + + col0 仅在 history(首行)出现 100,targets 全为 0:只按 targets + 算统计时 std 落到 1e-8 地板值,z=(100-0)/1e-8=1e10。 + """ + with tempfile.TemporaryDirectory() as tmpdir: + base = Path(tmpdir) + ep_dir = base / "episode_0001" + images_dir = ep_dir / "images" + images_dir.mkdir(parents=True, exist_ok=True) + for i in range(7): + Image.fromarray(np.random.randint(0, 255, (16, 16, 3), dtype=np.uint8) + ).save(images_dir / f"{i:04d}.jpg") + actions = np.zeros((7, 4), dtype=np.float32) + actions[0, 0] = 100.0 + np.save(ep_dir / "actions.npy", actions) + ds = ActionDataset(tmpdir, history_len=5, action_dim=4, + normalize_actions=True) + self.assertGreater(ds._action_std[0], 1.0) # 而非 1e-8 地板值 + z_max = (np.abs(actions[:, 0] - ds._action_mean[0]) + / ds._action_std[0]).max() + self.assertLess(z_max, 10.0) + class TestConfig(unittest.TestCase): """测试配置和 CLI 参数""" diff --git a/actpred/train.py b/actpred/train.py index b99956a..57fa1e3 100644 --- a/actpred/train.py +++ b/actpred/train.py @@ -89,7 +89,8 @@ def main(): stats_path = os.path.join(cfg.train.checkpoint_dir, "action_stats.npy") np.save(stats_path, train_ds.get_action_stats()) print(f"Action stats saved to: {stats_path}") - cfg.data.action_stats_path = stats_path + # 只存裸文件名:推理端按 checkpoint 目录解析相对路径 + cfg.data.action_stats_path = "action_stats.npy" val_ds = ActionDataset( cfg.data.data_dir, cfg.data.history_len, cfg.data.action_dim, cfg.data.image_width, cfg.data.image_height, cfg.data.normalize_actions,