"""Two-axis control probe: does the colour word move hue, does the body word move shape? Colour axis: body fixed, 4 colour words -> hue + identical-code rate. Body axis: colour fixed, 6 body words -> identical-code rate (1.0 = ignored). """ import argparse import itertools import os import numpy as np import torch import generate as G from model import GPT from probe_ctrl import HUE_RANGE, hue_of, image_token_acc, in_hue, load_rows from tokenizers import Tokenizer COLOURS = ["red", "blue", "green", "yellow"] BODIES = ["sedan", "suv", "truck", "convertible", "sports"] # hatchback only 187 total def main(): ap = argparse.ArgumentParser() ap.add_argument("--ckpt", required=True) ap.add_argument("--data_dir", default="../data_mm_car_t") ap.add_argument("--tok", default="../data_mm_car_t/tokenizer.json") ap.add_argument("--vq_ckpt", default="../ckpt/vq_ds16_t2i.pt") ap.add_argument("--row_len", type=int, default=128) ap.add_argument("--n", type=int, default=4) ap.add_argument("--temperature", type=float, default=0.6) ap.add_argument("--top_k", type=int, default=20) ap.add_argument("--out", default="../data_mm/colour_eval") ap.add_argument("--label", default="armF") args = ap.parse_args() device = "cuda" G.IMG_SIDE, G.LATENT, G.N_IMG = 128, 8, 64 tok = Tokenizer.from_file(args.tok) vq = G.load_vq(args.vq_ckpt, device) ck = torch.load(args.ckpt, map_location=device, weights_only=False) model = GPT(ck["cfg"]).to(device) model.load_state_dict(ck["model"]) model.eval() print(f"model: {model.num_params()/1e6:.2f}M total, " f"{model.num_params(non_embedding=True)/1e6:.2f}M non-emb") tr = load_rows(os.path.join(args.data_dir, "train.bin"), args.row_len) va = load_rows(os.path.join(args.data_dir, "val.bin"), args.row_len) a_tr, _ = image_token_acc(model, tr, device) a_va, _ = image_token_acc(model, va, device, max_rows=10_000) print(f"image-token top-1 train {a_tr:.3f} val {a_va:.3f}") os.makedirs(args.out, exist_ok=True) def gen(prompt, save): torch.manual_seed(0) cd, _ = G.text_to_image(model, tok, prompt, device, 1e-5, 1) if save: G.codes_to_pil(vq, cd, device).save(os.path.join(args.out, f"{args.label}_{save}.png")) return cd.cpu().tolist() # ---- colour axis, body fixed ---- # "car" is the bucket where the VLM caption named no body type; it has the # widest per-colour support (158-532 rows) so the colour axis is measured cleanly. body0 = "car" cc, ch = {}, {} for c in COLOURS: cc[c] = gen(f"a {c} {body0}", f"c_{c}") ch[c] = hue_of(G.codes_to_pil(vq, torch.tensor([cc[c]], device=device), device)) idc = [sum(1 for x, y in zip(cc[a], cc[b]) if x == y) / 64 for a, b in itertools.combinations(COLOURS, 2)] hs = [h for h in ch.values() if not np.isnan(h)] print(f"\ncolour axis (body={body0}) greedy hue: " + " ".join(f"{c}={ch[c]:.0f}" for c in COLOURS)) print(f" hue spread {max(hs)-min(hs):.1f} deg hit " f"{sum(1 for c in COLOURS if in_hue(ch[c], HUE_RANGE[c]))}/{len(COLOURS)}" f" cross-colour code identity {np.mean(idc):.3f}") # ---- body axis, colour fixed ---- col0 = "white" # widest body coverage: sedan 73, suv 67, truck 40, conv 48, sports 47 bb = {} for b in BODIES: bb[b] = gen(f"a {col0} {b}", f"b_{b}") idb = [sum(1 for x, y in zip(bb[a], bb[b]) if x == y) / 64 for a, b in itertools.combinations(BODIES, 2)] print(f"body axis (colour={col0}) cross-body code identity {np.mean(idb):.3f}" f" (1.0 = body word ignored)") # ---- stochastic colour, low temperature ---- print(f"stochastic colour {args.temperature}/top{args.top_k}:") hits = 0 for c in COLOURS: hs2 = [] for i in range(args.n): torch.manual_seed(2000 + i) cd, _ = G.text_to_image(model, tok, f"a {c} {body0}", device, args.temperature, args.top_k) G.codes_to_pil(vq, cd, device).save(os.path.join(args.out, f"{args.label}_{c}_{i}.png")) hs2.append(hue_of(G.codes_to_pil(vq, cd, device))) mh = float(np.nanmedian(hs2)) ok = in_hue(mh, HUE_RANGE[c]) hits += int(ok) print(f" {c:>7} hue={mh:6.1f} {'HIT' if ok else ' '}") print(f" hit {hits}/{len(COLOURS)}") if __name__ == "__main__": main()