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

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.attacks import BasicIterativeMethod
  23. from mindarmour.attacks import MomentumIterativeMethod
  24. from mindarmour.attacks import ProjectedGradientDescent
  25. from mindarmour.attacks import IterativeGradientMethod
  26. from mindarmour.attacks import DiverseInputIterativeMethod
  27. from mindarmour.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. for i in range(5):
  116. attack = DiverseInputIterativeMethod(Net())
  117. ms_adv_x = attack.generate(input_np, label)
  118. assert np.any(ms_adv_x != input_np), 'Diverse input iterative method: generate' \
  119. ' value must not be equal to' \
  120. ' original value.'
  121. @pytest.mark.level0
  122. @pytest.mark.platform_arm_ascend_training
  123. @pytest.mark.platform_x86_ascend_training
  124. @pytest.mark.env_card
  125. @pytest.mark.component_mindarmour
  126. def test_momentum_diverse_input_iterative_method():
  127. """
  128. Momentum diverse input iterative method unit test.
  129. """
  130. input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32)
  131. label = np.asarray([2], np.int32)
  132. label = np.eye(3)[label].astype(np.float32)
  133. for i in range(5):
  134. attack = MomentumDiverseInputIterativeMethod(Net())
  135. ms_adv_x = attack.generate(input_np, label)
  136. assert np.any(ms_adv_x != input_np), 'Momentum diverse input iterative method: ' \
  137. 'generate value must not be equal to' \
  138. ' original value.'
  139. @pytest.mark.level0
  140. @pytest.mark.platform_arm_ascend_training
  141. @pytest.mark.platform_x86_ascend_training
  142. @pytest.mark.env_card
  143. @pytest.mark.component_mindarmour
  144. def test_error():
  145. with pytest.raises(ValueError):
  146. # check_param_multi_types
  147. assert IterativeGradientMethod(Net(), bounds=None)
  148. attack = IterativeGradientMethod(Net(), bounds=(0.0, 1.0))
  149. with pytest.raises(NotImplementedError):
  150. input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32)
  151. label = np.asarray([2], np.int32)
  152. label = np.eye(3)[label].astype(np.float32)
  153. assert attack.generate(input_np, label)

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