完整搜尋頁面 →

隨機平滑的實作

選讀・DLCV 14・穩健性:分布偏移、對抗樣本與異常 的補充資料(第 5/6 則)。主線不依賴這則補充;引用編號對應本頁底部的參考文獻。

導覽:本頁與本系列: 導覽:本頁與本系列

本頁內容

補充資料 · 6

  1. 有效穩健性與線上的準確率
  2. 測試時的熵最小化
  3. 領域對抗訓練與梯度反轉層
  4. 決定評估結果的攻擊設定
  5. 隨機平滑的實作
  6. PatchCore 逐步解析

論文導讀 · 3

  1. Madry 等人:Towards Deep Learning Models Resistant to Adversarial Attacks(ICLR 2018)
  2. Athalye 等人:Obfuscated Gradients Give a False Sense of Security(ICML 2018)
  3. Gulrajani 與 Lopez-Paz:In Search of Lost Domain Generalization(ICLR 2021)

電腦視覺深度學習

  1. 02 · 深度影像分類
  2. 03 · 物件偵測:從 R-CNN 到 DETR
  3. 09 · 深度影像復原:低階視覺
查看完整課程樹 →

這則筆記走過 Cohen 等人的認證流程,說明為什麼認證半徑有上限,並給出一段在手寫數字上測試過的程式碼。

流程。 給定基底分類器 ff、雜訊水準 σ\sigma、輸入 x\mathbf{x}、樣本數 n0n_0 與 nn,以及失敗機率 α\alpha,CERTIFY 流程為 [31]:

  1. 抽取 n0n_0 個加了雜訊的副本 x+η\mathbf{x} + \boldsymbol{\eta},用 ff 分類,取最常出現的類別 c^A\hat{c}_A(對最高類別的猜測)。
  2. 再抽取 nn 個新的加雜訊副本,數有多少個被分類成 c^A\hat{c}_A;把這個數目記作 kk。
  3. 由 nn 次試驗中 kk 次成功,計算二項機率的單邊 (1−α)(1 - \alpha) 信賴下界 pA‾\underline{p_A};Cohen 等人使用 Clopper–Pearson 界。
  4. 如果 pA‾>1/2\underline{p_A} > 1/2,回傳 c^A\hat{c}_A 與半徑 R=σ Φ−1(pA‾)R = \sigma\, \Phi^{-1}(\underline{p_A});否則棄權。

第 4 步對所有其他類別使用界 pB‾=1−pA‾\overline{p_B} = 1 - \underline{p_A},這使正文中的半徑變成 σ2(Φ−1(pA‾)−Φ−1(1−pA‾))=σ Φ−1(pA‾)\frac{\sigma}{2}\big(\Phi^{-1}(\underline{p_A}) - \Phi^{-1}(1 - \underline{p_A})\big) = \sigma\, \Phi^{-1}(\underline{p_A}),因為 Φ−1(1−p)=−Φ−1(p)\Phi^{-1}(1 - p) = -\Phi^{-1}(p)。以至少 1−α1 - \alpha 的機率,回傳的類別就是平滑分類器 gg 在半徑 RR 內處處的預測 [31]。第 1 步與第 2 步使用不同的樣本,可以讓界保持有效:若用同一批樣本來選類別並估計它的機率,估計就會有偏差。

為什麼半徑有上限。 即使全部 nn 個加雜訊副本都一致(k=nk = n),Clopper–Pearson 下界也只有 pA‾=α1/n\underline{p_A} = \alpha^{1/n},所以 R≤σ Φ−1(α1/n)R \le \sigma\, \Phi^{-1}(\alpha^{1/n})。取 n=2000n = 2000、α=0.001\alpha = 0.001、σ=0.5\sigma = 0.5,這個上限約為 1.351.35(我們的計算)。更大的半徑需要多得多的樣本,而論文指出 RR 隨 nn 增加得非常慢 [31]。

訓練基底分類器。 認證對任何 ff 都成立,但只有當 ff 能把加了雜訊的輸入分類得好時才有用,所以 ff 是在輸入加上同樣 σ\sigma 的高斯雜訊下訓練的。

程式碼 · Python · 26 行
import numpy as np
from scipy.stats import beta, norm
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier

X, y = load_digits(return_X_y=True)
X = X / 16.0
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.3, stratify=y, random_state=0)
rng = np.random.default_rng(0)
sigma = 0.5
Xnoisy = np.vstack([Xtr + rng.normal(0, sigma, Xtr.shape) for _ in range(4)])   # train f under noise
f = MLPClassifier(hidden_layer_sizes=(128,), alpha=1e-3, max_iter=200, early_stopping=True, random_state=0).fit(Xnoisy, np.tile(ytr, 4))

def certify(x, n0=100, n=2000, alpha=0.001):
    votes0 = np.bincount(f.predict(x + rng.normal(0, sigma, (n0, 64))), minlength=10)
    c = votes0.argmax()                                   # guess the top class with a small sample
    k = np.sum(f.predict(x + rng.normal(0, sigma, (n, 64))) == c)
    p_low = beta.ppf(alpha, k, n - k + 1)                 # one-sided Clopper-Pearson lower bound
    return (c, sigma * norm.ppf(p_low)) if p_low > 0.5 else (-1, 0.0)   # -1 means abstain

res = [certify(x) for x in Xte[:200]]
for r in [0, 0.25, 0.5, 0.75, 1.0]:
    ok = np.mean([(c == t) and (R >= r) for (c, R), t in zip(res, yte[:200])])
    print(f"certified accuracy at l2 radius {r:.2f}: {ok:.3f}")
print("abstained:", sum(c == -1 for c, _ in res), "of", len(res))

它印出:

程式碼 · 6 行
certified accuracy at l2 radius 0.00: 0.825
certified accuracy at l2 radius 0.25: 0.590
certified accuracy at l2 radius 0.50: 0.290
certified accuracy at l2 radius 0.75: 0.070
certified accuracy at l2 radius 1.00: 0.000
abstained: 32 of 200

(我們的玩具實驗。)半徑 rr 下的認證準確率(certified accuracy),是被認證為 R≥rR \ge r 且分類正確的測試影像所占的比例。在 r=0r = 0 時,它就是把棄權算成錯誤的平滑分類器的準確率。為了讓半徑有個尺度感:在這些 [0,1][0, 1] 影像上,ℓ2\ell_2 變化 0.5,相當於 64 個像素每一個都改變 0.0625。注意雜訊沒有被截到 [0,1][0, 1];定理不需要這一點。

邊界情況。 認證是針對 ℓ2\ell_2 的;維度為 dd 的半徑 ε\varepsilon 的 ℓ∞\ell_\infty 球,包含長度最多 εd\varepsilon\sqrt{d} 的 ℓ2\ell_2 擾動,所以對大影像來說,ℓ2\ell_2 的認證換成 ℓ∞\ell_\infty 就很弱。保證是關於 gg 的,而 gg 只能透過取樣得到,所以預測也是隨機的,每張影像要花 nn 次前向傳遞。

參考文獻

  1. D. Hendrycks, K. Zhao, S. Basart, J. Steinhardt and D. Song, “Natural Adversarial Examples,” CVPR, 2021. CVF
  2. P. W. Koh, S. Sagawa, H. Marklund, S. M. Xie, M. Zhang, A. Balsubramani et al., “WILDS: A Benchmark of in-the-Wild Distribution Shifts,” ICML, PMLR 139, 2021. PMLR
  3. J. Yang, K. Zhou, Y. Li and Z. Liu, “Generalized Out-of-Distribution Detection: A Survey,” International Journal of Computer Vision, vol. 132, no. 12, pp. 5635–5662, 2024. doi · arXiv
  4. D. Hendrycks and T. Dietterich, “Benchmarking Neural Network Robustness to Common Corruptions and Perturbations,” ICLR, 2019. arXiv
  5. D. Hendrycks, S. Basart, N. Mu, S. Kadavath, F. Wang, E. Dorundo et al., “The Many Faces of Robustness: A Critical Analysis of Out-of-Distribution Generalization,” ICCV, 2021. CVF
  6. A. Barbu, D. Mayo, J. Alverio, W. Luo, C. Wang, D. Gutfreund, J. Tenenbaum and B. Katz, “ObjectNet: A large-scale bias-controlled dataset for pushing the limits of object recognition models,” NeurIPS, 2019. proceedings
  7. B. Recht, R. Roelofs, L. Schmidt and V. Shankar, “Do ImageNet Classifiers Generalize to ImageNet?,” ICML, PMLR 97, 2019. PMLR
  8. R. Taori, A. Dave, V. Shankar, N. Carlini, B. Recht and L. Schmidt, “Measuring Robustness to Natural Distribution Shifts in Image Classification,” NeurIPS, 2020. proceedings
  9. J. Miller, R. Taori, A. Raghunathan, S. Sagawa, P. W. Koh, V. Shankar, P. Liang, Y. Carmon and L. Schmidt, “Accuracy on the Line: on the Strong Correlation Between Out-of-Distribution and In-Distribution Generalization,” ICML, PMLR 139, 2021. PMLR
  10. D. Hendrycks, N. Mu, E. D. Cubuk, B. Zoph, J. Gilmer and B. Lakshminarayanan, “AugMix: A Simple Data Processing Method to Improve Robustness and Uncertainty,” ICLR, 2020. arXiv
  11. R. Geirhos, P. Rubisch, C. Michaelis, M. Bethge, F. A. Wichmann and W. Brendel, “ImageNet-trained CNNs are biased towards texture; increasing shape bias improves accuracy and robustness,” ICLR, 2019. arXiv
  12. A. Radford, J. W. Kim, C. Hallacy, A. Ramesh, G. Goh, S. Agarwal et al., “Learning Transferable Visual Models From Natural Language Supervision,” ICML, PMLR 139, 2021. PMLR
  13. A. Fang, G. Ilharco, M. Wortsman, Y. Wan, V. Shankar, A. Dave and L. Schmidt, “Data Determines Distributional Robustness in Contrastive Language Image Pre-training (CLIP),” ICML, PMLR 162, 2022. PMLR
  14. M. Wortsman, G. Ilharco, J. W. Kim, M. Li, S. Kornblith, R. Roelofs et al., “Robust Fine-Tuning of Zero-Shot Models,” CVPR, 2022. CVF
  15. S. Schneider, E. Rusak, L. Eck, O. Bringmann, W. Brendel and M. Bethge, “Improving robustness against common corruptions by covariate shift adaptation,” NeurIPS, 2020. proceedings
  16. D. Wang, E. Shelhamer, S. Liu, B. Olshausen and T. Darrell, “Tent: Fully Test-Time Adaptation by Entropy Minimization,” ICLR, 2021. arXiv
  17. Y. Ganin, E. Ustinova, H. Ajakan, P. Germain, H. Larochelle, F. Laviolette, M. Marchand and V. Lempitsky, “Domain-Adversarial Training of Neural Networks,” Journal of Machine Learning Research, vol. 17, no. 59, pp. 1–35, 2016. JMLR
  18. K. Zhou, Z. Liu, Y. Qiao, T. Xiang and C. C. Loy, “Domain Generalization: A Survey,” IEEE Transactions on Pattern Analysis and Machine Intelligence, 2022. doi · arXiv
  19. I. Gulrajani and D. Lopez-Paz, “In Search of Lost Domain Generalization,” ICLR, 2021. arXiv
  20. Y. Ovadia, E. Fertig, J. Ren, Z. Nado, D. Sculley, S. Nowozin, J. V. Dillon, B. Lakshminarayanan and J. Snoek, “Can you trust your model’s uncertainty? Evaluating predictive uncertainty under dataset shift,” NeurIPS, 2019. proceedings
  21. C. Szegedy, W. Zaremba, I. Sutskever, J. Bruna, D. Erhan, I. Goodfellow and R. Fergus, “Intriguing properties of neural networks,” ICLR, 2014. arXiv
  22. I. J. Goodfellow, J. Shlens and C. Szegedy, “Explaining and Harnessing Adversarial Examples,” ICLR, 2015. arXiv
  23. A. Madry, A. Makelov, L. Schmidt, D. Tsipras and A. Vladu, “Towards Deep Learning Models Resistant to Adversarial Attacks,” ICLR, 2018. arXiv
  24. F. Croce, M. Andriushchenko, V. Sehwag, E. Debenedetti, N. Flammarion, M. Chiang, P. Mittal and M. Hein, “RobustBench: a standardized adversarial robustness benchmark,” NeurIPS Datasets and Benchmarks Track, 2021. proceedings · leaderboard
  25. N. Papernot, P. McDaniel, I. Goodfellow, S. Jha, Z. B. Celik and A. Swami, “Practical Black-Box Attacks against Machine Learning,” ACM Asia Conference on Computer and Communications Security (AsiaCCS), pp. 506–519, 2017. doi
  26. N. Carlini and D. Wagner, “Towards Evaluating the Robustness of Neural Networks,” IEEE Symposium on Security and Privacy (SP), pp. 39–57, 2017. doi
  27. D. Tsipras, S. Santurkar, L. Engstrom, A. Turner and A. Madry, “Robustness May Be at Odds with Accuracy,” ICLR, 2019. arXiv
  28. H. Zhang, Y. Yu, J. Jiao, E. P. Xing, L. El Ghaoui and M. I. Jordan, “Theoretically Principled Trade-off between Robustness and Accuracy,” ICML, PMLR 97, 2019. PMLR
  29. A. Athalye, N. Carlini and D. Wagner, “Obfuscated Gradients Give a False Sense of Security: Circumventing Defenses to Adversarial Examples,” ICML, PMLR 80, 2018. PMLR
  30. F. Croce and M. Hein, “Reliable evaluation of adversarial robustness with an ensemble of diverse parameter-free attacks,” ICML, PMLR 119, 2020. PMLR
  31. J. Cohen, E. Rosenfeld and J. Z. Kolter, “Certified Adversarial Robustness via Randomized Smoothing,” ICML, PMLR 97, 2019. PMLR
  32. K. Eykholt, I. Evtimov, E. Fernandes, B. Li, A. Rahmati, C. Xiao, A. Prakash, T. Kohno and D. Song, “Robust Physical-World Attacks on Deep Learning Visual Classification,” CVPR, 2018. CVF
  33. T. B. Brown, D. Mané, A. Roy, M. Abadi and J. Gilmer, “Adversarial Patch,” arXiv:1712.09665, 2017. arXiv
  34. D. Hendrycks and K. Gimpel, “A Baseline for Detecting Misclassified and Out-of-Distribution Examples in Neural Networks,” ICLR, 2017. arXiv
  35. W. Liu, X. Wang, J. D. Owens and Y. Li, “Energy-based Out-of-distribution Detection,” NeurIPS, 2020. proceedings
  36. K. Lee, K. Lee, H. Lee and J. Shin, “A Simple Unified Framework for Detecting Out-of-Distribution Samples and Adversarial Attacks,” NeurIPS, 2018. proceedings
  37. Y. Sun, Y. Ming, X. Zhu and Y. Li, “Out-of-Distribution Detection with Deep Nearest Neighbors,” ICML, PMLR 162, 2022. PMLR
  38. D. Hendrycks, M. Mazeika and T. Dietterich, “Deep Anomaly Detection with Outlier Exposure,” ICLR, 2019. arXiv
  39. J. Zhang, J. Yang, P. Wang, H. Wang, Y. Lin, H. Zhang et al., “OpenOOD v1.5: Enhanced Benchmark for Out-of-Distribution Detection,” arXiv:2306.09301, 2023. arXiv
  40. P. Bergmann, M. Fauser, D. Sattlegger and C. Steger, “MVTec AD — A Comprehensive Real-World Dataset for Unsupervised Anomaly Detection,” CVPR, 2019. CVF
  41. L. Heckler-Kram, J.-H. Neudeck, U. Scheler, R. König and C. Steger, “The MVTec AD 2 Dataset: Advanced Scenarios for Unsupervised Anomaly Detection,” International Journal of Computer Vision, vol. 134, no. 4, 2026. doi · arXiv
  42. K. Roth, L. Pemula, J. Zepeda, B. Schölkopf, T. Brox and P. Gehler, “Towards Total Recall in Industrial Anomaly Detection,” CVPR, 2022. CVF
  43. J. Bitterwolf, M. Müller and M. Hein, “In or Out? Fixing ImageNet Out-of-Distribution Detection Evaluation,” ICML, PMLR 202, 2023. PMLR
  44. J. Liang, R. He and T. Tan, “A Comprehensive Survey on Test-Time Adaptation Under Distribution Shifts,” International Journal of Computer Vision, vol. 133, no. 1, pp. 31–64, 2025. doi · arXiv
  45. N. Akhtar, A. Mian, N. Kardan and M. Shah, “Advances in Adversarial Attacks and Defenses in Computer Vision: A Survey,” IEEE Access, vol. 9, pp. 155161–155196, 2021. doi · arXiv
  46. G. Pang, C. Shen, L. Cao and A. van den Hengel, “Deep Learning for Anomaly Detection: A Review,” ACM Computing Surveys, vol. 54, no. 2, pp. 1–38, 2021. doi · arXiv
  47. J. Liu, G. Xie, J. Wang, S. Li, C. Wang, F. Zheng and Y. Jin, “Deep Industrial Image Anomaly Detection: A Survey,” Machine Intelligence Research, vol. 21, no. 1, pp. 104–135, 2024. doi · arXiv
  48. Z. Wang, T. Pang, C. Du, M. Lin, W. Liu and S. Yan, “Better Diffusion Models Further Improve Adversarial Training,” ICML, PMLR 202, 2023. PMLR
  49. J. Yang, P. Wang, D. Zou, Z. Zhou, K. Ding, W. Peng et al., “OpenOOD: Benchmarking Generalized Out-of-Distribution Detection,” NeurIPS Datasets and Benchmarks Track, 2022. proceedings
  50. J. Jeong, Y. Zou, T. Kim, D. Zhang, A. Ravichandran and O. Dabeer, “WinCLIP: Zero-/Few-Shot Anomaly Classification and Segmentation,” CVPR, 2023. CVF