You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mindarmour.adv_robustness.detectors.rst 13 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. mindarmour.adv_robustness.detectors
  2. ===================================
  3. 此模块包括用于区分对抗样本和良性样本的检测器方法。
  4. .. py:class:: mindarmour.adv_robustness.detectors.ErrorBasedDetector(auto_encoder, false_positive_rate=0.01, bounds=(0.0, 1.0))
  5. 检测器重建输入样本,测量重建误差,并拒绝重建误差大的样本。
  6. 参考文献: `MagNet: a Two-Pronged Defense against Adversarial Examples, by Dongyu Meng and Hao Chen, at CCS 2017. <https://arxiv.org/abs/1705.09064>`_。
  7. **参数:**
  8. - **auto_encoder** (Model) - 一个(训练过的)自动编码器,通过减少编码表示输入。
  9. - **false_positive_rate** (float) - 检测器的误报率。默认值:0.01。
  10. - **bounds** (tuple) - (clip_min, clip_max)。默认值:(0.0, 1.0)。
  11. .. py:method:: detect(inputs)
  12. 检测输入样本是否具有对抗性。
  13. **参数:**
  14. - **inputs** (numpy.ndarray) - 待判断的可疑样本。
  15. **返回:**
  16. - **list[int]** - 样本是否具有对抗性。如果res[i]=1,则索引为i的输入样本是对抗性的。
  17. .. py:method:: detect_diff(inputs)
  18. 检测原始样本和重建样本之间的距离。
  19. **参数:**
  20. - **inputs** (numpy.ndarray) - 输入样本。
  21. **返回:**
  22. - **float** - 重建样本和原始样本之间的距离。
  23. .. py:method:: fit(inputs, labels=None)
  24. 查找给定数据集的阈值,以区分对抗样本。
  25. **参数:**
  26. - **inputs** (numpy.ndarray) - 输入样本。
  27. - **labels** (numpy.ndarray) - 输入样本的标签。默认值:None。
  28. **返回:**
  29. - **float** - 区分对抗样本和良性样本的阈值。
  30. .. py:method:: set_threshold(threshold)
  31. 设置参数阈值。
  32. **参数:**
  33. - **threshold** (float) - 检测阈值。
  34. .. py:method:: transform(inputs)
  35. 重建输入样本。
  36. **参数:**
  37. - **inputs** (numpy.ndarray) - 输入样本。
  38. **返回:**
  39. - **numpy.ndarray** - 重建图像。
  40. .. py:class:: mindarmour.adv_robustness.detectors.DivergenceBasedDetector(auto_encoder, model, option='jsd', t=1, bounds=(0.0, 1.0))
  41. 基于发散的检测器学习通过js发散来区分正常样本和对抗样本。
  42. 参考文献: `MagNet: a Two-Pronged Defense against Adversarial Examples, by Dongyu Meng and Hao Chen, at CCS 2017. <https://arxiv.org/abs/1705.09064>`_。
  43. **参数:**
  44. - **auto_encoder** (Model) - 编码器模型。
  45. - **model** (Model) - 目标模型。
  46. - **option** (str) - 用于计算发散的方法。默认值:'jsd'。
  47. - **t** (int) - 用于克服数值问题的温度。默认值:1。
  48. - **bounds** (tuple) - 数据的上下界。以(clip_min, clip_max)的形式出现。默认值:(0.0, 1.0)。
  49. .. py:method:: detect_diff(inputs)
  50. 检测原始样本和重建样本之间的距离。
  51. 距离由JSD计算。
  52. **参数:**
  53. - **inputs** (numpy.ndarray) - 输入样本。
  54. **返回:**
  55. - **float** - 距离。
  56. **异常:**
  57. - **NotImplementedError** - 不支持参数 `option` 。
  58. .. py:class:: mindarmour.adv_robustness.detectors.RegionBasedDetector(model, number_points=10, initial_radius=0.0, max_radius=1.0, search_step=0.01, degrade_limit=0.0, sparse=False)
  59. 参考文献: `Mitigating evasion attacks to deep neural networks via region-based classification <https://arxiv.org/abs/1709.05583>`_。
  60. **参数:**
  61. - **model** (Model) - 目标模型。
  62. - **number_points** (int) - 从原始样本的超立方体生成的样本数。默认值:10。
  63. - **initial_radius** (float) - 超立方体的初始半径。默认值:0.0。
  64. - **max_radius** (float) - 超立方体的最大半径。默认值:1.0。
  65. - **search_step** (float) - 搜索半径期间增量。默认值:0.01。
  66. - **degrade_limit** (float) - 分类精度的可接受下降。默认值:0.0。
  67. - **sparse** (bool) - 如果为True,则输入标签为稀疏编码。如果为False,则输入标签为onehot编码。默认值:False。
  68. .. py:method:: detect(inputs)
  69. 判断输入样本是否具有对抗性。
  70. **参数:**
  71. - **inputs** (numpy.ndarray) - 待判断的可疑样本。
  72. **返回:**
  73. - **list[int]** - 样本是否具有对抗性。如果res[i]=1,则索引为i的输入样本是对抗性的。
  74. .. py:method:: detect_diff(inputs)
  75. 返回原始预测结果和基于区域的预测结果。
  76. **参数:**
  77. - **inputs** (numpy.ndarray) - 输入样本。
  78. **返回:**
  79. - **numpy.ndarray** - 输入样本的原始预测结果和基于区域的预测结果。
  80. .. py:method:: fit(inputs, labels=None)
  81. 训练检测器来决定最佳半径。
  82. **参数:**
  83. - **inputs** (numpy.ndarray) - 良性样本。
  84. - **labels** (numpy.ndarray) - 输入样本的ground truth标签。默认值:None。
  85. **返回:**
  86. - **float** - 最佳半径。
  87. .. py:method:: set_radius(radius)
  88. 设置半径。
  89. **参数:**
  90. - **radius** (float) - 区域的半径。
  91. .. py:method:: transform(inputs)
  92. 为输入样本生成超级立方体。
  93. **参数:**
  94. - **inputs** (numpy.ndarray) - 输入样本。
  95. **返回:**
  96. - **numpy.ndarray** - 超立方体对应于每个样本。
  97. .. py:class:: mindarmour.adv_robustness.detectors.SpatialSmoothing(model, ksize=3, is_local_smooth=True, metric='l1', false_positive_ratio=0.05)
  98. 基于空间平滑的检测方法。
  99. 使用高斯滤波、中值滤波和均值滤波,模糊原始图像。当模型在样本模糊前后的预测值之间有很大的阈值差异时,将其判断为对抗样本。
  100. **参数:**
  101. - **model** (Model) - 目标模型。
  102. - **ksize** (int) - 平滑窗口大小。默认值:3。
  103. - **is_local_smooth** (bool) - 如果为True,则触发局部平滑。如果为False,则无局部平滑。默认值:True。
  104. - **metric** (str) - 距离方法。默认值:'l1'。
  105. - **false_positive_ratio** (float) - 良性样本上的假正率。默认值:0.05。
  106. .. py:method:: detect(inputs)
  107. 检测输入样本是否为对抗样本。
  108. **参数:**
  109. - **inputs** (numpy.ndarray) - 待判断的可疑样本。
  110. **返回:**
  111. - **list[int]** - 样本是否具有对抗性。如果res[i]=1,则索引为i的输入样本是对抗性的。
  112. .. py:method:: detect_diff(inputs)
  113. 返回输入样本与其平滑对应样本之间的原始距离值(在应用阈值之前)。
  114. **参数:**
  115. - **inputs** (numpy.ndarray) - 待判断的可疑样本。
  116. **返回:**
  117. - **float** - 距离。
  118. .. py:method:: fit(inputs, labels=None)
  119. 训练检测器来决定阈值。适当的阈值能够确保良性样本上的实际假正率小于给定值。
  120. **参数:**
  121. - **inputs** (numpy.ndarray) - 良性样本。
  122. - **labels** (numpy.ndarray) - 默认None。
  123. **返回:**
  124. - **float** - 阈值,大于该距离的距离报告为正,即对抗性。
  125. .. py:method:: set_threshold(threshold)
  126. 设置参数阈值。
  127. **参数:**
  128. - **threshold** (float) - 检测阈值。
  129. .. py:class:: mindarmour.adv_robustness.detectors.EnsembleDetector(detectors, policy='vote')
  130. **参数:**
  131. - **detectors** (Union[tuple, list]) - 检测器方法列表。
  132. - **policy** (str) - 决策策略,取值可为'vote'、'all'、'any'。默认值:'vote'
  133. .. py:method:: detect(inputs)
  134. 从输入样本中检测对抗性示例。
  135. **参数:**
  136. - **inputs** (numpy.ndarray) - 输入样本。
  137. **返回:**
  138. - **list[int]** - 样本是否具有对抗性。如果res[i]=1,则索引为i的输入样本是对抗性的。
  139. **异常:**
  140. - **ValueError** - 不支持策略。
  141. .. py:method:: detect_diff(inputs)
  142. 此方法在此类中不可用。
  143. **参数:**
  144. - **inputs** (Union[numpy.ndarray, list, tuple]) - 数据被用作创建对抗样本的引用。
  145. **异常:**
  146. - **NotImplementedError** - 此函数在集成中不可用。
  147. .. py:method:: fit(inputs, labels=None)
  148. 像机器学习模型一样拟合检测器。此方法在此类中不可用。
  149. **参数:**
  150. - **inputs** (numpy.ndarray) - 计算阈值的数据。
  151. - **labels** (numpy.ndarray) - 数据的标签。默认值:None。
  152. **异常:**
  153. - **NotImplementedError** - 此函数在集成中不可用。
  154. .. py:method:: transform(inputs)
  155. 过滤输入样本中的对抗性噪声。
  156. 此方法在此类中不可用。
  157. **参数:**
  158. - **inputs** (Union[numpy.ndarray, list, tuple]) - 数据被用作创建对抗样本的引用。
  159. **异常:**
  160. - **NotImplementedError** - 此函数在集成中不可用。
  161. .. py:class:: mindarmour.adv_robustness.detectors.SimilarityDetector(trans_model, max_k_neighbor=1000, chunk_size=1000, max_buffer_size=10000, tuning=False, fpr=0.001)
  162. 检测器测量相邻查询之间的相似性,并拒绝与以前的查询非常相似的查询。
  163. 参考文献: `Stateful Detection of Black-Box Adversarial Attacks by Steven Chen, Nicholas Carlini, and David Wagner. at arxiv 2019 <https://arxiv.org/abs/1907.05587>`_。
  164. **参数:**
  165. - **trans_model** (Model) - 一个MindSpore模型,将输入数据编码为低维向量。
  166. - **max_k_neighbor** (int) - 最近邻的最大数量。默认值:1000。
  167. - **chunk_size** (int) - 缓冲区大小。默认值:1000。
  168. - **max_buffer_size** (int) - 最大缓冲区大小。默认值:10000。默认值:False。
  169. - **tuning** (bool) - 计算k个最近邻的平均距离,如果'tuning'为true,k=K。如果为False,k=1,...,K。默认值:False。
  170. - **fpr** (float) - 合法查询序列上的误报率。默认值:0.001
  171. .. py:method:: clear_buffer()
  172. 清除缓冲区内存。
  173. .. py:method:: detect(inputs)
  174. 处理查询以检测黑盒攻击。
  175. **参数:**
  176. - **inputs** (numpy.ndarray) - 查询顺序。
  177. **异常:**
  178. - **ValueError** - 阈值或num_of_neighbors的参数不可用。
  179. .. py:method:: detect_diff(inputs)
  180. 从输入样本中检测对抗样本,如常见机器学习模型中的predict_proba函数。
  181. **参数:**
  182. - **inputs** (Union[numpy.ndarray, list, tuple]) - 数据被用作创建对抗样本的引用。
  183. **异常:**
  184. - **NotImplementedError** - 此函数在类 `SimilarityDetector` 中不可用。
  185. .. py:method:: fit(inputs, labels=None)
  186. 处理输入训练数据以计算阈值。
  187. 适当的阈值应确保假正率低于给定值。
  188. **参数:**
  189. - **inputs** (numpy.ndarray) - 用于计算阈值的训练数据。
  190. - **labels** (numpy.ndarray) - 训练数据的标签。
  191. **返回:**
  192. - **list[int]** - 最近邻的数量。
  193. - **list[float]** - 不同K的计算阈值。
  194. **异常:**
  195. - **ValueError** - 训练数据个数小于max_k_neighbor!
  196. .. py:method:: get_detected_queries()
  197. 获取检测到的查询的索引。
  198. **返回:**
  199. - **list[int]** - 检测到的恶意查询的序列号。
  200. .. py:method:: get_detection_interval()
  201. 获取相邻检测之间的间隔。
  202. **返回:**
  203. - **list[int]** - 相邻检测之间的查询数。
  204. .. py:method:: set_threshold(threshold, num_of_neighbors)
  205. 设置参数num_of_neighbors和threshold。
  206. **参数:**
  207. - **num_of_neighbors** (int) - 最近邻的数量。
  208. - **threshold** (float) - 检测阈值。
  209. .. py:method:: transform(inputs)
  210. 过滤输入样本中的对抗性噪声。
  211. **参数:**
  212. - **inputs** (Union[numpy.ndarray, list, tuple]) - 数据被用作创建对抗样本的引用。
  213. **异常:**
  214. - **NotImplementedError** - 此函数在类 `SimilarityDetector` 中不可用。

MindArmour关注AI的安全和隐私问题。致力于增强模型的安全可信、保护用户的数据隐私。主要包含3个模块:对抗样本鲁棒性模块、Fuzz Testing模块、隐私保护与评估模块。 对抗样本鲁棒性模块 对抗样本鲁棒性模块用于评估模型对于对抗样本的鲁棒性,并提供模型增强方法用于增强模型抗对抗样本攻击的能力,提升模型鲁棒性。对抗样本鲁棒性模块包含了4个子模块:对抗样本的生成、对抗样本的检测、模型防御、攻防评估。