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_cw.py 4.8 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. CW-Attack test.
  16. """
  17. import gc
  18. import numpy as np
  19. import pytest
  20. import mindspore.ops.operations as M
  21. from mindspore.nn import Cell
  22. from mindspore import context
  23. from mindarmour.adv_robustness.attacks import CarliniWagnerL2Attack
  24. # for user
  25. class Net(Cell):
  26. """
  27. Construct the network of target model.
  28. Examples:
  29. >>> net = Net()
  30. """
  31. def __init__(self):
  32. """
  33. Introduce the layers used for network construction.
  34. """
  35. super(Net, self).__init__()
  36. self._softmax = M.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_cw_attack_ascend():
  51. """
  52. Feature: CW-Attack test for ascend
  53. Description: Given multiple images, we want to make sure the adversarial examples
  54. generated are different from the images
  55. Expectation: input_np != ms_adv_x
  56. """
  57. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  58. net = Net()
  59. input_np = np.array([[0.1, 0.2, 0.7, 0.5, 0.4]]).astype(np.float32)
  60. label_np = np.array([3]).astype(np.int64)
  61. num_classes = input_np.shape[1]
  62. attack = CarliniWagnerL2Attack(net, num_classes, targeted=False)
  63. adv_data = attack.generate(input_np, label_np)
  64. assert np.any(input_np != adv_data)
  65. del input_np, label_np, adv_data
  66. gc.collect()
  67. @pytest.mark.level0
  68. @pytest.mark.platform_x86_cpu
  69. @pytest.mark.env_card
  70. @pytest.mark.component_mindarmour
  71. def test_cw_attack_cpu():
  72. """
  73. Feature: CW-Attack test for cpu
  74. Description: Given multiple images, we want to make sure the adversarial examples
  75. generated are different from the images
  76. Expectation: input_np != ms_adv_x
  77. """
  78. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  79. net = Net()
  80. input_np = np.array([[0.1, 0.2, 0.7, 0.5, 0.4]]).astype(np.float32)
  81. label_np = np.array([3]).astype(np.int64)
  82. num_classes = input_np.shape[1]
  83. attack = CarliniWagnerL2Attack(net, num_classes, targeted=False)
  84. adv_data = attack.generate(input_np, label_np)
  85. assert np.any(input_np != adv_data)
  86. del input_np, label_np, adv_data
  87. gc.collect()
  88. @pytest.mark.level0
  89. @pytest.mark.platform_arm_ascend_training
  90. @pytest.mark.platform_x86_ascend_training
  91. @pytest.mark.env_card
  92. @pytest.mark.component_mindarmour
  93. def test_cw_attack_targeted_ascend():
  94. """
  95. Feature: CW-Attack-Targeted test for ascend
  96. Description: Given multiple images, we want to make sure the adversarial examples
  97. generated are different from the images
  98. Expectation: input_np != ms_adv_x
  99. """
  100. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  101. net = Net()
  102. input_np = np.array([[0.1, 0.2, 0.7, 0.5, 0.4]]).astype(np.float32)
  103. target_np = np.array([1]).astype(np.int64)
  104. num_classes = input_np.shape[1]
  105. attack = CarliniWagnerL2Attack(net, num_classes, targeted=True)
  106. adv_data = attack.generate(input_np, target_np)
  107. assert np.any(input_np != adv_data)
  108. del input_np, target_np, adv_data
  109. gc.collect()
  110. @pytest.mark.level0
  111. @pytest.mark.platform_x86_cpu
  112. @pytest.mark.env_card
  113. @pytest.mark.component_mindarmour
  114. def test_cw_attack_targeted_cpu():
  115. """
  116. Feature: CW-Attack-Targeted test for cpu
  117. Description: Given multiple images, we want to make sure the adversarial examples
  118. generated are different from the images
  119. Expectation: input_np != ms_adv_x
  120. """
  121. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  122. net = Net()
  123. input_np = np.array([[0.1, 0.2, 0.7, 0.5, 0.4]]).astype(np.float32)
  124. target_np = np.array([1]).astype(np.int64)
  125. num_classes = input_np.shape[1]
  126. attack = CarliniWagnerL2Attack(net, num_classes, targeted=True)
  127. adv_data = attack.generate(input_np, target_np)
  128. assert np.any(input_np != adv_data)
  129. del input_np, target_np, adv_data
  130. gc.collect()

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