完整搜尋頁面 →

PatchCore 逐步解析

選讀・DLCV 14・穩健性:分布偏移、對抗樣本與異常 的補充資料(第 6/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 · 深度影像復原:低階視覺
查看完整課程樹 →

這則筆記給出正文概述過的 PatchCore 區塊特徵、核心集選擇與評分的細節 [42]。

帶有局部上下文的區塊特徵。 令 ϕj(x)∈Rc∗×h∗×w∗\phi_j(\mathbf{x}) \in \mathbb{R}^{c^* \times h^* \times w^*} 為在 ImageNet 預訓練網路第 jj 層的特徵圖,在 h∗×w∗h^* \times w^* 的格點上有 c∗c^* 個通道,ϕj(x,h,w)∈Rc∗\phi_j(\mathbf{x}, h, w) \in \mathbb{R}^{c^*} 為格點位置 (h,w)(h, w) 的特徵向量。對奇數的鄰域大小 pp,(h,w)(h, w) 的鄰域為

Np(h,w)={(a,b):∣a−h∣≤⌊p/2⌋, ∣b−w∣≤⌊p/2⌋},\mathcal{N}_p^{(h,w)} = \{(a, b) : |a - h| \le \lfloor p/2 \rfloor,\ |b - w| \le \lfloor p/2 \rfloor\},

而具局部感知的特徵,是對該鄰域內的特徵向量所做的聚合(論文中為自適應平均池化)。預設是結合網路第 2 與第 3 區塊的特徵,讓每個區塊特徵混合較細與較粗的資訊 [42]。所有正常訓練影像上所有這類特徵的集合,就構成記憶庫 M\mathcal{M}。

核心集選擇。 目標是選出大小固定的子集 MC⊂M\mathcal{M}_C \subset \mathcal{M},使 M\mathcal{M} 中的每個特徵都靠近 MC\mathcal{M}_C 中的某個特徵。PatchCore 使用極小化極大的設施選址目標

MC∗=arg⁡min⁡MC⊂M max⁡m∈M min⁡n∈MC∥m−n∥2,\mathcal{M}_C^{*} = \arg\min_{\mathcal{M}_C \subset \mathcal{M}}\ \max_{\mathbf{m} \in \mathcal{M}}\ \min_{\mathbf{n} \in \mathcal{M}_C} \lVert \mathbf{m} - \mathbf{n} \rVert_2,

也就是任何一個儲存的特徵到它最近的保留特徵的最大距離 [42]。這個問題是 NP-hard,所以用貪婪法近似:從一個特徵開始,然後反覆加入離目前子集距離最大的特徵(主要程式碼中的 greedy_coreset 函式)。論文也在選擇期間套用隨機線性投影來降低維度,以節省時間。同樣大小的隨機子樣本,保留下來的大多是常見的區塊,而丟掉罕見的正常區塊,它們在測試時就會顯得異常。

評分。 對於區塊特徵為 P(xtest)\mathcal{P}(\mathbf{x}_{\text{test}}) 的測試影像,找出離它最近的記憶特徵最遠的那個測試區塊,

mtest,∗,m∗=arg⁡max⁡mtest∈P(xtest) arg⁡min⁡m∈MC∥mtest−m∥2,s∗=∥mtest,∗−m∗∥2.\mathbf{m}^{\text{test},*}, \mathbf{m}^{*} = \arg\max_{\mathbf{m}^{\text{test}} \in \mathcal{P}(\mathbf{x}_{\text{test}})}\ \arg\min_{\mathbf{m} \in \mathcal{M}_C} \lVert \mathbf{m}^{\text{test}} - \mathbf{m} \rVert_2, \qquad s^{*} = \lVert \mathbf{m}^{\text{test},*} - \mathbf{m}^{*} \rVert_2 .

影像分數 ss 依 m∗\mathbf{m}^* 在記憶庫中的孤立程度對 s∗s^* 重新加權:如果最近的記憶特徵本身離它自己的鄰居 Nb(m∗)\mathcal{N}_b(\mathbf{m}^*)(最近的 bb 個記憶特徵)也很遠,它就是一個罕見的正常模式,分數就會被調高 [42]:

s=(1−exp⁡∥mtest,∗−m∗∥2∑m∈Nb(m∗)exp⁡∥mtest,∗−m∥2) s∗.s = \Big(1 - \frac{\exp \lVert \mathbf{m}^{\text{test},*} - \mathbf{m}^{*} \rVert_2}{\sum_{\mathbf{m} \in \mathcal{N}_b(\mathbf{m}^{*})} \exp \lVert \mathbf{m}^{\text{test},*} - \mathbf{m} \rVert_2}\Big)\, s^{*} .

把每個區塊的距離放回它的格點位置,以雙線性內插上取樣到輸入解析度,再用高斯平滑,就得到分割圖 [42]。

陷阱。 記憶庫隨訓練影像數與特徵解析度成長,所以核心集大小是在速度與涵蓋範圍之間取捨。訓練中從未出現的正常變化(新一批材料、新的光線)會被標記為異常,這對感官異常偵測器來說是正確的,但可能不是工廠想要的;MVTec AD 2 為此加入了光線改變的測試情境 [41]。另外,ImageNet 特徵可能不適合差異很大的成像方式(X 光、暗場照明)。

參考文獻

  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