[LAB 20 · 尺度跨度与两对象匹配](/course/notebooks/9315996660f54e92847770355b648f9e.html) # 同一组对象换了排列,损失不该凭空变大 · 实现代码 [独立阅读](/course/notebook-blocks/0f45eba379c5437685bb52cf7ae38a2d.html) 按问题继续 步幅占格、两对象排列、分类/位置/IoU联合匹配代价 展开完整实现代码 ``` # Writable ephemeral workspace for detached readonly cloud runs. import os, tempfile os.chdir(tempfile.mkdtemp(prefix='lec03_lab23_')) import numpy as np from itertools import permutations boxes=np.array([[.61,.12,.91,.43],[.08,.10,.32,.40]]) truth=np.array([[.10,.10,.35,.42],[.60,.10,.90,.45]]) prob=np.array([[.15,.85],[.9,.1]]); labels=np.array([0,1]) def iou(a,b): wh=np.maximum(0,np.minimum(a[2:],b[2:])-np.maximum(a[:2],b[:2])); inter=wh.prod() return inter/(np.prod(a[2:]-a[:2])+np.prod(b[2:]-b[:2])-inter) C=np.array([[-np.log(prob[i,labels[j]])+2*abs(boxes[i]-truth[j]).sum()+1-iou(boxes[i],truth[j]) for j in range(2)] for i in range(2)]) costs={str(p):sum(C[i,j] for i,j in enumerate(p)) for p in permutations(range(2))} print('cost matrix',C.round(4).tolist(),'assignment costs',costs,'selected',min(costs,key=costs.get)) assert min(costs,key=costs.get)=='(1, 0)' for stride in [4,8,16,32]: print('stride',stride,'target span',16/stride,'cells') print('with 3 predictions/2 targets, unmatched slot needs no-object classification, not an invented third target') ```