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_pso_attack.py 4.5 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. PSO-Attack test.
  16. """
  17. import numpy as np
  18. import pytest
  19. from mindspore import Tensor
  20. import mindspore.nn as nn
  21. from mindspore.nn import Cell
  22. from mindspore import context
  23. from mindarmour.attacks.black.pso_attack import PSOAttack
  24. from mindarmour.attacks.black.black_model import BlackModel
  25. # for user
  26. class ModelToBeAttacked(BlackModel):
  27. """model to be attack"""
  28. def __init__(self, network):
  29. super(ModelToBeAttacked, self).__init__()
  30. self._network = network
  31. def predict(self, inputs):
  32. """predict"""
  33. result = self._network(Tensor(inputs.astype(np.float32)))
  34. return result.asnumpy()
  35. class SimpleNet(Cell):
  36. """
  37. Construct the network of target model.
  38. Examples:
  39. >>> net = SimpleNet()
  40. """
  41. def __init__(self):
  42. """
  43. Introduce the layers used for network construction.
  44. """
  45. super(SimpleNet, self).__init__()
  46. self._relu = nn.ReLU()
  47. def construct(self, inputs):
  48. """
  49. Construct network.
  50. Args:
  51. inputs (Tensor): Input data.
  52. """
  53. out = self._relu(inputs)
  54. return out
  55. @pytest.mark.level0
  56. @pytest.mark.platform_arm_ascend_training
  57. @pytest.mark.platform_x86_ascend_training
  58. @pytest.mark.env_card
  59. @pytest.mark.component_mindarmour
  60. def test_pso_attack():
  61. """
  62. PSO_Attack test
  63. """
  64. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  65. batch_size = 6
  66. net = SimpleNet()
  67. inputs = np.random.rand(batch_size, 10)
  68. model = ModelToBeAttacked(net)
  69. labels = np.random.randint(low=0, high=10, size=batch_size)
  70. labels = np.eye(10)[labels]
  71. labels = labels.astype(np.float32)
  72. attack = PSOAttack(model, bounds=(0.0, 1.0), pm=0.5, sparse=False)
  73. _, adv_data, _ = attack.generate(inputs, labels)
  74. assert np.any(inputs != adv_data)
  75. @pytest.mark.level0
  76. @pytest.mark.platform_arm_ascend_training
  77. @pytest.mark.platform_x86_ascend_training
  78. @pytest.mark.env_card
  79. @pytest.mark.component_mindarmour
  80. def test_pso_attack_targeted():
  81. """
  82. PSO_Attack test
  83. """
  84. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  85. batch_size = 6
  86. net = SimpleNet()
  87. inputs = np.random.rand(batch_size, 10)
  88. model = ModelToBeAttacked(net)
  89. labels = np.random.randint(low=0, high=10, size=batch_size)
  90. labels = np.eye(10)[labels]
  91. labels = labels.astype(np.float32)
  92. attack = PSOAttack(model, bounds=(0.0, 1.0), pm=0.5, targeted=True,
  93. sparse=False)
  94. _, adv_data, _ = attack.generate(inputs, labels)
  95. assert np.any(inputs != adv_data)
  96. @pytest.mark.level0
  97. @pytest.mark.platform_x86_gpu_inference
  98. @pytest.mark.env_card
  99. @pytest.mark.component_mindarmour
  100. def test_pso_attack_gpu():
  101. """
  102. PSO_Attack test
  103. """
  104. context.set_context(device_target="GPU")
  105. batch_size = 6
  106. net = SimpleNet()
  107. inputs = np.random.rand(batch_size, 10)
  108. model = ModelToBeAttacked(net)
  109. labels = np.random.randint(low=0, high=10, size=batch_size)
  110. labels = np.eye(10)[labels]
  111. labels = labels.astype(np.float32)
  112. attack = PSOAttack(model, bounds=(0.0, 1.0), pm=0.5, sparse=False)
  113. _, adv_data, _ = attack.generate(inputs, labels)
  114. assert np.any(inputs != adv_data)
  115. @pytest.mark.level0
  116. @pytest.mark.platform_x86_cpu
  117. @pytest.mark.env_card
  118. @pytest.mark.component_mindarmour
  119. def test_pso_attack_cpu():
  120. """
  121. PSO_Attack test
  122. """
  123. context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
  124. batch_size = 6
  125. net = SimpleNet()
  126. inputs = np.random.rand(batch_size, 10)
  127. model = ModelToBeAttacked(net)
  128. labels = np.random.randint(low=0, high=10, size=batch_size)
  129. labels = np.eye(10)[labels]
  130. labels = labels.astype(np.float32)
  131. attack = PSOAttack(model, bounds=(0.0, 1.0), pm=0.5, sparse=False)
  132. _, adv_data, _ = attack.generate(inputs, labels)
  133. assert np.any(inputs != adv_data)

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