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 4.4 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  27. # for user
  28. class Net(Cell):
  29. """
  30. Construct the network of target model.
  31. Examples:
  32. >>> net = Net()
  33. """
  34. def __init__(self):
  35. super(Net, self).__init__()
  36. self._softmax = P.Softmax()
  37. def construct(self, inputs):
  38. """
  39. Construct network.
  40. Args:
  41. inputs (Tensor): Input data.
  42. """
  43. out = self._softmax(inputs)
  44. return out
  45. @pytest.mark.level0
  46. @pytest.mark.platform_arm_ascend_training
  47. @pytest.mark.platform_x86_ascend_training
  48. @pytest.mark.env_card
  49. @pytest.mark.component_mindarmour
  50. def test_basic_iterative_method():
  51. """
  52. Basic iterative method unit test.
  53. """
  54. input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32)
  55. label = np.asarray([2], np.int32)
  56. label = np.eye(3)[label].astype(np.float32)
  57. for i in range(5):
  58. net = Net()
  59. attack = BasicIterativeMethod(net, nb_iter=i + 1)
  60. ms_adv_x = attack.generate(input_np, label)
  61. assert np.any(
  62. ms_adv_x != input_np), 'Basic iterative method: generate value' \
  63. ' must not be equal to original value.'
  64. @pytest.mark.level0
  65. @pytest.mark.platform_arm_ascend_training
  66. @pytest.mark.platform_x86_ascend_training
  67. @pytest.mark.env_card
  68. @pytest.mark.component_mindarmour
  69. def test_momentum_iterative_method():
  70. """
  71. Momentum iterative method unit test.
  72. """
  73. input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32)
  74. label = np.asarray([2], np.int32)
  75. label = np.eye(3)[label].astype(np.float32)
  76. for i in range(5):
  77. attack = MomentumIterativeMethod(Net(), nb_iter=i + 1)
  78. ms_adv_x = attack.generate(input_np, label)
  79. assert np.any(ms_adv_x != input_np), 'Basic iterative method: generate' \
  80. ' value must not be equal to' \
  81. ' original value.'
  82. @pytest.mark.level0
  83. @pytest.mark.platform_arm_ascend_training
  84. @pytest.mark.platform_x86_ascend_training
  85. @pytest.mark.env_card
  86. @pytest.mark.component_mindarmour
  87. def test_projected_gradient_descent_method():
  88. """
  89. Projected gradient descent method unit test.
  90. """
  91. input_np = np.asarray([[0.1, 0.2, 0.7]], np.float32)
  92. label = np.asarray([2], np.int32)
  93. label = np.eye(3)[label].astype(np.float32)
  94. for i in range(5):
  95. attack = ProjectedGradientDescent(Net(), nb_iter=i + 1)
  96. ms_adv_x = attack.generate(input_np, label)
  97. assert np.any(
  98. ms_adv_x != input_np), 'Projected gradient descent method: ' \
  99. 'generate value must not be equal to' \
  100. ' original value.'
  101. @pytest.mark.level0
  102. @pytest.mark.platform_arm_ascend_training
  103. @pytest.mark.platform_x86_ascend_training
  104. @pytest.mark.env_card
  105. @pytest.mark.component_mindarmour
  106. def test_error():
  107. with pytest.raises(ValueError):
  108. # check_param_multi_types
  109. assert IterativeGradientMethod(Net(), bounds=None)
  110. attack = IterativeGradientMethod(Net(), bounds=(0.0, 1.0))
  111. with pytest.raises(NotImplementedError):
  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. assert attack.generate(input_np, label)

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