Files
tiny-gpt/mm/vqres.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

23 lines
1.1 KiB
Python

import sys, io, tarfile, numpy as np, torch
from PIL import Image
sys.path.insert(0,"../vendor/llamagen_vq")
from vq_model import VQ_16
m = VQ_16(); sd = torch.load("../ckpt/vq_ds16_t2i.pt", map_location="cpu", weights_only=False)
if isinstance(sd,dict) and "model" in sd: sd = sd["model"]
m.load_state_dict(sd, strict=False); m.eval().cuda()
tf = tarfile.open("../data_mm/shards/cc3m-0000.tar")
blobs=[tf.extractfile(x).read() for x in tf if x.isfile() and x.name.endswith(".jpg")][:6]
for side in (256,128):
arrs=[]
for b in blobs:
im=Image.open(io.BytesIO(b)).convert("RGB").resize((side,side), Image.BICUBIC)
arrs.append(np.asarray(im,dtype=np.float32)/127.5-1.0)
x=torch.from_numpy(np.stack(arrs)).permute(0,3,1,2).cuda()
with torch.no_grad():
q,_,info=m.encode(x); rec=m.decode(q).clamp(-1,1)
mse=torch.mean((rec-x)**2).item()
g=side//16
print("side=%d grid=%dx%d tokens=%d MSE=%.5f PSNR=%.2f dB" % (side,g,g,g*g,mse,10*np.log10(4.0/mse)))
a=((rec[0].float().cpu()+1)*127.5).clamp(0,255).byte().permute(1,2,0).numpy()
Image.fromarray(a).save("../data_mm/vq_check/rec_%d.png" % side)