Files
tiny-gpt/mm/probe_cross.py
T
liuhuajie 5d2d3a2fb5 feat(eval): 行为探针与统一训练报告
核心教训:loss 判断不了可控性,必须看行为。

- colour_eval: 色相统计(粗指标,会被场景带偏)
- probe_ctrl: 反事实换词,只改颜色词看 64 个 VQ 码变多少
- probe_simple / probe_body: 单轴 / 双轴贪心生成
- probe_cross: 颜色x车身 4x5 交叉积,唯一能照出“只会背格子”的工具
- probe_cf_tv: 词级 TV,测一个词进入预测的程度
- pos_eval: 图像码按位置分桶,区分 prompt 依赖与上下文续写
- stream_probe: 实测 4 路流耦合度(层 7-9 余弦 0.995+)
- train_report: 四层体检合成一条命令

chk/curve/split/distcheck/modecheck/vqres/smoke 为早期诊断小工具,一并归档。
2026-12-03 10:37:55 +08:00

86 lines
3.3 KiB
Python

"""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()