Datasets:
Upload folder using huggingface_hub
Browse files- harness/README.md +9 -0
- harness/world_codec.py +174 -0
harness/README.md
CHANGED
|
@@ -64,3 +64,12 @@ trail (including the snow-on-clouds failure and its fix).
|
|
| 64 |
(ort-web WASM has no ConvInteger). 16.5 MB -> 4.8 MB. A/B on the recall
|
| 65 |
harness: circles 28/34 -> 31/34; coverage 0.92 -> 0.80 (remainder
|
| 66 |
partition absorbs it); tokens 3-4x (tighter object-scale masks).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
(ort-web WASM has no ConvInteger). 16.5 MB -> 4.8 MB. A/B on the recall
|
| 65 |
harness: circles 28/34 -> 31/34; coverage 0.92 -> 0.80 (remainder
|
| 66 |
partition absorbs it); tokens 3-4x (tighter object-scale masks).
|
| 67 |
+
|
| 68 |
+
## 5. Play it back (codec world tour)
|
| 69 |
+
- `world_codec.py` — builds codec tracks straight from the shards (teacher
|
| 70 |
+
mask -> EFD polygon, multispec -> 7-channel fill, exact-date clock), hides
|
| 71 |
+
every third observation with CLOUD tokens, imputes with codec v3 (trained
|
| 72 |
+
on 3 sites / 3 years, applied zero-shot), renders GT-vs-decode strips.
|
| 73 |
+
Zero-shot verdict: beats persistence on stable farmland (IoU 0.93 vs 0.86
|
| 74 |
+
over a 41-year track), loses on unseen event dynamics — the retrain on this
|
| 75 |
+
corpus is the point.
|
harness/world_codec.py
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""End-to-end codec demo on the WORLD corpus: one exemplar track per concept.
|
| 2 |
+
|
| 3 |
+
For each exemplar: build the codec track from its shard (polygon from the
|
| 4 |
+
teacher mask, 7-channel spectral fill from the raw multispec, exact-date
|
| 5 |
+
clock), hide a third of its observations with CLOUD mask tokens, let codec v3
|
| 6 |
+
impute them, and render ground truth vs decode side by side.
|
| 7 |
+
|
| 8 |
+
Writes renders/world_codec_<tag>.png + world_codec_report.json and the
|
| 9 |
+
Space asset spaces/hrm-space/assets/worldtour/*.png.
|
| 10 |
+
|
| 11 |
+
Usage: python world_codec.py (needs shards in experiments/recall/)
|
| 12 |
+
"""
|
| 13 |
+
import json, os, pathlib, sys
|
| 14 |
+
import numpy as np
|
| 15 |
+
import cv2
|
| 16 |
+
import torch
|
| 17 |
+
|
| 18 |
+
HERE = pathlib.Path(__file__).parent
|
| 19 |
+
sys.path.insert(0, str(HERE))
|
| 20 |
+
os.environ["PLAYBACK_ONLY"] = "1"
|
| 21 |
+
import codec_v3 as C3 # loads codec_v3.pt, no training
|
| 22 |
+
from codec_mlm import encode_shape, decode_shape, iou_poly
|
| 23 |
+
from codec_time import timefeat, F_MU, F_SD, L_MU, L_SD, NV, CC
|
| 24 |
+
|
| 25 |
+
SH = HERE.parent / "experiments/recall"
|
| 26 |
+
from huggingface_hub import hf_hub_download
|
| 27 |
+
|
| 28 |
+
EXEMPLARS = [ # (shard tag, concept substring, display name)
|
| 29 |
+
("kansas", "center pivot", "center-pivot field · Kansas"),
|
| 30 |
+
("guayas", "aquaculture pond", "shrimp pond · Guayas"),
|
| 31 |
+
("parkfire_ev", "burned", "burn scar · Park Fire 2024"),
|
| 32 |
+
("indus_ev", "flood", "flooded farmland · Indus 2010/22"),
|
| 33 |
+
("atacama", "mine", "open-pit mine · Atacama"),
|
| 34 |
+
("oregon", "clearcut", "clearcut · Oregon"),
|
| 35 |
+
("aralsea", "lake", "shrinking lake · Aral Sea"),
|
| 36 |
+
]
|
| 37 |
+
|
| 38 |
+
def months_since(datestr):
|
| 39 |
+
y, mo = int(str(datestr)[:4]), int(str(datestr)[5:7])
|
| 40 |
+
return (y - 2023) * 12 + (mo - 1)
|
| 41 |
+
|
| 42 |
+
def fill7(px): # px: (N,4) r,g,b,nir reflectance
|
| 43 |
+
r, g, b, nir = px[:, 0].mean(), px[:, 1].mean(), px[:, 2].mean(), px[:, 3].mean()
|
| 44 |
+
s = r + g + b + 1e-6
|
| 45 |
+
return np.array([r, g, b, (nir - r) / (nir + r + 1e-6),
|
| 46 |
+
(2 * g - r - b) / s, 1 - np.clip(s / 3 / 0.12, 0, 1), s / 3],
|
| 47 |
+
np.float32)
|
| 48 |
+
|
| 49 |
+
def build_track(tag, concept_sub):
|
| 50 |
+
sh = np.load(SH / f"shard_{tag}.npz")
|
| 51 |
+
lf = hf_hub_download("cp500/hyperkd-rs", f"labels/labels_{tag}.npz", repo_type="dataset")
|
| 52 |
+
lb = np.load(lf)
|
| 53 |
+
names = lb["concepts"]; sl = lb["sample_labels"]
|
| 54 |
+
samples, masks, images = sh["samples"], sh["masks"], sh["images"]
|
| 55 |
+
dates = [str(d) for d in sh["dates"]]
|
| 56 |
+
ms = sh["multispec"].astype(np.float32)
|
| 57 |
+
want = {int(r[0]) for r in sl if concept_sub in str(names[int(r[1])])}
|
| 58 |
+
# longest track containing a wanted sample
|
| 59 |
+
by_track = {}
|
| 60 |
+
for i, r in enumerate(samples):
|
| 61 |
+
by_track.setdefault(int(r[7]), []).append(i)
|
| 62 |
+
cands = [(len(v), t) for t, v in by_track.items() if any(i in want for i in v)]
|
| 63 |
+
if not cands: return None
|
| 64 |
+
cands.sort(reverse=True)
|
| 65 |
+
_, tid = cands[0]
|
| 66 |
+
steps = []
|
| 67 |
+
for i in sorted(by_track[tid], key=lambda i: int(samples[i][0])):
|
| 68 |
+
r = samples[i]; si = int(r[0]); mi = int(r[3 + int(r[6])])
|
| 69 |
+
if mi < 0: continue
|
| 70 |
+
m8 = (masks[mi] > 0).astype(np.uint8)
|
| 71 |
+
cs, _ = cv2.findContours(m8, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 72 |
+
if not cs: continue
|
| 73 |
+
cnt = max(cs, key=cv2.contourArea)
|
| 74 |
+
if cv2.contourArea(cnt) < 20 or len(cnt) < 8: continue
|
| 75 |
+
P = cnt[:, 0, :].astype(float) * (800 / 256)
|
| 76 |
+
try: vec, pose = encode_shape(P)
|
| 77 |
+
except Exception: continue
|
| 78 |
+
m160 = cv2.resize(m8, (160, 160), interpolation=cv2.INTER_NEAREST) > 0
|
| 79 |
+
px = ms[si][m160]
|
| 80 |
+
if len(px) < 4: continue
|
| 81 |
+
steps.append(dict(vec=vec, pose=pose, logs=pose[1], fill=fill7(px),
|
| 82 |
+
tm=months_since(dates[si]), period=dates[si][:7],
|
| 83 |
+
si=si, m8=m8))
|
| 84 |
+
if len(steps) < 5: return None
|
| 85 |
+
s = dict(tag=tag, steps=steps)
|
| 86 |
+
s["X"] = np.stack([np.concatenate([st["vec"], [(st["logs"] - L_MU) / L_SD],
|
| 87 |
+
(st["fill"] - F_MU) / F_SD]).astype(np.float32)
|
| 88 |
+
for st in steps])
|
| 89 |
+
s["T"] = np.stack([timefeat(st["tm"]) for st in steps])
|
| 90 |
+
return s
|
| 91 |
+
|
| 92 |
+
def stretch_rgb(fill):
|
| 93 |
+
return tuple(int(np.clip((max(float(v), 0) / 0.35) ** (1 / 2.2), 0, 1) * 255)
|
| 94 |
+
for v in fill[:3][::-1]) # BGR for cv2
|
| 95 |
+
|
| 96 |
+
OUTD = HERE / "renders"; OUTD.mkdir(exist_ok=True)
|
| 97 |
+
SPACED = HERE.parent / "spaces/hrm-space/assets/worldtour"; SPACED.mkdir(parents=True, exist_ok=True)
|
| 98 |
+
report = []
|
| 99 |
+
TILE = 128
|
| 100 |
+
for tag, sub, title in EXEMPLARS:
|
| 101 |
+
s = build_track(tag, sub)
|
| 102 |
+
if s is None:
|
| 103 |
+
print(f"{tag}: no track for '{sub}'"); continue
|
| 104 |
+
T = len(s["steps"])
|
| 105 |
+
hide = list(range(1, T, 3)) # hide every 3rd obs (CLOUD)
|
| 106 |
+
pred = C3.impute(s, hide) # rows for hidden idxs
|
| 107 |
+
# metrics vs persistence
|
| 108 |
+
ious, pious, fmae, pfmae = [], [], [], []
|
| 109 |
+
sh_img = np.load(SH / f"shard_{tag}.npz")["images"]
|
| 110 |
+
for k, t in enumerate(hide):
|
| 111 |
+
st, ref = s["steps"][t], s["steps"][t - 1]
|
| 112 |
+
e = pred[k]
|
| 113 |
+
vec = e[:NV]; logs = e[NV] * L_SD + L_MU; fill = e[NV + 1:] * F_SD + F_MU
|
| 114 |
+
pose = (st["pose"][0], logs, ref["pose"][2], ref["pose"][3])
|
| 115 |
+
ious.append(iou_poly(decode_shape(vec, pose), decode_shape(st["vec"], st["pose"])))
|
| 116 |
+
pious.append(iou_poly(decode_shape(ref["vec"], ref["pose"]),
|
| 117 |
+
decode_shape(st["vec"], st["pose"])))
|
| 118 |
+
fmae.append(np.abs(fill - st["fill"]).mean())
|
| 119 |
+
pfmae.append(np.abs(ref["fill"] - st["fill"]).mean())
|
| 120 |
+
s["steps"][t]["dec"] = (vec, pose, fill)
|
| 121 |
+
# render strip: up to 10 columns, GT on top, decode below
|
| 122 |
+
cols = [t for t in range(T)][:: max(1, T // 10)][:10]
|
| 123 |
+
tiles_top, tiles_bot = [], []
|
| 124 |
+
# common crop around the track's union bbox (256 grid)
|
| 125 |
+
u = np.zeros((256, 256), np.uint8)
|
| 126 |
+
for st in s["steps"]: u |= st["m8"]
|
| 127 |
+
ys, xs = np.where(u > 0)
|
| 128 |
+
cx, cy = int(xs.mean()), int(ys.mean())
|
| 129 |
+
r = max(int(max(xs.max()-xs.min(), ys.max()-ys.min()) * 0.8), 40)
|
| 130 |
+
x0, y0 = np.clip(cx - r, 0, 255), np.clip(cy - r, 0, 255)
|
| 131 |
+
x1, y1 = np.clip(cx + r, 1, 256), np.clip(cy + r, 1, 256)
|
| 132 |
+
for t in cols:
|
| 133 |
+
st = s["steps"][t]
|
| 134 |
+
img = sh_img[st["si"]][y0:y1, x0:x1].copy()
|
| 135 |
+
cs, _ = cv2.findContours(st["m8"][y0:y1, x0:x1], cv2.RETR_EXTERNAL,
|
| 136 |
+
cv2.CHAIN_APPROX_SIMPLE)
|
| 137 |
+
cv2.drawContours(img, cs, -1, (90, 220, 90), 1)
|
| 138 |
+
gt = cv2.resize(img, (TILE, TILE), interpolation=cv2.INTER_NEAREST)
|
| 139 |
+
gt = cv2.copyMakeBorder(gt, 16, 0, 0, 0, cv2.BORDER_CONSTANT, value=(15, 15, 15))
|
| 140 |
+
cv2.putText(gt, st["period"], (3, 12), cv2.FONT_HERSHEY_SIMPLEX, 0.33,
|
| 141 |
+
(230, 230, 230), 1)
|
| 142 |
+
tiles_top.append(gt[..., ::-1]) # to BGR
|
| 143 |
+
bot = np.full((TILE, TILE, 3), 24, np.uint8)
|
| 144 |
+
if t in hide and "dec" in st:
|
| 145 |
+
vec, pose, fill = st["dec"]
|
| 146 |
+
poly = decode_shape(vec, pose) * (256 / 800)
|
| 147 |
+
poly = (poly - [x0, y0]) * (TILE / max(x1 - x0, 1))
|
| 148 |
+
cv2.fillPoly(bot, [poly.astype(np.int32)], stretch_rgb(fill))
|
| 149 |
+
cv2.polylines(bot, [poly.astype(np.int32)], True, (80, 160, 255), 1)
|
| 150 |
+
tagtxt, col = "IMPUTED", (80, 160, 255)
|
| 151 |
+
else:
|
| 152 |
+
poly = st["m8"][y0:y1, x0:x1]
|
| 153 |
+
cs, _ = cv2.findContours(poly, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 154 |
+
scale = TILE / max(x1 - x0, 1)
|
| 155 |
+
for c in cs:
|
| 156 |
+
cv2.fillPoly(bot, [(c[:, 0, :] * scale).astype(np.int32)],
|
| 157 |
+
stretch_rgb(st["fill"]))
|
| 158 |
+
tagtxt, col = "OBSERVED", (120, 200, 120)
|
| 159 |
+
bot = cv2.copyMakeBorder(bot, 16, 0, 0, 0, cv2.BORDER_CONSTANT, value=(15, 15, 15))
|
| 160 |
+
cv2.putText(bot, tagtxt, (3, 12), cv2.FONT_HERSHEY_SIMPLEX, 0.33, col, 1)
|
| 161 |
+
tiles_bot.append(bot)
|
| 162 |
+
strip = np.vstack([np.hstack(tiles_top), np.hstack(tiles_bot)])
|
| 163 |
+
fn = f"world_codec_{tag}.png"
|
| 164 |
+
cv2.imwrite(str(OUTD / fn), strip)
|
| 165 |
+
cv2.imwrite(str(SPACED / fn), strip)
|
| 166 |
+
rec = dict(tag=tag, title=title, obs=T, hidden=len(hide),
|
| 167 |
+
iou=round(float(np.mean(ious)), 3),
|
| 168 |
+
pers_iou=round(float(np.mean(pious)), 3),
|
| 169 |
+
fmae=round(float(np.mean(fmae)), 4),
|
| 170 |
+
pers_fmae=round(float(np.mean(pfmae)), 4))
|
| 171 |
+
report.append(rec); print(rec)
|
| 172 |
+
json.dump(report, open(HERE / "world_codec_report.json", "w"), indent=1)
|
| 173 |
+
json.dump(report, open(SPACED / "report.json", "w"), indent=1)
|
| 174 |
+
print(f"-> {len(report)} concept tracks rendered")
|