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)