生成模型评测:质量、覆盖与盲点
Evaluating generative models
计算分布精确率与召回率,找出矩相同却不同的分布。
generative-evaluation好分数可能藏着哪一种失败?
生成器可能只会生成某一类的精彩样本。另一个覆盖所有类别,却夹杂无意义内容。第三个只是复制一小批训练数据。单个分数能给它们排序,却未必说明哪种问题影响实际用途。
评测应明确目标分布、生成分布、样本预算和表示方式。FID的原始说明比较Inception特征的Gaussian矩;分布精确率与召回率区分遗漏的目标质量和额外生成质量。它们回答不同问题,不是“图像质量”的同义词。先读生成模型和协方差。
比较矩、共同质量与似然
将特征分布近似为Gaussian,其均值为μP、μQ,半正定协方差为CP、CQ,则Gaussian二阶Wasserstein距离的平方为:
对称夹心形式可以用实特征分解计算。对于Gaussian,它是分布距离;对于任意特征,它只比较前两阶矩。只有配合指定Inception表示和预处理,才构成名为FID的协议,不能任取二维特征就称FID。参考代码使用协方差乘积的矩阵平方根,并明确处理数值问题。
对同一有限状态空间上的P、Q,PRD定义询问:能否把一个归一化共同分布μ嵌入两者?
α表示精确率,β表示召回率;剩余分布解释额外生成质量或共同部分未覆盖的目标质量。一条曲线描述一个固定生成器,沿曲线移动不意味着调整分类阈值或改变生成器。用正斜率λ可以计算边界:
原因是每个状态能分给共同部分的质量最多为min(P/β,Q/α)。在λ=1处,重叠量与总变差联系起来:
似然回答另一个问题:模型给目标观测分配多少概率。有限支持上的平均负对数似然为:
若某个目标概率为正的状态被Q赋零,NLL就是无限。较好似然本身不证明模型适合特定下游任务,也不证明没有记忆训练样本。
考虑矩距离的反例:P在−1、1上各放一半质量;Q在−2、0、2上分别放1/8、3/4、1/8。于是:
前两阶矩完全相同,支持集却不相交。这是关于矩摘要的精确反例,不是某个Inception模型的实测失败率。
在已知分布上计算分数
保存为generative_evaluation.py,使用Python3.12与NumPy1.26.4。程序实现四状态PRD、加权矩和对称半正定平方根。在明确数值容差内的微小负特征值会被截断,真正为负的协方差特征值会报错;没有图像编码器或聚类。
S个状态、L个斜率需要O(LS)计算,并生成L×S中间量。n个d维特征的矩计算需要O(nd²),稠密矩阵平方根需要O(d³)。这里使用总体权重;实际样本估计还须说明协方差约定与样本量。
"""Original finite-distribution evaluation and Gaussian-moment counterexamples."""
import json
import numpy as np
def probability(p):
p = np.asarray(p, dtype=float)
if p.ndim != 1 or not 1 <= len(p) <= 10000 or not np.isfinite(p).all() or np.any(p < 0) or not np.isclose(p.sum(), 1, atol=1e-12, rtol=0):
raise ValueError('Expected normalized finite probability vector')
return p
def prd(p, q, slopes):
p, q = probability(p), probability(q); slopes = np.asarray(slopes, dtype=float)
if p.shape != q.shape or slopes.ndim != 1 or not 1 <= len(slopes) <= 1000 or not np.isfinite(slopes).all() or np.any(slopes <= 0):
raise ValueError('Matching supports and positive finite slopes required')
mass = np.minimum(slopes[:, None]*p, q).sum(axis=1)
return mass, mass/slopes
def moments(x, p):
x = np.asarray(x, dtype=float); p = probability(p)
if x.ndim != 2 or len(x) != len(p) or not 1 <= x.shape[1] <= 32 or not np.isfinite(x).all():
raise ValueError('Invalid feature matrix')
mu = p @ x; delta = x-mu
return mu, (delta.T*p) @ delta
def psd_root(c):
c = np.asarray(c, dtype=float)
if c.ndim != 2 or c.shape[0] != c.shape[1] or not 1 <= len(c) <= 32 or not np.isfinite(c).all() or not np.allclose(c, c.T, atol=1e-12, rtol=0):
raise ValueError('Expected symmetric finite covariance')
val, vec = np.linalg.eigh(c)
if val.min() < -1e-10: raise ValueError('Covariance must be positive semidefinite')
return (vec*np.sqrt(np.maximum(val, 0))) @ vec.T
def gaussian_distance(mu, c, nu, d):
mu, nu = np.asarray(mu, dtype=float), np.asarray(nu, dtype=float)
c, d = np.asarray(c, dtype=float), np.asarray(d, dtype=float)
if mu.ndim != 1 or mu.shape != nu.shape or c.shape != (mu.size, mu.size) or d.shape != c.shape or not np.isfinite(mu).all() or not np.isfinite(nu).all():
raise ValueError('Incompatible Gaussian dimensions')
root = psd_root(c); psd_root(d)
sandwich = root @ d @ root; sandwich = (sandwich+sandwich.T)/2
value = float((mu-nu) @ (mu-nu) + np.trace(c+d-2*psd_root(sandwich)))
if value < -1e-8: raise ArithmeticError('Unexpected negative distance')
return max(value, 0.)
def demo():
p = np.array([.5, .5, 0, 0]); slopes = np.array([.25, .5, 1., 2., 4.]); models = {}
for name, q in [('correct', p), ('drop', [1, 0, 0, 0]), ('noise', [.25, .25, .25, .25]), ('reweight', [.9, .1, 0, 0])]:
q = probability(q); a, b = prd(p, q, slopes)
models[name] = dict(precision=a.tolist(), recall=b.tolist(), total_variation=float(abs(p-q).sum()/2),
max_precision=float(q[p > 0].sum()), max_recall=float(p[q > 0].sum()))
x = np.array([[-1.], [1.], [-2.], [0.], [2.]])
p1 = np.array([.5, .5, 0, 0, 0]); p2 = np.array([0, 0, .125, .75, .125])
m1, c1 = moments(x, p1); m2, c2 = moments(x, p2)
fourth_moment_gap = float(abs(p1 @ x[:, 0]**4-p2 @ x[:, 0]**4))
return dict(slopes=slopes.tolist(), models=models, moment_collision=dict(mean=m1.tolist(), variance=c1.tolist(),
gaussian_distance=gaussian_distance(m1, c1, m2, c2), total_variation=float(abs(p1-p2).sum()/2),
fourth_moment_gap=fourth_moment_gap),
scalar_distance=gaussian_distance([0], [[1]], [2], [[4]]),
scope='Exact synthetic supports and supplied features; no Inception network, image FID or human-quality score.')
if __name__ == '__main__': print(json.dumps(demo(), indent=2))
对P=[.5,.5,0,0],丢掉一种模式的Q=[1,0,0,0]最大精确率为1,最大召回率为.5;混入无关质量的Q=[.25,.25,.25,.25]恰好相反。若只把原有两个状态改权为[.9,.1,0,0],两个最大值仍是1,但λ=1处只有(.6,.6)。整条曲线包含仅看最大值会丢掉的信息。另一个标量Gaussian算例距离为5:均值差平方4,加标准差差平方1。
测量流程也是结果的一部分
对连续图像计算PRD,需要经验表示和近似。论文讨论特征与离散化;这里的有限精确计算避开了这些估计误差,不能说明某个特征网络怎样处理罕见物体或特定文化中的视觉细节。
比较时固定训练/测试分离、预处理、编码器版本、样本数、条件提示和择优预算。涉及采样时,应报告多种子或不确定性。多次生成后精选的画廊,与未经筛选的样本分布,是两个评测对象。分布逼真度、条件遵循、用途和记忆应分别检查,一个较好总分不能同时解决四个问题。
选择缺失的测量
- 计算。 P=[.5,.5]、Q=[.9,.1]在λ=1处的PRD点是什么? 答案:两个坐标都为min(.5,.9)+min(.5,.1)=.6,总变差为.4。
- 诊断。 矩反例为什么得到零距离? 答案:两者均值都是零、方差都是一,而该距离只使用这些量。四阶矩和支持集仍然不同。
- 解释。 最大精确率与最大召回率都为一,能推出P=Q吗? 答案:不能,两个最大值可能在不同曲线点取得。能够同时达到(1,1)才是更强条件;只有相同支持集不够。
- 迁移。 用玩具原始坐标算出的距离可以报成FID吗? 答案:不可以,它是这些坐标上的Gaussian矩距离。表示与预处理共同定义了具体图像指标。
把指标分歧当成线索
FID提供有用的特征矩比较,PRD让某些不对称失败更容易识别。两篇论文都没有让所有评测问题消失。继续读合成数据,看看相近下游用途为何能与不同数据分布同时出现。
先判断,再看解释
Gaussian矩距离为零,意味着任意分布相同吗?
学习目标
- 计算有限分布精确率与召回率。
- 解释零矩距离反例。
- 说明特征空间和评测协议。
先修与延伸
查看知识关联图 →参考文献
- GANs Trained by a Two Time-Scale Update Rule Converge to a Nash Equilibrium (v1)
Martin Heusel; Hubert Ramsauer; Thomas Unterthiner; Bernhard Nessler; Günter Klambauer; Sepp Hochreiter · 2017 - Assessing Generative Models via Precision and Recall (NeurIPS2018)
Mehdi S. M. Sajjadi; Olivier Bachem; Mario Lucic; Olivier Bousquet; Sylvain Gelly · 2018 - bioinf-jku/TTUR: fid.py @ec65610
bioinf-jku/TTUR contributors
版本1 · 编写状态: 已完成记录中的技术复核
查看技术检查记录(AI)
Codex · 2026-09-20 · Single AI author checked TTUR v1 Performance Measure/equation8, PRD sections3.2-3.4/opening4 and fixed FID matrix-square-root code. Six paired equations and four explained exercises; exact finite PRD, common-component witnesses, TV duality, PSD Gaussian distance and disjoint-support moment collision. Supplied synthetic features, not image FID or metric benchmark reproduction.
- 核PRD独立手算/共同分量、TV恒等、PSD平方根与矩碰撞;两个单独最大值为1不等于可达(1,1)。
- 每题四张计算SVG;15数值/几何与36响应式组合通过,实看12张最终中文手机/英文桌面图。初版文案整理和英文手机标题越界修正保留;完整页面验收另记。
核对的来源位置:
- GANs Trained by a Two Time-Scale Update Rule Converge to a Nash Equilibrium (v1) · Read Performance Measure and equation8; PDF6 viewed. Title/authors follow this first PDF, not later metadata. No convergence-proof review.
- Assessing Generative Models via Precision and Recall (NeurIPS2018) · Read sections3.2-3.4 and opening4; PDF5 viewed. Finite common-component definition and total-variation identity, not a clustering benchmark reproduction.
- bioinf-jku/TTUR: fid.py @ec65610 · Read109-162: Gaussian distance, matrix square root and numerical checks. Apache2 heading/opening only; no upstream execution.
相关主题