Lecture 03 学习讲义
正文
工具
按问题查找
Lecture 034. 根据实际输出解释每次条件变化 · 实现代码

4. 根据实际输出解释每次条件变化 · 实现代码

独立阅读

按问题继续

RGB规则提框、错误关键词基线、目标/参照/关系与干预

展开完整实现代码
fig,axes=plt.subplots(2,2,figsize=(11,7))
for ax,(condition,pixels,phrase),row in zip(axes.flat,cases,results):
    ax.imshow(pixels)
    for role,color in [('target','#c93636'),('reference','#2a5ed0')]:
        obj=row.get(role)
        if obj:
            x1,y1,x2,y2=obj['box'];ax.add_patch(Rectangle((x1,y1),x2-x1,y2-y1,fill=False,ec=color,lw=2.5));ax.text(x1,y1-3,role,color=color,fontsize=10,weight='bold')
    ax.set(xlim=(0,160),ylim=(96,0),xlabel='x (pixels)',ylabel='y (pixels)')
    ax.set_title(condition+'\n'+phrase+'\n'+row['status'],fontsize=11)
fig.suptitle('LAB15: image pixels -> candidate boxes -> explicit relation rules',fontsize=14)
fig.tight_layout();fig.savefig(OUT/'lab15-grounding-interventions.png',dpi=160);plt.show();plt.close(fig)
fig,axes=plt.subplots(1,2,figsize=(8,3))
for ax,edited,title in [(axes[0],occluded,'Only target region covered'),(axes[1],swapped,'Only cup colors exchanged')]:
    changed=np.any(base!=edited,axis=-1);ax.imshow(changed,cmap='gray',vmin=0,vmax=1);ax.set_title(title+'\nchanged pixels: '+str(int(changed.sum())),fontsize=10);ax.axis('off')
fig.tight_layout();fig.savefig(OUT/'lab15-pixel-differences.png',dpi=160);plt.show();plt.close(fig)
report={'notebook_id':'7dc83786d067465aa76264475db2c62d','execution':'local fresh Python process; deterministic rules; no neural pretrained inference','created_utc':datetime.now(timezone.utc).isoformat(),'environment':{'python':sys.version,'numpy':np.__version__,'platform':platform.platform()},'coordinate_convention':'xyxy half-open pixels; relation compares horizontal box centers; category cup only','baseline_error':{'phrase':phrases[-1],'keyword_output':'right-blue','correct_target':'left red cup','failure':'blue modifies the reference, not the target'},'results':results,'pixel_changes':{'occlusion':int(np.any(base!=occluded,axis=-1).sum()),'color_exchange':int(np.any(base!=swapped,axis=-1).sum())},'pretrained_grounding_status':'not run; official GroundingDINO implementation and evidence linked in notebook','input_sha256':{p.name:hashlib.sha256(p.read_bytes()).hexdigest() for p in sorted(OUT.glob('lab15-*.png')) if p.name in ['lab15-base.png','lab15-target-occluded.png','lab15-colors-exchanged.png']}}
(OUT/'lab15-results.json').write_text(json.dumps(report,indent=2));print('saved',OUT)