Files
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

43 lines
1.9 KiB
Python

import io, os, tarfile, numpy as np, torch
from PIL import Image
import generate as G
from model import GPT
from tokenizers import Tokenizer
G.IMG_SIDE, G.LATENT, G.N_IMG = 128, 8, 64
tok = Tokenizer.from_file("../data_mm/tokenizer.json")
vq = G.load_vq("../ckpt/vq_ds16_t2i.pt", "cuda")
def prep(im):
im = im.convert("RGB").resize((128,128), Image.BICUBIC)
return np.asarray(im, dtype=np.float32)/127.5-1.0
real=[]
tf = tarfile.open("../data_mm/shards/cc3m-0000.tar")
for m in tf:
if m.isfile() and m.name.endswith(".jpg"):
real.append(prep(Image.open(io.BytesIO(tf.extractfile(m).read()))))
if len(real)>=60: break
x = torch.from_numpy(np.stack(real)).permute(0,3,1,2).cuda()
with torch.no_grad(): rc = vq.encode(x)[2][2].view(-1,64).cpu().numpy()
ck = torch.load("out_v2a/ckpt_iter5100.pt", map_location="cuda", weights_only=False)
mt = GPT(ck["cfg"]).cuda(); mt.load_state_dict(ck["model"]); mt.eval()
torch.manual_seed(0)
with torch.device("cuda"): mr = GPT(ck["cfg"])
mr.eval()
gen={}
for nm,mo in (("trained",mt),("random",mr)):
out=[]
for i in range(20):
torch.manual_seed(500+i)
c,_ = G.text_to_image(mo, tok, "a duck on a pond", "cuda", 0.9, 50)
out.append(c.cpu().numpy())
gen[nm]=np.stack(out)
print("%-10s %10s %12s" % ("source","uniq/img","top10 overlap"))
ref_hist = np.bincount(rc.ravel(), minlength=16384).astype(float); ref_hist/=ref_hist.sum()
ref_top = set(np.argsort(-ref_hist)[:10])
print("%-10s %10.1f %12s" % ("real", rc.reshape(len(rc),-1).shape[1] and np.mean([len(np.unique(r)) for r in rc]), "-"))
for nm in ("trained","random"):
g = gen[nm].reshape(-1,64)
h = np.bincount(g.ravel(), minlength=16384).astype(float); h/=h.sum()
top = set(np.argsort(-h)[:10])
print("%-10s %10.1f %12d/10" % (nm, np.mean([len(np.unique(r)) for r in g]), len(top & ref_top)))
print(" cosine vs real: %.3f" % (h@ref_hist/(np.linalg.norm(h)*np.linalg.norm(ref_hist)+1e-9)))