syCen commited on
Commit
d5aba64
·
verified ·
1 Parent(s): adfeb76

Create diagnose_hard_clips.py

Browse files
Files changed (1) hide show
  1. diagnose_hard_clips.py +96 -0
diagnose_hard_clips.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ diagnose_hard_clips.py
3
+
4
+ Run the force VAE over all clips, rank them by reconstruction loss, and report
5
+ which clips are the 'stubborn' high-loss ones + their force magnitude stats.
6
+ This tells you whether a few extreme clips are dragging the average, vs broad
7
+ failure.
8
+
9
+ Run:
10
+ python examples/wanvideo/model_training/diagnose_hard_clips.py \
11
+ --clips ... --stats ... --source_root ... \
12
+ --ckpt ./vae_ckpt/force_overfit/force_vae_ep39.pt \
13
+ --modality force --force_clip 5.0
14
+ """
15
+ import argparse, os, sys
16
+ sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
17
+ import numpy as np
18
+ import torch
19
+ from tqdm import tqdm
20
+ from diffsynth.models.physical_vae import ContactVAE, ForceVAE
21
+ from physical_dataset import PhysicalClipDataset
22
+
23
+
24
+ def main():
25
+ ap = argparse.ArgumentParser()
26
+ ap.add_argument("--clips", required=True)
27
+ ap.add_argument("--stats", required=True)
28
+ ap.add_argument("--source_root", required=True)
29
+ ap.add_argument("--ckpt", required=True)
30
+ ap.add_argument("--modality", choices=["contact","force"], default="force")
31
+ ap.add_argument("--force_clip", type=float, default=5.0)
32
+ ap.add_argument("--contact_gain", type=float, default=5.0)
33
+ ap.add_argument("--w_active", type=float, default=50.0)
34
+ ap.add_argument("--dtype", choices=["fp32","bf16"], default="bf16")
35
+ args = ap.parse_args()
36
+
37
+ device = "cuda"
38
+ dtype = torch.bfloat16 if args.dtype=="bf16" else torch.float32
39
+ ds = PhysicalClipDataset(args.clips, args.stats, args.source_root, args.modality,
40
+ force_clip=args.force_clip, contact_gain=args.contact_gain)
41
+ Cls = ContactVAE if args.modality=="contact" else ForceVAE
42
+ vae = Cls(); vae._adapt_channels()
43
+ vae = vae.to(device=device, dtype=dtype)
44
+ sd = torch.load(args.ckpt, map_location="cpu")
45
+ vae.model.load_state_dict(sd["model"]); vae.model.eval()
46
+
47
+ rows = []
48
+ for i in tqdm(range(len(ds)), desc="clips"):
49
+ item = ds[i]
50
+ x = item["data"].unsqueeze(0).to(device, dtype=dtype)
51
+ active = item["active_mask"].unsqueeze(0).to(device)
52
+ with torch.no_grad():
53
+ x_rec, _, _ = vae(x, sample=False)
54
+ xn = x[0].float().cpu().numpy()
55
+ rn = x_rec[0].float().cpu().numpy()
56
+ # weighted L1 (same as training, w_active)
57
+ a = active[0,0].cpu().numpy() # (T,H,W)
58
+ w = 1.0 + (args.w_active - 1.0) * a
59
+ err = np.abs(rn - xn).sum(0) # sum over channels -> (T,H,W)
60
+ wl1 = (w * err).sum() / w.sum()
61
+ # active-region L1
62
+ act = a > 0
63
+ act_l1 = err[act].mean() if act.any() else 0.0
64
+ # input magnitude stats
65
+ in_absmax = np.abs(xn).max()
66
+ in_active_frac = float(a.mean())
67
+ rows.append((i, wl1, act_l1, in_absmax, in_active_frac))
68
+
69
+ rows.sort(key=lambda r: -r[1]) # sort by weighted L1 desc
70
+ print("\n" + "="*72)
71
+ print(f"{'clip':>4s} {'w_L1':>8s} {'act_L1':>8s} {'in_absmax':>10s} {'act_frac':>9s}")
72
+ print("-"*72)
73
+ print("HARDEST 10:")
74
+ for r in rows[:10]:
75
+ print(f"{r[0]:4d} {r[1]:8.4f} {r[2]:8.4f} {r[3]:10.4f} {r[4]:9.4f}")
76
+ print("EASIEST 5:")
77
+ for r in rows[-5:]:
78
+ print(f"{r[0]:4d} {r[1]:8.4f} {r[2]:8.4f} {r[3]:10.4f} {r[4]:9.4f}")
79
+
80
+ wl1s = np.array([r[1] for r in rows])
81
+ print("="*72)
82
+ print(f"weighted L1: mean={wl1s.mean():.4f} median={np.median(wl1s):.4f} "
83
+ f"max={wl1s.max():.4f}")
84
+ print(f"clips with w_L1 > 1.0 (stubborn): {(wl1s>1.0).sum()}/{len(wl1s)}")
85
+ print(f"clips with w_L1 < 0.1 (learned): {(wl1s<0.1).sum()}/{len(wl1s)}")
86
+ # correlation between hardness and input magnitude
87
+ absmaxes = np.array([r[3] for r in rows])
88
+ if len(rows) > 5:
89
+ hard = wl1s > np.median(wl1s)
90
+ print(f"\nhard clips avg in_absmax: {absmaxes[hard].mean():.3f}")
91
+ print(f"easy clips avg in_absmax: {absmaxes[~hard].mean():.3f}")
92
+ print("(if hard >> easy, the stubborn clips are the extreme-force ones)")
93
+
94
+
95
+ if __name__ == "__main__":
96
+ main()