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_iterative_gradient_method.py 6.1 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. Iterative-gradient Attack test.
  16. """
  17. import numpy as np
  18. import pytest
  19. from mindspore.ops import operations as P
  20. from mindspore.nn import Cell
  21. from mindspore import context
  22. from mindarmour.adv_robustness.attacks import BasicIterativeMethod
  23. from mindarmour.adv_robustness.attacks import MomentumIterativeMethod
  24. from mindarmour.adv_robustness.attacks import ProjectedGradientDescent
  25. from mindarmour.adv_robustness.attacks import IterativeGradientMethod
  26. from mindarmour.adv_robustness.attacks import DiverseInputIterativeMethod
  27. from mindarmour.adv_robustness.attacks import MomentumDiverseInputIterativeMethod
  28. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  29. # for user
  30. class Net(Cell):
  31. """
  32. Construct the network of target model.
  33. Examples:
  34. >>> net = Net()
  35. """
  36. def __init__(self):
  37. super(Net, self).__init__()
  38. self._softmax = P.Softmax()
  39. def construct(self, inputs):
  40. """
  41. Construct network.
  42. Args:
  43. inputs (Tensor): Input data.
  44. """
  45. out = self._softmax(inputs)
  46. return out
  47. @pytest.mark.level0
  48. @pytest.mark.platform_arm_ascend_training
  49. @pytest.mark.platform_x86_ascend_training
  50. @pytest.mark.env_card
  51. @pytest.mark.component_mindarmour
  52. def test_basic_iterative_method():
  53. """
  54. Basic iterative method unit test.
  55. """
  56. input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32)
  57. label = np.asarray([2], np.int32)
  58. label = np.eye(3)[label].astype(np.float32)
  59. for i in range(5):
  60. net = Net()
  61. attack = BasicIterativeMethod(net, nb_iter=i + 1)
  62. ms_adv_x = attack.generate(input_np, label)
  63. assert np.any(
  64. ms_adv_x != input_np), 'Basic iterative method: generate value' \
  65. ' must not be equal to original value.'
  66. @pytest.mark.level0
  67. @pytest.mark.platform_arm_ascend_training
  68. @pytest.mark.platform_x86_ascend_training
  69. @pytest.mark.env_card
  70. @pytest.mark.component_mindarmour
  71. def test_momentum_iterative_method():
  72. """
  73. Momentum iterative method unit test.
  74. """
  75. input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32)
  76. label = np.asarray([2], np.int32)
  77. label = np.eye(3)[label].astype(np.float32)
  78. for i in range(5):
  79. attack = MomentumIterativeMethod(Net(), nb_iter=i + 1)
  80. ms_adv_x = attack.generate(input_np, label)
  81. assert np.any(ms_adv_x != input_np), 'Momentum iterative method: generate' \
  82. ' value must not be equal to' \
  83. ' original value.'
  84. @pytest.mark.level0
  85. @pytest.mark.platform_arm_ascend_training
  86. @pytest.mark.platform_x86_ascend_training
  87. @pytest.mark.env_card
  88. @pytest.mark.component_mindarmour
  89. def test_projected_gradient_descent_method():
  90. """
  91. Projected gradient descent method unit test.
  92. """
  93. input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32)
  94. label = np.asarray([2], np.int32)
  95. label = np.eye(3)[label].astype(np.float32)
  96. for i in range(5):
  97. attack = ProjectedGradientDescent(Net(), nb_iter=i + 1)
  98. ms_adv_x = attack.generate(input_np, label)
  99. assert np.any(
  100. ms_adv_x != input_np), 'Projected gradient descent method: ' \
  101. 'generate value must not be equal to' \
  102. ' original value.'
  103. @pytest.mark.level0
  104. @pytest.mark.platform_arm_ascend_training
  105. @pytest.mark.platform_x86_ascend_training
  106. @pytest.mark.env_card
  107. @pytest.mark.component_mindarmour
  108. def test_diverse_input_iterative_method():
  109. """
  110. Diverse input iterative method unit test.
  111. """
  112. input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32)
  113. label = np.asarray([2], np.int32)
  114. label = np.eye(3)[label].astype(np.float32)
  115. attack = DiverseInputIterativeMethod(Net())
  116. ms_adv_x = attack.generate(input_np, label)
  117. assert np.any(ms_adv_x != input_np), 'Diverse input iterative method: generate' \
  118. ' value must not be equal to' \
  119. ' original value.'
  120. @pytest.mark.level0
  121. @pytest.mark.platform_arm_ascend_training
  122. @pytest.mark.platform_x86_ascend_training
  123. @pytest.mark.env_card
  124. @pytest.mark.component_mindarmour
  125. def test_momentum_diverse_input_iterative_method():
  126. """
  127. Momentum diverse input iterative method unit test.
  128. """
  129. input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32)
  130. label = np.asarray([2], np.int32)
  131. label = np.eye(3)[label].astype(np.float32)
  132. attack = MomentumDiverseInputIterativeMethod(Net())
  133. ms_adv_x = attack.generate(input_np, label)
  134. assert np.any(ms_adv_x != input_np), 'Momentum diverse input iterative method: ' \
  135. 'generate value must not be equal to' \
  136. ' original value.'
  137. @pytest.mark.level0
  138. @pytest.mark.platform_arm_ascend_training
  139. @pytest.mark.platform_x86_ascend_training
  140. @pytest.mark.env_card
  141. @pytest.mark.component_mindarmour
  142. def test_error():
  143. with pytest.raises(TypeError):
  144. # check_param_multi_types
  145. assert IterativeGradientMethod(Net(), bounds=None)
  146. attack = IterativeGradientMethod(Net(), bounds=(0.0, 1.0))
  147. with pytest.raises(NotImplementedError):
  148. input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32)
  149. label = np.asarray([2], np.int32)
  150. label = np.eye(3)[label].astype(np.float32)
  151. assert attack.generate(input_np, label)

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