- harness/eval/__init__.py:EQUIVALENT_SOURCES 列出精确的「冻结指纹 → 现指纹」对,contract_holds 判断现文件 仍实现冻结合同(完全相同,或同一模块的已批准改写)。阶段检查、基线检查、expand 比对三处都走它。 - 批准的只有 f56ac2f 的 5 个任务文件:上板由逐张改为按实测字节分批,板上仍逐个样本推理 (board_bench.infer_inputs),每张的预处理、后处理和指标一字未改。 - 批次的冻结记录不动,基线缓存按冻结指纹寻址,照常命中,不用重算。 - 用户 10-15:不建新批次。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
130 lines
6.6 KiB
Python
130 lines
6.6 KiB
Python
"""HPatches homography AUC. Single-image inference, paired geometric evaluation.
|
|
|
|
Reference: cvg/glue-factory 2d17e3b, HPatchesPipeline / AUCMetric /
|
|
homography_corner_error / OpenCVHomographyEstimator. Descriptor similarities
|
|
only establish mutual nearest neighbours; the gate always uses geometry.
|
|
"""
|
|
import hashlib
|
|
import json
|
|
|
|
import numpy as np
|
|
|
|
from ..adapters import feature_common as common
|
|
|
|
METRIC = 'hpatches_homography_auc'
|
|
PROTOCOL = {
|
|
'version': 'hpatches-static640x480-mnn-opencv-v1', 'max_keypoints': 1024,
|
|
'network_image_size': [640, 480], 'network_resize': 'opencv INTER_LINEAR; anisotropic fixed NCHW',
|
|
'evaluation_coordinates': 'official short-side=480 preserving aspect; integer floor; diagonal scale',
|
|
'matcher': 'mutual nearest neighbour on L2-normalized descriptors; no ratio/distance threshold',
|
|
'homography_estimator': 'OpenCV RANSAC', 'ransac_threshold_px': 1.0,
|
|
'ransac_max_iters': 3000, 'ransac_confidence': .995, 'ransac_seed': 20260914,
|
|
'error': 'mean transformed source-image corner distance; corners (0,0),(W,0),(W,H),(0,H)',
|
|
'auc_thresholds_px': [1, 3, 5], 'scalar': 'mean normalized AUC@1/3/5; range 0..1',
|
|
'failure': 'infinite corner error; counted in recall denominator',
|
|
'reference': 'https://github.com/cvg/glue-factory/tree/2d17e3b3bd7d30f0c828d4c4d3eac4ecefbf283d',
|
|
'boundary': 'bench static-input protocol; not a reproduction of published variable-aspect network results',
|
|
}
|
|
PROTOCOL_SHA = hashlib.sha256(json.dumps(PROTOCOL, sort_keys=True).encode()).hexdigest()
|
|
|
|
|
|
def inputs(t1, item, input_spec, model):
|
|
if input_spec != common.input_spec(model): raise ValueError('feature input recipe mismatch')
|
|
return common.preprocess(t1.image(item), input_spec, model)
|
|
|
|
|
|
def mutual_matches(a, b):
|
|
import torch
|
|
if len(a) == 0 or len(b) == 0: return np.empty((0, 2), np.int64)
|
|
a, b = torch.from_numpy(a), torch.from_numpy(b)
|
|
sim = a @ b.T
|
|
# topk tie behaviour agrees with the official nearest-neighbour matcher.
|
|
ab = sim.topk(1, dim=1).indices[:, 0]; ba = sim.T.topk(1, dim=1).indices[:, 0]
|
|
ids = torch.arange(len(a)); keep = ba[ab] == ids
|
|
return torch.stack((ids[keep], ab[keep]), -1).numpy()
|
|
|
|
|
|
def evaluation_size(size):
|
|
w, h = size
|
|
return (int(480 * w / h), 480) if w >= h else (480, int(480 * h / w))
|
|
|
|
|
|
def corner_error(H, ground_truth, size):
|
|
w, h = size
|
|
corners = np.asarray([[0, 0, 1], [w, 0, 1], [w, h, 1], [0, h, 1]], np.float64)
|
|
def transform(T):
|
|
v = corners @ T.T
|
|
if np.any(abs(v[:, 2]) < 1e-12): return None
|
|
return v[:, :2] / v[:, 2:]
|
|
pred, truth = transform(H), transform(ground_truth)
|
|
if pred is None or truth is None: return float('inf')
|
|
error = float(np.linalg.norm(pred - truth, axis=1).mean())
|
|
return error if np.isfinite(error) else float('inf')
|
|
|
|
|
|
def pair_metrics(first, second, H, size0, size1):
|
|
import cv2
|
|
matches = mutual_matches(first['descriptors'], second['descriptors'])
|
|
sizes = [evaluation_size(s) for s in (size0, size1)]
|
|
scales = [np.diag([e[0] / o[0], e[1] / o[1], 1.]) for e, o in zip(sizes, (size0, size1))]
|
|
truth = scales[1] @ np.asarray(H, np.float64) @ np.linalg.inv(scales[0])
|
|
k0 = first['keypoints'][matches[:, 0]].astype(np.float64) * (np.asarray(sizes[0]) / [640, 480])
|
|
k1 = second['keypoints'][matches[:, 1]].astype(np.float64) * (np.asarray(sizes[1]) / [640, 480])
|
|
estimate = None; mask = None
|
|
if len(matches) >= 4:
|
|
cv2.setRNGSeed(PROTOCOL['ransac_seed'])
|
|
estimate, mask = cv2.findHomography(k0, k1, cv2.RANSAC, PROTOCOL['ransac_threshold_px'],
|
|
maxIters=3000, confidence=.995)
|
|
error = corner_error(estimate, truth, sizes[0]) if estimate is not None else float('inf')
|
|
return {'matches': len(matches), 'inliers': int(mask.sum()) if mask is not None else 0,
|
|
'keypoints0': len(first['keypoints']), 'keypoints1': len(second['keypoints']),
|
|
'corner_error_px': error if np.isfinite(error) else None,
|
|
'success': bool(np.isfinite(error)), 'estimated_H': estimate.tolist() if estimate is not None else None}
|
|
|
|
|
|
def auc(errors, thresholds=(1, 3, 5)):
|
|
if not errors: raise ValueError('no geometric samples')
|
|
values = np.sort(np.asarray([float('inf') if e is None else e for e in errors]))
|
|
recall = (np.arange(len(values)) + 1.) / len(values)
|
|
values = np.r_[0., values]; recall = np.r_[0., recall]
|
|
results = []
|
|
for t in thresholds:
|
|
last = np.searchsorted(values, t)
|
|
e = np.r_[values[:last], t]; r = np.r_[recall[:last], recall[last - 1]]
|
|
results.append(float(np.trapz(r, x=e) / t))
|
|
return results
|
|
|
|
|
|
def evaluate(runner, t1, input_spec, model):
|
|
if t1.manifest.get('dataset') != 'hpatches': raise ValueError('HPatches T1 required')
|
|
from .common import groups, run_batched
|
|
cache = {}; sizes = {}; samples = []
|
|
for names in groups(runner, list(dict.fromkeys(item[f] for item in t1.items for f in ('file', 'file2')))):
|
|
images = [t1.image({'file': name}) for name in names]
|
|
outs = run_batched(runner, [common.preprocess(image, input_spec, model) for image in images])
|
|
for name, image, out in zip(names, images, outs, strict=True):
|
|
sizes[name] = image.size
|
|
cache[name] = common.decode(model, out)
|
|
for item in t1.items:
|
|
samples.append({'id': item['id'], **pair_metrics(cache[item['file']], cache[item['file2']],
|
|
item['H_0to1'], sizes[item['file']], sizes[item['file2']])})
|
|
values = auc([s['corner_error_px'] for s in samples])
|
|
metrics = {METRIC: float(np.mean(values)), 'auc_1px': values[0], 'auc_3px': values[1], 'auc_5px': values[2],
|
|
'n': len(samples), 'unique_images': len(cache), 'homography_failures': sum(not s['success'] for s in samples)}
|
|
return metrics, samples
|
|
|
|
|
|
def run_fp32(runner, t1, input_spec, out, resize=0):
|
|
out['metrics'], out['_feature_samples'] = evaluate(runner, t1, input_spec, out['model'])
|
|
out['feature_protocol'] = PROTOCOL; out['feature_protocol_sha256'] = PROTOCOL_SHA
|
|
|
|
|
|
def run_quant(runner, t1, input_spec, baseline, out, resize=0):
|
|
if baseline.get('model') != out['model'] or baseline.get('feature_protocol_sha256') != PROTOCOL_SHA:
|
|
raise ValueError('feature model/protocol differs from baseline')
|
|
run_fp32(runner, t1, input_spec, out, resize)
|
|
if [s['id'] for s in baseline['_feature_samples']] != [s['id'] for s in out['_feature_samples']]:
|
|
raise ValueError('feature pair identities differ from baseline')
|
|
out['G3'] = {'metric': METRIC, 'fp32': baseline['metrics'][METRIC], 'quant': out['metrics'][METRIC],
|
|
'n': out['metrics']['n'], 'tier': 't1', 'se': None}
|