STER / code /ster_export_vectors.py
eduzrh's picture
Full autonomous run: 8 proposals with real results (few-shot WM wins K<=10; zero-shot via multi-LoD 0.67; noisy/cross-LoD solved by baseline) + all code, results, derived data
99f65bd verified
Raw
History Blame Contribute Delete
4.53 kB
"""
STER — export per-building 25-D property vectors for the World-Model few-shot method.
Runs on the POD (needs the geometry stack + cached Hague property dicts + crawled
multi-LoD data). Produces a single npz consumed by ster_wm.py on the GPU.
Exports:
lod12_X, lod22_X : (N,25) aligned per-building props for common multi-LoD ids
ml_ids : the N BAG ids (self-supervised pretraining pairs)
cand_ids, cand_X : Hague candidate per-building props (from cached dict)
index_ids, index_X : Hague index per-building props
train_pairs, train_y : Hague train pairs (cand_i, index_i as indices) + labels
test_pairs, test_y : Hague test pairs + labels
"""
import os, sys, json, numpy as np, joblib
sys.path.insert(0, ".")
import config
from object_properties import ObjectPropertiesProcessor
PROPS = config.Features.object_properties
ML = "../../data/3dbag_multilod"
PROPDIR = "data/property_dicts"
TRAIN_PD = f"{PROPDIR}/Hague_130425_train_matching_small_neg_samples_num=2_vector_normalization=True_seed=1.joblib"
TEST_PD = f"{PROPDIR}/Hague_130425_test_matching_small_neg_samples_num=2_vector_normalization=True_seed=1.joblib"
PART = "data/dataset_partitions/Hague_seed1.pkl"
OUT = "../../data/ster_wm_vectors.npz"
def vec_from_propdict(pd, side, ids):
"""Build (len(ids),25) matrix from a {prop:{side:{id:val}}} dict."""
X = np.zeros((len(ids), len(PROPS)), dtype=np.float32)
keep = []
for r, i in enumerate(ids):
try:
X[len(keep)] = [pd[p][side][i] for p in PROPS]
keep.append(i)
except KeyError:
continue
return X[:len(keep)], keep
# ---- multi-LoD: compute per-building props via official OPP ----
lod12 = joblib.load(os.path.join(ML, "3dbag_lod12.joblib"))
lod22 = joblib.load(os.path.join(ML, "3dbag_lod22.joblib"))
common = sorted(set(lod12) & set(lod22))
print(f"multi-LoD common buildings: {len(common)}", flush=True)
od = {'cands': {k: lod12[k] for k in common}, 'index': {k: lod22[k] for k in common}}
pdict = ObjectPropertiesProcessor(od, vector_normalization=True).prop_vals_dict
lod12_X, ids12 = vec_from_propdict(pdict, 'cands', common)
lod22_X, ids22 = vec_from_propdict(pdict, 'index', common)
ml_ids = [i for i in ids12 if i in set(ids22)]
i12 = {i: r for r, i in enumerate(ids12)}; i22 = {i: r for r, i in enumerate(ids22)}
lod12_X = np.array([lod12_X[i12[i]] for i in ml_ids], dtype=np.float32)
lod22_X = np.array([lod22_X[i22[i]] for i in ml_ids], dtype=np.float32)
print(f"aligned multi-LoD vectors: {lod12_X.shape}", flush=True)
# ---- Hague per-building props from cached dicts ----
train_pd = joblib.load(TRAIN_PD); test_pd = joblib.load(TEST_PD); part = joblib.load(PART)
# union of cand/index ids across train+test property dicts
cand_ids = sorted(set(train_pd[PROPS[0]]['cands']) | set(test_pd[PROPS[0]]['cands']))
index_ids = sorted(set(train_pd[PROPS[0]]['index']) | set(test_pd[PROPS[0]]['index']))
def merged(side, ids):
X = np.zeros((len(ids), len(PROPS)), dtype=np.float32); keep = []
for i in ids:
src = train_pd if i in train_pd[PROPS[0]][side] else (test_pd if i in test_pd[PROPS[0]][side] else None)
if src is None:
continue
X[len(keep)] = [src[p][side][i] for p in PROPS]; keep.append(i)
return X[:len(keep)], keep
cand_X, cand_ids = merged('cands', cand_ids)
index_X, index_ids = merged('index', index_ids)
cidx = {i: r for r, i in enumerate(cand_ids)}; iidx = {i: r for r, i in enumerate(index_ids)}
print(f"Hague cand={cand_X.shape} index={index_X.shape}", flush=True)
def pairs_to_idx(pairs):
P, Y = [], []
for c, i in pairs:
if c in cidx and i in iidx:
P.append((cidx[c], iidx[i])); Y.append(1 if c == i else 0)
return np.array(P), np.array(Y)
tr_P, tr_Y = pairs_to_idx(part['train']['blocking-based']['small'][2])
te_P, te_Y = pairs_to_idx(part['test']['matching']['blocking-based']['small'][2])
print(f"train pairs={tr_P.shape} pos={tr_Y.sum()} | test pairs={te_P.shape} pos={te_Y.sum()}", flush=True)
np.savez_compressed(OUT,
lod12_X=lod12_X, lod22_X=lod22_X, ml_ids=np.array(ml_ids),
cand_X=cand_X, index_X=index_X,
cand_ids=np.array(cand_ids), index_ids=np.array(index_ids),
train_pairs=tr_P, train_y=tr_Y, test_pairs=te_P, test_y=te_Y,
props=np.array(PROPS))
print("Saved ->", OUT, "size(MB)=", round(os.path.getsize(OUT) / 1e6, 2), flush=True)