[LAB 04 · CLIP:候选、提示与关系检验](/course/notebooks/0ba963931ddf4866b507b210d93a43bf.html) # 5 · 2026-09-22 保存实测:固定权重复核与关系分数 · 实现代码 [独立阅读](/course/notebook-blocks/facd5784eb654f089d7c523bab835dce.html) 主线阅读与优先实验 真实CLIP输入/权重/分数、候选改动、四条件规则与独立关系推理 展开完整实现代码 ``` # 独立材料,不依赖另一册 notebook 的内存;重复运行复用本项目已下载内容。 from pathlib import Path import io, json, zipfile, requests, sys, hashlib _candidates=[Path.cwd()/'lecture03-clip-retrieval', Path.cwd()] RETRIEVAL_ROOT=next((p for p in _candidates if (p/'cache/clip-features.npz').exists()),None) if RETRIEVAL_ROOT is None: _response=requests.get('https://codingai-lec03.pages.dev/course/materials/lecture03-clip-retrieval.zip',timeout=60) _response.raise_for_status() _data=_response.content print('下载材料 ZIP:',len(_data),'bytes; SHA256',hashlib.sha256(_data).hexdigest()) with zipfile.ZipFile(io.BytesIO(_data)) as z: for info in z.infolist(): target=(Path.cwd()/info.filename).resolve() assert target.is_relative_to(Path.cwd().resolve()), '拒绝越界路径' if info.is_dir():target.mkdir(parents=True,exist_ok=True) elif not target.exists(): target.parent.mkdir(parents=True,exist_ok=True);target.write_bytes(z.read(info)) RETRIEVAL_ROOT=Path.cwd()/'lecture03-clip-retrieval' assert (RETRIEVAL_ROOT/'cache/provenance.json').is_file() sys.path.insert(0,str(RETRIEVAL_ROOT)) import run_clip_retrieval as cr import numpy as np p=json.loads((RETRIEVAL_ROOT/'cache/provenance.json').read_text()) assert cr.sha(RETRIEVAL_ROOT/'cache/clip-features.npz')==p['cache_sha256'] assert cr.sha(RETRIEVAL_ROOT/'assets/manifest.json')==p['manifest_sha256'] manifest=json.loads((RETRIEVAL_ROOT/'assets/manifest.json').read_text()) for r in manifest['gallery']+manifest['relation']+manifest['holder']: assert cr.sha(RETRIEVAL_ROOT/r['path'])==r['sha256'] a=np.load(RETRIEVAL_ROOT/'cache/clip-features.npz') print('已核验真实 CLIP 缓存;生成时间',p['generated_at_utc']) print('本格只载入缓存,没有在当前 notebook 运行 CLIP 编码器。') print('gallery, queries:',a['gallery'].shape,a['queries'].shape) ```