"""Cross-product probe: colour x body, greedy, all 20 cells. If the two attributes compose, hue must depend on the COLOUR (row) and be roughly constant down a column, and the pixel structure must depend on the BODY (column). Outputs: - 4x5 hue matrix - mean hue spread within a body (across colours) vs within a colour (across bodies) - pairwise 32x32 pixel distance, split into same-body / same-colour / different-both """ 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_of from tokenizers import Tokenizer COLOURS = ["red", "blue", "green", "yellow"] BODIES = ["sedan", "suv", "truck", "convertible", "sports"] def thumb(im, side=32): a = np.asarray(im.convert("RGB").resize((side, side)), dtype=np.float32) / 255.0 return a.reshape(-1) def main(): ap = argparse.ArgumentParser() ap.add_argument("--ckpt", required=True) 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("--out", default="../data_mm/colour_eval") ap.add_argument("--label", default="cross") 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() os.makedirs(args.out, exist_ok=True) hue, vec, imgs = {}, {}, {} for c in COLOURS: for b in BODIES: torch.manual_seed(0) cd, _ = G.text_to_image(model, tok, f"a {c} {b}", device, 1e-5, 1) im = G.codes_to_pil(vq, cd, device) imgs[(c, b)] = im im.save(os.path.join(args.out, f"{args.label}_{c}_{b}.png")) hue[(c, b)] = hue_of(im) vec[(c, b)] = thumb(im) print("hue matrix (rows=colour, cols=body):") print(" " + "".join(f"{b[:9]:>10}" for b in BODIES)) for c in COLOURS: print(f"{c:>7} " + "".join(f"{hue[(c,b)]:>10.0f}" for b in BODIES)) within_body = [np.nanmax([hue[(c, b)] for c in COLOURS]) - np.nanmin([hue[(c, b)] for c in COLOURS]) for b in BODIES] within_col = [np.nanmax([hue[(c, b)] for b in BODIES]) - np.nanmin([hue[(c, b)] for b in BODIES]) for c in COLOURS] print(f"\nmean hue spread within a body (across colours): {np.nanmean(within_body):5.1f} deg") print(f"mean hue spread within a colour (across bodies): {np.nanmean(within_col):5.1f} deg") print(" -> colour works if the first number is large and the second small") keys = list(vec.keys()) d = {(a, b): float(np.linalg.norm(vec[a] - vec[b])) for a, b in itertools.combinations(keys, 2)} same_body = [v for (x, y), v in d.items() if x[1] == y[1]] same_col = [v for (x, y), v in d.items() if x[0] == y[0] and x[1] != y[1]] diff = [v for (x, y), v in d.items() if x[0] != y[0] and x[1] != y[1]] print(f"\npixel distance same body {np.mean(same_body):.3f} same colour {np.mean(same_col):.3f}" f" different both {np.mean(diff):.3f}") print(" -> structure follows the body if same-body is the smallest") if __name__ == "__main__": main()