Create compute_dataset_stats.py
Browse files- compute_dataset_stats.py +106 -0
compute_dataset_stats.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
compute_force_stats.py
|
| 3 |
+
Scan the full force dataset and compute per-dimension min / max / mean / std,
|
| 4 |
+
with mean/std computed ONLY over active pixels (union contact region),
|
| 5 |
+
and all-zero frames excluded from the frame count.
|
| 6 |
+
|
| 7 |
+
Usage:
|
| 8 |
+
python compute_force_stats.py \
|
| 9 |
+
--clips /net/.../vae_index/train_1500_0629.json \
|
| 10 |
+
--source_root /net/.../grasping \
|
| 11 |
+
--modality force \
|
| 12 |
+
--eps 1e-6
|
| 13 |
+
|
| 14 |
+
Streaming: keeps only per-dim (count, sum, sumsq, min, max) in float64,
|
| 15 |
+
so memory is O(6), not O(dataset). std = sqrt(E[x^2] - E[x]^2).
|
| 16 |
+
"""
|
| 17 |
+
import argparse, json, os, numpy as np
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def main():
|
| 21 |
+
ap = argparse.ArgumentParser()
|
| 22 |
+
ap.add_argument("--clips", required=True)
|
| 23 |
+
ap.add_argument("--source_root", required=True)
|
| 24 |
+
ap.add_argument("--modality", default="force")
|
| 25 |
+
ap.add_argument("--eps", type=float, default=1e-6,
|
| 26 |
+
help="a pixel is active if its L2 magnitude across channels > eps")
|
| 27 |
+
ap.add_argument("--max_frames", type=int, default=None,
|
| 28 |
+
help="cap for a quick sample; None = all")
|
| 29 |
+
args = ap.parse_args()
|
| 30 |
+
|
| 31 |
+
key = f"{args.modality}_paths"
|
| 32 |
+
clips = json.load(open(args.clips))["clips"]
|
| 33 |
+
|
| 34 |
+
# dedupe frames: same npy can appear in overlapping clips
|
| 35 |
+
paths = []
|
| 36 |
+
seen = set()
|
| 37 |
+
for c in clips:
|
| 38 |
+
for rel in c[key]:
|
| 39 |
+
full = os.path.join(args.source_root, c["episode"], rel)
|
| 40 |
+
if full not in seen:
|
| 41 |
+
seen.add(full); paths.append(full)
|
| 42 |
+
print(f"[scan] {len(paths)} unique {args.modality} frames from {len(clips)} clips")
|
| 43 |
+
|
| 44 |
+
C = None
|
| 45 |
+
cnt = sum_ = sumsq = vmin = vmax = None # per-dim accumulators (float64)
|
| 46 |
+
n_total = n_active = n_missing = 0
|
| 47 |
+
|
| 48 |
+
for i, p in enumerate(paths):
|
| 49 |
+
if args.max_frames and i >= args.max_frames:
|
| 50 |
+
break
|
| 51 |
+
if not os.path.exists(p):
|
| 52 |
+
n_missing += 1; continue
|
| 53 |
+
a = np.load(p).astype(np.float64) # (C,H,W)
|
| 54 |
+
if C is None:
|
| 55 |
+
C = a.shape[0]
|
| 56 |
+
cnt = np.zeros(C)
|
| 57 |
+
sum_ = np.zeros(C)
|
| 58 |
+
sumsq = np.zeros(C)
|
| 59 |
+
vmin = np.full(C, np.inf)
|
| 60 |
+
vmax = np.full(C, -np.inf)
|
| 61 |
+
|
| 62 |
+
n_total += 1
|
| 63 |
+
mag = np.sqrt((a ** 2).sum(0)) # (H,W)
|
| 64 |
+
active = mag > args.eps
|
| 65 |
+
# min/max over ALL pixels (background zeros never beat real extrema here,
|
| 66 |
+
# but include them so a channel that is all-zero this frame still reports 0)
|
| 67 |
+
vmin = np.minimum(vmin, a.reshape(C, -1).min(1))
|
| 68 |
+
vmax = np.maximum(vmax, a.reshape(C, -1).max(1))
|
| 69 |
+
if not active.any():
|
| 70 |
+
continue # all-zero frame: excluded from mean/std
|
| 71 |
+
n_active += 1
|
| 72 |
+
av = a[:, active] # (C, n_active_px) -- union active region
|
| 73 |
+
cnt += av.shape[1]
|
| 74 |
+
sum_ += av.sum(1)
|
| 75 |
+
sumsq += (av ** 2).sum(1)
|
| 76 |
+
|
| 77 |
+
if (i + 1) % 2000 == 0:
|
| 78 |
+
print(f" ...{i+1}/{len(paths)}")
|
| 79 |
+
|
| 80 |
+
mean = sum_ / np.clip(cnt, 1, None)
|
| 81 |
+
var = sumsq / np.clip(cnt, 1, None) - mean ** 2
|
| 82 |
+
std = np.sqrt(np.clip(var, 0, None))
|
| 83 |
+
|
| 84 |
+
print(f"\n[frames] total={n_total} active={n_active} "
|
| 85 |
+
f"all-zero={n_total-n_active} missing={n_missing}")
|
| 86 |
+
print(f"[active px per active frame] mean={cnt.sum()/max(n_active,1):.0f}\n")
|
| 87 |
+
print("per-dim stats (mean/std over union-active pixels; min/max over all pixels):")
|
| 88 |
+
for c in range(C):
|
| 89 |
+
print(f" dim{c}: min={vmin[c]:+.5f} max={vmax[c]:+.5f} "
|
| 90 |
+
f"mean={mean[c]:+.5f} std={std[c]:.5f}")
|
| 91 |
+
|
| 92 |
+
out = {"modality": args.modality, "eps": args.eps,
|
| 93 |
+
"n_frames_total": int(n_total), "n_frames_active": int(n_active),
|
| 94 |
+
"per_dim": {f"dim{c}": {"min": float(vmin[c]), "max": float(vmax[c]),
|
| 95 |
+
"mean": float(mean[c]), "std": float(std[c])}
|
| 96 |
+
for c in range(C)},
|
| 97 |
+
# convenience arrays for normalization code:
|
| 98 |
+
"mean": mean.tolist(), "std": std.tolist(),
|
| 99 |
+
"min": vmin.tolist(), "max": vmax.tolist()}
|
| 100 |
+
outpath = f"{args.modality}_stats_active.json"
|
| 101 |
+
json.dump(out, open(outpath, "w"), indent=2)
|
| 102 |
+
print(f"\nsaved -> {outpath}")
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
if __name__ == "__main__":
|
| 106 |
+
main()
|