torch.manual_seed(0)dev = d2l.try_gpu()net = UNet(ch=64, num_classes=10).to(dev)levels = {28: ['inc', 'down1', 'up0', 'outc'], 14: ['pool1', 'down2', 'up1'],7: ['pool2', 'mid']} # the blocks whose output has each sizefor side, names in levels.items(): params = [p for n in names for p ingetattr(net, n).parameters()] kernels =sum(p.numel() for p in params if p.dim() ==4) # convolutionsprint(f'{side} x {side}: {sum(p.numel() for p in params):,} parameters, 'f'{kernels * side **2/1e6:.0f}M multiply-adds per image')print(f'total: {d2l.num_params(net):,} parameters')
28 x 28: 211,329 parameters, 152M multiply-adds per image
14 x 14: 493,376 parameters, 92M multiply-adds per image
7 x 7: 459,776 parameters, 22M multiply-adds per image
total: 1,198,913 parameters
轮廓在 t = 0.6 前锁定:即便状态 \mathbf{x}_{0.6} 仍是 60\% 高斯噪声,\hat{\mathbf{x}}_0 也已锁定最终样本的剪影(距 t = 0 最终样本 \textrm{RMS} \le 0.14)。
后期步数(t < 0.4):精修高频边缘、明暗与纹理。
RMS from the image: 0.00 0.07 0.11 0.16 0.24 0.43
RMS from the class mean: 0.46 0.46 0.45 0.43 0.36 0.07
RMS from the sample: 0.00 0.03 0.07 0.12 0.19 0.32
def fd_and_agreement(cnn, imgs, f_ref):"""FD to the reference features, and the fraction of samples that the feature network assigns to the requested class i % 10.""" f = fd_features(cnn, imgs) cls = cnn.head(f.to(imgs.device)).argmax(1) agree = (cls == torch.arange(len(imgs), device=cls.device) %10)return d2l.fid_score(f, f_ref), agree.float().mean().item()fd_ema, agree_ema = fd_and_agreement(cnn, generate(ema), f_test[:5000])print(f'5,000 samples: FD {fd_ema:.2f}, class agreement {agree_ema:.3f}')
两条铁律:(1)绝不跨样本量 n 比较 FD(真实下限从 n = 1{,}000 处的 0.87 骤降 5\times 至 n = 5{,}000 处的 0.17);(2)忽略小于多种子噪声底线的差异(n = 5{,}000 时为 \pm 0.05)。
校准 FD:1/n 有限样本下限(代码)
为何原始 FD 数值必须配参考标尺
f_real = fd_features(cnn, X[:25_000]) # five disjoint sets of 5,000for n in (1000, 5000): sets = [f_real[i * n:(i +1) * n] for i inrange(5)] fd = np.array([float(d2l.fid_score(f, f_test[:n])) for f in sets]) kid = np.array([float(d2l.kid_score(f, f_test[:n])) for f in sets])print(f'{n:,} real images, five sets: FD {fd.mean():.2f} 'f'({fd.min():.2f} to {fd.max():.2f}), KID {kid.mean():.4f} 'f'({kid.min():.4f} to {kid.max():.4f})')
1,000 real images, five sets: FD 0.88 (0.82 to 0.96), KID 0.0053 (-0.0070 to 0.0227)
5,000 real images, five sets: FD 0.17 (0.15 to 0.19), KID -0.0018 (-0.0051 to 0.0023)
for seed inrange(3): # three draws of the initial noise fd = [fd_and_agreement(cnn, generate(model, sampler=sampler, seed=seed), f_test[:5000])[0]for model, sampler in ((ema, euler50), (net, euler50), (ema, ancestral1000))]print(f'noise {seed}: Euler 50 average {fd[0]:.2f}, last weights 'f'{fd[1]:.2f} ({fd[1] - fd[0]:+.2f}); chain with 1,000 steps 'f'{fd[2]:.2f} ({fd[2] - fd[0]:+.2f})')
noise 0: Euler 50 average 0.82, last weights 1.36 (+0.55); chain with 1,000 steps 0.69 (-0.13)
noise 1: Euler 50 average 0.84, last weights 1.46 (+0.63); chain with 1,000 steps 0.66 (-0.18)
noise 2: Euler 50 average 0.83, last weights 1.36 (+0.53); chain with 1,000 steps 0.68 (-0.15)
EMA 带来显著的 \approx +0.5 FD 优势:每次抽样中原始末步权重得分 \approx 1.3–1.5,而 EMA 为 \approx 0.8,且 EMA 跨噪声种子仅波动 \pm 0.02。
gamma 0.0: mean term 0.71 of FD 2.74
gamma 1.0: mean term 0.28 of FD 0.82
T-shirt: test images 0.101, gamma 0 0.097
trouser: test images 0.099, gamma 0 0.080
pullover: test images 0.102, gamma 0 0.144
dress: test images 0.102, gamma 0 0.097
coat: test images 0.099, gamma 0 0.100
sandal: test images 0.099, gamma 0 0.074
shirt: test images 0.096, gamma 0 0.104
sneaker: test images 0.103, gamma 0 0.095
bag: test images 0.100, gamma 0 0.099
ankle boot: test images 0.098, gamma 0 0.111
real images, uniform class mix: mean term 0.04 of FD 0.19
real images, gamma 0 class mix: mean term 0.13 of FD 0.38