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.

test_deep_fool.py 10 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """
  15. DeepFool-Attack test.
  16. """
  17. import gc
  18. import numpy as np
  19. import pytest
  20. import mindspore.ops.operations as P
  21. from mindspore.nn import Cell
  22. from mindspore import context
  23. from mindspore import Tensor
  24. from mindarmour.adv_robustness.attacks import DeepFool
  25. # for user
  26. class Net(Cell):
  27. """
  28. Construct the network of target model.
  29. Examples:
  30. >>> net = Net()
  31. """
  32. def __init__(self):
  33. """
  34. Introduce the layers used for network construction.
  35. """
  36. super(Net, self).__init__()
  37. self._softmax = P.Softmax()
  38. def construct(self, inputs):
  39. """
  40. Construct network.
  41. Args:
  42. inputs (Tensor): Input data.
  43. """
  44. out = self._softmax(inputs)
  45. return out
  46. class Net2(Cell):
  47. """
  48. Construct the network of target model, specifically for detection model test case.
  49. Examples:
  50. >>> net = Net2()
  51. """
  52. def __init__(self):
  53. super(Net2, self).__init__()
  54. self._softmax = P.Softmax()
  55. def construct(self, inputs1, inputs2):
  56. out1 = self._softmax(inputs1)
  57. out2 = self._softmax(inputs2)
  58. return out2, out1
  59. @pytest.mark.level0
  60. @pytest.mark.platform_arm_ascend_training
  61. @pytest.mark.platform_x86_ascend_training
  62. @pytest.mark.env_card
  63. @pytest.mark.component_mindarmour
  64. def test_deepfool_attack_ascend():
  65. """
  66. Feature: Deepfool-Attack test for ascend
  67. Description: Given multiple images, we want to make sure the adversarial examples
  68. generated are different from the images
  69. Expectation: input_np != ms_adv_x
  70. """
  71. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  72. net = Net()
  73. input_shape = (1, 5)
  74. _, classes = input_shape
  75. input_np = np.array([[0.1, 0.2, 0.7, 0.5, 0.4]]).astype(np.float32)
  76. input_me = Tensor(input_np)
  77. true_labels = np.argmax(net(input_me).asnumpy(), axis=1)
  78. attack = DeepFool(net, classes, max_iters=10, norm_level=2,
  79. bounds=(0.0, 1.0))
  80. adv_data = attack.generate(input_np, true_labels)
  81. # expected adv value
  82. expect_value = np.asarray([[0.10300991, 0.20332647, 0.59308802, 0.59651263,
  83. 0.40406296]])
  84. assert np.allclose(adv_data, expect_value), 'mindspore deepfool_method' \
  85. ' implementation error, ms_adv_x != expect_value'
  86. del input_np, true_labels, adv_data, expect_value
  87. gc.collect()
  88. @pytest.mark.level0
  89. @pytest.mark.platform_x86_cpu
  90. @pytest.mark.env_card
  91. @pytest.mark.component_mindarmour
  92. def test_deepfool_attack_cpu():
  93. """
  94. Feature: Deepfool-Attack test for cpu
  95. Description: Given multiple images, we want to make sure the adversarial examples
  96. generated are different from the images
  97. Expectation: input_np != ms_adv_x
  98. """
  99. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  100. net = Net()
  101. input_shape = (1, 5)
  102. _, classes = input_shape
  103. input_np = np.array([[0.1, 0.2, 0.7, 0.5, 0.4]]).astype(np.float32)
  104. input_me = Tensor(input_np)
  105. true_labels = np.argmax(net(input_me).asnumpy(), axis=1)
  106. attack = DeepFool(net, classes, max_iters=10, norm_level=2,
  107. bounds=(0.0, 1.0))
  108. adv_data = attack.generate(input_np, true_labels)
  109. # expected adv value
  110. expect_value = np.asarray([[0.10300991, 0.20332647, 0.59308802, 0.59651263,
  111. 0.40406296]])
  112. assert np.allclose(adv_data, expect_value), 'mindspore deepfool_method' \
  113. ' implementation error, ms_adv_x != expect_value'
  114. del input_np, true_labels, adv_data, expect_value
  115. gc.collect()
  116. @pytest.mark.level0
  117. @pytest.mark.platform_arm_ascend_training
  118. @pytest.mark.platform_x86_ascend_training
  119. @pytest.mark.env_card
  120. @pytest.mark.component_mindarmour
  121. def test_deepfool_attack_detection_ascend():
  122. """
  123. Feature: Deepfool-Attack-Detection test for ascend
  124. Description: Given multiple images, we want to make sure the adversarial examples
  125. generated are different from the images
  126. Expectation: input_np != ms_adv_x
  127. """
  128. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  129. net = Net2()
  130. inputs1_np = np.random.random((2, 10, 10)).astype(np.float32)
  131. inputs2_np = np.random.random((2, 10, 5)).astype(np.float32)
  132. gt_boxes, gt_logits = net(Tensor(inputs1_np), Tensor(inputs2_np))
  133. gt_boxes, gt_logits = gt_boxes.asnumpy(), gt_logits.asnumpy()
  134. gt_labels = np.argmax(gt_logits, axis=2)
  135. num_classes = 10
  136. attack = DeepFool(net, num_classes, model_type='detection', reserve_ratio=0.3,
  137. bounds=(0.0, 1.0))
  138. adv_data = attack.generate((inputs1_np, inputs2_np), (gt_boxes, gt_labels))
  139. assert np.any(adv_data != inputs1_np)
  140. del inputs1_np, inputs2_np, gt_labels, adv_data
  141. gc.collect()
  142. @pytest.mark.level0
  143. @pytest.mark.platform_x86_cpu
  144. @pytest.mark.env_card
  145. @pytest.mark.component_mindarmour
  146. def test_deepfool_attack_detection_cpu():
  147. """
  148. Feature: Deepfool-Attack-Detection test for cpu
  149. Description: Given multiple images, we want to make sure the adversarial examples
  150. generated are different from the images
  151. Expectation: input_np != ms_adv_x
  152. """
  153. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  154. net = Net2()
  155. inputs1_np = np.random.random((2, 10, 10)).astype(np.float32)
  156. inputs2_np = np.random.random((2, 10, 5)).astype(np.float32)
  157. gt_boxes, gt_logits = net(Tensor(inputs1_np), Tensor(inputs2_np))
  158. gt_boxes, gt_logits = gt_boxes.asnumpy(), gt_logits.asnumpy()
  159. gt_labels = np.argmax(gt_logits, axis=2)
  160. num_classes = 10
  161. attack = DeepFool(net, num_classes, model_type='detection', reserve_ratio=0.3,
  162. bounds=(0.0, 1.0))
  163. adv_data = attack.generate((inputs1_np, inputs2_np), (gt_boxes, gt_labels))
  164. assert np.any(adv_data != inputs1_np)
  165. del inputs1_np, inputs2_np, gt_labels, adv_data
  166. gc.collect()
  167. @pytest.mark.level0
  168. @pytest.mark.platform_arm_ascend_training
  169. @pytest.mark.platform_x86_ascend_training
  170. @pytest.mark.env_card
  171. @pytest.mark.component_mindarmour
  172. def test_deepfool_attack_inf_ascend():
  173. """
  174. Feature: Deepfool-Attack with inf-norm test for ascend
  175. Description: Given multiple images, we want to make sure the adversarial examples
  176. generated are different from the images
  177. Expectation: input_np != ms_adv_x
  178. """
  179. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  180. net = Net()
  181. input_shape = (1, 5)
  182. _, classes = input_shape
  183. input_np = np.array([[0.1, 0.2, 0.7, 0.5, 0.4]]).astype(np.float32)
  184. input_me = Tensor(input_np)
  185. true_labels = np.argmax(net(input_me).asnumpy(), axis=1)
  186. attack = DeepFool(net, classes, max_iters=10, norm_level=np.inf,
  187. bounds=(0.0, 1.0))
  188. adv_data = attack.generate(input_np, true_labels)
  189. assert np.any(input_np != adv_data)
  190. del input_np, true_labels, adv_data
  191. gc.collect()
  192. @pytest.mark.level0
  193. @pytest.mark.platform_x86_cpu
  194. @pytest.mark.env_card
  195. @pytest.mark.component_mindarmour
  196. def test_deepfool_attack_inf_cpu():
  197. """
  198. Feature: Deepfool-Attack with inf-norm test for cpu
  199. Description: Given multiple images, we want to make sure the adversarial examples
  200. generated are different from the images
  201. Expectation: input_np != ms_adv_x
  202. """
  203. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  204. net = Net()
  205. input_shape = (1, 5)
  206. _, classes = input_shape
  207. input_np = np.array([[0.1, 0.2, 0.7, 0.5, 0.4]]).astype(np.float32)
  208. input_me = Tensor(input_np)
  209. true_labels = np.argmax(net(input_me).asnumpy(), axis=1)
  210. attack = DeepFool(net, classes, max_iters=10, norm_level=np.inf,
  211. bounds=(0.0, 1.0))
  212. adv_data = attack.generate(input_np, true_labels)
  213. assert np.any(input_np != adv_data)
  214. del input_np, true_labels, adv_data
  215. gc.collect()
  216. @pytest.mark.level0
  217. @pytest.mark.platform_arm_ascend_training
  218. @pytest.mark.platform_x86_ascend_training
  219. @pytest.mark.env_card
  220. @pytest.mark.component_mindarmour
  221. def test_value_error_ascend():
  222. """
  223. Feature: value error test for ascend
  224. Description: value error for deep fool
  225. Expectation: attack.generate works
  226. """
  227. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  228. net = Net()
  229. input_shape = (1, 5)
  230. _, classes = input_shape
  231. input_np = np.array([[0.1, 0.2, 0.7, 0.5, 0.4]]).astype(np.float32)
  232. input_me = Tensor(input_np)
  233. true_labels = np.argmax(net(input_me).asnumpy(), axis=1)
  234. with pytest.raises(NotImplementedError):
  235. # norm_level=0 is not available
  236. attack = DeepFool(net, classes, max_iters=10, norm_level=1,
  237. bounds=(0.0, 1.0))
  238. assert attack.generate(input_np, true_labels)
  239. del input_np, true_labels
  240. gc.collect()
  241. @pytest.mark.level0
  242. @pytest.mark.platform_x86_cpu
  243. @pytest.mark.env_card
  244. @pytest.mark.component_mindarmour
  245. def test_value_error_cpu():
  246. """
  247. Feature: value error test for cpu
  248. Description: value error for deep fool
  249. Expectation: attack.generate works
  250. """
  251. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  252. net = Net()
  253. input_shape = (1, 5)
  254. _, classes = input_shape
  255. input_np = np.array([[0.1, 0.2, 0.7, 0.5, 0.4]]).astype(np.float32)
  256. input_me = Tensor(input_np)
  257. true_labels = np.argmax(net(input_me).asnumpy(), axis=1)
  258. with pytest.raises(NotImplementedError):
  259. # norm_level=0 is not available
  260. attack = DeepFool(net, classes, max_iters=10, norm_level=1,
  261. bounds=(0.0, 1.0))
  262. assert attack.generate(input_np, true_labels)
  263. del input_np, true_labels
  264. gc.collect()

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